[RUBY] How to deploy a container on AWS Lambda

Introduction

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!

Target

--People who want to know about new features of AWS Lambda --People who want to know the flow of container deployment

YouTube video

If you want to check the video, please use this!

Container deployment conditions

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

Concrete example

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.

Node.js 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" ]

Ruby sample

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"]

Run Lambda locally

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!"}'

Deploy

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. _2020-12-07_20.20.44.png

Summary

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

How to deploy a container on AWS Lambda
How to get inside a container running on AWS Fargate
How to deploy a kotlin (java) app on AWS fargate
How to deploy a Rails application on AWS (article summary)
How to deploy on heroku
How to deploy Java to AWS Lambda with Serverless Framework
How to deploy jQuery on Rails
How to deploy Laravel on CentOS 7
How to make a Java container
How to deploy Bootstrap on Rails
How to deploy a simple Java Servlet app on Heroku
How to deploy
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
How to send push notifications on AWS
How to create a web server on an EC2 instance on AWS
Create a Lambda Container Image based on Java 15
How to get a heapdump from a Docker container
Lambda on Terraform Container
How to use Java framework with AWS Lambda! ??
How to save images on Heroku to S3 on AWS
How to build a Pytorch environment on Ubuntu
How to install Ruby on an EC2 instance on AWS
Memo to build a Servlet environment on AWS EC2
How to deploy to Heroku from a local docker image
List how to learn from Docker to AKS on AWS
How to make JavaScript work on a specific page
[Java] How to execute tasks on a regular basis
[AWS] How to check logs
How to leave a comment
[AWS] How to automatically deploy a Web application created with Rails 6 to ECR / ECS using CircleCI ① Preparation [Container deployment]
How to insert a video
How to create a method
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
How to connect a container created later to an existing network
How to create a lightweight container image for Java apps
How to create an application server on an EC2 instance on AWS
How to display a graph in Ruby on Rails (LazyHighChart)
I tried running a Docker container on AWS IoT Greengrass 2.0
How to install GNOME as a desktop environment on CentOS 7
How to add columns to a table
How to "hollow" View on Android
[Java] How to update Java on Windows
How to install ImageMagick on Windows 10
How to use Ruby on Rails
How to sign a Minecraft MOD
To beginners launching Docker on AWS
How to make a JDBC driver
Run React on a Docker container
Build a Minecraft server on AWS
Is Java on AWS Lambda slow?
How to run JavaFX on Docker
Hello World on AWS Lambda + Java
How to use Bio-Formats on Ubuntu 20.04
Deploy a war file on Heroku
[Java] How to create a folder
How to write a ternary operator
How to install MariaDB 10.4 on CentOS 8
Rails on Tiles (how to write)
[Swift] How to send a notification
Run PureScript on a Docker container