ããã¯ããªã«ãããããŠæžãããã®ïŒ
AWS Lambda颿°ã®ãããã€ããã±ãŒãžã«ã¯zipãã¡ã€ã«ãšã³ã³ããã€ã¡ãŒãžããããŸãã
ãã®ãã¡zipãã¡ã€ã«ãã䜿ã£ãããšããªãã£ãã®ã§ãã³ã³ããã€ã¡ãŒãžã䜿ã£ãŠã¿ãããšæããŸããã§ãããŒã«ã«ã§åãããŠã¿ãŸãã
AWS Lambdaã®ãããã€ããã±ãŒãž
AWS Lambdaã®ãããã€ããã±ãŒãžã«ã¯ãzipãã¡ã€ã«ãšã³ã³ããã€ã¡ãŒãžããããŸãã
Lambda ã®äž»èŠãªæŠå¿µãçè§£ãã / ãããã€ããã±ãŒãž
AWS Lambda颿°ãã³ã³ããã€ã¡ãŒãžã§äœæããã«ã¯ã以äžã®3çš®é¡ã®æ¹æ³ãããããã§ãã
- AWS Lambda颿°çšã®AWSããŒã¹ã€ã¡ãŒãžã䜿çšãã
- AWSã®OSå°çšããŒã¹ã€ã¡ãŒãžã䜿çšãã
- GoãRustã®ãããªå®è¡æã«èšèªã®ã©ã³ã¿ã€ã ãäžèŠãªèšèªã®å ŽåããAWS Lambda颿°ãããŒã¹ã€ã¡ãŒãžãæäŸããŠããªãèšèªã®å Žåã«å©çš
- Amazon Linux 2023ãŸãã¯Amazon Linux 2ããéžæããã©ã³ã¿ã€ã ã€ã³ã¿ãŒãã§ã€ã¹ã¯ã©ã€ã¢ã³ããã€ã¡ãŒãžã«å«ããå¿ èŠããã
- ã«ã¹ã¿ã ã©ã³ã¿ã€ã ãšããŠæ§ç¯ããããšãå¯èœ
- éAWSããŒã¹ã€ã¡ãŒãžã䜿çšãã
- Docker Image Manifest V2 Schema 2ãŸãã¯Open Container InitiativeïŒOCIïŒä»æ§ã«æºæ ããã€ã¡ãŒãžã§ããå¿ èŠããã
- ã©ã³ã¿ã€ã ã€ã³ã¿ãŒãã§ã€ã¹ã¯ã©ã€ã¢ã³ããã€ã¡ãŒãžã«å«ããå¿ èŠããã
コンテナイメージを使用した Lambda 関数の作成 - AWS Lambda
åèšèªåãã«ããã³ã³ããã€ã¡ãŒãžã䜿ã£ãAWS Lambda颿°ã®æ§ç¯æ¹æ³ãæžãããŠããŸãã
Node.js Lambda 関数をコンテナイメージとともにデプロイする - AWS Lambda
コンテナイメージを使用して、トランスパイルされた TypeScript コードを Lambda にデプロイする - AWS Lambda
コンテナイメージで Python Lambda 関数をデプロイする - AWS Lambda
コンテナイメージで Ruby Lambda 関数をデプロイする - AWS Lambda
コンテナイメージを使用した Java Lambda 関数のデプロイ - AWS Lambda
コンテナイメージを使用して Go Lambda 関数をデプロイする - AWS Lambda
コンテナイメージを使用して.NET の Lambda 関数をデプロイする - AWS Lambda
ä»åã¯Pythonã䜿ãããšæããŸãã
ã³ã³ããã€ã¡ãŒãžã䜿ã£ãAWS Lambda颿°ãããŒã«ã«ã§åããã«ã¯ïŒ
ããŒã«ã«ã§AWSã«é¢ãããµãŒãã¹ããšãã¥ã¬ãŒã·ã§ã³ã§ã§ãåããããšæããšãLocalStackã䜿ãããšãå人çã«ã¯å€ãã§ãã
ãªã®ã§ãããã³ã³ããã€ã¡ãŒãžã䜿ãã«ã¯LocalStackã®æåçã䜿ãå¿ èŠããããŸãã

AWS Service Feature Coverage | Docs
ã©ããããããªïŒãšæã£ãã®ã§ãããAWS SAMã§ã¯ã³ã³ããã€ã¡ãŒãžã§ãã£ãŠãããŒã«ã«ã§åãããããªæãã ã£ãã®ã§ä»åã¯ãã¡ãã
䜿ã£ãŠã¿ãããšã«ããŸããã
ããšãããããåèšèªã®ã³ã³ããã€ã¡ãŒãžã«é¢ããããŒãžã«ãcurlã䜿ã£ãç¢ºèªæ¹æ³ãæžãããŠããã®ã§ãã¡ãã§ã詊ããŠã¿ãŸãããã
ä»åã¯ã³ã³ããã€ã¡ãŒãžã䜿ã£ãAWS Lambda颿°ã®ãã«ããåäœç¢ºèªãäžå¿ã«ããã®ã§ãããŸãåã£ãããšã¯ãããŸããã
ç°å¢
ä»åã®ç°å¢ã¯ãã¡ãã
$ python3 --version Python 3.12.3 $ pip3 --version pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)
Dockerã
$ docker version Client: Docker Engine - Community Version: 27.3.1 API version: 1.47 Go version: go1.22.7 Git commit: ce12230 Built: Fri Sep 20 11:40:59 2024 OS/Arch: linux/amd64 Context: default Server: Docker Engine - Community Engine: Version: 27.3.1 API version: 1.47 (minimum version 1.24) Go version: go1.22.7 Git commit: 41ca978 Built: Fri Sep 20 11:40:59 2024 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.7.24 GitCommit: 88bf19b2105c8b17560993bee28a01ddc2f97182 runc: Version: 1.2.2 GitCommit: v1.2.2-0-g7cb3632 docker-init: Version: 0.19.0 GitCommit: de40ad0
AWS SAMã
$ sam --version SAM CLI, version 1.131.0
$ aws --version aws-cli/2.22.12 Python/3.12.6 Linux/6.8.0-49-generic exe/x86_64.ubuntu.24
ã¯ã¬ãã³ã·ã£ã«ã¯ãããŒã®å€ãç°å¢å€æ°ã§èšå®ããŠãããŸãã
$ export AWS_ACCESS_KEY_ID=dummy $ export AWS_SECRET_ACCESS_KEY=dummy $ export AWS_DEFAULT_REGION=ap-northeast-1
AWS Lambda颿°ãã³ã³ããã€ã¡ãŒãžã§äœæããŠcurlã§åäœç¢ºèªãã
æåã¯Pythonåãã®ã¬ã€ãã«åŸã£ãŠãããŸãããã
コンテナイメージで Python Lambda 関数をデプロイする - AWS Lambda
Pythonã¯3.12ã䜿ãã®ã§ãããŒã¹ã€ã¡ãŒãžãPython 3.12ã®ãã®ã«ãªããŸãããªããPython 3.12ããããŒã¹ã€ã¡ãŒãžãAmazon Linux 2023ã«
ãªãããã§ãïŒ3.11以åã¯Amazon Linux 2ïŒã
Python 3.12 以éã®ããŒã¹ã€ã¡ãŒãžã¯ãAmazon Linux 2023 ã®æå°ã³ã³ããã€ã¡ãŒãžã«åºã¥ããŠããŸããPython 3.8ïœ3.11 ã®ããŒã¹ã€ã¡ãŒãžã¯ãAmazon Linux 2 ã®ã€ã¡ãŒãžã«åºã¥ããŠããŸããAL2023 ããŒã¹ã®ã€ã¡ãŒãžã«ã¯ããããã€ã®ãããããªã³ããå°ããããšããglibc ãªã©ã®ã©ã€ãã©ãªã®ããŒãžã§ã³ãæŽæ°ãããŠããããšãªã©ãAmazon Linux 2 ã«æ¯ã¹ãŠããã€ãã®å©ç¹ããããŸãã
ããŒã¹ã€ã¡ãŒãžã«ããã±ãŒãžã远å ãããå Žåã¯ãmicrodnfã䜿ãããã§ãã
AL2023 ããŒã¹ã®ã€ã¡ãŒãžã§ã¯ãAmazon Linux 2 ã®ããã©ã«ãã®ããã±ãŒãžãããŒãžã£ã§ãã yum ã®ä»£ããã« microdnf (dnf ãšããŠã·ã³ããªãã¯ãªã³ã¯) ãããã±ãŒãžãããŒãžã£ãšããŠäœ¿çšãããŠããŸããmicrodnf 㯠dnf ã®ã¹ã¿ã³ãã¢ãã³å®è£ ã§ããAL2023 ããŒã¹ã®ã€ã¡ãŒãžã«å«ãŸããããã±ãŒãžã®ãªã¹ãã«ã€ããŠã¯ããComparing packages installed on Amazon Linux 2023 Container Imagesãã®ãMinimal Containerãåãåç §ããŠãã ãããAL2023 ãš Amazon Linux 2 ã®éãã®è©³çްã«ã€ããŠã¯ãAWS ã³ã³ãã¥ãŒãã£ã³ã°ããã°ã®ãIntroducing the Amazon Linux 2023 runtime for AWS Lambdaããåç §ããŠãã ããã
ãŸãããã®ããããæ³šæã§ããã
ã³ãŒãã§ import ã¹ããŒãã¡ã³ãã䜿çšãããšãPython ã©ã³ã¿ã€ã ã¯ã¢ãžã¥ãŒã«ãŸãã¯ããã±ãŒãžãèŠã€ãããŸã§æ€çŽ¢ãã¹å ã®ãã£ã¬ã¯ããªãæ€çŽ¢ããŸããããã©ã«ãã§ã¯ãã©ã³ã¿ã€ã 㯠{LAMBDA_TASK_ROOT} ãã£ã¬ã¯ããªãå ã«æ€çŽ¢ããŸããã©ã³ã¿ã€ã ã«å«ãŸããã©ã€ãã©ãªã®ããŒãžã§ã³ãã€ã¡ãŒãžã«å«ããå Žåããã®ããŒãžã§ã³ããã©ã³ã¿ã€ã ã«å«ãŸããããŒãžã§ã³ãããåªå ãããŸãã
ã©ã³ã¿ã€ã ã«å«ãŸããã©ã€ãã©ãªãŒãšpipã䜿ã£ãŠã€ã³ã¹ããŒã«ããã©ã€ãã©ãªãŒã¯ãããŒã¹ã€ã¡ãŒãžã®ããŒãžã§ã³ã«ãã£ãŠåªå
é äœã
éãããã§ããPython 3.11以éã®ããŒã¹ã€ã¡ãŒãžã§ããã°ãpipã§ã€ã³ã¹ããŒã«ãããšã©ã³ã¿ã€ã ã«å«ãŸããã©ã€ãã©ãªãŒããªãŒããŒã©ã€ããã
ããšã«ãªãããã§ãã
Python 3.11 以é: ã©ã³ã¿ã€ã ã«å«ãŸããã©ã€ãã©ãªãš pip ã§ã€ã³ã¹ããŒã«ãããã©ã€ãã©ãªã¯ /var/lang/lib/python3.11/site-packages ãã£ã¬ã¯ããªã«ã€ã³ã¹ããŒã«ãããŸãããã®ãã£ã¬ã¯ããªã¯ãæ€çŽ¢ãã¹å ã§ /var/runtime ãããåªå ãããŸããpip ã䜿çšããŠæ°ããããŒãžã§ã³ãã€ã³ã¹ããŒã«ããããšã§ãSDK ããªãŒããŒã©ã€ãã§ããŸããpip ã䜿çšããŠãã©ã³ã¿ã€ã ã«å«ãŸãã SDK ãšãã®äŸåé¢ä¿ããã€ã³ã¹ããŒã«ããä»»æã®ããã±ãŒãžãšäºææ§ãããããšã確èªã§ããŸãã
Python 3.8-3.10: ã©ã³ã¿ã€ã ã«å«ãŸããã©ã€ãã©ãªã¯ /var/runtime ãã£ã¬ã¯ããªã«ã€ã³ã¹ããŒã«ãããŸããpip ã§ã€ã³ã¹ããŒã«ãããã©ã€ãã©ãªã¯ /var/lang/lib/python3.x/site-packages ãã£ã¬ã¯ããªã«ã€ã³ã¹ããŒã«ãããŸãã/var/runtime ãã£ã¬ã¯ããªã¯æ€çŽ¢ãã¹å ã§ /var/lang/lib/python3.x/site-packages ããåªå ãããŸãã
3.10以åã ãšé¢ä¿ãéã«ãªã£ãŠããã®ã§ã¡ãã£ãšæ³šæã§ããããâŠã
ã©ã³ã¿ã€ã ã€ã³ã¿ãŒãã§ã€ã¹ã¯ã©ã€ã¢ã³ããšéAWSããŒã¹ã€ã¡ãŒãžã䜿ã£ãäŸãæžãããŠããŸãããä»åã¯AWS Lambda颿°çšã®
AWSããŒã¹ã€ã¡ãŒãžã䜿çšããŠãããŸãã
ããã¥ã¡ã³ãã®ãµã³ãã«ã§ã¯äŸåé¢ä¿ããªãã®ã§ãé©åœã«PyYAMLã䜿ã£ãŠãããŸãããã
$ pip3 install pyyaml
ã€ã³ã¹ããŒã«ãããã©ã€ãã©ãªãŒäžèЧã
$ pip3 list Package Version ------- ------- pip 24.0 PyYAML 6.0.2
requirements.txtãå¿
èŠã«ãªãã®ã§ãäœæããŠãããŸãã
$ pip3 freeze > requirements.txt
ãããªAWS Lambda颿°ãäœæã
app.py
import yaml def lambda_handler(event, context): print(f"event = {event}") print(f"context = {context}") return yaml.dump({"message": event["message"]})
Dockerfileãäœæã
Dockerfile
FROM public.ecr.aws/lambda/python:3.12 COPY requirements.txt ${LAMBDA_TASK_ROOT} RUN pip install -r requirements.txt --no-cache-dir COPY app.py ${LAMBDA_TASK_ROOT} CMD [ "app.lambda_handler" ]
ã³ã³ããã€ã¡ãŒãžãäœæã
$ docker image build --platform linux/amd64 -t hello-lambda:0.0.1 .
ããã¥ã¡ã³ããèŠããšã--platform linux/amd64ãæå®ããæ¹ãããããã§ãããARMåãã«ã³ã³ããã€ã¡ãŒãžãäœæããå Žåã¯ã
--platform linux/arm64ãæå®ããŸãã
äœæããã³ã³ããã€ã¡ãŒãžã䜿ã£ãŠãAWS Lambda颿°ãèµ·åã
$ docker container run -it --rm --platform linux/amd64 --name lambda-function -p 9000:8080 hello-lambda:0.0.1 08 Dec 2024 07:22:24,604 [INFO] (rapid) exec '/var/runtime/bootstrap' (cwd=/var/task, handler=)
確èªããŠã¿ãŸãã
$ curl localhost:9000/2015-03-31/functions/function/invocations -d '{"message": "Hello Lambda"}'
"message: Hello Lambda\n"
ãªããæåŸã«æ¹è¡ãã€ããŠãŸããâŠãŸãããã§ãããããã€ããŒãã¯-dã§ãã®ãŸãŸJSONãéãã°ããããã§ãã
ã³ã³ããåŽã®ãã°ãèŠããšãã¡ãããšãã€ããŒããªã©ãèªèããŠããŸãã
event = {'message': 'Hello Lambda'}
context = LambdaContext([aws_request_id=0c90c2f1-4aee-4d35-b5d9-e463f29e610b,log_group_name=/aws/lambda/Functions,log_stream_name=$LATEST,function_name=test_function,memory_limit_in_mb=3008,function_version=$LATEST,invoked_function_arn=arn:aws:lambda:us-east-1:012345678912:function:test_function,client_context=None,identity=CognitoIdentity([cognito_identity_id=None,cognito_identity_pool_id=None])])
ãã¹ã¯/2015-03-31/functions/function/invocationsãªãã§ããã
ãšããã§ãããã¥ã¡ã³ãã§ã¯ããŒã«ã«ããŒãã9000ã«å²ãåœãŠãŠããã®ã§ããããªã«ãçç±ãããã®ããªïŒãšæã£ãŠ8080ããŒãã«å²ãåœãŠãŠ
ã¿ãŸããããç¹ã«åé¡ãªãã£ãã§ãã
$ docker container run -it --rm --platform linux/amd64 --name lambda-function -p 8080:8080 hello-lambda:0.0.1
çµæã
$ curl localhost:8080/2015-03-31/functions/function/invocations -d '{"message": "Hello Lambda"}'
"message: Hello Lambda\n"
ããŒã«ã«ããŒãã«ãã€ã³ãããã«ãã³ã³ããã®IPã«çŽæ¥ã¢ã¯ã»ã¹ããŠãããŸããŸããã
$ docker container run -it --rm --platform linux/amd64 --name lambda-function hello-lambda:0.0.1
$ curl 172.17.0.2:8080/2015-03-31/functions/function/invocations -d '{"message": "Hello Lambda"}'
"message: Hello Lambda\n"
ãã®ããŒãã¯ãããªãšããã§ããããã
AWS SAMã§ã³ã³ããã€ã¡ãŒãžã§äœæããŠåäœç¢ºèªããŸã
ç¶ããŠã¯ãAWS SAMã䜿ã£ãŠã¿ãŸããAWS SAMã§ã³ã³ããã€ã¡ãŒãžã䜿ã£ãAWS Lambda颿°ã®æ§ç¯ã«ã€ããŠèšèŒãããã®ã¯ããã®ããã
ã§ããããã
AWS SAM を使用した構築の概要 - AWS Serverless Application Model
AWS SAM を使用したデフォルトのビルド - AWS Serverless Application Model
ãŸãã¯AWS SAMãããžã§ã¯ããäœæããŠã¿ãŸãã
$ sam init
ã€ã³ã¿ã©ã¯ãã£ãã«å ¥åããå Žåã
You can preselect a particular runtime or package type when using the `sam init` experience.
Call `sam init --help` to learn more.
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
Choose an AWS Quick Start application template
1 - Hello World Example
2 - Data processing
3 - Hello World Example with Powertools for AWS Lambda
4 - Multi-step workflow
5 - Scheduled task
6 - Standalone function
7 - Serverless API
8 - Infrastructure event management
9 - Lambda Response Streaming
10 - Serverless Connector Hello World Example
11 - Multi-step workflow with Connectors
12 - GraphQLApi Hello World Example
13 - Full Stack
14 - Lambda EFS example
15 - DynamoDB Example
16 - Machine Learning
Template: 1
Use the most popular runtime and package type? (python3.13 and zip) [y/N]: n
Which runtime would you like to use?
1 - dotnet8
2 - dotnet6
3 - go (provided.al2)
4 - go (provided.al2023)
5 - graalvm.java11 (provided.al2)
6 - graalvm.java17 (provided.al2)
7 - java21
8 - java17
9 - java11
10 - java8.al2
11 - nodejs22.x
12 - nodejs20.x
13 - nodejs18.x
14 - nodejs16.x
15 - python3.9
16 - python3.8
17 - python3.13
18 - python3.12
19 - python3.11
20 - python3.10
21 - ruby3.3
22 - ruby3.2
23 - rust (provided.al2)
24 - rust (provided.al2023)
Runtime: 18
What package type would you like to use?
1 - Zip
2 - Image
Package type: 2
Based on your selections, the only dependency manager available is pip.
We will proceed copying the template using pip.
Would you like to enable X-Ray tracing on the function(s) in your application? [y/N]: n
Would you like to enable monitoring using CloudWatch Application Insights?
For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]: n
Would you like to set Structured Logging in JSON format on your Lambda functions? [y/N]: y
Structured Logging in JSON format might incur an additional cost. View https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html#monitoring-cloudwatchlogs-pricing for more details
Project name [sam-app]: hello-sam-docker
-----------------------
Generating application:
-----------------------
Name: hello-sam-docker
Base Image: amazon/python3.12-base
Architectures: x86_64
Dependency Manager: pip
Output Directory: .
Configuration file: hello-sam-docker/samconfig.toml
Next steps can be found in the README file at hello-sam-docker/README.md
Commands you can use next
=========================
[*] Create pipeline: cd hello-sam-docker && sam pipeline init --bootstrap
[*] Validate SAM template: cd hello-sam-docker && sam validate
[*] Test Function in the Cloud: cd hello-sam-docker && sam sync --stack-name {stack-name} --watch
ãããsam initã®ãªãã·ã§ã³ã ãã§è¡ã£ãå Žåã¯ãã¡ãã
$ sam init --name hello-sam-docker --base-image amazon/python3.12-base --app-template hello-world-lambda-image --package-type Image --no-tracing --no-application-insights --structured-logging
ã³ã³ããã€ã¡ãŒãžã䜿ãå Žåã¯ãããŒã¹ã€ã¡ãŒãžã--base-imageã§æå®ããããã§ãã
ã³ã³ããã€ã¡ãŒãžã®ãã³ãã¬ãŒãã¯ããããªã«å€ãã¯ãªãããã§ããã
$ curl -s https://raw.githubusercontent.com/aws/aws-sam-cli-app-templates/master/manifest-v2.json | jq '."amazon/python3.12-base"'
[
{
"directory": "python3.12/hello-img",
"displayName": "Hello World Lambda Image Example",
"dependencyManager": "pip",
"appTemplate": "hello-world-lambda-image",
"packageType": "Image",
"useCaseName": "Hello World Example"
},
{
"directory": "python3.12/apigw-scikit",
"displayName": "Scikit-learn Machine Learning Inference API",
"dependencyManager": "pip",
"appTemplate": "ml-apigw-scikit-learn",
"packageType": "Image",
"useCaseName": "Machine Learning"
},
{
"directory": "python3.12/apigw-xgboost",
"displayName": "XGBoost Machine Learning Inference API",
"dependencyManager": "pip",
"appTemplate": "ml-apigw-xgboost",
"packageType": "Image",
"useCaseName": "Machine Learning"
}
]
äœæããAWS SAMãããžã§ã¯ãå ã«ç§»åã
$ cd hello-sam-docker
ãã£ã¬ã¯ããªããªãŒã
$ tree -a
.
âââ .gitignore
âââ README.md
âââ __init__.py
âââ events
â  âââ event.json
âââ hello_world
â  âââ Dockerfile
â  âââ __init__.py
â  âââ app.py
â  âââ requirements.txt
âââ samconfig.toml
âââ template.yaml
âââ tests
âââ __init__.py
âââ unit
âââ __init__.py
âââ test_handler.py
5 directories, 13 files
æ°ã«ãªããã¡ã€ã«ãèŠãŠã¿ãŸãããã
AWS Lambda颿°ã
hello_world/app.py
import json def lambda_handler(event, context): """Sample pure Lambda function Parameters ---------- event: dict, required API Gateway Lambda Proxy Input Format Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format context: object, required Lambda Context runtime methods and attributes Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html Returns ------ API Gateway Lambda Proxy Output Format: dict Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html """ return { "statusCode": 200, "body": json.dumps( { "message": "hello world", } ), }
ããã¯ãµã€ãã®ãã³ãã¬ãŒãã§ããã
requirements.txtïœã¯requestsãã€ã³ã¹ããŒã«ããããã«ãªã£ãŠããŸãã
hello_world/requirements.txt
requests
Dockerfileã
hello_world/Dockerfile
FROM public.ecr.aws/lambda/python:3.12 COPY app.py requirements.txt ./ RUN python3.12 -m pip install -r requirements.txt -t . # Command can be overwritten by providing a different command in the template directly. CMD ["app.lambda_handler"]
AWS SAMã®ãã³ãã¬ãŒããã¡ã€ã«ã
template.yaml
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: > python3.12 Sample SAM Template for hello-sam-docker # More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst Globals: Function: Timeout: 3 # You can add LoggingConfig parameters such as the Logformat, Log Group, and SystemLogLevel or ApplicationLogLevel. Learn more here https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-loggingconfig. LoggingConfig: LogFormat: JSON 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: PackageType: Image Architectures: - x86_64 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 Metadata: Dockerfile: Dockerfile DockerContext: ./hello_world DockerTag: python3.12-v1 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
AWS Lambda颿°ã®éšåãèŠãŠã¿ãŸãããã
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: PackageType: Image Architectures: - x86_64 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 Metadata: Dockerfile: Dockerfile DockerContext: ./hello_world DockerTag: python3.12-v1
PackageTypeãImageã«ãªã£ãŠããããšãMetadataã«Dockeré¢ä¿ã®æ
å ±ãèšè¿°ãããŠãããšããããã€ã³ãã§ããããã
ãã®ãããã¯ãã¡ãã«æžãããŠããŸãã
AWS SAM ã䜿çšããããã©ã«ãã®ãã«ã / ã³ã³ããã€ã¡ãŒãžã®æ§ç¯
ãªãœãŒã¹ã®æ¹ã«ã¯ç¹ã«æžãããŠããªãããã§ãã
AWS::Serverless::Function - AWS Serverless Application Model
ãã³ãã¬ãŒãããçæãããAWS Lambda颿°ã¯ç¹ã«ãã©ã¡ãŒã¿ãŒãåãåããªãã®ã§ãå°ãã ã倿ŽããŠãããŸããã
return { "statusCode": 200, "body": json.dumps( { "message": json.loads(event["body"])["message"], } ), }
HTTPã¡ãœããã¯postã«ããŠãããŸãã
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: post
ã§ã¯ããã«ãããŸãã
$ sam build
zipãã¡ã€ã«ããã±ãŒãžããã«ãããŠããæãšã¯ãæ§åãå€ãããŸããã
Building codeuri: /path/to/hello-sam-docker runtime: None architecture: x86_64 functions: HelloWorldFunction Building image for HelloWorldFunction function Setting DockerBuildArgs for HelloWorldFunction function
ã³ã³ããã€ã¡ãŒãžãã§ããã¿ããã§ãã
Successfully tagged helloworldfunction:python3.12-v1
ã¡ãªã¿ã«ã¿ã°ã¯2ã€ã§ããŸããã
$ docker image ls | grep helloworldfunction helloworldfunction rapid-x86_64 810fef517975 16 minutes ago 519MB helloworldfunction python3.12-v1 92efcda2a0ac 16 minutes ago 501MB
ãã¡ãã«æ²¿ã£ãŠåãããŠã¿ãŸãããã
sam local コマンドを使用したテストの概要 - AWS Serverless Application Model
sam local invokeããã
sam local invoke を使用したテストの概要 - AWS Serverless Application Model
ãã€ããŒãã¯--eventïŒãŸãã¯-eïŒãªãã·ã§ã³ã§ãã¡ã€ã«ãšããŠæž¡ãã®ã§ãããæšæºå
¥åããäžããå Žåã¯ãããªãããã§ãã
$ echo '{"body": "{\"message\": \"Hello World!!\"}"}' | sam local invoke HelloWorldFunction --event -
HTTPããã£ãæååã§æž¡ããšãããé¢åã§ããâŠã
çµæã
Reading invoke payload from stdin (you can also pass it from file with --event)
No current session found, using default AWS::AccountId
Invoking Container created from helloworldfunction:python3.12-v1
Building image.................
Using local image: helloworldfunction:rapid-x86_64.
START RequestId: f16edd4a-3c8c-4792-bbb1-9a63cae96adb Version: $LATEST
END RequestId: 75e1ad44-a96d-4a53-a713-7e1d82f3a58c
REPORT RequestId: 75e1ad44-a96d-4a53-a713-7e1d82f3a58c Init Duration: 0.09 ms Duration: 51.82 ms Billed Duration: 52 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"message\": \"Hello World!!\"}"}
äœæçã«ã¯ãŸããŸãéãã5ç§ãããããããŸãã
次ã¯sam local start-apiã
sam local start-api を使用したテストの概要 - AWS Serverless Application Model
å®è¡ã
$ sam local start-api
Amazon API Gatewayã®ãšãã¥ã¬ãŒã¿ãŒãèµ·åããŸãã
No current session found, using default AWS::AccountId Initializing the lambda functions containers. Building image................. Using local image: helloworldfunction:rapid-x86_64. Containers Initialization is done. Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [POST] 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. If you used sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template 2024-12-08 17:14:05 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:3000 2024-12-08 17:14:05 Press CTRL+C to quit
確èªã
$ curl localhost:3000/hello -d '{"message": "Hello World!!"}'
{"message": "Hello World!!"}
OKã§ããã
æåŸã¯sam local start-lambdaã§ãã
を使用したテストの概要 sam local start-lambda - AWS Serverless Application Model
AWS Lambdaç°å¢ãèµ·åã
$ sam local start-lambda
ããã§ãšã³ããã€ã³ããhttp://127.0.0.1:3001ãšããŠãAWS CLIã§AWS Lambda颿°ãåŒã³åºãããšãã§ããŸãã
No current session found, using default AWS::AccountId Initializing the lambda functions containers. Building image................. Using local image: helloworldfunction:rapid-x86_64. Containers Initialization is done. Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint. 2024-12-08 17:15:10 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:3001 2024-12-08 17:15:10 Press CTRL+C to quit
åŒã³åºããŠã¿ãŸãã
$ aws --endpoint-url http://localhost:3001 lambda invoke --function-name HelloWorldFunction --cli-binary-format raw-in-base64-out --payload '{"body": "{\"message\": \"Hello World!!\"}"}' result.json
{
"StatusCode": 200
}
OKã§ããã
$ cat result.json
{"statusCode": 200, "body": "{\"message\": \"Hello World!!\"}"}
ãããã«
ãããã€ããã±ãŒãžãã³ã³ããã€ã¡ãŒãžãšããAWS Lambda颿°ãããŒã«ã«ã§åããæ¹æ³ãããããããšç¢ºèªããŠã¿ãŸããã
LocalStackã䜿ããªãã£ãã®ã§ã¡ãã£ãšå°ã£ãã®ã§ãããDockerã³ã³ããåäœã§ç¢ºèªããæ¹æ³ãAWS SAMãäœ¿ãæ¹æ³ãªã©ã®éžæè¢ã
確èªã§ããããããããã³ã³ããã€ã¡ãŒãžããããã€ããã±ãŒãžã«ããæ¹æ³ãèŠãŠããŠããªãã£ãã®ã§ãã®æ©äŒã«ç¢ºèªã§ããŠããã£ãã§ããã
ãã ãAWS SAMã䜿ããšã¡ãã£ãšéããã®ãããé£ç¹ã§ããããâŠã