Install gem in Serverless Framework and AWS Lambda with Ruby environment

How to deploy AWS Lambda Ruby script that requires gem installation with Serverless Framework.

Procedure outline

You can easily do it by inserting a plugin.

  1. serverless plugin install -n serverless-ruby-layer
  2. Create Gemfile
  3. After that, if you deploy normally, it will do various things without permission

Detailed procedure

Serverless Framework service creation

$ serverless create --template aws-ruby
Serverless: Generating boilerplate...
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v2.16.1
 -------'

Serverless: Successfully generated boilerplate for template: "aws-ruby"
Serverless: NOTE: Please update the "service" property in serverless.yml with your service name

Three files will be generated.

.gitignore
handler.rb
serverless.yml

Plugin installation

Install a plugin called serverless-ruby-layer.

$ serverless plugin install -n serverless-ruby-layer

The following files and directories will increase.

node_modules
package.json
package-lock.json

Since Serverless Framework is implemented in Node.js, it seems that node_modules and package.json exist even though it is a Ruby project.

Source code

serverless.yml

serverless.yml has the following contents. The description of plugins is added without permission when the plugin is installed.

service: sample

frameworkVersion: '2'

provider:
  name: aws
  runtime: ruby2.7
  region: ap-northeast-1

functions:
  hello:
    handler: handler.hello

plugins:
  - serverless-ruby-layer

Gemfile

Create a Gemfile with the following contents.

Let's use holiday_japan as a sample of gem. A gem that determines Japanese holidays.

source "https://rubygems.org"

gem 'holiday_japan'

Ruby source code

handler.rb

require 'json'
require 'holiday_japan'

def hello(event:, context:)
  holidayName = HolidayJapan.name(Date.new(2021, 8, 8))
  puts(holidayName) #To CloudWatch"Mountain day"Is written out
end

Deploy

If you create it so far and then deploy it with the serverless command, not only the Lambda itself but also the serverless command will automatically create a gem-installed image and upload it as a layer of AWS Lambda.

$ serverless deploy -v

Run

Looking at the deployment results in the AWS Management Console, it looks like this:

Lambda

image.png

Layer

image.png

Execution result CloudWatch Logs

image.png

Link

-(Python version) Pip installation in Serverless Framework and AWS Lambda with Python environment

Recommended Posts

Install gem in Serverless Framework and AWS Lambda with Ruby environment
How to deploy Java to AWS Lambda with Serverless Framework
I searched for a web framework with Gem in Ruby
Create and integrate Slack App and AWS Lambda (for ruby) in 30 minutes
Install ruby serial port in WSL environment
Postgresql gem doesn't install properly in Ruby 3.0
Ruby and Gem
How to use Java framework with AWS Lambda! ??
Be careful with requests and responses when using the Serverless Framework in Java
Install Ruby 3.0.0 with asdf
[AWS Lambda] Resize the image saved in S3 and save it in another S3 (using Ruby)
Install rbenv with apt on ubuntu and put ruby
Install Rust in WSL2 Ubuntu environment and build WASM build environment
Build WordPress environment with Docker (Local) and AWS (Production)
Notify Slack of AWS bills daily with Lambda for Ruby
Log aggregation and analysis (working with AWS Athena in Java)
Create a SlackBot with AWS lambda & API Gateway in Java
Install Ruby 3.0.0 Preview 1 with a combination of Homebrew and rbenv
Write DiscordBot to Spreadsheets Write in Ruby and run with Docker
AWS CLI install in Ubuntu 20.04
Update Ruby in Unicorn environment
Build AWS Lambda with Quarkus
Easily create virtual S3 and test the integration between AWS Lambda and S3 services in your local environment
Examine the system information of AWS Lambda operating environment in Java
Try running SlackBot made with Ruby x Sinatra on AWS Lambda
How to install the language used in Ubuntu and how to build the environment
I tried deploying a Docker container on Lambda with Serverless Framework
Create a periodical program with Ruby x AWS Lambda x CloudWatch Events
Install Rails in the development environment and create a new application
A story stuck with log output in Docker + Play framework environment
Install Ubuntu Server 20.04 in VirtualBox on Mac and connect with SSH
[Ruby on Rails] How to log in with only your name and password using the gem devise
Why I have 10 years of experience in Ruby and don't run the [absolutely] gem install rails command
Install Ruby on MSYS2 with pacman
Install Java and Tomcat with Ansible
Install CentOS Stream 8 in Hyper-V environment
[Ruby] then keyword and case in
How to install Bootstrap in Ruby
Write keys and values in Ruby
Install laravel/Dusk in docker environment (laravel6)
Install ruby on Ubuntu 20.04 with rbenv
[Firestore] Extract the collection with where condition in Ruby and delete the record
Things to keep in mind when using Apache PDFBox® with AWS Lambda
Getting Started with Micronaut 2.x ~ Native Build and Deploy to AWS Lambda ~
I wrote a Lambda function in Java and deployed it with SAM