AWS SAM ローカルでHello World実行まで

スポンサードサーチ

はじめに

MacにAWS SAM CLIをインストールします。
手順はディベロッパーガイドのmacOS への AWS SAM CLI のインストールに記載されています。

AWSのアカウントはない状態でもできます。
上記ガイドのステップ 1: AWS アカウントを作成する, ステップ 2: IAM アクセス許可を設定すると IAM アクセス許可を設定するAWS認証情報 はここではスキップします。

また、DockerとHomebrewはインストール済と仮定します。

AWS SAM CLIをインストール

$ brew tap aws/tap
$ brew install aws-sam-cli

インストールできたか確認

$ sam --version                                                                                                                                                                                                                                                     [±main ●]
SAM CLI, version 1.33.0

プロジェクトの作成

ここではPython3.9を選択します

$ sam init                                                                                                                                                                                                                                                          [±main ✓]
Which template source would you like to use?
	1 - AWS Quick Start Templates
	2 - Custom Template Location
Choice: 1
What package type would you like to use?
	1 - Zip (artifact is a zip uploaded to S3)
	2 - Image (artifact is an image uploaded to an ECR image repository)
Package type: 1

Which runtime would you like to use?
	1 - nodejs14.x
	2 - python3.9
	3 - ruby2.7
	4 - go1.x
	5 - java11
	6 - dotnetcore3.1
	7 - nodejs12.x
	8 - nodejs10.x
	9 - python3.8
	10 - python3.7
	11 - python3.6
	12 - python2.7
	13 - ruby2.5
	14 - java8.al2
	15 - java8
	16 - dotnetcore2.1
Runtime: 2

Project name [sam-app]:

Cloning from https://github.com/aws/aws-sam-cli-app-templates

AWS quick start application templates:
	1 - Hello World Example
	2 - EventBridge Hello World
	3 - EventBridge App from scratch (100+ Event Schemas)
	4 - Step Functions Sample App (Stock Trader)
	5 - Elastic File System Sample App
Template selection: 1

    -----------------------
    Generating application:
    -----------------------
    Name: sam-app
    Runtime: python3.9
    Architectures: x86_64
    Dependency Manager: pip
    Application Template: hello-world
    Output Directory: .

    Next steps can be found in the README file at ./sam-app/README.md

これで以下の構成のプロジェクトが作成されます。

.
┗━ sam-app/
      ┣━ .gitignore
      ┣━ __init__.py
      ┣━ events
      ┃   ┗━ event.json
      ┣━ hello_world
      ┃   ┣━ __init__.py
      ┃   ┣━ app.py
      ┃   ┗━ requirements.txt
      ┣━ template.yaml
      ┗━ tests
          ┣━ __init__.py
          ┣━ integration
          ┃   ┣━ __init__.py
          ┃   ┗━  test_api_gateway.py
          ┣━ requirements.txt
          ┗━ unit
              ┣━ __init__.py
              ┗━ test_header.py

ビルド

作成されたラムダ関数をビルドします

$ sam build
Building codeuri: /Users/xxxxxx/aws-sam-sample/sam-app/hello_world runtime: python3.9 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
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided

ローカルで実行

$ sam local start-api                                                                                                                                                                                                                                              [±main ●▴]

Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [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
2021-10-20 17:06:25  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

http://127.0.0.1:3000/hello [GET]

と表示されるので、curlで実行

$ curl http://127.0.0.1:3000/hello                                                                                        [±main ●▴]
{"message": "hello world"}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です