DEV Community

loading...

Try to use Ruby and Sinatra app with AWS Lambda

wind-up-bird
・4 min read

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;
Screen Shot 2018-12-02 at 12.12.38.png

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:
Screen Shot 2018-12-02 at 12.21.02.png

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.
Screen Shot 2018-12-02 at 12.35.54.png

{
  "statusCode": 200,
  "body": "\"Hello from Lambda!\""
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Also, I have to S3 bucket in order to upload the application later.

> aws s3 mb s3://sample-sinatra-application
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

I will install the packages with bundler as below.

> cd serverless-sinatra-sample
> bundle install --deployment
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Deployment

I deploy the application with sam command;

> sam deploy --template-file packaged-template.yaml \
 --stack-name LambdaSinatra \
 --capabilities CAPABILITY_IAM
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

I will be able to confirm the result in AWS Console;
Screen Shot 2018-12-02 at 15.17.30.png

Confirm the app

You have to click SinatraAPI which locate Resources in overview.
And you can click the URL as below;
Screen Shot 2018-12-02 at 15.30.02.png

{"Output":"Hello World!"}
Enter fullscreen mode Exit fullscreen mode

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.

Discussion (2)

Collapse
dbelyaeff profile image
Dmitriy Belyaev

Congratulate you with the first article on Dev.TO!

Collapse
windupbird profile image
wind-up-bird Author

thanks!! I will do my best on dev.to!!!