1
2

More than 3 years have passed since last update.

AWS SAM で API をローカルでテストする

Last updated at Posted at 2020-02-29

AWS で API Gateway → lambdaのテストをローカルで実施する。

環境

  • macOS Mojave 10.14.6
  • Python 3.7.5
  • Docker 19.03.5
  • AWS SAM CLI 0.43.0

AWS SAM CLI をインストール

$ pip install aws-sam-cli

確認
$ sam --version
SAM CLI, version 0.43.0

サンプルのAWS SAM アプリケーションをダウンロード

参考:チュートリアル: Hello World アプリケーションのデプロイ

$ sam init

以下を選択して、ダウンロードを開始する。

  • template source: AWS Quick Start Templates
  • RunTime: python3.7

作成されたアプリケーションを確認

$ tree sam-app
sam-app
├── README.md
├── events
│   └── event.json
├── hello_world
│   ├── __init__.py
│   ├── app.py
│   └── requirements.txt
├── samconfig.toml
├── template.yaml
└── tests
    ├── __init__.py
    └── unit
        ├── __init__.py
        └── test_handler.py

template.yamlの内容を確認

template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-app

  Sample SAM Template for sam-app

# 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.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: /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

ローカルで SAM アプリケーションをテスト

nginxを起動する。

$ docker run -d -p 8080:80 --name {コンテナ名} nginx
$ docker start {コンテナ名}

起動中のコンテナを確認
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
XXXXXXXXXXXX        nginx               "nginx -g 'daemon of…"   2 hours ago         Up 3 minutes        0.0.0.0:8080->80/tcp   {コンテナ名}

API をローカルでホストするパターン

$ sam local start-api

これでREST API エンドポイントをレプリケートするローカルエンドポイントが起動されたため、以下でリクエストを送信する。
(ブラウザから下記にアクセスしてもOK)

$ curl http://127.0.0.1:3000/hello
{"message": "hello world"}%

lambda関数を一度だけ呼び出すパターン

$ sam local invoke "HelloWorldFunction" -e events/event.json
Invoking app.lambda_handler (python3.7)

Fetching lambci/lambda:python3.7 Docker container image......
Mounting /XXX/sam-app/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: XXX Version: $LATEST
END RequestId: XXX
REPORT RequestId: XXX   Init Duration: 572.22 ms    Duration: 4.86 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 23 MB  

{"statusCode":200,"body":"{\"message\": \"hello world\"}"}

サンプルアプリケーションのため、固定でhello worldが返るようになっています。


1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2