Skip to content

Instantly share code, notes, and snippets.

@simplytunde
Created February 6, 2018 08:54
Show Gist options
  • Save simplytunde/7ac04bcd0320a890615e9eed0d8e0314 to your computer and use it in GitHub Desktop.
Save simplytunde/7ac04bcd0320a890615e9eed0d8e0314 to your computer and use it in GitHub Desktop.
Terraform DynamoDB Autoscaling
provider "aws" {
region = "us-west-1"
}
resource "aws_dynamodb_table" "test-database" {
name = "tenants"
read_capacity = 20
write_capacity = 20
hash_key = "roomNumber"
range_key = "name"
attribute {
name = "roomNumber"
type = "S"
}
attribute {
name = "name"
type = "S"
}
attribute {
name = "age"
type = "N"
}
attribute {
name = "ethnicity"
type = "S"
}
ttl {
attribute_name = "deletionTerm"
enabled = true
}
global_secondary_index {
name = "NameAgeIndex"
hash_key = "ethnicity"
range_key = "age"
write_capacity = 10 #This will be ignored but we only included it to meet the required
read_capacity = 10 #This will also be ignored
projection_type = "INCLUDE"
non_key_attributes = ["school","last4"] #This attributes
}
local_secondary_index {
name = "roomNumberNameIndex"
range_key = "age"
projection_type = "KEYS_ONLY" #we are not projecting any other attributes
}
tags {
Name = "dynamodb-test-table"
}
lifecycle {
ignore_changes = ["read_capacity","write_capacity"] #We want this ignored so that we can use app autoscaling
}
}
resource "aws_appautoscaling_target" "dynamodb_table_read_target" {
max_capacity = 100
min_capacity = 5
resource_id = "table/tenants"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}
resource "aws_appautoscaling_policy" "dynamodb_table_read_policy" {
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.dynamodb_table_read_target.resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = "${aws_appautoscaling_target.dynamodb_table_read_target.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.dynamodb_table_read_target.scalable_dimension}"
service_namespace = "${aws_appautoscaling_target.dynamodb_table_read_target.service_namespace}"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
target_value = 55 #set utilization to 55%
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment