This article is the 24th day post of Serverless Advent Calendar 2020.
Serverless Advent Calendar 2020 - Qiita
The other day, AWS Lambda is now able to run Docker containers.
AWS Lambda now supports container images as a packaging format
In addition, Serverless Framework supports this, and it is now possible to deploy with the following description.
service: example-service
provider:
name: aws
functions:
someFunction:
image: <account>.dkr.ecr.<region>.amazonaws.com/<repository>@<digest>
Container Image Support for AWS Lambda
This time, I tried this function.
Install Serverless Framework with the following command.
$ npm install -g serverless
This time, we have confirmed the operation with the following versions.
$ serverless version
Framework Core: 2.15.0
Plugin: 4.2.0
SDK: 2.3.2
Components: 3.4.3
If you do not have an account, please create one from the URL below.
After creating an account, log in with the following command.
$ serverless login
In this article, it is assumed that the Docker image specified by Serverless Framework is in the AWS ECR repository.
Describes the steps to upload a Docker image to AWS ECR.
Log in to AWS ECR with the following command.
$ aws ecr get-login-password --region <region> \
| docker login --username AWS \
--password-stdin <account>.dkr.ecr.<region>.amazonaws.com
Replace
If necessary, set the credentials with the —profile
option of the aws
command.
Prepare the following Dockerfile.
FROM public.ecr.aws/lambda/nodejs:12
ARG FUNCTION_DIR="/var/task"
# Create function directory
RUN mkdir -p ${FUNCTION_DIR}
# Copy handler function and package.json
COPY index.js ${FUNCTION_DIR}
COPY package.json ${FUNCTION_DIR}
# Install NPM dependencies for function
RUN npm install
# Set the CMD to your handler
CMD [ "index.handler" ]
This build requires package.json
and index.js
.
Generate package.json
with the following command.
$ npm init
Create a index.js
with the following content.
exports.handler = async function(event, context) {
console.log("EVENT: \n" + JSON.stringify(event, null, 2))
return context.logStreamName
};
Now that you are ready, specify the image name in the recommended format <service>-<stage>-<functionName>
and build.
$ docker build -t slssample-d-sample .
Run the following command to create a repository in AWS ECR.
$ aws ecr create-repository --repository-name slssample-d-sample --image-scanning-configuration scanOnPush=true
Use the command below to push the image to the repository you just created.
$ docker tag slssample-d-sample:latest \
<account>.dkr.ecr.<region>.amazonaws.com/slssample-d-sample:latest
$ docker push <account>.dkr.ecr.<region>.amazonaws.com/slssample-d-sample:latest
When you execute the command, the following information will be output.
latest: digest: sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx size: 2201
Make a note of the part of the digest sha256: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
.
Now that the Docker image is ready, deploy the Lambda function.
Create a serverless.yml
file with the following content.
service: sls-docker-image
provider:
name: aws
region: ap-northeast-1
functions:
someFunction:
image: <account>.dkr.ecr.<region>.amazonaws.com/slssample-d-sample@sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The reference information of the Docker image is described in the functions.someFunction.image part.
Write the digest you noted earlier as @ sha256: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
.
Execute the following command to run the Serverless Framework deployment process.
$ sls deploy
You can see that the deployed Lambda runs normally.
https://www.serverless.com/blog/container-support-for-lambda/
Recommended Posts