Skip to content

Commit 571f2ba

Browse files
committed
feat: Add variable definitions to replace any
1 parent 0d43671 commit 571f2ba

File tree

4 files changed

+78
-43
lines changed

4 files changed

+78
-43
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ No modules.
180180
| <a name="input_aliases"></a> [aliases](#input\_aliases) | A list of aliases to create. Note - due to the use of `toset()`, values must be static strings and not computed values | `list(string)` | `[]` | no |
181181
| <a name="input_aliases_use_name_prefix"></a> [aliases\_use\_name\_prefix](#input\_aliases\_use\_name\_prefix) | Determines whether the alias name is used as a prefix | `bool` | `false` | no |
182182
| <a name="input_bypass_policy_lockout_safety_check"></a> [bypass\_policy\_lockout\_safety\_check](#input\_bypass\_policy\_lockout\_safety\_check) | A flag to indicate whether to bypass the key policy lockout safety check. Setting this value to true increases the risk that the KMS key becomes unmanageable | `bool` | `null` | no |
183-
| <a name="input_computed_aliases"></a> [computed\_aliases](#input\_computed\_aliases) | A map of aliases to create. Values provided via the `name` key of the map can be computed from upstream resources | `any` | `{}` | no |
183+
| <a name="input_computed_aliases"></a> [computed\_aliases](#input\_computed\_aliases) | A map of aliases to create. Values provided via the `name` key of the map can be computed from upstream resources | <pre>map(object({<br/> name = string<br/> }))</pre> | `{}` | no |
184184
| <a name="input_create"></a> [create](#input\_create) | Determines whether resources will be created (affects all resources) | `bool` | `true` | no |
185185
| <a name="input_create_external"></a> [create\_external](#input\_create\_external) | Determines whether an external CMK (externally provided material) will be created or a standard CMK (AWS provided material) | `bool` | `false` | no |
186186
| <a name="input_create_replica"></a> [create\_replica](#input\_create\_replica) | Determines whether a replica standard CMK will be created (AWS provided material) | `bool` | `false` | no |
@@ -192,7 +192,7 @@ No modules.
192192
| <a name="input_enable_default_policy"></a> [enable\_default\_policy](#input\_enable\_default\_policy) | Specifies whether to enable the default key policy. Defaults to `true` | `bool` | `true` | no |
193193
| <a name="input_enable_key_rotation"></a> [enable\_key\_rotation](#input\_enable\_key\_rotation) | Specifies whether key rotation is enabled. Defaults to `true` | `bool` | `true` | no |
194194
| <a name="input_enable_route53_dnssec"></a> [enable\_route53\_dnssec](#input\_enable\_route53\_dnssec) | Determines whether the KMS policy used for Route53 DNSSEC signing is enabled | `bool` | `false` | no |
195-
| <a name="input_grants"></a> [grants](#input\_grants) | A map of grant definitions to create | `any` | `{}` | no |
195+
| <a name="input_grants"></a> [grants](#input\_grants) | A map of grant definitions to create | <pre>map(object({<br/> constraints = optional(object({<br/> encryption_context_equals = optional(map(string))<br/> encryption_context_subset = optional(map(string))<br/> }))<br/> grant_creation_tokens = optional(string)<br/> grantee_principal = string<br/> name = optional(string) # Will fall back to use map key<br/> operations = list(string)<br/> retire_on_delete = optional(bool)<br/> retiring_principal = optional(string)<br/> }))</pre> | `null` | no |
196196
| <a name="input_is_enabled"></a> [is\_enabled](#input\_is\_enabled) | Specifies whether the key is enabled. Defaults to `true` | `bool` | `null` | no |
197197
| <a name="input_key_administrators"></a> [key\_administrators](#input\_key\_administrators) | A list of IAM ARNs for [key administrators](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-default-allow-administrators) | `list(string)` | `[]` | no |
198198
| <a name="input_key_asymmetric_public_encryption_users"></a> [key\_asymmetric\_public\_encryption\_users](#input\_key\_asymmetric\_public\_encryption\_users) | A list of IAM ARNs for [key asymmetric public encryption users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-users-crypto) | `list(string)` | `[]` | no |
@@ -202,7 +202,7 @@ No modules.
202202
| <a name="input_key_owners"></a> [key\_owners](#input\_key\_owners) | A list of IAM ARNs for those who will have full key permissions (`kms:*`) | `list(string)` | `[]` | no |
203203
| <a name="input_key_service_roles_for_autoscaling"></a> [key\_service\_roles\_for\_autoscaling](#input\_key\_service\_roles\_for\_autoscaling) | A list of IAM ARNs for [AWSServiceRoleForAutoScaling roles](https://docs.aws.amazon.com/autoscaling/ec2/userguide/key-policy-requirements-EBS-encryption.html#policy-example-cmk-access) | `list(string)` | `[]` | no |
204204
| <a name="input_key_service_users"></a> [key\_service\_users](#input\_key\_service\_users) | A list of IAM ARNs for [key service users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-service-integration) | `list(string)` | `[]` | no |
205-
| <a name="input_key_statements"></a> [key\_statements](#input\_key\_statements) | A map of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for custom permission usage | `any` | `{}` | no |
205+
| <a name="input_key_statements"></a> [key\_statements](#input\_key\_statements) | A map of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for custom permission usage | <pre>list(object({<br/> sid = optional(string)<br/> actions = optional(list(string))<br/> not_actions = optional(list(string))<br/> effect = optional(string)<br/> resources = optional(list(string))<br/> not_resources = optional(list(string))<br/> principals = optional(list(object({<br/> type = string<br/> identifiers = list(string)<br/> })))<br/> not_principals = optional(list(object({<br/> type = string<br/> identifiers = list(string)<br/> })))<br/> condition = optional(list(object({<br/> test = string<br/> values = list(string)<br/> variable = string<br/> })))<br/> }))</pre> | `null` | no |
206206
| <a name="input_key_symmetric_encryption_users"></a> [key\_symmetric\_encryption\_users](#input\_key\_symmetric\_encryption\_users) | A list of IAM ARNs for [key symmetric encryption users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-users-crypto) | `list(string)` | `[]` | no |
207207
| <a name="input_key_usage"></a> [key\_usage](#input\_key\_usage) | Specifies the intended use of the key. Valid values: `ENCRYPT_DECRYPT` or `SIGN_VERIFY`. Defaults to `ENCRYPT_DECRYPT` | `string` | `null` | no |
208208
| <a name="input_key_users"></a> [key\_users](#input\_key\_users) | A list of IAM ARNs for [key users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-default-allow-users) | `list(string)` | `[]` | no |
@@ -211,9 +211,9 @@ No modules.
211211
| <a name="input_policy"></a> [policy](#input\_policy) | A valid policy JSON document. Although this is a key policy, not an IAM policy, an `aws_iam_policy_document`, in the form that designates a principal, can be used | `string` | `null` | no |
212212
| <a name="input_primary_external_key_arn"></a> [primary\_external\_key\_arn](#input\_primary\_external\_key\_arn) | The primary external key arn of a multi-region replica external key | `string` | `null` | no |
213213
| <a name="input_primary_key_arn"></a> [primary\_key\_arn](#input\_primary\_key\_arn) | The primary key arn of a multi-region replica key | `string` | `null` | no |
214-
| <a name="input_region"></a> [region](#input\_region) | (Optional) The Region where the resources will be managed. Defaults to the region set in the provider configuration. | `string` | `null` | no |
214+
| <a name="input_region"></a> [region](#input\_region) | Region where the resource(s) will be managed. Defaults to the Region set in the provider configuration | `string` | `null` | no |
215215
| <a name="input_rotation_period_in_days"></a> [rotation\_period\_in\_days](#input\_rotation\_period\_in\_days) | Custom period of time between each rotation date. Must be a number between 90 and 2560 (inclusive) | `number` | `null` | no |
216-
| <a name="input_route53_dnssec_sources"></a> [route53\_dnssec\_sources](#input\_route53\_dnssec\_sources) | A list of maps containing `account_ids` and Route53 `hosted_zone_arn` that will be allowed to sign DNSSEC records | `list(any)` | `[]` | no |
216+
| <a name="input_route53_dnssec_sources"></a> [route53\_dnssec\_sources](#input\_route53\_dnssec\_sources) | A list of maps containing `account_ids` and Route53 `hosted_zone_arn` that will be allowed to sign DNSSEC records | <pre>list(object({<br/> account_ids = optional(list(string))<br/> hosted_zone_arn = optional(string)<br/> }))</pre> | `null` | no |
217217
| <a name="input_source_policy_documents"></a> [source\_policy\_documents](#input\_source\_policy\_documents) | List of IAM policy documents that are merged together into the exported document. Statements must have unique `sid`s | `list(string)` | `[]` | no |
218218
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no |
219219
| <a name="input_valid_to"></a> [valid\_to](#input\_valid\_to) | Time at which the imported key material expires. When the key material expires, AWS KMS deletes the key material and the CMK becomes unusable. If not specified, key material does not expire | `string` | `null` | no |

main.tf

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -387,40 +387,40 @@ data "aws_iam_policy_document" "this" {
387387
}
388388

389389
dynamic "condition" {
390-
for_each = var.route53_dnssec_sources
390+
for_each = var.route53_dnssec_sources != null ? var.route53_dnssec_sources : []
391391

392392
content {
393393
test = "StringEquals"
394394
variable = "aws:SourceAccount"
395-
values = try(condition.value.account_ids, [local.account_id])
395+
values = coalescelist(condition.value.account_ids, [local.account_id])
396396
}
397397
}
398398

399399
dynamic "condition" {
400-
for_each = var.route53_dnssec_sources
400+
for_each = var.route53_dnssec_sources != null ? var.route53_dnssec_sources : []
401401

402402
content {
403403
test = "ArnLike"
404404
variable = "aws:SourceArn"
405-
values = [try(condition.value.hosted_zone_arn, "arn:${local.partition}:route53:::hostedzone/*")]
405+
values = [coalesce(condition.value.hosted_zone_arn, "arn:${local.partition}:route53:::hostedzone/*")]
406406
}
407407
}
408408
}
409409
}
410410

411411
dynamic "statement" {
412-
for_each = var.key_statements
412+
for_each = var.key_statements != null ? var.key_statements : []
413413

414414
content {
415-
sid = try(statement.value.sid, null)
416-
actions = try(statement.value.actions, null)
417-
not_actions = try(statement.value.not_actions, null)
418-
effect = try(statement.value.effect, null)
419-
resources = try(statement.value.resources, null)
420-
not_resources = try(statement.value.not_resources, null)
415+
sid = statement.value.sid
416+
actions = statement.value.actions
417+
not_actions = statement.value.not_actions
418+
effect = statement.value.effect
419+
resources = statement.value.resources
420+
not_resources = statement.value.not_resources
421421

422422
dynamic "principals" {
423-
for_each = try(statement.value.principals, [])
423+
for_each = statement.value.principals != null ? statement.value.principals : []
424424

425425
content {
426426
type = principals.value.type
@@ -429,7 +429,7 @@ data "aws_iam_policy_document" "this" {
429429
}
430430

431431
dynamic "not_principals" {
432-
for_each = try(statement.value.not_principals, [])
432+
for_each = statement.value.not_principals != null ? statement.value.not_principals : []
433433

434434
content {
435435
type = not_principals.value.type
@@ -438,7 +438,7 @@ data "aws_iam_policy_document" "this" {
438438
}
439439

440440
dynamic "condition" {
441-
for_each = try(statement.value.conditions, [])
441+
for_each = statement.value.conditions != null ? statement.value.conditions : []
442442

443443
content {
444444
test = condition.value.test
@@ -473,25 +473,24 @@ resource "aws_kms_alias" "this" {
473473
################################################################################
474474

475475
resource "aws_kms_grant" "this" {
476-
for_each = { for k, v in var.grants : k => v if var.create }
476+
for_each = var.create && var.grants != null ? var.grants : {}
477477

478478
region = var.region
479479

480-
name = try(each.value.name, each.key)
481-
key_id = try(aws_kms_key.this[0].key_id, aws_kms_external_key.this[0].id, aws_kms_replica_key.this[0].key_id, aws_kms_replica_external_key.this[0].key_id)
482-
grantee_principal = each.value.grantee_principal
483-
operations = each.value.operations
484-
485480
dynamic "constraints" {
486-
for_each = length(lookup(each.value, "constraints", {})) == 0 ? [] : [each.value.constraints]
481+
for_each = each.value.constraints != null ? each.value.constraints : []
487482

488483
content {
489-
encryption_context_equals = try(constraints.value.encryption_context_equals, null)
490-
encryption_context_subset = try(constraints.value.encryption_context_subset, null)
484+
encryption_context_equals = constraints.value.encryption_context_equals
485+
encryption_context_subset = constraints.value.encryption_context_subset
491486
}
492487
}
493488

494-
retiring_principal = try(each.value.retiring_principal, null)
495-
grant_creation_tokens = try(each.value.grant_creation_tokens, null)
496-
retire_on_delete = try(each.value.retire_on_delete, null)
489+
grant_creation_tokens = each.value.grant_creation_tokens
490+
grantee_principal = each.value.grantee_principal
491+
key_id = try(aws_kms_key.this[0].key_id, aws_kms_external_key.this[0].id, aws_kms_replica_key.this[0].key_id, aws_kms_replica_external_key.this[0].key_id)
492+
name = coalesce(each.value.name, each.key)
493+
operations = each.value.operations
494+
retire_on_delete = each.value.retire_on_delete
495+
retiring_principal = each.value.retiring_principal
497496
}

variables.tf

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ variable "create" {
55
}
66

77
variable "region" {
8-
description = "(Optional) The Region where the resources will be managed. Defaults to the region set in the provider configuration."
8+
description = "Region where the resource(s) will be managed. Defaults to the Region set in the provider configuration"
99
type = string
1010
default = null
1111
}
@@ -160,8 +160,28 @@ variable "key_asymmetric_sign_verify_users" {
160160

161161
variable "key_statements" {
162162
description = "A map of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for custom permission usage"
163-
type = any
164-
default = {}
163+
type = list(object({
164+
sid = optional(string)
165+
actions = optional(list(string))
166+
not_actions = optional(list(string))
167+
effect = optional(string)
168+
resources = optional(list(string))
169+
not_resources = optional(list(string))
170+
principals = optional(list(object({
171+
type = string
172+
identifiers = list(string)
173+
})))
174+
not_principals = optional(list(object({
175+
type = string
176+
identifiers = list(string)
177+
})))
178+
condition = optional(list(object({
179+
test = string
180+
values = list(string)
181+
variable = string
182+
})))
183+
}))
184+
default = null
165185
}
166186

167187
variable "source_policy_documents" {
@@ -184,8 +204,11 @@ variable "enable_route53_dnssec" {
184204

185205
variable "route53_dnssec_sources" {
186206
description = "A list of maps containing `account_ids` and Route53 `hosted_zone_arn` that will be allowed to sign DNSSEC records"
187-
type = list(any)
188-
default = []
207+
type = list(object({
208+
account_ids = optional(list(string))
209+
hosted_zone_arn = optional(string)
210+
}))
211+
default = null
189212
}
190213

191214
variable "rotation_period_in_days" {
@@ -238,8 +261,10 @@ variable "aliases" {
238261

239262
variable "computed_aliases" {
240263
description = "A map of aliases to create. Values provided via the `name` key of the map can be computed from upstream resources"
241-
type = any
242-
default = {}
264+
type = map(object({
265+
name = string
266+
}))
267+
default = {}
243268
}
244269

245270
variable "aliases_use_name_prefix" {
@@ -254,6 +279,17 @@ variable "aliases_use_name_prefix" {
254279

255280
variable "grants" {
256281
description = "A map of grant definitions to create"
257-
type = any
258-
default = {}
282+
type = map(object({
283+
constraints = optional(object({
284+
encryption_context_equals = optional(map(string))
285+
encryption_context_subset = optional(map(string))
286+
}))
287+
grant_creation_tokens = optional(string)
288+
grantee_principal = string
289+
name = optional(string) # Will fall back to use map key
290+
operations = list(string)
291+
retire_on_delete = optional(bool)
292+
retiring_principal = optional(string)
293+
}))
294+
default = null
259295
}

wrappers/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module "wrapper" {
1818
enable_default_policy = try(each.value.enable_default_policy, var.defaults.enable_default_policy, true)
1919
enable_key_rotation = try(each.value.enable_key_rotation, var.defaults.enable_key_rotation, true)
2020
enable_route53_dnssec = try(each.value.enable_route53_dnssec, var.defaults.enable_route53_dnssec, false)
21-
grants = try(each.value.grants, var.defaults.grants, {})
21+
grants = try(each.value.grants, var.defaults.grants, null)
2222
is_enabled = try(each.value.is_enabled, var.defaults.is_enabled, null)
2323
key_administrators = try(each.value.key_administrators, var.defaults.key_administrators, [])
2424
key_asymmetric_public_encryption_users = try(each.value.key_asymmetric_public_encryption_users, var.defaults.key_asymmetric_public_encryption_users, [])
@@ -28,7 +28,7 @@ module "wrapper" {
2828
key_owners = try(each.value.key_owners, var.defaults.key_owners, [])
2929
key_service_roles_for_autoscaling = try(each.value.key_service_roles_for_autoscaling, var.defaults.key_service_roles_for_autoscaling, [])
3030
key_service_users = try(each.value.key_service_users, var.defaults.key_service_users, [])
31-
key_statements = try(each.value.key_statements, var.defaults.key_statements, {})
31+
key_statements = try(each.value.key_statements, var.defaults.key_statements, null)
3232
key_symmetric_encryption_users = try(each.value.key_symmetric_encryption_users, var.defaults.key_symmetric_encryption_users, [])
3333
key_usage = try(each.value.key_usage, var.defaults.key_usage, null)
3434
key_users = try(each.value.key_users, var.defaults.key_users, [])
@@ -39,7 +39,7 @@ module "wrapper" {
3939
primary_key_arn = try(each.value.primary_key_arn, var.defaults.primary_key_arn, null)
4040
region = try(each.value.region, var.defaults.region, null)
4141
rotation_period_in_days = try(each.value.rotation_period_in_days, var.defaults.rotation_period_in_days, null)
42-
route53_dnssec_sources = try(each.value.route53_dnssec_sources, var.defaults.route53_dnssec_sources, [])
42+
route53_dnssec_sources = try(each.value.route53_dnssec_sources, var.defaults.route53_dnssec_sources, null)
4343
source_policy_documents = try(each.value.source_policy_documents, var.defaults.source_policy_documents, [])
4444
tags = try(each.value.tags, var.defaults.tags, {})
4545
valid_to = try(each.value.valid_to, var.defaults.valid_to, null)

0 commit comments

Comments
 (0)