さくっと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
参考リンク
- 公式のサンプル
- リファレンス
関連記事
aws の記事
- [2022年12月1日] CognitoのIDトークンをLambda関数で検証するサンプルコード
- [2022年7月11日] API Gateway + Cognito Authorizer + Lambda関数でeventから得られるjsonのメモ
- [2022年5月25日] DynamoDBにboto3で操作した時のレスポンス、エラー出る・出ない
- [2022年4月13日] RAK7246にBasic Stationを入れてAWS LoRaWAN NetworkServerに接続
- [2022年1月18日] 2022年1月18日 DyanmoDB、メタバース、アキネータ
- ---本記事---
- [2021年6月12日] DynamoDBの多対多で隣接リストやった時の冗長性対策どうしよう
- [2021年5月13日] Lambda関数でDeadLetterQueue(DLQ)を試す
- [2021年1月25日] AWS Lambdaのロギングを考える
- [2021年1月12日] AWS Lambdaをデプロイした時のダウンタイム
- [2020年12月8日] DynamoDB から DocumentClient で get() した StringSet型 の値を取り出す
スポンサーリンク