Note:
This is the article I translated myself from Qiita blog
Introduction
There were a lot of announcement in AWS re:invent in 2018.
Especially, related to AWS Lambda,
We have used the Lambda as the back end of ALB;
Lambda functions as targets for Application Load Balancers | Networking & Content Delivery
We don't have to package each functions anymore because Lambda Layers have launched;
New for AWS Lambda – Use Any Programming Language and Share Common Components | AWS News Blog
Today, I would like to introduce Ruby with AWS Lambda in this article.
Announcing Ruby Support for AWS Lambda | AWS Compute Blog
Hello World
First of all, I will do the tutorial in the blog.
I make a new function as below;
I'm able to choose the runtime of ruby. But you can select the Ruby 2.5
now.
Then, the sample code was shown in the lambda as usual:
So, I will execute this function.
When you click the Test
button you will find the new window and you should input the Event Name
. And then, you have to create the TEST clicking Create
button.
I will execute the test event I made before.
{
"statusCode": 200,
"body": "\"Hello from Lambda!\""
}
I can see it and it seems ok.
Deploy the Web application
Note: I used ap-northeast-1 (Tokyo) region.
Next, I try to deploy Web application with Sinatra which is written in the blog.
GitHub - Demo code for running Ruby Sinatra on AWS Lambda
We will use not only AWS lambda but also API Gateway and DynamoDB.
Prerequisite
I will use AWS SAM and install it in advance with pip command as below:
> pip install aws-sam-cli
Also, I have to S3 bucket in order to upload the application later.
> aws s3 mb s3://sample-sinatra-application
In this time, I set the bucket name as sample-sinatra-application
.
Clone sample application from Github
Clone the sample code.
> git clone https://github.com/aws-samples/serverless-sinatra-sample
I will install the packages with bundler
as below.
> cd serverless-sinatra-sample
> bundle install --deployment
Packaging
Then, I upload the application to S3 bucket I made using this command;
> sam package --template-file template.yaml \
--output-template-file packaged-template.yaml \
--s3-bucket sample-sinatra-application
You will find the result as below;
Uploading to XXXXXXXXXXXXXXXXXXXX 64727 / 64727.0 (100.00%)
Successfully packaged artifacts and wrote output template to file packaged-template.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /path/to/serverless-sinatra-sample/packaged-template.yaml --stack-name <YOUR STACK NAME>
Deployment
I deploy the application with sam command;
> sam deploy --template-file packaged-template.yaml \
--stack-name LambdaSinatra \
--capabilities CAPABILITY_IAM
You will find the result as below;
Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - LambdaSinatra
I will be able to confirm the result in AWS Console;
Confirm the app
You have to click SinatraAPI
which locate Resources
in overview.
And you can click the URL as below;
Example1: Top_Page
https://XXXXX.execute-api.ap-northeast-1.amazonaws.com/Prod/
Example2: API
https://XXXXX.execute-api.ap-northeast-1.amazonaws.com/Prod/hello-world
{"Output":"Hello World!"}
- Example3:* Feedback_Page https://XXXXX.execute-api.ap-northeast-1.amazonaws.com/Prod/feedback
Conclusion
It is the first time for me to write down a article in English. But I noticed that I do not need to do because the tutorial is written in English...
Now that I write, I will publish this article.
Top comments (2)
Congratulate you with the first article on Dev.TO!
thanks!! I will do my best on dev.to!!!