diff --git a/README.md b/README.md index 551e713..ec846c0 100644 --- a/README.md +++ b/README.md @@ -161,3 +161,69 @@ No inputs. | Name | Value | |------|-------| | s3_dump_file | Details about the dump file created | + + +## Requirements + +No requirements. + +## Providers + +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | n/a | +| [null](#provider\_null) | n/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 | +|------|-------------|------|---------|:--------:| +| [app\_name](#input\_app\_name) | n/a | `string` | `null` | no | +| [autoscaling\_enabled](#input\_autoscaling\_enabled) | Use autoscaling for read/write capacity | `bool` | `false` | no | +| [aws\_profile](#input\_aws\_profile) | n/a | `string` | `null` | no | +| [backup\_on\_destroy](#input\_backup\_on\_destroy) | Backup DB to S3 | `bool` | `true` | no | +| [billing\_mode](#input\_billing\_mode) | Indicates mode of billing. Value should be either PROVISIONED or PAY\_PER\_REQUEST | `string` | `"PROVISIONED"` | no | +| [dynamodb\_config](#input\_dynamodb\_config) | n/a | `any` | n/a | yes | +| [env\_name](#input\_env\_name) | n/a | `string` | `null` | no | +| [env\_type](#input\_env\_type) | n/a | `string` | `null` | no | +| [global\_secondary\_indeces](#input\_global\_secondary\_indeces) | Array of GSI definitions | `any` | `[]` | no | +| [init\_db\_aws\_profile](#input\_init\_db\_aws\_profile) | Source envirnment aws profile to restore db from | `string` | `"NULL"` | no | +| [init\_db\_env\_type](#input\_init\_db\_env\_type) | Source envirnment aws profile to restore db from | `string` | `"NULL"` | no | +| [init\_db\_environment](#input\_init\_db\_environment) | Source envirnment name to restore db from | `string` | `"NULL"` | no | +| [max\_read\_capacity](#input\_max\_read\_capacity) | Maximum read autoscaling capacity | `number` | `20` | no | +| [max\_write\_capacity](#input\_max\_write\_capacity) | Maximum write autoscaling capacity | `number` | `20` | no | +| [primary\_key](#input\_primary\_key) | n/a | `string` | n/a | yes | +| [primary\_key\_type](#input\_primary\_key\_type) | n/a | `string` | n/a | yes | +| [primary\_sort\_key](#input\_primary\_sort\_key) | n/a | `string` | `null` | no | +| [primary\_sort\_key\_type](#input\_primary\_sort\_key\_type) | n/a | `any` | `null` | no | +| [read\_capacity](#input\_read\_capacity) | n/a | `number` | `5` | no | +| [restore\_on\_create](#input\_restore\_on\_create) | Restore DB from dump file | `bool` | `true` | no | +| [table\_name](#input\_table\_name) | n/a | `string` | n/a | yes | +| [target\_utilization\_percent](#input\_target\_utilization\_percent) | Target utilization for read/write autoscaling capacity | `number` | `70` | no | +| [ttl\_attribute\_name](#input\_ttl\_attribute\_name) | n/a | `string` | `null` | no | +| [write\_capacity](#input\_write\_capacity) | n/a | `number` | `5` | no | + +## Outputs + +No outputs. + \ No newline at end of file diff --git a/data.tf b/data.tf index 36e82d5..aa21707 100644 --- a/data.tf +++ b/data.tf @@ -1,6 +1,6 @@ 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" } @@ -8,10 +8,10 @@ data "aws_s3_bucket_objects" "get_dump_list" { 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 = [ @@ -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}" diff --git a/main.tf b/main.tf index 1498c8d..b33522b 100644 --- a/main.tf +++ b/main.tf @@ -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" { @@ -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 @@ -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 } @@ -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 @@ -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 @@ -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 } } diff --git a/variables.tf b/variables.tf index a462977..b5afd13 100644 --- a/variables.tf +++ b/variables.tf @@ -1,9 +1,14 @@ +variable "dynamodb_config" { +} + variable "env_name" { type = string + default = null } variable "app_name" { type = string + default = null } variable "table_name" { @@ -24,7 +29,7 @@ variable "primary_sort_key" { } variable "primary_sort_key_type" { - default = null + default = null } variable "read_capacity" { @@ -36,10 +41,12 @@ variable "write_capacity" { } variable "env_type" { + default = null type = string } variable "aws_profile" { + default = null type = string }