-
A serverless application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks. Note that a serverless application is more than just a Lambda function it can include additional resources such as APIs, databases, and event source mappings.
-
Try this example for your project success.
- If you use Mac
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew --version
brew tap aws/tap
brew install aws-sam-cli
sam --version
- If you use Windows
- If you use Linux
- Download Docker Hub.
sam init
1 - AWS Quick Start Templates
1 - Zip (artifact is a zip uploaded to S3)
10 - python3.7
<YOUR PROJECT NAME>
1 - Hello World Example
cd <YOUR PROJECT NAME> # Example : cd aws-sam-lambda/
mkdir docker
cp -r hello_world/* docker
- Change from "hello_world_function" to "docker" in template yaml file.
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_function
Handler: hello_world/app.lambda_handler
Runtime: python3.7
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: docker # hello_world_function -> docker
Handler: hello_world/app.lambda_handler
Runtime: python3.7
- Insert comand in your shell
sam build --use-container
- Excute lambda in your Docker container.
sam local invoke
# TIP : sam build --use-container && sam local invoke
- Check result
Mounting /Users/XXX/Project/aws-sam-lambda-example/aws-sam-lambda/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: 8393e7be-6a67-4b8d-b49c-45edaf928150 Version: $LATEST
END RequestId: 8393e7be-6a67-4b8d-b49c-45edaf928150
REPORT RequestId: 8393e7be-6a67-4b8d-b49c-45edaf928150 Init Duration: 0.31 ms Duration: 119.65 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}%
Start debug your application with API Gateway with locally ( NOT IN CONTAINER ) / 로컬 환경에서 디버깅을 하려면 아래 순서로 진행합니다.
- Create Python3 virtual enviroment.
python3 -m venv ./venv
source .venv/bin/activate
alias python=python3
alias pip=pip3
python --version
- Check Python3 version and the Python3 version of template. yaml file is same.
- Make build folder and copy app.py
mkdir docker/build
cp docker/app.py docker/build/app.py
- Install Python pakage in build folder.
pip3 install -r docker/requirements.txt -t docker/build/
- Start API that triggers lambda
sam local start-api -d 5891
Mounting HelloWorldFunction at http://127.0.0.1:3000/events [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-12-28 14:43:28 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
- Create a lunch.json in Visual Studio Code and Change template yaml file. / 비쥬얼 스튜디오 코드에서 lunch.json 파일을 수정합니다.
- lunch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug with SAM CLI (Remote Debug)",
"type": "python",
"request": "attach",
"port": 5891,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}/docker/build",
"remoteRoot" : "/var/task"
}
]
}
]
}
- template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
aws-sam-lambda
Sample SAM Template for aws-sam-lambda
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 20
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: docker/build/
Handler: app.lambda_handler
Runtime: python3.7
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: '/events'
Method: get
- Connect your API in path of template yaml file. / yaml 파일에 정의된 API path를 통하여 브라우저로 접속합니다.
- Insert URL in your browser or Insert curl command your shell.
curl http://127.0.0.1:3000/events
or Enter the address below into your Chrome browser.
http://127.0.0.1:3000/events
# TIP : Access API before starting debugging in Visual Studio Code
- Check break point and start debug in Visual Studio Code. / 비쥬얼 스튜디오 코드에서 디버깅 버튼을 이용하여 디버깅 합니다.
- Confirm break point and debug consol.
sam deploy --guided
https://github.com/amazon-archives/serverless-app-examples/tree/master/python
https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md
https://www.youtube.com/watch?v=3gCzGLdhQao&ab_channel=AWSEvents