designs/build_for_layers.md
AWS Lambda Layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. Layers allow us to keep our deployment package small, which makes development easier. To include libraries in a layer, they need to be placed in pre decided folders. For more information check here. Once files are in proper folder structure it can be zipped and uploaded to S3 where it’ll be ready to be used by Lambda Function.
Current layer support of aws-sam-cli expects layer folder to be in final condition which then is zipped and uploaded to S3. That means if one need to compile java code, they have to do that outside of their sam-cli workflow. This design is solving this problem by including layer building process in sam build which will reduce the overhead to build layer separately outside of sam workflow.
sam build to build all functions and layers.sam build FunctionLogicalID to build single Function and all layers this function depend on.sam build LayerLogicalID to build single layer.Build for layers will build Layer from aws-sam-cli and prepare artifacts in expected folder structure. Check this for more details around expected folder structure. Build for layers will choose one of the following types of build workflows:
This is not exactly a workflow. However, if user do not want to build their Layer using build for layers, then they don’t have to do anything. This feature is opt-in and if no Metadata -> BuildMethod is provided then we’ll keep following current workflow.
Sample YAML:
MyLayer:
Type: AWS::Serverless::LayerVersion
Properties:
ContentUri: my_layer
CompatibleRuntimes:
- python3.8
Metadata:
BuildMethod: python3.8 (or nodejs8.10 etc..)
StandardBuildWorkflow do not expect any manifest file, and have simple 1:1 mapping with AWS Serverless Function runtime. This BuildMethod helps us choose correct build workflow for layer from aws-lambda-builders. If this workflow is chosen, aws-sam-cli will be responsible for putting build artifact in layer specific folder structure.
Sample YAML:
MyLayer:
Type: AWS::Serverless::LayerVersion
Properties:
ContentUri: my_layer
CompatibleRuntimes:
- python3.8
Metadata:
BuildMethod: makefile
This workflow will check for a Makefile with build target build-{LayerLogicalID}. For more details around this workflow check PR-166. User can use --manifest option of sam build command to override path to manifest file. If no path is provided, sam-cli will look for makefile in ContentUri of layer. If this workflow is chosen, user will be responsible for generating build artifacts in layer specific folder structure.
What if user want to use separate Makefile for Lambda and Layer but still want to pass --manifest option.
This edge case is currently not supported. Implementation of this require addition in sam-cli interface and would be a much bigger change. We will implement this feature if lot of users will ask for this.
No changes in CLI interface.
No breaking changes.