AWS SAM CLIとは
AWS SAM自体は以前の投稿
でも書いているので割愛。
AWS SAM CLIは、もともとsam localと呼ばれていたものの進化版?のようですね。
sam localは何度かつかってましたが、sam cliになったのを機に再度整理する。
イントール
参考:SAM CLI のインストール
dockerとイントールに使われるpip(Pythonのパッケージ管理)が必要になるが、詳細は割愛。
1
2
3
| $ pip install aws-sam-cli
$ sam --version
SAM CLI, version 0.6.0
|
参考サイトは0.3.0なので、少しずつ改善されているようですね。
sam initでサンプルを動かす
Simple App (sam init) の作成
ここではgolangを使って試してみる。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| $ sam init --runtime go
[+] Initializing project structure...
[SUCCESS] - Read sam-app/README.md for further instructions on how to proceed
[*] Project initialization is now complete
# こんなファイルが自動生成されます。
$ tree sam-app/
sam-app/
├── Makefile
├── README.md
├── hello-world
│ ├── main.go
│ └── main_test.go
└── template.yaml
1 directory, 5 files
|
ちなみに、template.yamlは、今までと変わってない(?)ので既存のリソースにも使えるはず。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| 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: 5
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: hello-world
Runtime: go1.x
Tracing: Active # https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html
Events:
CatchAll:
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
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
Variables:
PARAM1: VALUE
Outputs:
HelloWorldAPI:
Description: "API Gateway endpoint URL for Prod environment for First Function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "First Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
|
go test
テストも用意されてるので実行してみる(本題からそれますが、、)
1
2
3
4
| $ cd hello-world/
$ go test
PASS
ok _/Users/********/development/private/sam-cli/sam-app/hello-world 0.034s
|
go build
1
2
3
4
5
6
7
8
9
10
11
12
| $ GOOS=linux GOARCH=amd64 go build -o hello-world/hello-world ./hello-world
# hello-worldができてますね。
$ tree
.
├── Makefile
├── README.md
├── hello-world
│ ├── hello-world
│ ├── main.go
│ └── main_test.go
└── template.yaml
|
ローカルでAPIサーバ(api gateway + lambda)を起動
1
2
3
4
| $ sam local start-api
2018-08-31 16:16:58 Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2018-08-31 16:16:58 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
2018-08-31 16:16:58 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
|
http://127.0.0.1:3000/ で起動するので、http://127.0.0.1:3000/hello をブラウザで表示してみると
XXX.XXX.XXX.XXXはアクセス元のIPアドレスが表示されます。https://checkip.amazonaws.com/
の結果を表示しているだけ。
コンソールには下記の感じでログが表示されていきます。
1
2
3
4
5
6
7
8
9
10
| 2018-08-31 16:18:17 Invoking hello-world (go1.x)
2018-08-31 16:18:17 Found credentials in shared credentials file: ~/.aws/credentials
Fetching lambci/lambda:go1.x Docker container image......
2018-08-31 16:18:20 Mounting /Users/**********/development/private/sam-cli/sam-app/hello-world as /var/task:ro inside runtime container
START RequestId: d12296fa-78d8-1bf6-a821-f7bd494db81c Version: $LATEST
END RequestId: d12296fa-78d8-1bf6-a821-f7bd494db81c
REPORT RequestId: d12296fa-78d8-1bf6-a821-f7bd494db81c Duration: 763.56 ms Billed Duration: 800 ms Memory Size: 128 MB Max Memory Used: 11 MB
2018-08-31 16:18:22 No Content-Type given. Defaulting to 'application/json'.
2018-08-31 16:18:22 127.0.0.1 - - [31/Aug/2018 16:18:22] "GET /hello HTTP/1.1" 200 -
|
ローカルで検証できるととても便利。serverless開発には欠かせないですね。