事象
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
というエラーとなりスタックの作成が失敗する。
解決
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
おわりに
AWS SAMのymlテンプレートを通常のCloudFormationテンプレートに流用していたため利用できないプロパティが紛れていたことが根本原因だった。
以上