Try to use Ruby and Sinatra app with AWS Lambda 
Tags: aws, beginners, serverless, ruby
Published: 2018-12-11 13:56:06

---
title: Try to use Ruby and Sinatra app with AWS Lambda 
published: true
description: Try to use Ruby and Sinatra app with AWS Lambda
tags: aws, beginner, serverless, ruby
---

Note:
This is the article I translated myself from [Qiita blog](https://qiita.com/wind-up-bird/items/08b911a64baea962b1b8)

# Introduction

There were a lot of announcement in [AWS re:invent](https://reinvent.awsevents.com/) 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](https://aws.amazon.com/blogs/networking-and-content-delivery/lambda-functions-as-targets-for-application-load-balancers/)

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](https://aws.amazon.com/jp/blogs/aws/new-for-aws-lambda-use-any-programming-language-and-share-common-components/)

Today, I would like to introduce Ruby with AWS Lambda in this article.
[Announcing Ruby Support for AWS Lambda | AWS Compute Blog](https://aws.amazon.com/blogs/compute/announcing-ruby-support-for-aws-lambda/)

# Hello World

First of all, I will do the [tutorial](https://aws.amazon.com/blogs/compute/announcing-ruby-support-for-aws-lambda/) in the blog.
I make a new function as below;
![Screen Shot 2018-12-02 at 12.12.38.png](https://qiita-image-store.s3.amazonaws.com/0/118641/fccff7b3-9b64-ae72-667f-826f0d2fdbde.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](https://qiita-image-store.s3.amazonaws.com/0/118641/15670f8a-93bd-c476-353b-c955a8702de1.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](https://qiita-image-store.s3.amazonaws.com/0/118641/23a9e620-7d44-16a4-43d7-05aa89bc1dd3.png)

```json
{
  "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](https://github.com/aws-samples/serverless-sinatra-sample)

We will use not only AWS lambda but also [API Gateway](https://aws.amazon.com/api-gateway/) and [DynamoDB](https://aws.amazon.com/dynamodb/).

### Prerequisite

I will use [AWS SAM](https://aws.amazon.com/serverless/sam/) and install it in advance with pip command as below:

```bash
> pip install aws-sam-cli
```

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

```bash
> 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](https://github.com/aws-samples/serverless-sinatra-sample).

```bash
> git clone https://github.com/aws-samples/serverless-sinatra-sample
```

I will install the packages with `bundler` as below.

```bash
> cd serverless-sinatra-sample
> bundle install --deployment
```

### Packaging

Then, I upload the application to S3 bucket I made using this command;

```bash
> 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;

```bash
> 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;
![Screen Shot 2018-12-02 at 15.17.30.png](https://qiita-image-store.s3.amazonaws.com/0/118641/94413ef2-30e6-3323-3a5c-b59cace3502f.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](https://qiita-image-store.s3.amazonaws.com/0/118641/948b4e95-31ba-f8b5-264c-b12a433e7f30.png)

- **Example1:** **Top_Page**
https://XXXXX.execute-api.ap-northeast-1.amazonaws.com/Prod/
![Screen Shot 2018-12-02 at 15.28.35.png](https://qiita-image-store.s3.amazonaws.com/0/118641/033be1a2-5b62-98de-00be-06591cf7029e.png)

- **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
![Screen Shot 2018-12-02 at 15.35.54.png](https://qiita-image-store.s3.amazonaws.com/0/118641/b44e12ae-8519-d3fc-600e-d853c352b688.png)

# 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.