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
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.
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.
ʻ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\"}"}
ʻ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\"}"}
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