Creating a common library for all functions makes code:
Particularly when validating input and creating common response formats for all functions. I have a bad habit of inconsistent response formats that break other parsers 😛 Creating a common format and passing arguments from each function is much more maintainable
I ran into some headaches when my libraries required the installation of specific packages. Just sticking requirements.txt in the folder was not sufficient for SAM’s processes to automatically include them. Some extra elements are required in the tempalte.yaml file
To create a library of functions available across all lambda functions requires:
Project
|-.aws-sam
|-layers
| |-common
| | |-helpers.py
| | |-validator.py
| | |-requirements.txt
| |-special
| | |-quantum_entangler.py
| | |-requirements.txt
|-foo_function
| |-app.py
| |-requirements.txt
|-bar_function
| |-app.py
| |-requirements.txt
|-.gitignore
|-template.yaml
Layer code does not need to be stored inside a folder named dependencies\\python . This is only required if you are building your own layer and providing a .zip file in the template.
Globals: # Common to all lambda resources in the stack
Function:
Layers:
- !Ref CommonFunctionsLayer
Resources:
CommonFunctionsLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: sam-app-dependencies
Description: Common helper functions shared across Lambda functions
ContentUri: layers/common/
CompatibleRuntimes:
- python3.8
RetentionPolicy: Delete
Metadata: # THIS IS THE IMPORTANT BIT!
BuildMethod: python3.8
BuildArchitecture: x86_64 # arm64 is an alternative and recommended for performance (I suspect there may be compatability problems)
SpecialFunctionLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: special_layer
Description: Special helper function used only in one function
CompatibleRuntimes:
- python3.8
RetentionPolicy: Delete
Metadata:
BuildMethod: python3.8
BuildArchitecture: x86_64
FooFunction:
Type: AWS::Serverless::Function # More info about Function Resource: <https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction>
Description: this is some generic function
Properties:
CodeUri: foo_functioon/
Handler: app.lambda_handler
Runtime: python3.8
Events:
ApiEvent:
Type: Api # More info about API Event Source: <https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api>
Properties:
Path: /foo
Method: put
BarFunction:
Type: AWS::Serverless::Function # More info about Function Resource: <https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction>
Description: this is some generic function
Properties:
CodeUri: bar_functioon/
Handler: app.lambda_handler
Runtime: python3.8
Events:
ApiEvent:
Type: Api # More info about API Event Source: <https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api>
Properties:
Path: /foo
Method: put
Layers:
- !Ref SpecialFunctionLayer
Both metadata parameters must be defined for SAMs processes to build the layers from scratch