A long time ago, AWS Lambda supported container deployment. AWS Lambda now supports container images as a packaging format
This time, I'll show you how to deploy a container image on AWS Lambda!
--People who want to know about new features of AWS Lambda --People who want to know the flow of container deployment
If you want to check the video, please use this!
Lambda now supports container deployment, but not all containers are fine. You need to comply with the Runtime API for Lambda provided by AWS, and you need to create a container by one of the following methods.
Method 1:Use the base image provided by AWS
Method 2:Use an image that contains the Runtime API
You can use it by selecting the runtime you want to use from the following sites. Runtime support for Lambda container images
This time, I will use the base image and actually deploy the container to Lambda. Let's first look at a simple Node.js sample, then look at a practical Ruby sample.
If you write a simple sample, it will look like this.
#Get base image
FROM public.ecr.aws/lambda/nodejs:12
#The app that defines the Lambda function.Put js in the root directory when running Lambda
COPY app.js ${LAMBDA_TASK_ROOT}
# app.Execute js handler function
CMD [ "app.handler" ]
This sample creates a Lambda function to connect to MySQL from Lambda. Before supporting containers, it required a lot of tedious steps related to native extenstion, but now it's easier to use with support for containers.
FROM amazon/aws-lambda-ruby:2.7
WORKDIR ${LAMBDA_TASK_ROOT}
#Install the MySQL related dependencies
RUN yum install -y mysql-devel gcc-c++ make
COPY . ${LAMBDA_TASK_ROOT}
RUN bundle config --global silence_root_warning 1
RUN bundle config set path 'vendor/bundle'
RUN bundle install
CMD ["app.App::Handler.process"]
By containerizing it, you can actually try it locally.
If you want to open port 9000 and try it, do as follows.
docker run -p 9000:8080 <image name>
And you can actually check it by POSTing as follows.
Here, /2015-03-31/functions/function/invocations "
is fixed.
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" \
-d '{"payload":"hello world!"}'
First you need to deploy the container image to Amazon ECR. I will omit it this time. The following is detailed. Push Docker image (https://docs.aws.amazon.com/ja_jp/AmazonECR/latest/userguide/docker-push-ecr-image.html)
If you check how to create a Lambda function, there is an item called container image, so select that and use it.
This time, I explained how to deploy a container on AWS Lambda. If you get a rough idea through this article, I think it will be smooth when you set it yourself.
Also, if you use a container with AWS Lambda, it will take about 2 to 5 seconds for the first startup, but after that it will start smoothly. It's a new feature that expands the range of use of AWS Lambda, so please play with it!
Recommended Posts