Skip to content

Instantly share code, notes, and snippets.

@thiagoponte
Created October 28, 2022 13:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagoponte/807956d52a03d400932f4d8489169209 to your computer and use it in GitHub Desktop.
Save thiagoponte/807956d52a03d400932f4d8489169209 to your computer and use it in GitHub Desktop.
CF template to deploy an Amazon DynamoDB Table. The table should have a composite primary key, and one index of your choice. Explicitly define a scaling policy for the Table
AWSTemplateFormatVersion: 2010-09-09
Resources:
OrdersTable:
Type: "AWS::DynamoDB::Table"
Properties:
TableName: AuthorsTable
AttributeDefinitions:
- AttributeName: AuthorName
AttributeType: S
- AttributeName: BookTitle
AttributeType: S
- AttributeName: Year
AttributeType: "N"
KeySchema:
- AttributeName: AuthorName
KeyType: HASH
- AttributeName: BookTitle
KeyType: RANGE
TimeToLiveSpecification:
AttributeName: ExpirationTime
Enabled: true
ProvisionedThroughput:
ReadCapacityUnits: "10"
WriteCapacityUnits: "5"
LocalSecondaryIndexes:
- IndexName: "LSI"
KeySchema:
- AttributeName: "BookTitle"
KeyType: "HASH"
- AttributeName: "Year"
KeyType: "RANGE"
Projection:
NonKeyAttributes:
- "AuthorName"
ProjectionType: "INCLUDE"
DependsOn:
- DynamoDBQueryPolicy
MyTableWriteCapacityScalableTarget:
Type: "AWS::ApplicationAutoScaling::ScalableTarget"
Properties:
MaxCapacity: 100
MinCapacity: 5
ResourceId: !Sub table/${myTableName}
RoleARN: !Sub arn:aws:iam::${AWS::AccountId}:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable
ScalableDimension: "dynamodb:table:WriteCapacityUnits"
ServiceNamespace: dynamodb
DependsON: AuthorsTable
MyTableWriteScalingPolicy:
Type: "AWS::ApplicationAutoScaling::ScalingPolicy"
Properties:
PolicyName: WriteAutoScalingPolicy
PolicyType: TargetTrackingScaling
ScalingTargetId:
Ref: UserTableWriteCapacityScalableTarget
TargetTrackingScalingPolicyConfiguration:
TargetValue: 70
ScaleInCooldown: 60
ScaleOutCooldown: 60
PredefinedMetricSpecification:
PredefinedMetricType: DynamoDBWriteCapacityUtilization
DynamoDBQueryPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: DynamoDBQueryPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: "dynamodb:Query"
Resource: "*"
Roles:
- Ref: OrdersTableQueryRole
OrdersTableQueryRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- dynamodb.amazonaws.com
Action:
- "sts:AssumeRole"
Path: /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment