DEV Community

@kon_yu
@kon_yu

Posted on

Building AWS SAM environment and running Lambda with Ruby

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

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 with homebrew command like this

brew cask install docker
Enter fullscreen mode Exit fullscreen mode

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


`

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

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

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.

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

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

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)

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

sam local start-api


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

Enter fullscreen mode Exit fullscreen mode

{
"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.

Enter fullscreen mode Exit fullscreen mode

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

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.

Top comments (0)