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.
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.
Let's actually move the local emulation of HttpAPI.
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...
Select Python 3
from them.
Show ALL Definitions ...
. You should see Python 3
.
Then the template for the development container will be created in the folder.
Since the AWS SAM CLI uses Docker, it mounts docker.sock on the host.
Add this line to the same hierarchy as name
and build
in .devcontainer.json
."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
root@***:/workspaces/sam-sample# pip3 install aws-sam-cli
root@***:/workspaces/sam-sample# sam --version
SAM CLI, version 1.7.0
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
.
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
.
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.
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.
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