さくっとLambda関数でDynamoDBに何かしたい時、AWS SAMを使ってやるテンプレートのメモ。
テンプレートファイル
テーブル sample_table を作成し、そのテーブルをLambda関数からアクセスしたい時のテンプレートファイル。Lambda関数はCloudWatchでキックする。
なお、オンデマンドモードなので従量課金なので注意。このへんの設定変えるのはBillingModeだが、実際の値は後述の参考リンクにある公式サンプルを参照。
`AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: > dynamodb-sample Globals: Function: Timeout: 10 Resources: SampleFunc: Type: AWS::Serverless::Function Properties: CodeUri: src Handler: sample_func/app.lambda_handler Runtime: python3.8 Policies: - arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess MemorySize: 256 Environment: Variables: TABLE_NAME: 'sample_table' Events: SampleRuleDev: Type: Schedule Properties: Schedule: rate(5 minutes) DeadLetterQueue: Type: SNS TargetArn: arn:aws:sns:ap-northeast-1:xxxx:SampleTopic SampleTable: Type: AWS::DynamoDB::Table Properties: TableName: sample_table AttributeDefinitions: - AttributeName: sample_pk AttributeType: S - AttributeName: sample_sk AttributeType: N KeySchema: - AttributeName: sample_pk KeyType: HASH - AttributeName: sample_sk KeyType: RANGE BillingMode: PAY_PER_REQUEST `
