I tried deploying a Docker container on Lambda with Serverless Framework

Overview

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.

Serverless Framework setup

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

Log in to the Serverless Framework site

If you do not have an account, please create one from the URL below.

https://app.serverless.com/

After creating an account, log in with the following command.

$ serverless login

AWS ECR setup

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

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 with your own environment.

If necessary, set the credentials with the —profile option of the aws command.

Build a Docker image

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 .

Create an AWS ECR repository and push the image

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.

Deploy with Serverless Framework.

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.

reference

https://www.serverless.com/blog/container-support-for-lambda/

Recommended Posts

I tried deploying a Docker container on Lambda with Serverless Framework
I tried running Ansible on a Docker container
I tried running a Docker container on AWS IoT Greengrass 2.0
I tried BIND with Docker
I tried running WordPress with docker preview on M1 Mac.
I tried to create a padrino development environment with Docker
Run React on a Docker container
I tried running Docker on Windows Server 2019
I tried using Scalar DL with Docker
I tried playing with BottomNavigationView a little ①
Microservices 101-I tried putting Docker on Ubuntu-
Starting with installing Docker on EC2 and running Yellowfin in a container
I tried to build a Firebase application development environment with Docker in 2020
I tried running the route search engine (OSRM) easily with a container
How to deploy a container on AWS Lambda
Create a Lambda Container Image based on Java 15
I made a Docker container to run Maven
Update container image with KUSANAGI Runs on Docker
I tried installing docker on an EC2 instance
I tried to break a block with java (1)
I tried running Java on a Mac terminal
I made a portfolio with Ruby On Rails
I tried Getting Started with Gradle on Heroku
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
AWS Lambda supports container images so I tried Puppeteer
Create a Java (Gradle) project with VS Code and develop it on a Docker container
How to deploy Java to AWS Lambda with Serverless Framework
Lambda on Terraform Container
Error encountered with notes when deploying docker on rails
I tried using Log4j2 on a Java EE server
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
I tried OCR processing a PDF file with Java
Proceed with the official Rust documentation on a Docker container (2. Program a number guessing game)
Create a Java (Maven) project with VS Code and develop it on a Docker container
Workspace setting location when connecting remotely with VS Code and working on a Docker container
I tried to make an introduction to PHP + MySQL with Docker
I tried to create a java8 development environment with Chocolatey
I tried adding a separator line to TabLayout on Android
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Serverless Framework template to schedule start / stop EC2 on Lambda
I tried to modernize a Java EE application with OpenShift.
[Rails] I tried to create a mini app with FullCalendar
I searched for a web framework with Gem in Ruby
[Docker] Delete only the volume associated with a specific container
I tried to create React.js × TypeScript × Material-UI on docker environment
Time is wrong with the application launched on the Docker container
Try Hello World using plain Java on a Docker container
I tried OCR processing a PDF file with Java part2
I tried DI with Ruby
I tried the Docker tutorial!
I tried UPSERT with PostgreSQL.
I tried Jets (ruby serverless)
I can no longer connect to a VM with a Docker container that can be connected via SSH
I was angry with proc_open (): fork failed when trying to composer update inside a Docker container
I tried to make a machine learning application with Dash (+ Docker) part2 ~ Basic way of writing Dash ~
I tried printing a form with Spring MVC and JasperReports 1/3 (JasperReports settings)
Install gem in Serverless Framework and AWS Lambda with Ruby environment
I tried printing a form with Spring MVC and JasperReports 3/3 (Spring MVC control)
I created a Docker image of a container for learning OpenAI Gym
I tried running a letter of credit transaction application with Corda 1