In the project, I decided to create a simple API using Python, so I will prepare the environment to create the API quickly.
Editor ... VS Code OS…Windows10 Python 3.8 installed AWS CLI installed
Installing AWS CLI Version 2 on Windows-AWS Command Line Interface
According to the Official Document
--Single deployment configuration --AWS CloudFormation extensions --Built-in best practices --Local debugging and testing --Tight integration with development tools
Is a merit. Personally, being able to debug lambda locally was a big advantage. Since the actual situation is CloudFormation, it is also highly customizable.
Follow the Official Documents. In summary
- Install docker
- Install from the installer (.msi file) distributed by AWS or install with
pip install aws-sam-cli
- OK if the version is output with
sam --version
docker is installed to create a lambda execution environment on the local machine.
This is a VS Code extension for working with AWS SAMs from VS Code with a GUI. AWS Toolkit - Visual Studio Marketplace
Follow the Official Documents.
Create new SAM Application
hello_world / app.py
and select Debug Locally
to create a virtual environment in docker and execute the function.
.aws/templates.json
で設定できる
We will deploy based on template.yaml.
Deploy SAM Application
from the command palette
templete.yaml
You can see that the lambda is created as shown below. You can see that the API is also created in API Gateway.
The API created by default is
GET: / hello
, so you need to edit templete.yaml
to change this.
Below is the default templete.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
trial-sam
Sample SAM Template for trial-sam
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
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
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
You can change the path and HTTP method by rewriting this as follows
Before
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
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
After
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
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /anypath # hello → anypath
Method: post # get → post
POST: I was able to change to ʻanypath`.
All you have to do is assemble the logic according to your requirements.
If you deploy again, your changes will take effect.
Recommended Posts