2
0

More than 3 years have passed since last update.

CloudFormationでスタックを作成しようとしたら「Encountered unsupported property <Property Name>」となる

Last updated at Posted at 2020-02-12

事象

lambda.yml
Resources:
  DemoFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: demo_func
      Code:
        ZipFile: |
          import json
          def lambda_handler(event, context):
              jsn_str = json.dumps(event, ensure_ascii=False)
              print(jsn_str)
      Handler: lambda_function.lambda_handler
      MemorySize: 128
      Timeout: 30
      Role: !Sub arn:aws:iam::${AWS::AccountId}:role/Lambda_Basic_Role
      Runtime: python3.7
      Events:
        Stream:
          Type: DynamoDB
          Properties:
            Stream:
              Fn::ImportValue: demoTableStream
            StartingPosition: LATEST
            BatchSize: 1

AWS CLIでcreate-stackコマンドにより上記テンプレートlambda.ymlをもとにDynamoDB StreamをトリガーにしたLambda Functionを作成しようとした。

$ aws cloudformation create-stack \
    --stack-name demo-lambda \
    --template-body file://lambda.yml

しかし下記のようにEncountered unsupported property Eventsというエラーとなりスタックの作成が失敗する。

image.png

解決

CloudFormationテンプレートではType: AWS::Lambda::FunctionではEventsというプロパティは使えなかった。LambdaのDynamoDB Streamトリガーをテンプレートで定義する場合は下記のようにAWS::Lambda::EventSourceMappingリソースを使う必要があった。

lambda.yml
Resources:
  DemoFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: demo_func
      Code:
        ZipFile: |
          import json
          def lambda_handler(event, context):
              jsn_str = json.dumps(event, ensure_ascii=False)
              print(jsn_str)
      Handler: index.lambda_handler
      MemorySize: 128
      Timeout: 30
      Role: !Sub arn:aws:iam::${AWS::AccountId}:role/Lambda_Basic_Role
      Runtime: python3.7

  EventMapping:
    Type: AWS::Lambda::EventSourceMapping
    Properties:
      EventSourceArn: !ImportValue demoTableStream
      FunctionName: !Ref DemoFunction
      StartingPosition: LATEST

image.png

おわりに

AWS SAMのymlテンプレートを通常のCloudFormationテンプレートに流用していたため利用できないプロパティが紛れていたことが根本原因だった。

以上

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0