AWS SAMのチュートリアルをやってみた
チュートリアル: Hello World アプリケーションのデプロイ
AWS SAM CLIのインストール
環境:Amazon Linux 2
Dockerのインストール
1 2 3 4 5 6 |
sudo amazon-linux-extras install docker -y sudo service docker start sudo usermod -a -G docker ec2-user # 一度ログインし直す docker ps |
無事表示されたらインストールの確認終了
Homebrewのインストール
1 2 3 4 5 6 7 8 9 10 |
# gitないなら入れる yum install git -y sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)" # PATHにHomebrewを追加 test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv) test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv) test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile # 確認 brew --version |
無事表示されたらインストールの確認終了
AWS SAM CLIのインストール
1 2 3 4 |
brew tap aws/tap brew install aws-sam-cli # 確認 sam --version |
サンプルのAWS SAMアプリをダウンロード
1 |
sam init |
いろいろ聞かれるので以下を参考に進む
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 |
Which template source would you like to use? 1 - AWS Quick Start Templates 2 - Custom Template Location Choice: 1 Which runtime would you like to use? 1 - nodejs12.x 2 - python3.8 3 - ruby2.7 4 - go1.x 5 - java11 6 - dotnetcore2.1 7 - nodejs10.x 8 - python3.7 9 - python3.6 10 - python2.7 11 - ruby2.5 12 - java8 13 - dotnetcore2.0 14 - dotnetcore1.0 Runtime: 8 AWS quick start application templates: 1 - Hello World Example 2 - EventBridge Hello World 3 - EventBridge App from scratch (100+ Event Schemas) Template selection: 1 |
完了後、以下が出力される
1 2 3 4 5 6 7 8 9 10 |
----------------------- Generating application: ----------------------- Name: sam-app Runtime: python3.7 Dependency Manager: pip Application Template: hello-world Output Directory: . Next steps can be found in the README file at ./sam-app/README.md |
/home/ec2-user配下にsam-appがあるか確認
構築
1 2 |
cd sam-app/ sam build |
AWSにデプロイ
1 |
sam deploy --guided |
いろいろ聞かれるので適当に答える
1 2 3 4 5 6 7 8 9 |
Stack Name [sam-app]: sam-app AWS Region [us-east-1]: 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 Save arguments to samconfig.toml [Y/n]: Y Deploy this changeset? [y/N]: y |
完了したらCloudFormationに「sam-app」というスタックが作成されているので確認する
「出力」タブに「HelloWorldApi」とあるので、その値をcurlコマンドで試す
1 |
curl https://.execute-api.us-east-1.amazonaws.com/Prod/hello/ |
正常にデプロイできていれば以下の出力がある
1 |
{"message": "hello world"} |
クリーンアップ
CloudFormationで作成された「sam-app」というスタックを削除すればOK
aws cliならこれでサクッと削除できます
1 |
aws cloudformation delete-stack --stack-name aws-sam-getting-started --region region |
以上です。