AWS SAM CLI now supports local emulation of HttpApi, so I tried using it on WSL2 Docker

Introduction

Are you using the AWS SAM CLI? Speaking of AWS SAM CLI, in July of this year official release / whats-new / 2020/07 / announcing-aws-serverless-application-model-cli-generally-available-production-use /) Although it has just been done, AWS Lambda can do everything from local emulation of AWS Lambda to environment construction. It's a powerful tool for using.

Such an AWS SAM CLI was released today (October 30, 2020) v1.7.0 Now you can locally emulate an environment that combines HttpApi and Lambda as shown in the figure below.

httpapi.png

There has been support for HttpApi for some time

Actually, HttpApi has been supported for a long time, and it was released on December 5, 2019 v0.37.0 I was able to build it on AWS at the stage of. Document is also available, so if you just build the environment, there is no problem. I was able to use it, but the local emulation, which is the main feature of SAM, was out of scope.

Operation check

Let's actually move the local emulation of HttpAPI.

Operating environment

Development container construction

I don't want to install it locally, so I will build it to be completed within Docker on WSL2. First, create a working folder on WSL and open it with VS Code. Enter the command in powershell.

wsl #Switch to WSL
mkdir /path/to/your/workspace/sam-sample #Create a working folder
code -r /path/to/your/workspace/sam-sample #Open your working folder with VS Code

This will open VS Code, then open the command palette with Ctrl + Shift + p and create a template for your development container.

>Remote-Containers:Add Development Container Configuration Files...

image.png Select Python 3 from them.

"mounts": ["source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind"],

Open the defined environment. Open the command palette with Ctrl + Shift + p and open the defined environment.

>Remote-Containers:Reopen in Container

image.png

Installing the AWS SAM CLI

root@***:/workspaces/sam-sample# pip3 install aws-sam-cli
root@***:/workspaces/sam-sample# sam --version
SAM CLI, version 1.7.0

Project creation

Initialize the project with the sam init command.

root@***:/workspaces/sam-sample# sam init
Which template source would you like to use?
        1 - AWS Quick Start Templates
        2 - Custom Template Location
Choice: 1

Which runtime would you like to use?
        1 - nodejs12.x
        2 - python3.8
        3 - ruby2.7
        4 - go1.x
        5 - java11
        6 - dotnetcore3.1
        7 - nodejs10.x
        8 - python3.7
        9 - python3.6
        10 - python2.7
        11 - ruby2.5
        12 - java8.al2
        13 - java8
        14 - dotnetcore2.1
Runtime: 2

Project name [sam-app]: 

Cloning app templates from https://github.com/awslabs/aws-sam-cli-app-templates.git

AWS quick start application templates:
        1 - Hello World Example
        2 - EventBridge Hello World
        3 - EventBridge App from scratch (100+ Event Schemas)
        4 - Step Functions Sample App (Stock Trader)
        5 - Elastic File System Sample App
Template selection: 1

-----------------------
Generating application:
-----------------------
Name: sam-app
Runtime: python3.8
Dependency Manager: pip
Application Template: hello-world
Output Directory: .

Next steps can be found in the README file at ./sam-app/README.md

The template for Hello World is completed. In this state, it is still configured to use the previous RestAPI, so modify it a little. That said, all you have to do is switch the Type: Api part to Type: HttpApi. image.png

Run

Run with sam local start-api.

root@***:/workspaces/sam-sample# cd sam-app/
root@***:/workspaces/sam-sample/sam-app# sam local start-api -v /path/to/your/workspace/sam-sample/sam-app
Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2020-10-30 06:25:31  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

You can see that the Lambda function of / hello that was switched to Type: HttpApi earlier is recognized.

Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]

When you access it with a browser, hello world is displayed correctly, and you can confirm the support of HttApi. image.png

By the way, the -v option added at the time of sam local start-api is important this time, so be careful because you need to specify the path of the project on the host when executing AWS SAM CLI in Docker. please.

sam local start-api --help
...
  -v, --docker-volume-basedir TEXT
                                  Specifies the location basedir where the SAM
                                  file exists. If the Docker is running on a
                                  remote machine, you must mount the path
                                  where the SAM file exists on the docker
                                  machine and modify this value to match the
                                  remote machine.

Try with an earlier version

root@***:/workspaces/sam-sample/sam-app# pip3 install aws-sam-cli==1.6.2
root@***:/workspaces/sam-sample/sam-app# sam --version
SAM CLI, version 1.6.2
root@***:/workspaces/sam-sample/sam-app# sam local start-api -v /path/to/your/workspace/sam-sample/sam-app
Error: Template does not have any APIs connected to Lambda functions

In previous versions it wasn't recognized as an API in the first place.

Digression

The AWS SAM CLI HttpApi support I mentioned this time has contributed to PR of this function. I just publicized the missing functions for the PR that was originally open, but I was really nervous because it was my first OSS activity. .. .. I also learned how to manage a repository called OSS, which is committed by an unspecified person, so I realized that OSS activities are important. Even those who have never done OSS activities are encouraged to take on the challenge without hesitation.

Recommended Posts

AWS SAM CLI now supports local emulation of HttpApi, so I tried using it on WSL2 Docker
I tried running a Docker container on AWS IoT Greengrass 2.0
AWS Lambda says Container Image Support, so pseudo Cloud Run
I tried deploying a Docker container on Lambda with Serverless Framework
I tried using Pari gp container
AWS SAM CLI now supports local emulation of HttpApi, so I tried using it on WSL2 Docker
I tried to visualize the access of Lambda → Athena with AWS X-Ray
I tried to summarize Java lambda expressions
Java9 was included, so I tried jshell.
How to deploy a container on AWS Lambda
I tried running Ansible on a Docker container
First AWS Lambda (I tried to see what kind of environment it works in)
I tried using Docker Desktop for Windows on Windows 10 Home
I tried to build the environment of WSL2 + Docker + VSCode
I tried running a Docker container on AWS IoT Greengrass 2.0
Know the convenience of Docker (-compose) now (information list that I referred to when using it)
I tried using anakia + Jing now
Run the AWS CLI on Docker
Using Docker on Windows10 Home WSL2
I tried using GoogleHttpClient of Java