Building AWS SAM environment and running Lambda with Ruby
Tags: aws, sam, ruby, lambda
Published: 2020-05-22 02:11:46

---
title: Building AWS SAM environment and running Lambda with Ruby
published: true
description: 
tags: AWS, sam, Ruby, lambda 
---

# Building AWS SAM environment
Build the environment of AWS SAM and basically proceed based on the official AWS document, and run Lambda with Ruby.

[Installing the AWS SAM CLI - AWS Serverless Application Model](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)

## Operating environment
MacOS 10.14.2 Mojave

## Prepare what you need for the AWS SAM CLI
* Docker.
* Pip commands in Python 2.7 or 3.6
* AWS CLI: Command Line Tools

**Installation of Docker**
Docker for mac has already been installed, but for your reference, you can also install Docker by using [Homebrew Cask](http://caskroom.io/) with homebrew command like this

```
brew cask install docker
```

**Check if Pip is installed**
If python 2.7 or 3 system is installed and pip is installed, I will check the version of mac to see if pip can be used by default.

```
pip --version
pip 18.1 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
````

Apparently there is a pip in the default python environment on mac, so I'll just use the 2.7 version.

**AWS CLI: Installing the command line tools**
To install awscli, please refer to the official page here.
[Installing the AWS Command Line Interface - AWS Command Line Interface](https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-chap-install.html)

I just need one command from here.

```
pip install awscli --upgrade --user

# --upgrade option updates already installed awscli
# --user option doesn't seem to be placed in system areas such as executables. The AWS command is here /usr/local/bin/aws
# You don't need these options if you want to use this command on a bare server
```

**Configuring with the AWS CLI**

From [Quick Start - AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-quick-start.html)

> Before You Begin
> You must have an AWS account with an IAM user that has administrator permissions.

You must have an AWS account with an IAM user that has administrator permissions.

*When deploying, I think I should have only the authority of Cloudformation, but what is the authority required for local development, and I do not want to attach the authority of Cloudformation for the developer of the authority not to deploy, I will investigate such a requirement separately. The AWS documentation is not very good at explaining things*.

Run the configuration from the CLI. I set the region to ap-northeast-1 (Tokyo region) for now.

```
aws configure
AWS Access Key ID [****************AAAA]:
AWS Secret Access Key [********************BBBBB]:
Default region name [None]: ap-northeast-1
Default output format [None]:
````

If you are building python environment using virtualenv, you may want to read [How to install AWS Command Line Interface in virtual environment - AWS Command Line Interface](https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/install-virtualenv.html).


## Installing the AWS SAM CLI
Install AWS SAM CLI since I have everything to install.
It's a one-shot pip command, just like the AWS CLI.

```
pip install --user --upgrade aws-sam-cli
# --user and --upgrade options also mean the same as aws-cli
```

# Running Lambda locally using the AWS SAM CLI
Build the Lambda development environment based on this official document.
[Quick Start - AWS Serverless Application Model](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-quick-start.html)

Create a directory for sample and move it

```
mkdir sam_sample && cd sam_sample
```

In QuickStart, I write in Python, but I try to build in Ruby [Building Lambda Functions with Ruby - AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/lambda-ruby.html)

## Initialization

When you initialize a sam project in Ruby, a directory called sam-app is created and a lambda template file is created. 

```
sam init --runtime ruby2.5
````

I have a totally unknown http client in my Gemfile called [httparty](https://github.com/jnunemaker/httparty)

```
cd sam-app
sam_sample/sam-app " sam build --use-container
2019-01-10 20:39:56 Starting Build inside a container
2019-01-10 20:39:56 Found credentials in shared credentials file: ~/.aws/credentials
2019-01-10 20:39:56 Building resource 'HelloWorldFunction'

# error if you don't enter access token, secret key or region settings in AWS-CLI
```

*If you use "sam build --use-container" when building, it is written that it will run in a container close to the environment where AWS Lambda actually runs or something like that, but I honestly don't know because I use Docker at runtime even without options.

### Local startup

#### Start with the API Gateway

[Running API Gateway Locally - AWS Serverless Application Model](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-using-start-api.html).

```
sam local start-api
```

Access the following in your browser
http://127.0.0.1:3000/hello

```
{
"message": "Hello World!",
"location": "175.131.178.24\fn"
}
```

#### Start alone.

Run with reference to [Invoking Functions Locally - AWS Serverless Application Model](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-using-invoke.html)

If you look at the template.yaml file under sam-app, you'll see that
The key 'HelloWorldFunction' just below Resources is the setting for each Lambda function.

```
Resources:
    HelloWorldFunction:
        Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
        CodeUri: hello_world/
            CodeUri: hello_world/
            Handler: app.lambda_handler
            Runtime: ruby2.5

Runtime: ruby2.5


Passing an object from the standard input and invoking it

```
echo "{}" | sam local invoke "HelloWorldFunction"

START RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST
END RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72
REPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Duration: 656.91 ms Billed Duration: 700 ms Memory Size: 128 MB Max Memory Used: 29 MB
{"statusCode":200, "body":"{\if\fnDroid}":{\fnDroid}"Hello World!",{\fnDroid}"location\fnDroid}":{175.131.178.24\fnDroid}"}
```

I measured it with the time command and it took about 6.5 seconds. It's pretty slow.
When `sam build` is executed, it is slow with or without `--use-container` It's so easy to develop by executing the test code described next.