タイトル : HelloWorldからですね AWSサーバーレス2025
更新日 : 2025-01-19
カテゴリ : プログラミング
HelloWorldです
雑感です
-
sam init → validate → build → (localかAWS上で確認) → deploy の順ですね。
-
samでpython3.13が選択可だけどlocalstack側の問題でエラーになるので、3.12をとりあえず選択(詳細不明)
-
Project nameの入力の際に _ が使えるけどNG
CloudFormationのStack Nameの制約があるので _ は使わない方が良い。 _ ではなく - を使いました。
-
認証無しで作成して良いのか、途中でちゃんと確認があります
HelloWorldFunction has no authentication. Is this okay? [y/N]: y
-
chaliceの方が簡単と言えば簡単だけどフレームワーク込みという制限がきついかな(FastAPIを試す前だけど)
chaliceでBluePrint導入したりすれば、少しはFlaskっぽくなるかもだけど
-
invoke,deployまで簡単。samを極めれば、やっぱすごそう
実行例 sam init→validate→build→deploy
$ 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 - GraphQLApi Hello World Example
11 - Full Stack
12 - Lambda EFS example
13 - Serverless Connector Hello World Example
14 - Multi-step workflow with Connectors
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 - python3.9
15 - python3.8
16 - python3.13
17 - python3.12
18 - python3.11
19 - python3.10
20 - ruby3.3
21 - ruby3.2
22 - rust (provided.al2)
23 - rust (provided.al2023)
Runtime: 17
What package type would you like to use?
1 - Zip
2 - Image
Package type: 1
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]:
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]:
Would you like to set Structured Logging in JSON format on your Lambda functions? [y/N]:
Project name [sam-app]: hello-world-1st
-----------------------
Generating application:
-----------------------
Name: hello-world-1st
Runtime: python3.12
Architectures: x86_64
Dependency Manager: pip
Application Template: hello-world
Output Directory: .
Configuration file: hello-world-1st/samconfig.toml
Next steps can be found in the README file at hello-world-1st/README.md
Commands you can use next
=========================
[*] Create pipeline: cd hello-world-1st && sam pipeline init --bootstrap
[*] Validate SAM template: cd hello-world-1st && sam validate
[*] Test Function in the Cloud: cd hello-world-1st && sam sync --stack-name {stack-name} --watch
$ cd hello-world-1st
$ ls
README.md __init__.py events hello_world samconfig.toml template.yaml tests
$ sam validate
XXX/hello-world-1st/template.yaml is a valid SAM Template
$ sam build
Starting Build use cache
Manifest file is changed (new hash: 3298f13049d19cffaa37ca931dd4d421) or dependency folder
(.aws-sam/deps/8c65aa9b-ac81-49ba-bbfd-b87fff8f5583) is missing for (HelloWorldFunction), downloading dependencies and copying/building
source
Building codeuri: XXX/hello-world-1st/hello_world runtime: python3.12 architecture: x86_64
functions: HelloWorldFunction
Running PythonPipBuilder:CleanUp
Running PythonPipBuilder:ResolveDependencies
Running PythonPipBuilder:CopySource
Running PythonPipBuilder:CopySource
Build Succeeded
Built Artifacts : .aws-sam/build
Built Template : .aws-sam/build/template.yaml
Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided
$ sam deploy --guided
Configuring SAM deploy
======================
Looking for config file [samconfig.toml] : Found
Reading default arguments : Success
Setting default arguments for 'sam deploy'
=========================================
Stack Name [hello-world-1st]:
AWS Region [ap-northeast-1]:
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
Confirm changes before deploy [Y/n]:
#SAM needs permission to be able to create roles to connect to the resources in your template
Allow SAM CLI IAM role creation [Y/n]:
#Preserves the state of previously provisioned resources when an operation fails
Disable rollback [y/N]:
HelloWorldFunction has no authentication. Is this okay? [y/N]: y
Save arguments to configuration file [Y/n]:
SAM configuration file [samconfig.toml]:
SAM configuration environment [default]:
Looking for resources needed for deployment:
Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-87c82757
A different default S3 bucket can be set in samconfig.toml and auto resolution of buckets turned off by setting resolve_s3=False
Parameter "stack_name=hello-world-1st" in [default.deploy.parameters] is defined as a global parameter
[default.global.parameters].
This parameter will be only saved under [default.global.parameters] in
XXX/hello-world-1st/samconfig.toml.
Saved arguments to config file
Running 'sam deploy' for future deployments will use the parameters saved above.
The above parameters can be changed by modifying samconfig.toml
Learn more about samconfig.toml syntax at
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
Uploading to hello-world-1st/4102468423d0e71fcae40dfb1dcef858 574289 / 574289 (100.00%)
Deploying with following values
===============================
Stack name : hello-world-1st
Region : ap-northeast-1
Confirm changeset : True
Disable rollback : False
Deployment s3 bucket : aws-sam-cli-managed-default-samclisourcebucket-87c82757
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {}
Signing Profiles : {}
Initiating deployment
=====================
Uploading to hello-world-1st/2b5100a9dd15f94e3518971aae8424c7.template 1200 / 1200 (100.00%)
Waiting for changeset to be created..
CloudFormation stack changeset
-------------------------------------------------------------------------------------------------------------------------------------
Operation LogicalResourceId ResourceType Replacement
-------------------------------------------------------------------------------------------------------------------------------------
+ Add HelloWorldFunctionHelloWorldPer AWS::Lambda::Permission N/A
missionProd
+ Add HelloWorldFunctionRole AWS::IAM::Role N/A
+ Add ServerlessRestApiProdStage AWS::ApiGateway::Stage N/A
+ Add ServerlessRestApiDeployment47fc AWS::ApiGateway::Deployment N/A
2d5f9d
+ Add ServerlessRestApi AWS::ApiGateway::RestApi N/A
+ Add HelloWorldFunction AWS::Lambda::Function N/A
-------------------------------------------------------------------------------------------------------------------------------------
Changeset created successfully. arn:aws:cloudformation:ap-northeast-1:000000000000:changeSet/samcli-deploy1737254731/bea4fa0d
Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y
2025-01-19 11:45:34 - Waiting for stack create/update to complete
CloudFormation events from stack operations (refresh every 5.0 seconds)
-------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus ResourceType LogicalResourceId ResourceStatusReason
-------------------------------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS AWS::CloudFormation::Stack hello-world-1st -
CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole -
CREATE_COMPLETE AWS::IAM::Role HelloWorldFunctionRole -
CREATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction -
CREATE_COMPLETE AWS::Lambda::Function HelloWorldFunction -
CREATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi -
CREATE_COMPLETE AWS::ApiGateway::RestApi ServerlessRestApi -
CREATE_IN_PROGRESS AWS::Lambda::Permission HelloWorldFunctionHelloWorldPer -
missionProd
CREATE_COMPLETE AWS::Lambda::Permission HelloWorldFunctionHelloWorldPer -
missionProd
CREATE_IN_PROGRESS AWS::ApiGateway::Deployment ServerlessRestApiDeployment47fc -
2d5f9d
CREATE_COMPLETE AWS::ApiGateway::Deployment ServerlessRestApiDeployment47fc -
2d5f9d
CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage -
CREATE_COMPLETE AWS::ApiGateway::Stage ServerlessRestApiProdStage -
CREATE_COMPLETE AWS::CloudFormation::Stack hello-world-1st -
-------------------------------------------------------------------------------------------------------------------------------------
CloudFormation outputs from deployed stack
---------------------------------------------------------------------------------------------------------------------------------------
Outputs
---------------------------------------------------------------------------------------------------------------------------------------
Key HelloWorldApi
Description API Gateway endpoint URL for Prod stage for Hello World function
Value https://kkme1aq3kp.execute-api.amazonaws.com:4566/Prod/hello/
Key HelloWorldFunction
Description Hello World Lambda Function ARN
Value arn:aws:lambda:ap-northeast-1:000000000000:function:hello-world-1st-HelloWorldFunction-a816012f
Key HelloWorldFunctionIamRole
Description Implicit IAM Role created for Hello World function
Value arn:aws:iam::000000000000:role/hello-world-1st-HelloWorldFunctionRole-b69c9f30
---------------------------------------------------------------------------------------------------------------------------------------
Successfully created/updated stack - hello-world-1st in ap-northeast-1
$
確認と実行
作成されたことを確認
$ aws lambda list-functions --query 'Functions[*].[FunctionName]'
[
[
"hello-world-1st-HelloWorldFunction-a816012f"
]
]
$ aws apigateway get-rest-apis --query 'items[*].[name]'
[
[
"hello-world-1st-ServerlessRestApi-08565007"
]
]
$
invokeでローカル実行して確認
$ sam local invoke
Invoking app.lambda_handler (python3.12)
Local image was not found.
Removing rapid images for repo public.ecr.aws/sam/emulation-python3.12
Building image....................
Using local image: public.ecr.aws/lambda/python:3.12-rapid-x86_64.
Mounting XXX/hello-world-1st/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated,
inside runtime container
START RequestId: 3c2eecc1-6eb3-4eaa-82f9-06b07996b2f9 Version: $LATEST
END RequestId: 28475dce-7c3a-470e-8ad5-f16f3e6a2088
REPORT RequestId: 28475dce-7c3a-470e-8ad5-f16f3e6a2088 Init Duration: 0.10 ms Duration: 56.61 ms Billed Duration: 57 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}
$