Skip to content

feat: Support aws_dynamodb_resource_policy (closes #95) #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ No modules.
| [aws_appautoscaling_target.index_write](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
| [aws_appautoscaling_target.table_read](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
| [aws_appautoscaling_target.table_write](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
| [aws_dynamodb_resource_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_resource_policy) | resource |
| [aws_dynamodb_table.autoscaled](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_table) | resource |
| [aws_dynamodb_table.autoscaled_gsi_ignore](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_table) | resource |
| [aws_dynamodb_table.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_table) | resource |
Expand Down Expand Up @@ -120,6 +121,7 @@ No modules.
| <a name="input_range_key"></a> [range\_key](#input\_range\_key) | The attribute to use as the range (sort) key. Must also be defined as an attribute | `string` | `null` | no |
| <a name="input_read_capacity"></a> [read\_capacity](#input\_read\_capacity) | The number of read units for this table. If the billing\_mode is PROVISIONED, this field should be greater than 0 | `number` | `null` | no |
| <a name="input_replica_regions"></a> [replica\_regions](#input\_replica\_regions) | Region names for creating replicas for a global DynamoDB table. | `any` | `[]` | no |
| <a name="input_resource_policy"></a> [resource\_policy](#input\_resource\_policy) | The JSON definition of the resource-based policy. | `string` | `null` | no |
| <a name="input_restore_date_time"></a> [restore\_date\_time](#input\_restore\_date\_time) | Time of the point-in-time recovery point to restore. | `string` | `null` | no |
| <a name="input_restore_source_name"></a> [restore\_source\_name](#input\_restore\_source\_name) | Name of the table to restore. Must match the name of an existing table. | `string` | `null` | no |
| <a name="input_restore_source_table_arn"></a> [restore\_source\_table\_arn](#input\_restore\_source\_table\_arn) | ARN of the source table to restore. Must be supplied for cross-region restores. | `string` | `null` | no |
Expand Down
17 changes: 17 additions & 0 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ module "dynamodb_table" {
max_write_request_units = 1
}

resource_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowDummyRoleAccess",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::222222222222:role/DummyRole"
},
"Action": "dynamodb:GetItem",
"Resource": "__DYNAMODB_TABLE_ARN__"
}
]
}
POLICY

tags = {
Terraform = "true"
Environment = "staging"
Expand Down
11 changes: 11 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
locals {
dynamodb_table_arn = try(aws_dynamodb_table.this[0].arn, aws_dynamodb_table.autoscaled[0].arn, aws_dynamodb_table.autoscaled_gsi_ignore[0].arn, "")
}

resource "aws_dynamodb_table" "this" {
count = var.create_table && !var.autoscaling_enabled ? 1 : 0

Expand Down Expand Up @@ -376,3 +380,10 @@ resource "aws_dynamodb_table" "autoscaled_gsi_ignore" {
ignore_changes = [global_secondary_index, read_capacity, write_capacity]
}
}

resource "aws_dynamodb_resource_policy" "this" {
count = var.create_table && var.resource_policy != null ? 1 : 0

resource_arn = local.dynamodb_table_arn
policy = replace(var.resource_policy, "__DYNAMODB_TABLE_ARN__", local.dynamodb_table_arn)
}
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
output "dynamodb_table_arn" {
description = "ARN of the DynamoDB table"
value = try(aws_dynamodb_table.this[0].arn, aws_dynamodb_table.autoscaled[0].arn, aws_dynamodb_table.autoscaled_gsi_ignore[0].arn, "")
value = local.dynamodb_table_arn
}

output "dynamodb_table_id" {
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,9 @@ variable "restore_to_latest_time" {
type = bool
default = null
}

variable "resource_policy" {
description = "The JSON definition of the resource-based policy."
type = string
default = null
}
1 change: 1 addition & 0 deletions wrappers/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module "wrapper" {
range_key = try(each.value.range_key, var.defaults.range_key, null)
read_capacity = try(each.value.read_capacity, var.defaults.read_capacity, null)
replica_regions = try(each.value.replica_regions, var.defaults.replica_regions, [])
resource_policy = try(each.value.resource_policy, var.defaults.resource_policy, null)
restore_date_time = try(each.value.restore_date_time, var.defaults.restore_date_time, null)
restore_source_name = try(each.value.restore_source_name, var.defaults.restore_source_name, null)
restore_source_table_arn = try(each.value.restore_source_table_arn, var.defaults.restore_source_table_arn, null)
Expand Down