Skip to content

Commit eee22aa

Browse files
drosinantonbabenko
andauthored
feat: Support aws_dynamodb_resource_policy (closes #95) (#96)
Co-authored-by: Anton Babenko <anton@antonbabenko.com>
1 parent e47cf5f commit eee22aa

File tree

6 files changed

+38
-1
lines changed

6 files changed

+38
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ No modules.
9292
| [aws_appautoscaling_target.index_write](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
9393
| [aws_appautoscaling_target.table_read](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
9494
| [aws_appautoscaling_target.table_write](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource |
95+
| [aws_dynamodb_resource_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_resource_policy) | resource |
9596
| [aws_dynamodb_table.autoscaled](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_table) | resource |
9697
| [aws_dynamodb_table.autoscaled_gsi_ignore](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_table) | resource |
9798
| [aws_dynamodb_table.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dynamodb_table) | resource |
@@ -120,6 +121,7 @@ No modules.
120121
| <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 |
121122
| <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 |
122123
| <a name="input_replica_regions"></a> [replica\_regions](#input\_replica\_regions) | Region names for creating replicas for a global DynamoDB table. | `any` | `[]` | no |
124+
| <a name="input_resource_policy"></a> [resource\_policy](#input\_resource\_policy) | The JSON definition of the resource-based policy. | `string` | `null` | no |
123125
| <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 |
124126
| <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 |
125127
| <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 |

examples/basic/main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ module "dynamodb_table" {
5050
max_write_request_units = 1
5151
}
5252

53+
resource_policy = <<POLICY
54+
{
55+
"Version": "2012-10-17",
56+
"Statement": [
57+
{
58+
"Sid": "AllowDummyRoleAccess",
59+
"Effect": "Allow",
60+
"Principal": {
61+
"AWS": "arn:aws:iam::222222222222:role/DummyRole"
62+
},
63+
"Action": "dynamodb:GetItem",
64+
"Resource": "__DYNAMODB_TABLE_ARN__"
65+
}
66+
]
67+
}
68+
POLICY
69+
5370
tags = {
5471
Terraform = "true"
5572
Environment = "staging"

main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
locals {
2+
dynamodb_table_arn = try(aws_dynamodb_table.this[0].arn, aws_dynamodb_table.autoscaled[0].arn, aws_dynamodb_table.autoscaled_gsi_ignore[0].arn, "")
3+
}
4+
15
resource "aws_dynamodb_table" "this" {
26
count = var.create_table && !var.autoscaling_enabled ? 1 : 0
37

@@ -376,3 +380,10 @@ resource "aws_dynamodb_table" "autoscaled_gsi_ignore" {
376380
ignore_changes = [global_secondary_index, read_capacity, write_capacity]
377381
}
378382
}
383+
384+
resource "aws_dynamodb_resource_policy" "this" {
385+
count = var.create_table && var.resource_policy != null ? 1 : 0
386+
387+
resource_arn = local.dynamodb_table_arn
388+
policy = replace(var.resource_policy, "__DYNAMODB_TABLE_ARN__", local.dynamodb_table_arn)
389+
}

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
output "dynamodb_table_arn" {
22
description = "ARN of the DynamoDB table"
3-
value = try(aws_dynamodb_table.this[0].arn, aws_dynamodb_table.autoscaled[0].arn, aws_dynamodb_table.autoscaled_gsi_ignore[0].arn, "")
3+
value = local.dynamodb_table_arn
44
}
55

66
output "dynamodb_table_id" {

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,9 @@ variable "restore_to_latest_time" {
209209
type = bool
210210
default = null
211211
}
212+
213+
variable "resource_policy" {
214+
description = "The JSON definition of the resource-based policy."
215+
type = string
216+
default = null
217+
}

wrappers/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module "wrapper" {
2727
range_key = try(each.value.range_key, var.defaults.range_key, null)
2828
read_capacity = try(each.value.read_capacity, var.defaults.read_capacity, null)
2929
replica_regions = try(each.value.replica_regions, var.defaults.replica_regions, [])
30+
resource_policy = try(each.value.resource_policy, var.defaults.resource_policy, null)
3031
restore_date_time = try(each.value.restore_date_time, var.defaults.restore_date_time, null)
3132
restore_source_name = try(each.value.restore_source_name, var.defaults.restore_source_name, null)
3233
restore_source_table_arn = try(each.value.restore_source_table_arn, var.defaults.restore_source_table_arn, null)

0 commit comments

Comments
 (0)