Skip to content

Commit e516031

Browse files
committed
add documents
1 parent a27d640 commit e516031

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1020
-0
lines changed

LICENSE.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# LICENSE
2+
3+
## under the `docs` directory
4+
5+
[CC-BY 3.0](https://creativecommons.org/licenses/by/3.0/)
6+
<img src="https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by.svg" alt="CC-BY">
7+
8+
## under the `src` directory
9+
10+
MIT License
11+
12+
```
13+
Copyright 2021 Tomonori Niwa
14+
15+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
```

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# AWS Lambda Debug
2+
3+
本ドキュメントは、Visual Studio Code, AWS SAM CLI, JavaScript/TypeScriptを使った開発時におけるデバッグ方法の資料です。
4+
5+
ゴールは、AWSが公式に出している[Node.js 12.XのDockerイメージ](https://hub.docker.com/r/amazon/aws-sam-cli-emulation-image-nodejs12.x)を使って、
6+
SAM(Serverless Application Model)以外のデプロイ環境(例: CDK + TypeScript)でも、SAM CLIを使ってローカルでデバッグできるようになることです。
7+
8+
- [Step 1 環境インストール](./docs/01-installation.md)
9+
- [Step 2 最小限入門](./docs/02-minimum-getting-started.md)
10+
- [Step 3 解説とカスタマイズ](./docs/03-instruction-customize.md)
11+
- [Step 4 環境変数とイベント引数](./docs/04-env-event.md)
12+
- [Step 5 TypeScriptでデバッグ](./docs/05-typescript.md)
13+
- [Step 6 CDK + TypeScriptでデバッグ](./docs/06-cdk-typescript.md)
14+
15+
- [トラブルシューティング](./docs/xx-trouble-shoot.md)
16+
17+
## LICENSE
18+
19+
See: [LICENSE.md](./LICENSE.md)
20+
21+
Created by: [intptr-t](https://github.com/intptr-t)

docs/01-installation.md

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Step 1 環境準備
2+
3+
## インストール
4+
それぞれすでにインストール済みの場合はスキップして問題ありません。
5+
また、使い慣れた代替のツールがある場合そちらを使用しても問題ありません。
6+
7+
### Visual Studio Code
8+
- <https://code.visualstudio.com/>
9+
10+
コマンドで動作確認
11+
(※バージョンの値は2021/01/25時点の情報です)
12+
13+
```zsh
14+
% code -v
15+
1.52.1
16+
ea3859d4ba2f3e577a159bc91e3074c5d85c0523
17+
x64
18+
```
19+
20+
### Docker Desktop
21+
- <https://www.docker.com/get-started>
22+
23+
コマンドで動作確認
24+
(※バージョンの値は2021/01/25時点の情報です)
25+
26+
```zsh
27+
% docker -v
28+
Docker version 20.10.2, build 2291f61
29+
```
30+
31+
### AWS CLI
32+
- <https://aws.amazon.com/jp/cli/>
33+
34+
コマンドで動作確認
35+
(※バージョンの値は2021/01/25時点の情報です)
36+
37+
```zsh
38+
% aws --version
39+
aws-cli/2.1.8 Python/3.7.4 Darwin/19.6.0 exe/x86_64 prompt/off
40+
```
41+
42+
### SAM CLIインストール
43+
- <https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html>
44+
45+
コマンドで動作確認
46+
(※バージョンの値は2021/01/25時点の情報です)
47+
48+
```zsh
49+
% sam --version
50+
SAM CLI, version 1.15.0
51+
```
52+
53+
### Git
54+
- <https://git-scm.com/downloads>
55+
56+
```
57+
% git --version
58+
git version 2.24.3 (Apple Git-128)
59+
```
60+
61+
## 環境設定
62+
63+
- AWS CLI
64+
65+
このドキュメント全体で `local-test` という名称でプロファイルを作成します。
66+
別の名前で作成した場合、適宜読み替えてください。
67+
68+
```
69+
% aws configure --profile local-test
70+
AWS Access Key ID [None]: local-id
71+
AWS Secret Access Key [None]: local-secret
72+
Default region name [None]: ap-northeast-1
73+
Default output format [None]: json
74+
```
75+
76+
- Docker network
77+
78+
このドキュメント全体で、 `sam-local` という名称でネットワークの設定作成します。
79+
別の名前で作成した場合は、適宜読み替えてください。
80+
81+
```
82+
% docker network create sam-local
83+
```
84+
85+
Back to [README](../README.md)
86+
87+
Next to [Step 2 最小限入門](02-minimum-getting-started.md)

docs/02-minimum-getting-started.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Step 2 最小限入門
2+
3+
[Step 1 環境準備](./01-installation.md)で設定した環境が設定済みであることを前提に進めます。
4+
5+
## 実行
6+
7+
ターミナル(コマンドプロンプト, PowerShell, bash, zsh, etc.)を立ち上げ、以下コマンドを実行します。
8+
9+
```
10+
% git clone [email protected]:intptr-t/aws-lambda-local-debug.git
11+
% cd src/02-1-getting-started
12+
% code .
13+
```
14+
15+
Visual Studio Codeが立ち上がったら、`index.js` を開きブレークポイントをセットします。
16+
17+
次に Visual Studio Codeのターミナルを立ち上げます。
18+
立ち上がったターミナルに以下を貼り付け実行します。
19+
20+
```
21+
% sam local invoke "test" --template ./template.yaml --docker-network sam-local --no-event --debug-port 5858 --profile local-test
22+
```
23+
24+
Visual Studio Codeのターミナルに以下の内容が出力されたら、デバッグ開始の準備完了です。
25+
`F5`キーを入力し、デバッグ実行します。
26+
27+
```
28+
Debugger listening on ws://0.0.0.0:5858/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
29+
For help, see: https://nodejs.org/en/docs/inspector
30+
```
31+
32+
実行結果
33+
34+
```
35+
2021-01-24T18:12:43.649Z 289a5fc1-f44e-4614-8efa-ea443bcfd0eb INFO hello, world!
36+
END RequestId: 289a5fc1-f44e-4614-8efa-ea443bcfd0eb
37+
REPORT RequestId: 289a5fc1-f44e-4614-8efa-ea443bcfd0eb Init Duration: 0.82 ms Duration: 6741.21 ms Billed Duration: 6800 ms Memory Size: 128 MB Max Memory Used: 128 MB
38+
null
39+
```
40+
41+
<img src="./media/02-getting-started.gif" alt="実行結果">
42+
43+
## コマンドの詳細 公式リファレンス
44+
- [sam local invoke](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-invoke.html)
45+
- [Step-through debugging Lambda functions locally](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging.html)
46+
47+
Prev to [Step 1 環境準備](./01-installation.md)
48+
49+
Back to [README](../README.md)
50+
51+
Next To [Step 3 解説とカスタマイズ](./03-instruction-customize.md)

docs/03-instruction-customize.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Step 3 解説とカスタマイズ
2+
3+
## 解説
4+
5+
1つ前のステップ[Step 2 最小限入門](./02-minimum-getting-started.md) を解説します。
6+
7+
### index.js
8+
9+
このファイルは、AWS Lambda上で実行したい対象のファイルです。
10+
今回は、「hello,world!」を出力するだけのプログラムで、何も返却しない(`return`がない)ため結果は null になります。
11+
12+
### template.yaml
13+
14+
sam initでアプリケーションを作成した時の `template.yaml` の代替です。
15+
`sam local invoke` するのに必要な最低限の設定が記載されたファイルになります。
16+
値の詳細は [CloudFormation のリファレンス](https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html) を参照してください。
17+
18+
公式のドキュメントに無いパラメータとして、
19+
キモは [AWS CDK側のドキュメント](https://docs.aws.amazon.com/cdk/latest/guide/assets.html#assets_cfn)の記載にある リソースメタデータ `aws:asset:path` です。
20+
ここに記載されているローカルディレクトリのパスが `sam local invoke` で実行したとき、template.yamlから見て docker上の `/var/task` (LAMBDA_TASK_ROOT) にマウントされるパスになります。
21+
22+
### launch.json
23+
24+
Visual Studio Codeでデバッグするときの構成ファイルです。
25+
キモは2点、
26+
1つ目は [Node.js をリモートでデバッグする時の構成](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_remote-debugging)になっていること。
27+
2つ目は docker上で動いている Lambda 関数をVisual Studio Codeから見たディレクトリへ対応づけるパラメータ `localRoot` を正しく設定することです。
28+
29+
## カスタマイズの例
30+
31+
上記までの内容を総合するとディレクトリ構成をカスタマイズすることが可能になります。
32+
参考の構成を以下に示します。
33+
34+
詳細は本リポジトリの [03-instruction-customize](../src/03-instruction-customize) を参照してください。
35+
36+
### ディレクトリ構成
37+
38+
- template.yaml を デバッグ専用のディレクトリへ配置
39+
- index.js を src 配下に配置
40+
41+
```
42+
.
43+
├── .vscode
44+
│   └── launch.json
45+
├── debug
46+
│   └── template.yaml
47+
└── src
48+
└── index.js
49+
```
50+
51+
#### 上記構成のtemplate.yaml
52+
53+
aws:asset:path を template.yaml から見た src ディレクトリを指すようにします。
54+
(ターミナルのカレントディレクトリ(`.`)では無い点に注意してください)
55+
56+
```yaml
57+
aws:asset:path: "../src"
58+
```
59+
60+
#### 上記構成のlaunch.json
61+
62+
Visual Studio Codeワークスペースルートから見て、src を指すようにします。
63+
(はじめの解説で開いた ディレクトリ`02-1-getting-started` とは別に `02-2-getting-started-customize` を Visual Studio Codeルートとして開いている点に注意)
64+
65+
```json
66+
"localRoot": "${workspaceRoot}/src",
67+
```
68+
69+
#### 実行コマンド
70+
71+
ターミナルから見て debugディレクトリ配下の template.yaml を指定するため、パスが変更になります。
72+
(`--template ./debug/template.yaml`の箇所です)
73+
74+
```
75+
% sam local invoke "test" --template ./debug/template.yaml --docker-network sam-local --no-event --debug-port 5858 --profile local-test
76+
```
77+
78+
Prev to [Step 2 最小限入門](./02-minimum-getting-started.md)
79+
80+
Back to [README](../README.md)
81+
82+
Next To [Step 4 環境変数とイベント引数](./04-env-event.md)

docs/04-env-event.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Step 4 環境変数とイベント引数
2+
3+
本節の説明は、SAM CLI の基本的な使い方の内容のため、すでに把握されている方は飛ばして問題ありません。
4+
5+
対象ディレクトリ: [04-env-event](../src/04-env-event)
6+
7+
## 環境変数設定
8+
9+
まず、template.yaml の Resources > _関数名_ > Properties > Environment > Variables に環境変数を追加します。
10+
これは、AWS Lambda上の環境変数に設定される環境変数に相当します。
11+
12+
ただし、ローカルで実行する際に 認証情報 など秘匿性が高いものを記載するとリポジトリで管理する際に不便になってしまいます。
13+
そこで、SAM CLIの `---env-vars` で 環境変数を上書きします。
14+
注意点として、template.yaml に無い環境変数は**上書きできません**。(UPSERTではなく、UPDATE的であるということです。)
15+
16+
## イベント引数
17+
18+
関数が呼び出されたときに渡される引数のJSONファイルを指定します。
19+
20+
### 実行
21+
22+
- `--no-event` から `--event ./debug/debug-event.json` へ変更
23+
- `--env-vars ./debug/debug-env.json` を追加
24+
25+
```
26+
% sam local invoke "test" --template ./debug/template.yaml --docker-network sam-local --event ./debug/debug-event.json --env-vars ./debug/debug-env.json --debug-port 5858 --profile local-test
27+
```
28+
29+
### 実行結果
30+
31+
```
32+
2021-01-24T20:29:39.502Z 550e5067-c20e-4e63-aa75-68f0561e83ab INFO hello,world
33+
END RequestId: 550e5067-c20e-4e63-aa75-68f0561e83ab
34+
REPORT RequestId: 550e5067-c20e-4e63-aa75-68f0561e83ab Init Duration: 0.78 ms Duration: 3983.27 ms Billed Duration: 4000 ms Memory Size: 128 MB Max Memory Used: 128 MB
35+
"foobar"
36+
```
37+
38+
Prev to [Step 3 解説とカスタマイズ](./03-instruction-customize.md)
39+
40+
Back to [README](../README.md)
41+
42+
Next to [Step 5 TypeScriptでデバッグ](./05-typescript.md)

0 commit comments

Comments
 (0)