Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,69 @@ No inputs.
| Name | Value |
|------|-------|
| s3_dump_file | Details about the dump file created |

<!-- BEGIN_TF_DOCS -->
## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
| <a name="provider_null"></a> [null](#provider\_null) | n/a |
| <a name="provider_template"></a> [template](#provider\_template) | n/a |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_appautoscaling_policy.dynamodb_table_read_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_policy.dynamodb_table_write_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource |
| [aws_appautoscaling_target.dynamodb_table_read_target](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
| [aws_appautoscaling_target.dynamodb_table_write_target](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
| [aws_dynamodb_table.basic-dynamodb-table](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_table) | resource |
| [null_resource.db_backup](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.db_restore](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [aws_s3_bucket_objects.get_dump_list](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/s3_bucket_objects) | data source |
| [template_file.dynamo_backup](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
| [template_file.dynamo_restore](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_app_name"></a> [app\_name](#input\_app\_name) | n/a | `string` | `null` | no |
| <a name="input_autoscaling_enabled"></a> [autoscaling\_enabled](#input\_autoscaling\_enabled) | Use autoscaling for read/write capacity | `bool` | `false` | no |
| <a name="input_aws_profile"></a> [aws\_profile](#input\_aws\_profile) | n/a | `string` | `null` | no |
| <a name="input_backup_on_destroy"></a> [backup\_on\_destroy](#input\_backup\_on\_destroy) | Backup DB to S3 | `bool` | `true` | no |
| <a name="input_billing_mode"></a> [billing\_mode](#input\_billing\_mode) | Indicates mode of billing. Value should be either PROVISIONED or PAY\_PER\_REQUEST | `string` | `"PROVISIONED"` | no |
| <a name="input_dynamodb_config"></a> [dynamodb\_config](#input\_dynamodb\_config) | n/a | `any` | n/a | yes |
| <a name="input_env_name"></a> [env\_name](#input\_env\_name) | n/a | `string` | `null` | no |
| <a name="input_env_type"></a> [env\_type](#input\_env\_type) | n/a | `string` | `null` | no |
| <a name="input_global_secondary_indeces"></a> [global\_secondary\_indeces](#input\_global\_secondary\_indeces) | Array of GSI definitions | `any` | `[]` | no |
| <a name="input_init_db_aws_profile"></a> [init\_db\_aws\_profile](#input\_init\_db\_aws\_profile) | Source envirnment aws profile to restore db from | `string` | `"NULL"` | no |
| <a name="input_init_db_env_type"></a> [init\_db\_env\_type](#input\_init\_db\_env\_type) | Source envirnment aws profile to restore db from | `string` | `"NULL"` | no |
| <a name="input_init_db_environment"></a> [init\_db\_environment](#input\_init\_db\_environment) | Source envirnment name to restore db from | `string` | `"NULL"` | no |
| <a name="input_max_read_capacity"></a> [max\_read\_capacity](#input\_max\_read\_capacity) | Maximum read autoscaling capacity | `number` | `20` | no |
| <a name="input_max_write_capacity"></a> [max\_write\_capacity](#input\_max\_write\_capacity) | Maximum write autoscaling capacity | `number` | `20` | no |
| <a name="input_primary_key"></a> [primary\_key](#input\_primary\_key) | n/a | `string` | n/a | yes |
| <a name="input_primary_key_type"></a> [primary\_key\_type](#input\_primary\_key\_type) | n/a | `string` | n/a | yes |
| <a name="input_primary_sort_key"></a> [primary\_sort\_key](#input\_primary\_sort\_key) | n/a | `string` | `null` | no |
| <a name="input_primary_sort_key_type"></a> [primary\_sort\_key\_type](#input\_primary\_sort\_key\_type) | n/a | `any` | `null` | no |
| <a name="input_read_capacity"></a> [read\_capacity](#input\_read\_capacity) | n/a | `number` | `5` | no |
| <a name="input_restore_on_create"></a> [restore\_on\_create](#input\_restore\_on\_create) | Restore DB from dump file | `bool` | `true` | no |
| <a name="input_table_name"></a> [table\_name](#input\_table\_name) | n/a | `string` | n/a | yes |
| <a name="input_target_utilization_percent"></a> [target\_utilization\_percent](#input\_target\_utilization\_percent) | Target utilization for read/write autoscaling capacity | `number` | `70` | no |
| <a name="input_ttl_attribute_name"></a> [ttl\_attribute\_name](#input\_ttl\_attribute\_name) | n/a | `string` | `null` | no |
| <a name="input_write_capacity"></a> [write\_capacity](#input\_write\_capacity) | n/a | `number` | `5` | no |

## Outputs

No outputs.
<!-- END_TF_DOCS -->
20 changes: 10 additions & 10 deletions data.tf
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
data "aws_s3_bucket_objects" "get_dump_list" {
bucket = "${var.app_name}-${var.env_type}-dynamodb-dumps"
prefix = "${var.env_name}/dynamodb-${var.app_name}-${var.env_name}.json"
bucket = "${local.app_name}-${local.env_type}-dynamodb-dumps"
prefix = "${local.env_name}/dynamodb-${local.app_name}-${local.env_name}.json"
}



data "template_file" "dynamo_backup" {
template = "${file("${path.module}/files/dynamo_backup.tpl")}"
vars = {
SERVICE_NAME="${var.app_name}"
WORKSPACE="${var.env_name}"
ENV_TYPE="${var.env_type}"
AWS_PROFILE="${var.aws_profile}"
SERVICE_NAME="${local.app_name}"
WORKSPACE="${local.env_name}"
ENV_TYPE="${local.env_type}"
AWS_PROFILE="${local.aws_profile}"
TABLE_NAME="${local.table_name}"
}
depends_on = [
Expand All @@ -22,10 +22,10 @@ data "template_file" "dynamo_backup" {
data "template_file" "dynamo_restore" {
template = "${file("${path.module}/files/dynamo_restore.tpl")}"
vars = {
SERVICE_NAME="${var.app_name}"
WORKSPACE="${var.env_name}"
ENV_TYPE="${var.env_type}"
AWS_PROFILE="${var.aws_profile}"
SERVICE_NAME="${local.app_name}"
WORKSPACE="${local.env_name}"
ENV_TYPE="${local.env_type}"
AWS_PROFILE="${local.aws_profile}"
INIT_DB_ENVIRONMENT="${var.init_db_environment}"
SOURCE_AWS_PROFILE="${var.init_db_aws_profile}"
SOURCE_ENV_TYPE="${var.init_db_env_type}"
Expand Down
42 changes: 25 additions & 17 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
locals {
env_name = var.env_name

app_name = var.app_name == null ? var.dynamodb_config.app_name : var.app_name
env_name = var.env_name == null ? var.dynamodb_config.env_name : var.env_name
env_type = var.env_type == null ? var.dynamodb_config.env_type : var.env_type
aws_profile = var.aws_profile == null ? var.dynamodb_config.aws_profile : var.aws_profile
write_capacity = var.write_capacity == null ? var.dynamodb_config.write_capacity : var.write_capacity
read_capacity = var.read_capacity == null ? var.dynamodb_config.read_capacity : var.read_capacity
max_write_capacity = var.max_write_capacity == null ? var.dynamodb_config.max_write_capacity : var.max_write_capacity
max_read_capacity = var.max_read_capacity == null ? var.dynamodb_config.max_read_capacity : var.max_read_capacity
autoscaling_enabled = var.autoscaling_enabled == null ? var.dynamodb_config.autoscaling_enabled : var.autoscaling_enabled
target_utilization_percent = var.target_utilization_percent == null ? var.dynamodb_config.target_utilization_percent : var.target_utilization_percent
# at different places, scripts are using app_name instead of table_name,
# and expect user to give same value for app_name and table_name.
# Until code is refactored to use table_name appropriately, we will use app_name
# in all places, and ignore table_name variable, to avoid issues

table_name = try("${var.table_name}","dynamodb-${var.app_name}-${local.env_name}")
table_name = try("${var.table_name}","dynamodb-${local.app_name}-${local.env_name}")
}

resource "aws_dynamodb_table" "basic-dynamodb-table" {
Expand All @@ -15,8 +23,8 @@ resource "aws_dynamodb_table" "basic-dynamodb-table" {
range_key = var.primary_sort_key
billing_mode = var.billing_mode

read_capacity = var.billing_mode == "PROVISIONED" ? var.read_capacity : null
write_capacity = var.billing_mode == "PROVISIONED" ? var.write_capacity : null
read_capacity = var.billing_mode == "PROVISIONED" ? local.read_capacity : null
write_capacity = var.billing_mode == "PROVISIONED" ? local.write_capacity : null

attribute {
name = var.primary_key
Expand Down Expand Up @@ -60,8 +68,8 @@ resource "aws_dynamodb_table" "basic-dynamodb-table" {
name = index.value.name
hash_key = index.value.hash_key
range_key = try(index.value.range_key, null)
read_capacity = var.billing_mode == "PROVISIONED" ? var.read_capacity : null
write_capacity = var.billing_mode == "PROVISIONED" ? var.write_capacity : null
read_capacity = var.billing_mode == "PROVISIONED" ? local.read_capacity : null
write_capacity = var.billing_mode == "PROVISIONED" ? local.write_capacity : null
projection_type = try(index.value.projection_type, "ALL")
non_key_attributes = try(index.value.projection_type, "ALL") == "INCLUDE" ? try(index.value.non_key_attributes, null) : null
}
Expand All @@ -84,16 +92,16 @@ resource "aws_dynamodb_table" "basic-dynamodb-table" {


resource "aws_appautoscaling_target" "dynamodb_table_read_target" {
count = var.autoscaling_enabled ? 1 : 0
max_capacity = var.max_read_capacity
min_capacity = var.read_capacity
count = local.autoscaling_enabled ? 1 : 0
max_capacity = local.max_read_capacity
min_capacity = local.read_capacity
resource_id = "table/${local.table_name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}

resource "aws_appautoscaling_policy" "dynamodb_table_read_policy" {
count = var.autoscaling_enabled ? 1 : 0
count = local.autoscaling_enabled ? 1 : 0
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.dynamodb_table_read_target[count.index].resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.dynamodb_table_read_target[count.index].resource_id
Expand All @@ -105,21 +113,21 @@ resource "aws_appautoscaling_policy" "dynamodb_table_read_policy" {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}

target_value = var.target_utilization_percent
target_value = local.target_utilization_percent
}
}

resource "aws_appautoscaling_target" "dynamodb_table_write_target" {
count = var.autoscaling_enabled ? 1 : 0
max_capacity = var.max_write_capacity
min_capacity = var.write_capacity
count = local.autoscaling_enabled ? 1 : 0
max_capacity = local.max_write_capacity
min_capacity = local.write_capacity
resource_id = "table/${local.table_name}"
scalable_dimension = "dynamodb:table:WriteCapacityUnits"
service_namespace = "dynamodb"
}

resource "aws_appautoscaling_policy" "dynamodb_table_write_policy" {
count = var.autoscaling_enabled ? 1 : 0
count = local.autoscaling_enabled ? 1 : 0
name = "DynamoDBWriteCapacityUtilization:${aws_appautoscaling_target.dynamodb_table_write_target[count.index].resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.dynamodb_table_write_target[count.index].resource_id
Expand All @@ -131,7 +139,7 @@ resource "aws_appautoscaling_policy" "dynamodb_table_write_policy" {
predefined_metric_type = "DynamoDBWriteCapacityUtilization"
}

target_value = var.target_utilization_percent
target_value = local.target_utilization_percent
}
}

Expand Down
9 changes: 8 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
variable "dynamodb_config" {
}

variable "env_name" {
type = string
default = null
}

variable "app_name" {
type = string
default = null
}

variable "table_name" {
Expand All @@ -24,7 +29,7 @@ variable "primary_sort_key" {
}

variable "primary_sort_key_type" {
default = null
default = null
}

variable "read_capacity" {
Expand All @@ -36,10 +41,12 @@ variable "write_capacity" {
}

variable "env_type" {
default = null
type = string
}

variable "aws_profile" {
default = null
type = string
}

Expand Down