Skip to content

Commit de8ad8e

Browse files
authored
Adding design doc for 'build for layers' (#1945)
1 parent 23212ef commit de8ad8e

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

designs/build_for_layers.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
What is the problem?
2+
--------------------
3+
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](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path). 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.
4+
5+
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.
6+
7+
Out-of-Scope
8+
------------
9+
1. Implementation of Makefile based build. That is already in progress under [PR-166](https://github.com/awslabs/aws-lambda-builders/pull/166)
10+
11+
User Experience Walkthrough
12+
---------------------------
13+
1. Add Metadata (check proposal for more details) to the layers in template.yaml file to opt-in to build for layers.
14+
1. Provide ManifestPath if applicable.
15+
1. Run `sam build` to build all functions and layers.
16+
1. Run `sam build FunctionLogicalID` to build single Function and all layers this function depend on.
17+
1. Run `sam build LayerLogicalID` to build single layer.
18+
19+
Implementation
20+
==============
21+
### Proposal
22+
Build for layers will build Layer from aws-sam-cli and prepare artifacts in expected folder structure. Check [this](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path) for more details around expected folder structure.
23+
Build for layers will choose one of the following types of build workflows:
24+
#### NoBuildWorkflow
25+
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.
26+
27+
#### StandardBuildWorkflow
28+
Sample YAML:
29+
```
30+
MyLayer:
31+
Type: AWS::Serverless::LayerVersion
32+
Properties:
33+
ContentUri: my_layer
34+
CompatibleRuntimes:
35+
- python3.8
36+
Metadata:
37+
BuildMethod: python3.8 (or nodejs8.10 etc..)
38+
```
39+
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.
40+
41+
#### MakefileBuildWorkflow
42+
Sample YAML:
43+
```
44+
MyLayer:
45+
Type: AWS::Serverless::LayerVersion
46+
Properties:
47+
ContentUri: my_layer
48+
CompatibleRuntimes:
49+
- python3.8
50+
Metadata:
51+
BuildMethod: makefile
52+
```
53+
This workflow will check for a Makefile with build target `build-{LayerLogicalID}`. For more details around this workflow check [PR-166](https://github.com/awslabs/aws-lambda-builders/pull/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.
54+
55+
#### Alternatives Considered
56+
* Instead of making Metadata field mandatory we can also try to detect build workflow based on compatible runtime provided. The problem with this approach is that it somewhat breaks backwards compatibility. Eg: If somebody have a python layer, but is already using Makefile to custom build it then us running python build workflow will not work. Even if we detect makefile we don’t know the actions needed to build the layer.
57+
* Instead of using Metadata in template.yaml file, we can make entries in samconfig.toml file. We can use samconfig.toml file, however, that file is intended to configure sam-cli. If we decide to support only one build type at a time (eg: one can only use python workflow to build all layers) then it might work. However, for all other cases we have to mention LogicalID of layer in samconfig toml file, which is not the intended purpose of samconfig.
58+
59+
FAQs
60+
----
61+
**What if user want to use separate Makefile for Lambda and Layer but still want to pass `--manifest` option.**
62+
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.
63+
64+
CLI Changes
65+
-----------
66+
67+
No changes in CLI interface.
68+
69+
### Breaking Change
70+
71+
No breaking changes.
72+
73+
Task Breakdown
74+
==============
75+
76+
- \[x\] Send a Pull Request with this design document
77+
- \[ \] Build the command line interface
78+
- \[ \] Build the underlying library
79+
- \[ \] Unit tests
80+
- \[ \] Functional Tests
81+
- \[ \] Integration tests
82+
- \[ \] Run all tests on Windows
83+
- \[ \] Update documentation

0 commit comments

Comments
 (0)