AWS lambdas - deploy quickstart

How to create a layer

Example to create a Pandas python layer

  1. Create a directory called python, and install a library locally via pip:
    1
    2
    3
    mkdir python
    cd python
    pip install -t pandas .
  2. Zip it on a parent folder.
    1
    2
    zip -r pandas_layer.zip .
    # Your zip file should have a folder named python with all content inside
  3. Upload your pandas_layer.zip to your s3 bucket.
  4. Next, deploy the AWS SAM template to create the layer:
    1
    sam deploy --guided
    For the Stack name, enter aws-sdk-layer. Enter your preferred AWS Region and accept the other defaults.
  5. After the deployment completes, the new Lambda layer is available to use. Check the available layers:
    1
    aws lambda list-layers

Example to create an AWS SDK nodeJS layer:

  1. Clone this blog post’s GitHub repo. From a terminal, execute:
    1
    2
    3
    4
    git clone https://github.com/aws-samples/aws-lambda-layers-aws-sam-examples
    # install it
    cd ./aws-sdk-layer
    npm install
  2. Create the layer directory defined in the AWS SAM template and the nodejs directory required by Lambda. Next, move the node_modules directory:
    1
    2
    mkdir -p ./layer/nodejs
    mv ./node_modules ./layer/nodejs
  3. On the AWS Console, navigate to “Lambda -> Layers” and create a layer. You may test its reference works with some dummy code like:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    import json
    import pandas as pd

    def lambda_handler(event, context):

    series = pd.Series([2, 4, 6, 8])
    return {
    'statusCode': 200,
    'body': json.dumps('Hello from Pandas, max value is '
    + str(series.max()) )
    }

How to create a lambda which uses layers

  1. Upload your lambda code on a zip to an S3 bucket.
  2. If you don’t need the layers, just remove them from the template, just like the X-Ray part. Then deploy this template on CloudFormation.
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
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: org-env-account-stack-templates-s3
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: MyTest/Lambda_Test.zip
LambdaObjectVersion:
Description: Version of the Lambda code.
Type: String
Default: abcdeFGHij12LmN3opqRSTuV45wx67yz
LambdaRole:
Description: IAM role with permissions required to execute the Lambda function.
Type: String
Default: arn:aws:iam::123456789012:role/service-role/ChromelessTest-role-a1bcd2ef
LambdaRuntime:
Description: IAM role with permissions required to execute the Lambda function.
Type: String
Default: python3.7
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:01234567890: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.7"
PackageType: "Zip"
Layers:
- !Ref LambdaLayer1 #LayerInsights
- !Ref LambdaLayer2 #LayerSeleniumPython