Open6

MacでSAMる

takaha4ktakaha4k

環境セットアップ

Official Docs

HomebrewでSAM CLIを入れる。

brew update
brew -v

バージョンは以下

Homebrew 3.4.11
Homebrew/homebrew-core (git revision 1e4f6bb7a64; last commit 2022-06-01)
Homebrew/homebrew-cask (git revision 87b1606239; last commit 2022-05-31)

CLIインストールするがエラーる

brew tap aws/tap
brew install aws-sam-cli
Error: python@3.8: the bottle needs the Apple Command Line Tools to be installed.

Python3.8入れる

brew install --build-from-source python@3.8

仕切り直してSAM CLIインストール

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

OKできた

SAM CLI, version 1.51.0

aws-cliも入れる必要があった。
Docs確認する。
GUIインストール(pkgダウンロードして実行)する

AWSの設定入れる

aws configure

AWS Access Key ID [None]: *****
AWS Secret Access Key [None]: ******
Default region name [None]: ap-northeast-1
Default output format [None]:

SAM CLIを試す

sam init

対話形式で聞かれるので答える。
Quick Start Templates > Hello world Exampleを選択。

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]: y

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

Project name [sam-app]: 
takaha4ktakaha4k

ローカルで試す

ビルドコマンドを実行する

sam build --use-container

ローカルで実行する

sam local invoke  

うまく行ったぽい

Invoking app.lambda_handler (python3.9)
Image was not found.
Removing rapid images for repo public.ecr.aws/sam/emulation-python3.9
Building image........................
Skip pulling image and use local one: public.ecr.aws/sam/emulation-python3.9:rapid-1.51.0-x86_64.

Mounting /Users/takaha4k/repos/cdk-sam-example/sam-app/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: a2c77edf-16ee-4dda-af20-195b781dd856 Version: $LATEST
END RequestId: a2c77edf-16ee-4dda-af20-195b781dd856
REPORT RequestId: a2c77edf-16ee-4dda-af20-195b781dd856  Init Duration: 0.34 ms  Duration: 105.52 ms     Billed Duration: 106 ms Memory Size: 128 MB   Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}
takaha4ktakaha4k

API動かす

sam local start-api

動いた

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
2022-06-01 13:15:15  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
2022-06-01 13:15:18 127.0.0.1 - - [01/Jun/2022 13:15:18] "GET / HTTP/1.1" 403 -
2022-06-01 13:15:18 127.0.0.1 - - [01/Jun/2022 13:15:18] "GET /favicon.ico HTTP/1.1" 403 -
^C% 
takaha4ktakaha4k

デプロイしてみる

sam deploy --guided
# 対話形式で入力していく
        Stack Name [sam-app]: 
        AWS Region [ap-northeast-1]: 
        #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
        Confirm changes before deploy [y/N]: y
        #SAM needs permission to be able to create roles to connect to the resources in your template
        Allow SAM CLI IAM role creation [Y/n]: y
        #Preserves the state of previously provisioned resources when an operation fails
        Disable rollback [y/N]: y
        HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
        Save arguments to configuration file [Y/n]: y
        SAM configuration file [samconfig.toml]: 
        SAM configuration environment [default]: 

おお、できた