AWS_EN

Create Lambda layer using CFN

スポンサーリンク
Create Lambda layers using CloudFormation AWS_EN
スポンサーリンク
スポンサーリンク

Creating Lambda Layer using CloudFormation

This page reviews how to create a Lambda layer in CloudFormation.

Lambda layers provide a convenient way to package libraries and other dependencies that you can use with your Lambda functions.

Creating and sharing Lambda layers

Create a Lambda layer for Lambda functions inside and outside the VPC and verify actual behavior.

Environment

Diagram of create Lambda layers using CloudFormation.

Create one Lambda function each inside and outside the VPC.
Create a Function URL for each function and configure it so that it can be accessed over the Internet.
The runtime environment for Lambda functions is Python 3.8.
Create a Lambda layer for both functions.

CloudFormation template files

The above configuration is built using CloudFormation.
The CloudFormation template is located at the following URL

https://github.com/awstut-an-r/awstut-fa/tree/main/040

Explanation of key points of the template files

The majority of this configuration is similar to the following page.

For example, please refer to the above page for the Lambda Function URL and how to place the Lambda function in the VPC.

This page will focus on the contents related to the Lambda layer.

Zip Lambda layer package and upload it to S3 bucket

Prepare packages to be deployed in the Lambda layer.
Package configurations are defined for each language.
Please refer to the following page for details.

Working with Lambda layers - AWS Lambda
This section describes how to use AWS layers to separate your application code from its dependencies.

This time, Python is selected as the runtime environment, so the following directory structure is prepared.

python
└── mylayer 
    ├── __init__.py
    └── mylayer.pyCode language: plaintext (plaintext)

Create mylayer.py as a module to be read from the Lambda function.
The contents of the module are as follows.

def myfunc(func_name):
    return 'hello from {func_name}.'.format(func_name=func_name)
Code language: Python (python)

This is a simple function.
It uses the string passed as an argument to create a new string and return it.

Once the package is ready, the next step is to zip the package.

$ zip -r layer.zip python
  adding: python/ (stored 0%)
  adding: python/mylayer/ (stored 0%)
  adding: python/mylayer/mylayer.py (deflated 29%)
  adding: python/mylayer/__init__.py (stored 0%)
Code language: Bash (bash)

Upload the ZIP file to the S3 bucket.
The following is an example of uploading with AWS CLI.

$ aws s3 cp layer.zip s3://[s3-bucket-name]/
upload: ./layer.zip to s3://[s3-bucket-name]/layer.zip
Code language: Bash (bash)

Create Lambda layer

Confirm the Lambda layer.

Resources:
  LambdaLayer:
    Type: AWS::Lambda::LayerVersion
    Properties:
      CompatibleArchitectures:
        - arm64
        - x86_64
      CompatibleRuntimes:
        - python3.8
      Content:
        S3Bucket: !Ref S3Bucket
        S3Key: !Ref S3Key
      Description: !Ref Prefix
      LayerName: !Ref Prefix
Code language: YAML (yaml)

The CompatibleArchitectures property allows you to specify the architectures of functions that can use this layer.
As described below, we will create two Lambda functions. We will create two Lambda functions, one on ARM64 and the other on x86_64. Therefore, two different architectures are set.

The CompatibleRuntimes property allows specifying the runtime environment in which this layer can be used.
As described later, the runtime environment for the two functions to be created this time is Python 3.8. Therefore, the same environment is set.

In the Content property, set information about the packages to be included in the layer.
Packages are zipped and placed in an S3 bucket, and the object is specified. Specify the file name and bucket name of the package file mentioned earlier.

Apply Lambda layer to Lambda function

Check out how to specify a Lambda layer to a Lambda function.

Resources:
  Function1:
    Type: AWS::Lambda::Function
    Properties:
      Architectures:
        - arm64
      Code:
        ZipFile: |
          from mylayer import mylayer
          
          def lambda_handler(event, context):
            return mylayer.myfunc('func1')
      FunctionName: !Sub "${Prefix}-function-01"
      Handler: !Ref Handler
      Layers:
        - !Ref LambdaLayer
      Runtime: !Ref Runtime
      Role: !GetAtt FunctionRole1.Arn
Code language: YAML (yaml)

Two functions are created, but they are almost identical, so we will only cover Function 1.

The key point is the Layers property.
By specifying the layers we just checked, we can use the packages in the Lambda layer.

There are three differences between Function 1 and Function 2.
The first is architecture. ARM64 and x86_64 are set respectively.
The second is the settings related to VPC. Each function is configured to run outside the VPC and inside the VPC, respectively.
The third is the function to be executed. When executing the aforementioned function (myfunc), “func1” and “func2” are passed as arguments, respectively.

Architecting

Using CloudFormation, we will build this environment and check the actual behavior.

Create CloudFormation stacks and check resources in stacks

Create a CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following page

After checking the resources in each stack, information on the main resources created this time is as follows

  • Lambda layer: fa-040
  • Function URL for Lambda function 1: https://hvbnf6cbrsxwd53u5xgmocpvne0rgnzv.lambda-url.ap-northeast-1.on.aws/
  • Function URL for Lambda function 2: https://nfnixjukfrdu2pctiqmizkdj6a0iynea.lambda-url.ap-northeast-1.on.aws/

The Lambda layer is also confirmed from the AWS Management Console.

Lambda layers.

The Lambda layer has been successfully created.
The target runtime environment and architecture are also as specified in the CloudFormation template.

Functions related to this layer can also be confirmed.
The two functions we have created can be confirmed.
As mentioned above, even functions configured on different architectures can refer to the unified layer.

Confirmation of Operation

Now that everything is ready, access each Function URL.
First, let’s look at Function 1.

The Result of Lambda Function 1 Invocation.

The response is returned normally.
As you can see, even functions outside the VPC can refer to the Lambda layer and execute the function.

Next is function 2.

The Result of Lambda Function 2 Invocation.

This function also returned a normal response.
In this way, even functions inside the VPC can refer to the Lambda layer and execute the function.

Summary

We have confirmed how to create a Lambda layer using CloudFormation and how to apply it to a Lambda function.
We confirmed that a Lambda layer can be applied regardless of the architecture of the function side or whether it is inside or outside the VPC.

タイトルとURLをコピーしました