When I try to use the AWS SDK with Ruby + Lambda, `sam local` is messed up.

What happened

I was using Lambda in a Ruby implementation. I was developing using the AWS SAM CLI. The development itself went smoothly, but when I changed to use the AWS SDK to add features, sam local became very slow. I found out that it would be strange to be so slow with such a simple sample app.

Sample project here

Just a quick conclusion

The AWS SDK is installed in the Lambda runtime for each environment, so it does not need to be included in the Lambda deployment package.

https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/lambda-runtimes.html

Especially in Ruby, ** you don't need to write "gem'aws-sdk','~> 3'" in Gemfile **! It's very unpleasant if you have a habit of writing the gems you normally use in the Gemfile, but there is no problem because you can require gems installed in global gems as they are.

Compare when "gem'aws-sdk','~> 3'" is written in Gemfile (before modification) and deleted (after modification)

The size of the package changes greatly depending on whether or not it is written in the Gemfile (the AWS SDK alone is close to 23M), so the difference is large.

By the way, before the correction, sam local barely worked, so only the comparison. After deployment, it didn't work unless I increased Lambda's memory and timeout time considerably.

Before correction

ʻInit Duration: 54544.65 ms`, which takes quite some time.

$ sam local invoke HelloWorldFunction --event events/event.json --env-vars env.json
Invoking app.lambda_handler (ruby2.5)

Fetching lambci/lambda:ruby2.5 Docker container image......
Mounting /home/ubuntu/environment/test/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: 22ddc38d-745a-1d37-0ce9-324760f75ec4 Version: $LATEST
END RequestId: 22ddc38d-745a-1d37-0ce9-324760f75ec4
REPORT RequestId: 22ddc38d-745a-1d37-0ce9-324760f75ec4  Init Duration: 54544.65 ms      Duration: 801.49 ms     Billed Duration: 900 ms Memory Size: 128 MBMax Memory Used: 60 MB

{"statusCode":200,"body":"{\"message\":\"t2.micro\"}"}

Revised

ʻInit Duration: 2266.99 ms` and improved!

$ sam local invoke HelloWorldFunction --event events/event.json --env-vars env.json
Invoking app.lambda_handler (ruby2.5)

Fetching lambci/lambda:ruby2.5 Docker container image......
Mounting /home/ubuntu/environment/test/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: a81dffd9-915d-13db-3ed7-4fe0e3b63464 Version: $LATEST
END RequestId: a81dffd9-915d-13db-3ed7-4fe0e3b63464
REPORT RequestId: a81dffd9-915d-13db-3ed7-4fe0e3b63464  Init Duration: 2266.99 ms       Duration: 557.91 ms     Billed Duration: 600 ms Memory Size: 128 MBMax Memory Used: 58 MB

{"statusCode":200,"body":"{\"message\":\"t2.micro\"}"}

Finally

If you use Lambda, you will have to pay extra if you do not pay attention to size and performance. Light processing time and package size. I would like to support it by using Lambda division and AWS Lambda Layers.

Recommended Posts

When I try to use the AWS SDK with Ruby + Lambda, `sam local` is messed up.
[Ruby] This is the solution. When should I use instance variables?
Rubocop gets angry when I try to fill zeros (fill 0s) with Ruby
I tried to visualize the access of Lambda → Athena with AWS X-Ray
When I try to sign up with devise, it automatically redirects to root_path
How to use Java framework with AWS Lambda! ??
I get an error when I try to use "^" or "$" in ruby ​​regular expression
Until you run a Java program with the AWS SDK local to Windows
A warning is displayed when trying to use a huge integer with the special variables $ 1, $ 2, $ 3 ...
The story I was addicted to when setting up STS
I tried to solve the problem of "multi-stage selection" with Ruby
When requested access to the resource is denied when pushing with Docker
Try running SlackBot made with Ruby x Sinatra on AWS Lambda
I got an IllegalAccessError when trying to use PowerMock with JUnit
What I did when the DB did not start with docker-compose up
I get a Ruby version error when I try to start Rails.
[IOS] What to do when the image is filled with one color
Problems I was addicted to when building the digdag environment with docker
[Ruby] I tried to diet the if statement code with the ternary operator
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
Things to keep in mind when using Apache PDFBox® with AWS Lambda
I tried to make full use of the CPU core in Ruby
Notes on what to do when EC2 is set up with t2.micro
[Ruby] How to prevent errors when nil is included in the operation
[Rails] [Parent-child relationship] I want to register the foreign key in the child with nil when the parent is deleted.
I want to use DBViewer with Eclipse 2018-12! !!
When I pushed to Docker Hub, I got requested access to the resource is denied
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
Initialize Ruby array with 0 like Java, that is, set the default value to 0
I tried to get the distance from the address string to the nearest station with ruby
When I try to run docker-compose, I get the error "Cannot locate specified Dockerfile: Dockerfile"
[Eclipse] I want to use the completion function, but I want to manage to confirm the completion with spaces.
Try local development of AWS (S3, DynamoDB) with AWS SDK for JavaScript and Docker
When I tried to use a Wacom tablet with ubuntu 20.04, I didn't recognize it.
I want to solve the problem that JS is not displayed properly unless reloaded when transitioning with Turbolinks: link_to
You can do it right away with Serverless Framework Serverless on AWS (API + Lambda [java] is easy to set up)