AWSバックエンド開発の環境構築

## 初めに

## 目次
- 環境構築
  - 使用ソフト
  - インストール手順
- ツール
- SAMのデバッグ環境
   - 使用ソフト
   - インストール手順
-  最後に
## 環境構築
### 使用ソフト
Visual Studio Code
https://code.visualstudio.com/Download

Node.js
https://nodejs.org/ja/

Aws CLI
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/getting-started-install.html

SourceTree
https://www.sourcetreeapp.com/


### インストール手順

① AWS CLIの設定
```
$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json
```

https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-quickstart.html


② Visual Studio Codeの拡張機能

- AWS CLI Configure
- Docker
- EsLint
- npm
- IntelliCode
- YAML
- Japanese Language Pack for Visual Studio Code
- Jest

## ツール
- NoSQL Workbench 
DynamoDBのGUIツール
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/workbench.html
※コンソールよりサクサク動いて良い

## デバッグ環境
SAMを使用して、ローカルデバッグできる環境を構築する

### 使用ソフト
AWS SAM CLI
https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-install-windows.html

Docker
https://www.docker.com/get-started/

Wsl
https://docs.microsoft.com/ja-jp/windows/wsl/install

### ローカルデバッグ方法

#### ①samの初期化
```
$ 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: 11 

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 npm.
We will proceed copying the template using npm.

Select your starter template
        1 - Hello World Example
        2 - Hello World Example TypeScript
Template: 1

Would you like to enable X-Ray tracing on the function(s) in your application?  [y/N]: n

Project name [sam-app]: mySam

省略
```

sam init
https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-init.html

#### ①lunch.jsonの作成

```
{
    "configurations": [
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "my-example:HelloWorldFunction",
            "invokeTarget": {
                "target": "template",
                "templatePath": "template.yaml",
                "logicalId": "HelloWorldFunction"
            },
            "lambda": {
                "payload": {},
                "environmentVariables": {}
            }
        }
    ]
}
```

サーバーレスアプリケーションのデバッグ用設定オプション
https://docs.aws.amazon.com/ja_jp/toolkit-for-vscode/latest/userguide/serverless-apps-run-debug-config-ref.html

### ③template.yamlの修正
```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  mySam

  Sample SAM Template for mySam
  
# 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.lambdaHandler
      Runtime: nodejs14.x
      Layers:
        - LayerのARNを設定
      Enviromnet:
        Variables:
         環境変数を指定していく
         ENV:dev ←例
```

AWS::Serverless::Function
https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-resource-function.html

AWS SAM テンプレートの Globals セクション
https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-specification-template-anatomy-globals.html

## 最後に
samのcliは慣れるのに少し大変ですが、AWS上で実行するより断然楽でした。
また、タイムアウト時間が短いと、レイヤーをDocker上で動かすので、初期ロードに時間（1分ほど）がかかります。
1度、ロードが終われば、2回目以降は問題なく動作しました。