エンジニアのブログ

最近クラウドを触りはじめたエンジニアのブログ

VS Codeでラムダのコードを書く(デプロイ編)

バケットを作る

aws s3 mb s3://【バケット名】」コマンドより、バケットを作成する。

$ aws s3 mb s3://【バケット名】
make_bucket: 【バケット名】

バケット名は世界で一意にする必要があり、重複している場合は下記エラーが表示される。

$ aws s3 mb s3://【バケット名】
make_bucket failed: s3://【バケット名】 An error occurred (InvalidBucketName) when calling the CreateBucket operation: The specified bucket is not valid.


SAMプロジェクトのAWSへのデプロイ
  1. コマンドパレットより、「AWS:Deploy SAM Application」を選択して実行
  2. 「Which SAM template would you like to deploy to AWS?」:sam-project/template.yml
  3. 「Which AWS region would you like to deploy to?」:Asia Pasific (Tokyo)
  4. 「Enter the AWS S3 bucket to which your code should be deployed」:【バケット名】
  5. 「Enter the name to use for the deployed stack」:sam-project
Starting SAM Application deployment...
Building SAM Application...
Packaging SAM Application to S3 Bucket: 【バケット名】 with profile: default
Deploying SAM Application to CloudFormation Stack: sam-project with profile: default
Successfully deployed SAM Application to CloudFormation Stack: sam-project with profile: default


デプロイされたSAMプロジェクトの実行
  1. マネージメントコンソールのAPI Gatewayページへ遷移
  2. ステージを選んで、作成されたメソッドの「URL呼び出し」のURLをクリック   例)https://xxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/hello
  3. {"message": "hello world"}が表示されることを確認


その他

SAMコマンドには今回実行したもの以外にも「sam local start-api」等があるが、どういう時に使うのかはわかっていないのでいつか理解したい。
AWS SAMのコマンドをまとめてみた - Qiita