How to create an AWS lambda layer

Prepare the layer

Be Careful!
Having all dependencies in a directory, and not downloading from an index, only works when the directory contains all packages. The directory should therefore contain all dependencies but also all packages that those dependencies depend on. You should therefore manually include these in requirements.txt (so that the first step downloads them explicitly, or you should install all packages using PyPI and then pip freeze > requirements.txt to store the list of all packages needed.

  1. Create a new folder for this project:
1
2
mkdir aws-lambda-layer
cd aws-lambda-layer
  1. Create a folder structure for the modules that need to be installed.
1
mkdir -p lambda-layer/python/lib/python3.8/site-packages
  1. Install modules in that folder. The structure is important, because that is where Python expects to find the modules.
1
pip install requests --target lambda-layer/python/lib/python3.8/site-packages
  1. Go into the lambda-layer folder and create a zip file for the layer. It will be uploaded using the console.
1
2
cd lambda-layer
zip -r9 lambda-layer.zip .

Create the layer

  1. Log into the AWS console and go to Services -> Lambda -> Layers
  2. Click on Create layer
    • Name (e.g.: myRequestsLayer)
    • Upload - Select your zip file from before
    • Runtime (Python 3.8)
  3. Click on Create

Creating the lambda

Manually

Create the code

  1. Log in to in the AWS console go to Services -> Lambda
  2. Click on Create function
    • Author from scratch
    • Function name (e.g.: randomDadJokes)
    • Runtime (Python 3.8)
  3. Click on Create function
  4. Replace the code in the editor with the following code, and hit Save:
1
2
3
4
5
6
7
8
9
import json
import requests

url = 'https://icanhazdadjoke.com'

def lambda_handler(event, context):
r = requests.get(url, headers={"Accept": "application/json"})
result = json.loads(r.text)
return result['joke']

Connect the layer

  1. On the lambda screen, in the Designer section, click on the Layers box.
    • Add a layer
    • Select from list of runtime compatible layers
    • Name (chose your layer)
    • Version 1
    • Add
  2. Click on the dropdown Select a test event -> Configure test events
    • Add Event Name (e.g.: Test)
    • Inputs don’t matter so you can just leave it as is or delete those keys
    • Click on Create
  3. Run it by clicking on the Test button.

Via Cloud Formation template

  1. Zip the folder that contains the lambda function
1
2
cd  my_lambda_function
zip my-deployment-package.zip lambda_function.py
  1. Upload the zip to an AWS S3 bucket.
  2. Then, the template will look similar to this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
AWSTemplateFormatVersion: '2010-09-09'
Description: "This stack creates a Lambda function that gets its code from an
S3 bucket and makes use of 2 different Lambda layers"
Parameters:
LambdaBucket:
Description: S3 URI where the Lambda code zip will be located.
Type: String
Default: accountname_projectname
LambdaDescription:
Description: Description of the Lambda Function.
Type: String
Default: Lambda for tests.
LambdaHandler:
Description: Handler of the Lambda Function.
Type: String
Default: lambda_function.lambda_handler
LambdaMemory:
Description: Memory available to the Lambda Function.
Type: Number
Default: 832
LambdaName:
Description: Name of the Lambda function.
Type: String
Default: TestLambda
LambdaObject:
Description: Name of the zip file that includes the Lambda code.
Type: String
Default: isltest/Lambda_Api.zip
LambdaObjectVersion:
Description: Version of the Lambda code.
Type: String
Default: iegmgNFWyh86SeM8lszZWKxK60ueO4ne
LambdaRole:
Description: IAM role with permissions required to execute the Lambda function.
Type: String
Default: arn:aws:iam::123456789012:role/service-role/ChromelessTest-role-p5jqa0rk
LambdaRuntime:
Description: The programming language in which it will be written.
Type: String
Default: python3.8
LambdaTimeout:
Description: Timeout (in seconds) to execute the Lambda Function.
Type: Number
Default: 63
TracingMode:
Description: Tracing mode for integration with AWS X-Ray if needed.
Type: String
Default: Active
LambdaLayer1:
Description: ARNs of the layers that will be applied to the Lambda function,
in case you need dependencies.
Type: String
Default: arn:aws:lambda:eu-west-1:123456789012:layer:LambdaInsightsExtension:10
LambdaLayer2:
Description: ARNs of the layers that will be applied to the Lambda function,
in case you need dependencies.
Type: String
Default: arn:aws:lambda:eu-west-1:123456789012:layer:selenium-python-dependencies:12
Resources:
LambdaFunction:
Type: 'AWS::Lambda::Function'
Properties:
MemorySize: !Ref LambdaMemory
Description: !Ref LambdaDescription
TracingConfig:
Mode: !Ref TracingMode
Timeout: !Ref LambdaTimeout
Code:
S3ObjectVersion: !Ref LambdaObjectVersion
S3Bucket: !Ref LambdaBucket
S3Key: !Ref LambdaObject
Role: !Ref LambdaRole
Handler: !Ref LambdaHandler
FunctionName: !Ref LambdaName
Runtime: !Ref LambdaRuntime #"python3.8"
PackageType: "Zip"
Layers:
- !Ref LambdaLayer1 #LayerInsights
- !Ref LambdaLayer2 #LayerSeleniumPython