Skip to content

Instantly share code, notes, and snippets.

@em-shea
Last active April 15, 2024 18:54
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save em-shea/fd6753816ef855f0844216833b389cd6 to your computer and use it in GitHub Desktop.
Save em-shea/fd6753816ef855f0844216833b389cd6 to your computer and use it in GitHub Desktop.
An example SAM template that creates a DynamoDB table, a Lambda function that writes to DynamoDB, and an EventBridge trigger
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An app that includes a DynamoDB table, a Lambda function that writes to DynamoDB, and a scheduled EventBridge event
Resources:
LambdaWriteToDynamoDB:
# A function that writes to a DynamoDB table on a schedule
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: LambdaWriteToDynamoDB
Handler: LambdaWriteToDynamoDB.lambda_handler
Runtime: python3.7
CodeUri: ./LambdaWriteToDynamoDB
Description: A function that writes to a DynamoDB table on a schedule
MemorySize: 128
Timeout: 120
Policies:
# Read more about SAM policy templates here
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html
- DynamoDBCrudPolicy:
TableName: !Ref DynamoDBTable
Environment:
Variables:
TABLE_NAME: !Ref DynamoDBTable
Events:
ScheduledEvent:
Type: Schedule
Properties:
# Read more about schedule expressions here:
# https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html
# This event runs every Friday at 16 UTC/9AM PST
Schedule: cron(0 16 ? * FRI *)
DynamoDBTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Retain
Properties:
TableName: DynamoDBTable
AttributeDefinitions:
- AttributeName: ListId
AttributeType: S
- AttributeName: Date
AttributeType: S
KeySchema:
- AttributeName: ListId
KeyType: HASH
- AttributeName: Date
KeyType: RANGE
BillingMode: PAY_PER_REQUEST
ProvisionedThroughput:
ReadCapacityUnits: 0
WriteCapacityUnits: 0
@lwitzani
Copy link

lwitzani commented Mar 9, 2022

thx this helped me setting up my DynamoDB table :)

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