All Projects → magnetikonline → lambda-smush-py

magnetikonline / lambda-smush-py

Licence: MIT License
Gain additional code space via cheeky compression for Python AWS Lambda functions defined in-line to CloudFormation templates.

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to lambda-smush-py

Lambstatus
[Maintenance mode] Serverless Status Page System
Stars: ✭ 1,323 (+7682.35%)
Mutual labels:  lambda, cloudformation, aws-lambda
super-serverless-sample
Backend serverless que simula o sistema de votação do BBB
Stars: ✭ 30 (+76.47%)
Mutual labels:  lambda, aws-lambda
CloudWatch2S3
Logging infrastructure for exporting all CloudWatch logs from multiple accounts to a single S3 bucket
Stars: ✭ 31 (+82.35%)
Mutual labels:  lambda, cloudformation
nuxt-on-lambda
Nuxt.jsをAWS Lambdaで動かす
Stars: ✭ 78 (+358.82%)
Mutual labels:  lambda, aws-lambda
shim
HTTP Handler shim for Go projects running on AWS Lambda
Stars: ✭ 64 (+276.47%)
Mutual labels:  lambda, aws-lambda
amazon-ivs-simple-chat-web-demo
⚠️ IMPORTANT ⚠️ This repository is no longer actively maintained and will be archived at the end of 2022. A basic live chat implementation built with WebSockets, that can be used in conjunction with Amazon IVS to build compelling customer experiences for live video streams with chat use cases.
Stars: ✭ 53 (+211.76%)
Mutual labels:  cloudformation, aws-lambda
cfn-api-gateway-custom-domain
API Gateway custom domains as CloudFormation resources, backed by Let's Encrypt
Stars: ✭ 17 (+0%)
Mutual labels:  cloudformation, aws-lambda
deflate-rs
An implementation of a DEFLATE encoder in rust
Stars: ✭ 47 (+176.47%)
Mutual labels:  compression, gzip
cfn-encrypt
🔑🔐☁️ Cloudformation custom resource that enables creation of KMS encrypted strings and SSM secure parameters
Stars: ✭ 13 (-23.53%)
Mutual labels:  lambda, cloudformation
pyrus-cramjam
Thin Python wrapper to de/compression algorithms in Rust - lightweight & no dependencies
Stars: ✭ 40 (+135.29%)
Mutual labels:  compression, gzip
serverless-data-pipeline-sam
Serverless Data Pipeline powered by Kinesis Firehose, API Gateway, Lambda, S3, and Athena
Stars: ✭ 78 (+358.82%)
Mutual labels:  cloudformation, aws-lambda
ratarmount
Random Access Read-Only Tar Mount
Stars: ✭ 217 (+1176.47%)
Mutual labels:  compression, gzip
sanic compress
An extension which allows you to easily compress your Sanic responses with gzip.
Stars: ✭ 26 (+52.94%)
Mutual labels:  compression, gzip
node-lambda-babel-template
A minimal template for an ES2015+ Node.js app running on AWS Lambda (w/ babel and webpack).
Stars: ✭ 40 (+135.29%)
Mutual labels:  lambda, aws-lambda
SecretsManagerwithCloudFormation
Implements a Lambda-backed CloudFormation Custom Resource for AWS Secrets Manager
Stars: ✭ 20 (+17.65%)
Mutual labels:  lambda, cloudformation
whats-your-name
Sample app for AWS Serverless Repository - uses Amazon Rekognition to recognize person on the photo
Stars: ✭ 17 (+0%)
Mutual labels:  cloudformation, aws-lambda
aws-lambda-edge-basic-auth-terraform
A Terraform module that creates AWS Lambda@Edge resources to protect CloudFront distributions with Basic Authentication.
Stars: ✭ 18 (+5.88%)
Mutual labels:  lambda, aws-lambda
http compression
🗜️ Deno HTTP compression middleware
Stars: ✭ 34 (+100%)
Mutual labels:  compression, gzip
cim
CIM takes the pain out of Infrastructure as Code and CloudFormation
Stars: ✭ 51 (+200%)
Mutual labels:  lambda, cloudformation
twitter
A serverless social network that's under development with some cool stuff, such as Serverless Framework, AppSync, GraphQL, Lambda, DynamoDB, Cognito, Kinesis Firehose, and Algolia ☁️
Stars: ✭ 29 (+70.59%)
Mutual labels:  lambda, aws-lambda

Lambda smush py

Utility with the sole aim to squeeze that little bit more code out of Python based AWS Lambda functions defined in-line to CloudFormation templates.

How it works

CloudFormation offers the ability to define Lambda function code directly within templates for Python and Node.js runtimes via the Code: property - negating the need for an S3 code bucket and great for keeping smaller functions tightly coupled with a stack.

Unfortunately the allowed code size - including whitespace, is limited to 4096 characters. With this utility a Python based Lambda function is transformed to a compressed equivalent which is inflated at runtime to gain additional kilobytes of usable space for in-lined functions.

At time of invoke the following steps are executed:

  • Temporary file created within the Lambda container.
  • File populated with Base64 decoded and inflated function source.
  • Python then loads the source as a new module (imp.load_source()).
  • Proxy function acting as the handler then calls module handler, executing the original source.
  • Additional function invokes for the lifetime of the container will simply call the proxy function - the decode and inflate steps only occur once, adding very little additional overhead.

The bootloader ends up being nothing more than:

import base64,imp,tempfile,zlib
_='''\
SOURCE_FUNCTION_BASE64_ENCODED_AND_COMPRESSED\
'''
l=tempfile.mkdtemp()+'/l.py'
h=open(l,'w')
h.write(zlib.decompress(base64.b64decode(_)))
h.close()
m=imp.load_source('l',l)
def HANDLER_NAME(e,c):
	return m.HANDLER_NAME(e,c)

Usage

usage: lambdasmushpy.py [-h] --source SOURCE --handler-name HANDLER_NAME
                        [--strip-comments] [--strip-empty-lines]
                        [--template YAML] [--template-placeholder PLACEHOLDER]
                        [--output FILE]

Generates compressed Python based AWS Lambda functions designed to fit within
the 4096 byte in-line limit of a CloudFormation template

optional arguments:
  -h, --help            show this help message and exit
  --source SOURCE       path to Lambda function
  --handler-name HANDLER_NAME
                        name of handler Lambda calls to invoke function
  --strip-comments      remove comment only lines to further reduce function
                        size
  --strip-empty-lines   remove empty lines to further reduce function size
  --template YAML       merge generated code into given YAML CloudFormation
                        template
  --template-placeholder PLACEHOLDER
                        place holder text within CloudFormation template
  --output FILE         write output to given filename, otherwise send to
                        console

Examples

Source function /path/to/lambda.py with handler my_handler() is compressed, with the result sent to console:

$ ./lambdasmushpy.py" \
	--source "/path/to/lambda.py" \
	--handler-name "my_handler"

Generated functions can also be embedded directly into CloudFormation YAML templates through the use of a placeholder.

For this example the compressed source is further reduced by removing comment and empty lines, the final embedded result is written to /path/to/final/template.yaml:

$ ./lambdasmushpy.py" \
	--source "/path/to/lambda.py" \
	--handler-name "my_handler" \
	--strip-comments \
	--strip-empty-lines \
	--template "/path/to/source/template.yaml" \
	--template-placeholder SMUSH_FUNCTION \
	--output "/path/to/final/template.yaml"

The bundled example/ shows this process end-to-end in detail.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].