AWS SAM CLI Hello world (cloudshell利用)


sam cliにHello worldした時の備忘録です。

sam(serverless Application Model)はcloudformationの一部でサーバレスに特化して、cloudformationより簡単に記述しやすくなっています。今回はcloud shellでsam cliを操作してHello worldをAWSにデプロイしたため、簡単にご紹介します。

※cloud shellではコンテナを動かせない為、デプロイ前のローカルテストは出来ません!一般的にはcloud9を利用したほうが幸せです。


cloud shellでsam cliの基本操作

cloudshellでもsam cliは動かすことが出来ます。

$sam --version
SAM CLI, version 1.52.0

※cloudshellはdockerが使えないため、dockerを利用する「sam local start-api」等が実施できない制約はありますが、sam cliの雰囲気はつかめます。

sam init


  • hello worldテンプレートを利用
    ※api-gateway, lamdaが構築する内容です。
  • python3.7を指定
$ 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 - Multi-step workflow
        3 - Serverless API
        4 - Scheduled task
        5 - Standalone function
        6 - Data processing
        7 - Infrastructure event management
        8 - Machine Learning
Template: 1

Use the most popular runtime and package type? (Python and zip) [y/N]: n

Which runtime would you like to use?
        1 - dotnet6
        2 - dotnet5.0
        3 - dotnetcore3.1
        4 - go1.x
        5 - graalvm.java11 (provided.al2)
        6 - graalvm.java17 (provided.al2)
        7 - java11
        8 - java8.al2
        9 - java8
        10 - nodejs16.x
        11 - nodejs14.x
        12 - nodejs12.x
        13 - python3.9
        14 - python3.8
        15 - python3.7
        16 - python3.6
        17 - ruby2.7
        18 - rust (provided.al2)
Runtime: 15

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]: n

Project name [sam-app]: 

Cloning from (process may take a moment)

    Generating application:
    Name: sam-app
    Runtime: python3.7
    Architectures: x86_64
    Dependency Manager: pip
    Application Template: hello-world
    Output Directory: .
    Next steps can be found in the README file at ./sam-app/

    Commands you can use next
    [*] Create pipeline: cd sam-app && sam pipeline init --bootstrap
    [*] Validate SAM template: sam validate
    [*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch



  • samテンプレート(template.yaml)
  • lambda関数(
$cd sam-app/
$cat template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >

  Sample SAM Template for sam-app

# More info about Globals:
    Timeout: 3

    Type: AWS::Serverless::Function # More info about Function Resource:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.7
          Type: Api # More info about API Event Source:
            Path: /hello
            Method: get

  # 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
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}"
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn
$cat hello_world/ 
import json

# import requests

def lambda_handler(event, context):
    """Sample pure Lambda function

    event: dict, required
        API Gateway Lambda Proxy Input Format

        Event doc:

    context: object, required
        Lambda Context runtime methods and attributes

        Context doc:

    API Gateway Lambda Proxy Output Format: dict

        Return doc:

    # try:
    #     ip = requests.get("")
    # except requests.RequestException as e:
    #     # Send some context about this error to Lambda Logs
    #     print(e)

    #     raise e

    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "hello world",
            # "location": ip.text.replace("\n", "")

api-gatewayのurlにアクセスするとlambda関数がコールされ、「hello world」を返してくれるコードとなっています


sam build


$sam build
Your template contains a resource with logical ID "ServerlessRestApi", which is a reserved logical ID in AWS SAM. It could result in unexpected behaviors and is not recommended.
Building codeuri: /home/cloudshell-user/sam-app/hello_world runtime: python3.7 metadata: {} architecture: x86_64 functions: ['HelloWorldFunction']
Running PythonPipBuilder:ResolveDependencies
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



$sam deploy --guided

Configuring SAM deploy

        Looking for config file [samconfig.toml] :  Not found

        Setting default arguments for 'sam deploy'
        Stack Name [sam-app]: 
        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]: y
        #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]: y
        #Preserves the state of previously provisioned resources when an operation fails
        Disable rollback [y/N]: n
        HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
        Save arguments to configuration file [Y/n]: y
        SAM configuration file [samconfig.toml]: 
        SAM configuration environment [default]: 

        Looking for resources needed for deployment:
         Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-xxxxxxxxx
         A different default S3 bucket can be set in 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

Uploading to sam-app/a749410668d0b70bdxxxxxxx  465116 / 465116  (100.00%)

        Deploying with following values
        Stack name                   : sam-app
        Region                       : ap-northeast-1
        Confirm changeset            : True
        Disable rollback             : False
        Deployment s3 bucket         : aws-sam-cli-managed-default-samclisourcebucket-xxxxxxxxxx
        Capabilities                 : ["CAPABILITY_IAM"]
        Parameter overrides          : {}
        Signing Profiles             : {}

Initiating deployment
Uploading to sam-app/03ec28a1387579cb1axxxxxxxxx.template  1144 / 1144  (100.00%)

Waiting for changeset to be created..
CloudFormation stack changeset
Operation      LogicalResourceId                                    ResourceType                         Replacement       
+ Add          HelloWorldFunctionHelloWorldPermissionProd           AWS::Lambda::Permission              N/A               
+ Add          HelloWorldFunctionRole                               AWS::IAM::Role                       N/A               
+ Add          HelloWorldFunction                                   AWS::Lambda::Function                N/A               
+ Add          ServerlessRestApiDeployment47xxxxxxxx                AWS::ApiGateway::Deployment          N/A               
+ Add          ServerlessRestApiProdStage                           AWS::ApiGateway::Stage               N/A               
+ Add          ServerlessRestApi                                    AWS::ApiGateway::RestApi             N/A               

Changeset created successfully. arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxx:changeSet/samcli-deploy16xxxx/ee71499b-xxxx-42d4-bfec-xxxxxxx

Previewing CloudFormation changeset before deployment
Deploy this changeset? [y/N]: y

2022-08-23 02:23:55 - Waiting for stack create/update to complete

CloudFormation events from stack operations (refresh every 0.5 seconds)
ResourceStatus       ResourceType                      LogicalResourceId                              ResourceStatusReason       
CREATE_IN_PROGRESS   AWS::IAM::Role                    HelloWorldFunctionRole                         -                          
CREATE_IN_PROGRESS   AWS::IAM::Role                    HelloWorldFunctionRole                         Resource creation Initiated
CREATE_COMPLETE      AWS::IAM::Role                    HelloWorldFunctionRole                         -                          
CREATE_IN_PROGRESS   AWS::Lambda::Function             HelloWorldFunction                             -                          
CREATE_IN_PROGRESS   AWS::Lambda::Function             HelloWorldFunction                             Resource creation Initiated
CREATE_COMPLETE      AWS::Lambda::Function             HelloWorldFunction                             -                          
CREATE_IN_PROGRESS   AWS::ApiGateway::RestApi          ServerlessRestApi                              -                          
CREATE_IN_PROGRESS   AWS::ApiGateway::RestApi          ServerlessRestApi                              Resource creation Initiated
CREATE_COMPLETE      AWS::ApiGateway::RestApi          ServerlessRestApi                              -                          
CREATE_IN_PROGRESS   AWS::ApiGateway::Deployment       ServerlessRestApiDeployment47fcxxxxxxxxx       -                          
CREATE_IN_PROGRESS   AWS::Lambda::Permission           HelloWorldFunctionHelloWorldPermissionProd     -                          
CREATE_IN_PROGRESS   AWS::Lambda::Permission           HelloWorldFunctionHelloWorldPermissionProd     Resource creation Initiated
CREATE_IN_PROGRESS   AWS::ApiGateway::Deployment       ServerlessRestApiDeployment47fxxxxxxxx         Resource creation Initiated
CREATE_COMPLETE      AWS::ApiGateway::Deployment       ServerlessRestApiDeployment47fxxxxxxxxx        -                          
CREATE_IN_PROGRESS   AWS::ApiGateway::Stage            ServerlessRestApiProdStage                     -                          
CREATE_IN_PROGRESS   AWS::ApiGateway::Stage            ServerlessRestApiProdStage                     Resource creation Initiated
CREATE_COMPLETE      AWS::ApiGateway::Stage            ServerlessRestApiProdStage                     -                          
CREATE_COMPLETE      AWS::Lambda::Permission           HelloWorldFunctionHelloWorldPermissionProd     -                          
CREATE_COMPLETE      AWS::CloudFormation::Stack        sam-app                                        -                          
CloudFormation outputs from deployed stack
Key                 HelloWorldFunctionIamRole                                                     
Description         Implicit IAM Role created for Hello World function                            
Value               arn:aws:iam::xxxxxxxxxxxxx:role/sam-app-HelloWorldFunctionRole-UMXXXXXXXX     

Key                 HelloWorldApi                                                                 
Description         API Gateway endpoint URL for Prod stage for Hello World function              

Key                 HelloWorldFunction                                                            
Description         Hello World Lambda Function ARN                                               
Value               arn:aws:lambda:ap-northeast-1:xxxxxxxxxxxxxxx:function:sam-app-HelloWorldFunction-jimSNxxxx                                               

Successfully created/updated stack - sam-app in ap-northeast-1



message: "hello world"


  • sam build
  • sam deploy


sam delete

samプロジェクトフォルダ(sam-app)でsam deleteを実行すれば、

  • lambda関数
  • api-gateway
  • role
  • s3


$sam delete
        Are you sure you want to delete the stack sam-app in the region ap-northeast-1 ? [y/N]: y
        Are you sure you want to delete the folder sam-app in S3 which contains the artifacts? [y/N]: y
        - Deleting S3 object with key sam-app/599d1e96fc75b648a6bfd472db8f9051
        - Deleting S3 object with key sam-app/03ec28a1387579cb1ab12fee97d73b28.template
        - Deleting S3 object with key sam-app/1d02fc33fa3aeb45f5a86f73e8e83b33.template
        - Deleting S3 object with key sam-app/a749410668d0b70bd0c12c169aec16b3
        - Deleting Cloudformation stack sam-app

Deleted successfully


今回はaws資格のDAVに備えて色々と勉強する中で、sam cliにcloudshellで少しだけ触れました。

sam cliで記述することで、デプロイは確実に行え、簡単にサーバレスアプリケーションを構築することが出来ます。ただ、サーバレスアプリケーションの構成要素であるLambda、API Gateway、及びその連携をある程度理解しないと、sam cliのtemplate内容の理解はドキュメントを見ても難しいかなって思います。ある程度理解した後に、サーバレステンプレートの雛形として開発メンバに配ったりすると開発が加速出来そうです。