Skip to content

Instantly share code, notes, and snippets.

@3panda
Last active April 1, 2022 04:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3panda/820d9e3657edf5cab09f1bba02380656 to your computer and use it in GitHub Desktop.
Save 3panda/820d9e3657edf5cab09f1bba02380656 to your computer and use it in GitHub Desktop.
AWS SAM( Serverless Framework Model)の基本的な実装方法のメモ (随時更新)

前提

AWS CLIのインストールなどはこちら

構成

今回の構成は以下の通り

├── functions
│   └── lambda_function.py
└── template.yaml

実行するUserに必要なポリシー  CreateChangeSetとExecuteChangeSet

実行するUserにはCloudformationのCreateChangeSet(変更)とExecuteChangeSet(実行)の許可が必要 ハマりどころなので注意!

既存のポリシーにはないので自作

サービスはcloudformationを選択 以下にチェック CreateChangeSet ExecuteChangeSet

JSON

JSONで表すと以下

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "cloudformation:CreateChangeSet",
                "cloudformation:ExecuteChangeSet"
            ],
            "Resource": "*"
        }
    ]
}

S3 バケットの作成

パッケージングしたデータは一旦S3に保存されるのでバケットを作成する ※同じリージョンで同一の名前は作成出来ないので注意!

aws s3 mb s3://sam-sample-xxxxxxx

Template

  • 今回のTemplateの名前はtemplate.yaml
  • Resources直下はLambdaの関数名(任意)
  • Roleは必要な権限を(CreateChangeSetとExecuteChangeSetのポリシーを忘れずに)
  • Runtimeは使用する言語
  • HandlerはLambdaのhandlerをPathから指定
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Create Lambda function by using AWS SAM.
Resources:
  SamSampleLambda:
    Type: AWS::Serverless::Function
    Properties:
      Handler: functions/lambda_function.lambda_handler
      Runtime: python3.6
      Role: arn:aws:iam::111111111111:role/xxxxxxxx
      Timeout: 30

Package

Template、Package後のファイル名(packaged-template.yaml)、保存先のs3-bucket名を指定しPackage

aws cloudformation package \
    --template-file template.yaml \
    --s3-bucket sam-sample-xxxxxx \
    --output-template-file packaged-template.yaml \
    --profile xxxxx

Deploy

パッケージングしたテンプレート(packaged-template.yaml)を用いて Cfn の実行

aws cloudformation deploy \
    --template-file packaged-template.yaml \
    --stack-name sam-sample-stack \
    --capabilities CAPABILITY_IAM \
    --profile xxxxx

成功するとLambdaに関数が作成されている

Deploy時にハマりどころなエラー

ネットなどにあまり情報が無くて困ったエラー(個人的に)

Userが存在しないなどのエラー

このエラーはCloudformation上にStackも作成されていない(はず)

An error occurred (AccessDenied) when calling the CreateChangeSet operation: User: arn:aws:iam::111111111111:user/user is not authorized to perform: cloudformation:CreateChangeSet on resource: arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxx:stack/sam-sample-stack/*

CreateChangeSetをUserが行えない

このエラーはCloudformation上にStackの作成は成功しロールの問題で失敗している

Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: User: arn:aws:iam::111111111111:user/username is not authorized to perform: cloudformation:CreateChangeSet on resource: arn:aws:cloudformation:ap-northeast-1:aws:transform/Serverless-2016-10-31

解決策

  • 上記に説明したUserにCreateChangeSetとExecuteChangeSetの権限を付与
  • ロールにLamdaのアクセス権限があるか確認
  • ロールが存在するものか確認

Templateで記述ミスなどを犯している

Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Invalid template property or properties XXXXXX

解決策

XXXXXXの部分がヒント

参考

こちら拝見し理解したところをメモにしています AWS Serverless Application Model 入門ハンズオンシリーズ(http://www.ketancho.net/entry/aws_sam)

@takaahii
Copy link

takaahii commented May 1, 2019

ちょうど不明だった箇所が解消されました。ありがとうございます。
(CloudFrontの権限を付与していなかったので、、)
サーバーレスアプリケーション開発ガイドにも、CloudFrontの権限には言及していませんでした。(まだ、途中ですが、、)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment