From 5427830feae592eb85af26c02fa567b573850f5f Mon Sep 17 00:00:00 2001 From: Robert Fairburn <8029478+rfairburn@users.noreply.github.com> Date: Tue, 24 Mar 2026 06:44:00 -0500 Subject: [PATCH] Add KMS CMK for carves (#198) --- addons/logging-alb/README.md | 3 + addons/logging-alb/main.tf | 95 +++--- addons/osquery-carve/.header.md | 13 + addons/osquery-carve/README.md | 27 +- addons/osquery-carve/main.tf | 174 +++++++++- addons/osquery-carve/outputs.tf | 1 + addons/osquery-carve/variables.tf | 66 ++++ byo-vpc/byo-db/byo-ecs/.header.md | 2 +- byo-vpc/byo-db/byo-ecs/.terraform-docs.yml | 1 + byo-vpc/byo-db/byo-ecs/README.md | 10 +- byo-vpc/byo-db/byo-ecs/iam.tf | 27 +- byo-vpc/byo-db/byo-ecs/main.tf | 213 ++++++++++++- byo-vpc/byo-db/byo-ecs/variables.tf | 58 ++++ byo-vpc/byo-db/main.tf | 120 ++++++- byo-vpc/byo-db/variables.tf | 42 +++ byo-vpc/main.tf | 349 +++++++++++++++++++-- main.tf | 61 +++- variables.tf | 23 ++ 18 files changed, 1147 insertions(+), 138 deletions(-) create mode 100644 byo-vpc/byo-db/byo-ecs/.terraform-docs.yml diff --git a/addons/logging-alb/README.md b/addons/logging-alb/README.md index b01284b..07b3f60 100644 --- a/addons/logging-alb/README.md +++ b/addons/logging-alb/README.md @@ -129,6 +129,7 @@ No requirements. | [aws_iam_policy_document.lambda_sweep_assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | | [aws_iam_policy_document.s3_athena_bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | | [aws_iam_policy_document.s3_log_bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source | | [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source | ## Inputs @@ -140,6 +141,8 @@ No requirements. | [extra\_kms\_policies](#input\_extra\_kms\_policies) | n/a | `list(any)` | `[]` | no | | [extra\_s3\_athena\_policies](#input\_extra\_s3\_athena\_policies) | n/a | `list(any)` | `[]` | no | | [extra\_s3\_log\_policies](#input\_extra\_s3\_log\_policies) | n/a | `list(any)` | `[]` | no | +| [kms\_base\_policy](#input\_kms\_base\_policy) | Optional base KMS key-policy statements to apply to module-created CMKs before module-required service access statements are merged in. If null, the module defaults to the historical root `kms:*` statement. |
list(object({
sid = string
effect = string
principals = object({
type = string
identifiers = list(string)
})
actions = list(string)
resources = list(string)
conditions = optional(list(object({
test = string
variable = string
values = list(string)
})), [])
})) | `null` | no |
+| [lambda\_log\_retention\_in\_days](#input\_lambda\_log\_retention\_in\_days) | CloudWatch log retention in days for the re-encrypt and sweep Lambda functions | `number` | `365` | no |
| [prefix](#input\_prefix) | n/a | `string` | `"fleet"` | no |
| [s3\_expiration\_days](#input\_s3\_expiration\_days) | n/a | `number` | `90` | no |
| [s3\_newer\_noncurrent\_versions](#input\_s3\_newer\_noncurrent\_versions) | n/a | `number` | `5` | no |
diff --git a/addons/logging-alb/main.tf b/addons/logging-alb/main.tf
index dda2f57..99a39ec 100644
--- a/addons/logging-alb/main.tf
+++ b/addons/logging-alb/main.tf
@@ -57,15 +57,65 @@ locals {
}
]
- kms_policies = concat(local.kms_base_policy_statements, local.kms_service_statements, var.extra_kms_policies)
-
s3_path_prefix = coalesce(var.alt_path_prefix, var.prefix)
}
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "kms" {
dynamic "statement" {
- for_each = local.kms_policies
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = try(statement.value.sid, "")
+ actions = try(statement.value.actions, [])
+ resources = try(statement.value.resources, [])
+ effect = try(statement.value.effect, null)
+ dynamic "principals" {
+ for_each = try([statement.value.principals], [])
+ content {
+ type = principals.value.type
+ identifiers = principals.value.identifiers
+ }
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = local.kms_service_statements
+ content {
+ sid = try(statement.value.sid, "")
+ actions = try(statement.value.actions, [])
+ resources = try(statement.value.resources, [])
+ effect = try(statement.value.effect, null)
+ dynamic "principals" {
+ for_each = try([statement.value.principals], [])
+ content {
+ type = principals.value.type
+ identifiers = principals.value.identifiers
+ }
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.extra_kms_policies
content {
sid = try(statement.value.sid, "")
actions = try(statement.value.actions, [])
@@ -239,19 +289,6 @@ data "aws_iam_policy_document" "lambda_reencrypt" {
]
}
- statement {
- sid = "UseKmsKey"
- actions = [
- "kms:Encrypt",
- "kms:Decrypt",
- "kms:GenerateDataKey*",
- "kms:DescribeKey",
- ]
- resources = [
- aws_kms_key.logs.arn,
- ]
- }
-
statement {
sid = "WriteLogs"
actions = [
@@ -362,19 +399,6 @@ data "aws_iam_policy_document" "lambda_sweep" {
]
}
- statement {
- sid = "UseKmsKey"
- actions = [
- "kms:Encrypt",
- "kms:Decrypt",
- "kms:GenerateDataKey*",
- "kms:DescribeKey",
- ]
- resources = [
- aws_kms_key.logs.arn,
- ]
- }
-
statement {
sid = "CreateBatchJob"
actions = [
@@ -498,19 +522,6 @@ data "aws_iam_policy_document" "batch_reencrypt" {
"${module.s3_bucket_for_logs.s3_bucket_arn}/*",
]
}
-
- statement {
- sid = "UseKmsKey"
- actions = [
- "kms:Encrypt",
- "kms:GenerateDataKey",
- "kms:GenerateDataKeyWithoutPlaintext",
- "kms:DescribeKey",
- ]
- resources = [
- aws_kms_key.logs.arn,
- ]
- }
}
resource "aws_iam_role" "batch_reencrypt" {
diff --git a/addons/osquery-carve/.header.md b/addons/osquery-carve/.header.md
index f7cc3d6..3981cd7 100644
--- a/addons/osquery-carve/.header.md
+++ b/addons/osquery-carve/.header.md
@@ -1,2 +1,15 @@
# Osquery Carve Bucket Addon
This addon provides a S3 bucket for Osquery Carve results.
+
+## KMS considerations
+
+If `osquery_carve_s3_bucket.kms.create_kms_key = true`, this module now creates the KMS key first and manages the custom key policy through a separate attachment resource. If `fleet_role_name` is omitted, the module still creates the key but leaves the default KMS key policy in place and does not attach a custom policy.
+
+When a module-created customer-managed key must trust the Fleet IAM role, Terraform needs to resolve that role through `data.aws_iam_role` in order to build the KMS key policy. Because that data source cannot read a role that is being created in the same apply, this is a technical limitation that requires a two-stage apply:
+
+1. Apply once without `osquery_carve_s3_bucket.kms.fleet_role_name` so the Fleet IAM role and the KMS key are created.
+2. Set `osquery_carve_s3_bucket.kms.fleet_role_name` to the existing Fleet role name and apply again so the module can resolve the role ARN and attach the KMS key policy.
+
+The IAM policy exported by this addon is intentionally S3-only. KMS authorization is handled in the KMS key policy for module-created keys. If `osquery_carve_s3_bucket.kms.kms_key_arn` is set, this module does not manage the referenced key policy and does not export a generic KMS IAM policy for it.
+
+When bringing your own KMS key, it is your responsibility to ensure that key policy and any separately managed IAM permissions allow the Fleet role to perform the required KMS actions for S3 object encryption and decryption.
diff --git a/addons/osquery-carve/README.md b/addons/osquery-carve/README.md
index 304a385..0300f7c 100644
--- a/addons/osquery-carve/README.md
+++ b/addons/osquery-carve/README.md
@@ -1,6 +1,19 @@
# Osquery Carve Bucket Addon
This addon provides a S3 bucket for Osquery Carve results.
+## KMS considerations
+
+If `osquery_carve_s3_bucket.kms.create_kms_key = true`, this module now creates the KMS key first and manages the custom key policy through a separate attachment resource. If `fleet_role_name` is omitted, the module still creates the key but leaves the default KMS key policy in place and does not attach a custom policy.
+
+When a module-created customer-managed key must trust the Fleet IAM role, Terraform needs to resolve that role through `data.aws_iam_role` in order to build the KMS key policy. Because that data source cannot read a role that is being created in the same apply, this is a technical limitation that requires a two-stage apply:
+
+1. Apply once without `osquery_carve_s3_bucket.kms.fleet_role_name` so the Fleet IAM role and the KMS key are created.
+2. Set `osquery_carve_s3_bucket.kms.fleet_role_name` to the existing Fleet role name and apply again so the module can resolve the role ARN and attach the KMS key policy.
+
+The IAM policy exported by this addon is intentionally S3-only. KMS authorization is handled in the KMS key policy for module-created keys. If `osquery_carve_s3_bucket.kms.kms_key_arn` is set, this module does not manage the referenced key policy and does not export a generic KMS IAM policy for it.
+
+When bringing your own KMS key, it is your responsibility to ensure that key policy and any separately managed IAM permissions allow the Fleet role to perform the required KMS actions for S3 object encryption and decryption.
+
## Requirements
No requirements.
@@ -9,7 +22,7 @@ No requirements.
| Name | Version |
|------|---------|
-| [aws](#provider\_aws) | 5.39.1 |
+| [aws](#provider\_aws) | 6.37.0 |
## Modules
@@ -20,21 +33,29 @@ No modules.
| Name | Type |
|------|------|
| [aws_iam_policy.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
+| [aws_kms_alias.osquery_carve](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
+| [aws_kms_key.osquery_carve](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
+| [aws_kms_key_policy.osquery_carve](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key_policy) | resource |
| [aws_s3_bucket.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
| [aws_s3_bucket_lifecycle_configuration.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration) | resource |
| [aws_s3_bucket_public_access_block.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
| [aws_s3_bucket_server_side_encryption_configuration.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
+| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
+| [aws_iam_policy_document.osquery_carve_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
+| [aws_iam_role.osquery_carve_fleet](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_role) | data source |
+| [aws_kms_key.osquery_carve_provided](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/kms_key) | data source |
+| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
-| [osquery\_carve\_s3\_bucket](#input\_osquery\_carve\_s3\_bucket) | n/a | object({
name = optional(string, "fleet-osquery-results-archive")
expires_days = optional(number, 1)
}) | {
"expires_days": 1,
"name": "fleet-osquery-results-archive"
} | no |
+| [osquery\_carve\_s3\_bucket](#input\_osquery\_carve\_s3\_bucket) | Configuration for the osquery carve S3 bucket, including optional customer-managed KMS settings. | object({
name = optional(string, "fleet-osquery-results-archive")
expires_days = optional(number, 1)
kms = optional(object({
kms_key_arn = optional(string, null)
create_kms_key = optional(bool, false)
kms_alias = optional(string, "osquery-carve")
kms_base_policy = optional(list(object({
sid = string
effect = string
principals = object({
type = string
identifiers = list(string)
})
actions = list(string)
resources = list(string)
conditions = optional(list(object({
test = string
variable = string
values = list(string)
})), [])
})), null)
extra_kms_policies = optional(list(any), [])
fleet_role_name = optional(string, null)
}), {
kms_key_arn = null
create_kms_key = false
kms_alias = "osquery-carve"
kms_base_policy = null
extra_kms_policies = []
fleet_role_name = null
})
}) | {
"expires_days": 1,
"kms": {
"create_kms_key": false,
"extra_kms_policies": [],
"fleet_role_name": null,
"kms_alias": "osquery-carve",
"kms_base_policy": null,
"kms_key_arn": null
},
"name": "fleet-osquery-results-archive"
} | no |
## Outputs
| Name | Description |
|------|-------------|
| [fleet\_extra\_environment\_variables](#output\_fleet\_extra\_environment\_variables) | n/a |
-| [fleet\_extra\_iam\_policies](#output\_fleet\_extra\_iam\_policies) | n/a |
+| [fleet\_extra\_iam\_policies](#output\_fleet\_extra\_iam\_policies) | IAM policies required for Fleet to access the osquery carve S3 bucket. |
diff --git a/addons/osquery-carve/main.tf b/addons/osquery-carve/main.tf
index e1b0237..219c691 100644
--- a/addons/osquery-carve/main.tf
+++ b/addons/osquery-carve/main.tf
@@ -1,11 +1,86 @@
-// Customer keys are not supported in our Fleet Terraforms at the moment. We will evaluate the
-// possibility of providing this capability in the future.
// No versioning on this bucket is by design.
// Bucket logging is not supported in our Fleet Terraforms at the moment. It can be enabled by the
// organizations deploying Fleet, and we will evaluate the possibility of providing this capability
// in the future.
-resource "aws_s3_bucket" "main" { #tfsec:ignore:aws-s3-encryption-customer-key:exp:2022-07-01 #tfsec:ignore:aws-s3-enable-versioning #tfsec:ignore:aws-s3-enable-bucket-logging:exp:2022-06-15
+locals {
+ osquery_carve_provided_kms_key_ref = var.osquery_carve_s3_bucket.kms.kms_key_arn
+ osquery_carve_create_kms_key = local.osquery_carve_provided_kms_key_ref == null && var.osquery_carve_s3_bucket.kms.create_kms_key == true
+ osquery_carve_fleet_role_name = (
+ var.osquery_carve_s3_bucket.kms.fleet_role_name != null &&
+ trimspace(var.osquery_carve_s3_bucket.kms.fleet_role_name) != ""
+ ) ? trimspace(var.osquery_carve_s3_bucket.kms.fleet_role_name) : null
+ osquery_carve_kms_key_id = local.osquery_carve_provided_kms_key_ref != null ? data.aws_kms_key.osquery_carve_provided[0].key_id : (
+ local.osquery_carve_create_kms_key == true ? aws_kms_key.osquery_carve[0].id : null
+ )
+ osquery_carve_kms_key_arn = local.osquery_carve_provided_kms_key_ref != null ? data.aws_kms_key.osquery_carve_provided[0].arn : (
+ local.osquery_carve_create_kms_key == true ? aws_kms_key.osquery_carve[0].arn : null
+ )
+ osquery_carve_create_kms_key_policy = local.osquery_carve_create_kms_key && (
+ local.osquery_carve_fleet_role_name != null ||
+ var.osquery_carve_s3_bucket.kms.kms_base_policy != null ||
+ length(var.osquery_carve_s3_bucket.kms.extra_kms_policies) > 0
+ )
+
+ kms_base_policy_statements = var.osquery_carve_s3_bucket.kms.kms_base_policy != null ? var.osquery_carve_s3_bucket.kms.kms_base_policy : [
+ {
+ sid = "EnableRootPermissions"
+ effect = "Allow"
+ actions = ["kms:*"]
+ resources = ["*"]
+ principals = {
+ type = "AWS"
+ identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"]
+ }
+ conditions = []
+ }
+ ]
+
+ osquery_carve_kms_task_role_statements = local.osquery_carve_fleet_role_name != null ? [
+ {
+ sid = "AllowFleetRoleUseOfTheKey"
+ effect = "Allow"
+ actions = [
+ "kms:ReEncrypt*",
+ "kms:GenerateDataKey*",
+ "kms:Encrypt*",
+ "kms:Describe*",
+ "kms:Decrypt*"
+ ]
+ resources = ["*"]
+ principals = {
+ type = "AWS"
+ identifiers = [data.aws_iam_role.osquery_carve_fleet[0].arn]
+ }
+ conditions = []
+ }
+ ] : []
+}
+
+data "aws_caller_identity" "current" {}
+
+data "aws_partition" "current" {}
+
+check "kms_base_policy_requires_module_managed_cmk" {
+ assert {
+ condition = var.osquery_carve_s3_bucket.kms.kms_base_policy == null || (
+ local.osquery_carve_create_kms_key == true
+ )
+ error_message = "osquery_carve_s3_bucket.kms.kms_base_policy is not used by osquery-carve unless this module is creating the osquery carve CMK. When kms_key_arn is provided, external key policies remain caller-managed."
+ }
+}
+
+data "aws_kms_key" "osquery_carve_provided" {
+ count = local.osquery_carve_provided_kms_key_ref != null ? 1 : 0
+ key_id = local.osquery_carve_provided_kms_key_ref
+}
+
+data "aws_iam_role" "osquery_carve_fleet" {
+ count = local.osquery_carve_create_kms_key && local.osquery_carve_fleet_role_name != null ? 1 : 0
+ name = local.osquery_carve_fleet_role_name
+}
+
+resource "aws_s3_bucket" "main" { #tfsec:ignore:aws-s3-encryption-customer-key:exp:2028-07-01 #tfsec:ignore:aws-s3-enable-versioning #tfsec:ignore:aws-s3-enable-bucket-logging:exp:2028-07-01
bucket = var.osquery_carve_s3_bucket.name
}
@@ -25,11 +100,102 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "main" {
bucket = aws_s3_bucket.main.bucket
rule {
apply_server_side_encryption_by_default {
- sse_algorithm = "aws:kms"
+ kms_master_key_id = local.osquery_carve_kms_key_id
+ sse_algorithm = "aws:kms"
}
}
}
+resource "aws_kms_key" "osquery_carve" {
+ count = local.osquery_carve_create_kms_key ? 1 : 0
+ description = "CMK for Fleet osquery carve S3 bucket object encryption."
+ enable_key_rotation = true
+}
+
+resource "aws_kms_alias" "osquery_carve" {
+ count = local.osquery_carve_create_kms_key ? 1 : 0
+ target_key_id = aws_kms_key.osquery_carve[0].id
+ name = "alias/${var.osquery_carve_s3_bucket.kms.kms_alias}"
+}
+
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
+data "aws_iam_policy_document" "osquery_carve_kms" {
+ count = local.osquery_carve_create_kms_key_policy ? 1 : 0
+
+ dynamic "statement" {
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = try(statement.value.sid, "")
+ effect = try(statement.value.effect, null)
+ actions = try(statement.value.actions, [])
+ resources = try(statement.value.resources, [])
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.osquery_carve_s3_bucket.kms.extra_kms_policies
+ content {
+ sid = try(statement.value.sid, "")
+ effect = try(statement.value.effect, null)
+ actions = try(statement.value.actions, [])
+ resources = try(statement.value.resources, [])
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = local.osquery_carve_kms_task_role_statements
+ content {
+ sid = try(statement.value.sid, "")
+ effect = try(statement.value.effect, null)
+ actions = try(statement.value.actions, [])
+ resources = try(statement.value.resources, [])
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+}
+
+resource "aws_kms_key_policy" "osquery_carve" {
+ count = local.osquery_carve_create_kms_key_policy ? 1 : 0
+ key_id = aws_kms_key.osquery_carve[0].id
+ policy = data.aws_iam_policy_document.osquery_carve_kms[0].json
+}
+
resource "aws_s3_bucket_public_access_block" "main" {
bucket = aws_s3_bucket.main.id
block_public_acls = true
diff --git a/addons/osquery-carve/outputs.tf b/addons/osquery-carve/outputs.tf
index c62c013..7fd4cf4 100644
--- a/addons/osquery-carve/outputs.tf
+++ b/addons/osquery-carve/outputs.tf
@@ -6,6 +6,7 @@ output "fleet_extra_environment_variables" {
}
output "fleet_extra_iam_policies" {
+ description = "IAM policies required for Fleet to access the osquery carve S3 bucket."
value = [
aws_iam_policy.main.arn
]
diff --git a/addons/osquery-carve/variables.tf b/addons/osquery-carve/variables.tf
index 9e437c2..5e17bf6 100644
--- a/addons/osquery-carve/variables.tf
+++ b/addons/osquery-carve/variables.tf
@@ -2,9 +2,75 @@ variable "osquery_carve_s3_bucket" {
type = object({
name = optional(string, "fleet-osquery-results-archive")
expires_days = optional(number, 1)
+ kms = optional(object({
+ kms_key_arn = optional(string, null)
+ create_kms_key = optional(bool, false)
+ kms_alias = optional(string, "osquery-carve")
+ kms_base_policy = optional(list(object({
+ sid = string
+ effect = string
+ principals = object({
+ type = string
+ identifiers = list(string)
+ })
+ actions = list(string)
+ resources = list(string)
+ conditions = optional(list(object({
+ test = string
+ variable = string
+ values = list(string)
+ })), [])
+ })), null)
+ extra_kms_policies = optional(list(any), [])
+ fleet_role_name = optional(string, null)
+ }), {
+ kms_key_arn = null
+ create_kms_key = false
+ kms_alias = "osquery-carve"
+ kms_base_policy = null
+ extra_kms_policies = []
+ fleet_role_name = null
+ })
})
+ description = "Configuration for the osquery carve S3 bucket, including optional customer-managed KMS settings."
default = {
name = "fleet-osquery-results-archive"
expires_days = 1
+ kms = {
+ kms_key_arn = null
+ create_kms_key = false
+ kms_alias = "osquery-carve"
+ kms_base_policy = null
+ extra_kms_policies = []
+ fleet_role_name = null
+ }
+ }
+
+ validation {
+ condition = !(var.osquery_carve_s3_bucket.kms.kms_key_arn != null && var.osquery_carve_s3_bucket.kms.create_kms_key == true)
+ error_message = "osquery_carve_s3_bucket.kms.kms_key_arn and osquery_carve_s3_bucket.kms.create_kms_key are mutually exclusive; set one or the other, not both."
+ }
+
+ validation {
+ condition = (
+ length(var.osquery_carve_s3_bucket.kms.extra_kms_policies) == 0 ||
+ (
+ var.osquery_carve_s3_bucket.kms.create_kms_key == true &&
+ var.osquery_carve_s3_bucket.kms.kms_key_arn == null
+ )
+ )
+ error_message = "osquery_carve_s3_bucket.kms.extra_kms_policies can be set only when osquery-carve is creating the osquery carve CMK."
+ }
+
+ validation {
+ condition = (
+ var.osquery_carve_s3_bucket.kms.fleet_role_name == null ||
+ trimspace(var.osquery_carve_s3_bucket.kms.fleet_role_name) == "" ||
+ (
+ var.osquery_carve_s3_bucket.kms.create_kms_key == true &&
+ var.osquery_carve_s3_bucket.kms.kms_key_arn == null
+ )
+ )
+ error_message = "osquery_carve_s3_bucket.kms.fleet_role_name can be set only when osquery-carve is creating the osquery carve CMK."
}
}
diff --git a/byo-vpc/byo-db/byo-ecs/.header.md b/byo-vpc/byo-db/byo-ecs/.header.md
index 7b36ada..032cc52 100644
--- a/byo-vpc/byo-db/byo-ecs/.header.md
+++ b/byo-vpc/byo-db/byo-ecs/.header.md
@@ -22,7 +22,7 @@ For each feature:
- If a key ARN is provided, this module uses that key and does not create a CMK.
- For software installers, setting `software_installers.kms_key_arn` is sufficient to use that key even if `create_kms_key = false`.
-IAM permissions for using these keys are managed in-module where possible. If a task role ARN is provided and belongs to the same AWS account, KMS/S3 policy attachments and `extra_iam_policies` attachments are applied to that role.
+IAM permissions for using these keys are managed in-module where possible. For module-created keys, the module prefers granting usage in the KMS key policy itself. If a caller-provided external key still needs IAM authorization and the task role ARN belongs to the same AWS account, the module attaches a separate resource-scoped KMS IAM policy to that role instead of mixing KMS access into the broader S3 or secret-access policy. `extra_iam_policies` attachments continue to be applied to the same-account task role when provided.
## Example: Use Module-Managed Keys
diff --git a/byo-vpc/byo-db/byo-ecs/.terraform-docs.yml b/byo-vpc/byo-db/byo-ecs/.terraform-docs.yml
new file mode 100644
index 0000000..1d139dd
--- /dev/null
+++ b/byo-vpc/byo-db/byo-ecs/.terraform-docs.yml
@@ -0,0 +1 @@
+header-from: .header.md
diff --git a/byo-vpc/byo-db/byo-ecs/README.md b/byo-vpc/byo-db/byo-ecs/README.md
index 90af9f6..2287afb 100644
--- a/byo-vpc/byo-db/byo-ecs/README.md
+++ b/byo-vpc/byo-db/byo-ecs/README.md
@@ -22,7 +22,7 @@ For each feature:
- If a key ARN is provided, this module uses that key and does not create a CMK.
- For software installers, setting `software_installers.kms_key_arn` is sufficient to use that key even if `create_kms_key = false`.
-IAM permissions for using these keys are managed in-module where possible. If a task role ARN is provided and belongs to the same AWS account, KMS/S3 policy attachments and `extra_iam_policies` attachments are applied to that role.
+IAM permissions for using these keys are managed in-module where possible. For module-created keys, the module prefers granting usage in the KMS key policy itself. If a caller-provided external key still needs IAM authorization and the task role ARN belongs to the same AWS account, the module attaches a separate resource-scoped KMS IAM policy to that role instead of mixing KMS access into the broader S3 or secret-access policy. `extra_iam_policies` attachments continue to be applied to the same-account task role when provided.
## Example: Use Module-Managed Keys
@@ -149,16 +149,20 @@ No modules.
| [aws_ecs_service.fleet](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
| [aws_ecs_task_definition.backend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource |
| [aws_iam_policy.execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
+| [aws_iam_policy.execution_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
+| [aws_iam_policy.software_installers_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_role.execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy_attachment.execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.execution_extras](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
+| [aws_iam_role_policy_attachment.execution_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.extras](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.role_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
+| [aws_iam_role_policy_attachment.software_installers_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_kms_alias.application_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
| [aws_kms_alias.private_key_secret](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
| [aws_kms_alias.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
@@ -179,8 +183,10 @@ No modules.
| [aws_iam_policy_document.assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.fleet](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.fleet-execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
+| [aws_iam_policy_document.fleet-execution-kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.private_key_secret_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
+| [aws_iam_policy_document.software_installers_external_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.software_installers_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_kms_key.software_installers_provided](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/kms_key) | data source |
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
@@ -191,7 +197,7 @@ No modules.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| [ecs\_cluster](#input\_ecs\_cluster) | The name of the ECS cluster to use | `string` | n/a | yes |
-| [fleet\_config](#input\_fleet\_config) | The configuration object for Fleet itself. Fields that default to null will have their respective resources created if not specified. For published KMS blocks, legacy `enabled` is deprecated and still accepted; prefer `cmk_enabled`. | object({
task_mem = optional(number, null)
task_cpu = optional(number, null)
ephemeral_storage = optional(object({
size_in_gib = number
}), null)
mem = optional(number, 4096)
cpu = optional(number, 512)
pid_mode = optional(string, null)
image = optional(string, "fleetdm/fleet:v4.81.2")
family = optional(string, "fleet")
sidecars = optional(list(any), [])
depends_on = optional(list(any), [])
mount_points = optional(list(any), [])
volumes = optional(list(any), [])
extra_environment_variables = optional(map(string), {})
extra_iam_policies = optional(list(string), [])
extra_execution_iam_policies = optional(list(string), [])
extra_secrets = optional(map(string), {})
security_group_name = optional(string, "fleet")
iam_role_arn = optional(string, null)
repository_credentials = optional(string, "")
private_key_secret_name = optional(string, "fleet-server-private-key")
private_key_secret_kms = optional(object({
cmk_enabled = optional(bool, null)
enabled = optional(bool, null)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-server-private-key")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-server-private-key"
extra_kms_policies = []
})
server_tls_enabled = optional(bool, false)
service = optional(object({
name = optional(string, "fleet")
}), {
name = "fleet"
})
database = object({
password_secret_arn = string
password_secret_kms_key_arn = optional(string, null)
user = string
database = string
address = string
rr_address = optional(string, null)
})
redis = object({
address = string
use_tls = optional(bool, true)
})
awslogs = optional(object({
name = optional(string, null)
region = optional(string, null)
create = optional(bool, true)
prefix = optional(string, "fleet")
retention = optional(number, 5)
kms = optional(object({
cmk_enabled = optional(bool, null)
enabled = optional(bool, null)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-application-logs")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-application-logs"
extra_kms_policies = []
})
}), {
name = null
region = null
create = true
prefix = "fleet"
retention = 5
kms = {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-application-logs"
extra_kms_policies = []
}
})
loadbalancer = object({
arn = string
})
extra_load_balancers = optional(list(any), [])
networking = object({
subnets = optional(list(string), null)
security_groups = optional(list(string), null)
ingress_sources = object({
cidr_blocks = optional(list(string), [])
ipv6_cidr_blocks = optional(list(string), [])
security_groups = optional(list(string), [])
prefix_list_ids = optional(list(string), [])
})
assign_public_ip = optional(bool, false)
})
autoscaling = optional(object({
max_capacity = optional(number, 5)
min_capacity = optional(number, 1)
memory_tracking_target_value = optional(number, 80)
cpu_tracking_target_value = optional(number, 80)
}), {
max_capacity = 5
min_capacity = 1
memory_tracking_target_value = 80
cpu_tracking_target_value = 80
})
iam = optional(object({
role = optional(object({
name = optional(string, "fleet-role")
policy_name = optional(string, "fleet-iam-policy")
}), {
name = "fleet-role"
policy_name = "fleet-iam-policy"
})
execution = optional(object({
name = optional(string, "fleet-execution-role")
policy_name = optional(string, "fleet-execution-role")
}), {
name = "fleet-execution-role"
policy_name = "fleet-iam-policy-execution"
})
}), {
name = "fleetdm-execution-role"
})
software_installers = optional(object({
create_bucket = optional(bool, true)
bucket_name = optional(string, null)
bucket_prefix = optional(string, "fleet-software-installers-")
s3_object_prefix = optional(string, "")
cloudfront_distribution_arn = optional(string, null)
enable_bucket_versioning = optional(bool, false)
expire_noncurrent_versions = optional(bool, true)
noncurrent_version_expiration_days = optional(number, 30)
create_kms_key = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-software-installers")
extra_kms_policies = optional(list(any), [])
tags = optional(map(string), {})
}), {
create_bucket = true
bucket_name = null
bucket_prefix = "fleet-software-installers-"
s3_object_prefix = ""
cloudfront_distribution_arn = null
enable_bucket_versioning = false
expire_noncurrent_versions = true
noncurrent_version_expiration_days = 30
create_kms_key = false
kms_key_arn = null
kms_alias = "fleet-software-installers"
extra_kms_policies = []
tags = {}
})
}) | {
"autoscaling": {
"cpu_tracking_target_value": 80,
"max_capacity": 5,
"memory_tracking_target_value": 80,
"min_capacity": 1
},
"awslogs": {
"create": true,
"kms": {
"cmk_enabled": null,
"enabled": null,
"extra_kms_policies": [],
"kms_alias": "fleet-application-logs",
"kms_key_arn": null
},
"name": null,
"prefix": "fleet",
"region": null,
"retention": 5
},
"cpu": 256,
"database": {
"address": null,
"database": null,
"password_secret_arn": null,
"rr_address": null,
"user": null
},
"depends_on": [],
"ephemeral_storage": null,
"extra_environment_variables": {},
"extra_execution_iam_policies": [],
"extra_iam_policies": [],
"extra_load_balancers": [],
"extra_secrets": {},
"family": "fleet",
"iam": {
"execution": {
"name": "fleet-execution-role",
"policy_name": "fleet-iam-policy-execution"
},
"role": {
"name": "fleet-role",
"policy_name": "fleet-iam-policy"
}
},
"iam_role_arn": null,
"image": "fleetdm/fleet:v4.81.2",
"loadbalancer": {
"arn": null
},
"mem": 512,
"mount_points": [],
"networking": {
"assign_public_ip": false,
"ingress_sources": {
"cidr_blocks": [],
"ipv6_cidr_blocks": [],
"prefix_list_ids": [],
"security_groups": []
},
"security_groups": null,
"subnets": null
},
"pid_mode": null,
"private_key_secret_kms": {
"cmk_enabled": null,
"enabled": null,
"extra_kms_policies": [],
"kms_alias": "fleet-server-private-key",
"kms_key_arn": null
},
"private_key_secret_name": "fleet-server-private-key",
"redis": {
"address": null,
"use_tls": true
},
"repository_credentials": "",
"security_group_name": "fleet",
"server_tls_enabled": false,
"service": {
"name": "fleet"
},
"sidecars": [],
"software_installers": {
"bucket_name": null,
"bucket_prefix": "fleet-software-installers-",
"cloudfront_distribution_arn": null,
"create_bucket": true,
"create_kms_key": false,
"enable_bucket_versioning": false,
"expire_noncurrent_versions": true,
"extra_kms_policies": [],
"kms_alias": "fleet-software-installers",
"kms_key_arn": null,
"noncurrent_version_expiration_days": 30,
"s3_object_prefix": "",
"tags": {}
},
"task_cpu": null,
"task_mem": null,
"volumes": []
} | no |
+| [fleet\_config](#input\_fleet\_config) | The configuration object for Fleet itself. Fields that default to null will have their respective resources created if not specified. For published KMS blocks, legacy `enabled` is deprecated and still accepted; prefer `cmk_enabled`. | object({
task_mem = optional(number, null)
task_cpu = optional(number, null)
ephemeral_storage = optional(object({
size_in_gib = number
}), null)
mem = optional(number, 4096)
cpu = optional(number, 512)
pid_mode = optional(string, null)
image = optional(string, "fleetdm/fleet:v4.82.1")
family = optional(string, "fleet")
sidecars = optional(list(any), [])
depends_on = optional(list(any), [])
mount_points = optional(list(any), [])
volumes = optional(list(any), [])
extra_environment_variables = optional(map(string), {})
extra_iam_policies = optional(list(string), [])
extra_execution_iam_policies = optional(list(string), [])
extra_secrets = optional(map(string), {})
security_group_name = optional(string, "fleet")
iam_role_arn = optional(string, null)
repository_credentials = optional(string, "")
private_key_secret_name = optional(string, "fleet-server-private-key")
private_key_secret_kms = optional(object({
cmk_enabled = optional(bool, null)
enabled = optional(bool, null)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-server-private-key")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-server-private-key"
extra_kms_policies = []
})
server_tls_enabled = optional(bool, false)
service = optional(object({
name = optional(string, "fleet")
}), {
name = "fleet"
})
database = object({
password_secret_arn = string
password_secret_kms_key_arn = optional(string, null)
user = string
database = string
address = string
rr_address = optional(string, null)
})
redis = object({
address = string
use_tls = optional(bool, true)
})
awslogs = optional(object({
name = optional(string, null)
region = optional(string, null)
create = optional(bool, true)
prefix = optional(string, "fleet")
retention = optional(number, 5)
kms = optional(object({
cmk_enabled = optional(bool, null)
enabled = optional(bool, null)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-application-logs")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-application-logs"
extra_kms_policies = []
})
}), {
name = null
region = null
create = true
prefix = "fleet"
retention = 5
kms = {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-application-logs"
extra_kms_policies = []
}
})
loadbalancer = object({
arn = string
})
extra_load_balancers = optional(list(any), [])
networking = object({
subnets = optional(list(string), null)
security_groups = optional(list(string), null)
ingress_sources = object({
cidr_blocks = optional(list(string), [])
ipv6_cidr_blocks = optional(list(string), [])
security_groups = optional(list(string), [])
prefix_list_ids = optional(list(string), [])
})
assign_public_ip = optional(bool, false)
})
autoscaling = optional(object({
max_capacity = optional(number, 5)
min_capacity = optional(number, 1)
memory_tracking_target_value = optional(number, 80)
cpu_tracking_target_value = optional(number, 80)
}), {
max_capacity = 5
min_capacity = 1
memory_tracking_target_value = 80
cpu_tracking_target_value = 80
})
iam = optional(object({
role = optional(object({
name = optional(string, "fleet-role")
policy_name = optional(string, "fleet-iam-policy")
}), {
name = "fleet-role"
policy_name = "fleet-iam-policy"
})
execution = optional(object({
name = optional(string, "fleet-execution-role")
policy_name = optional(string, "fleet-execution-role")
}), {
name = "fleet-execution-role"
policy_name = "fleet-iam-policy-execution"
})
}), {
name = "fleetdm-execution-role"
})
software_installers = optional(object({
create_bucket = optional(bool, true)
bucket_name = optional(string, null)
bucket_prefix = optional(string, "fleet-software-installers-")
s3_object_prefix = optional(string, "")
cloudfront_distribution_arn = optional(string, null)
enable_bucket_versioning = optional(bool, false)
expire_noncurrent_versions = optional(bool, true)
noncurrent_version_expiration_days = optional(number, 30)
create_kms_key = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-software-installers")
extra_kms_policies = optional(list(any), [])
tags = optional(map(string), {})
}), {
create_bucket = true
bucket_name = null
bucket_prefix = "fleet-software-installers-"
s3_object_prefix = ""
cloudfront_distribution_arn = null
enable_bucket_versioning = false
expire_noncurrent_versions = true
noncurrent_version_expiration_days = 30
create_kms_key = false
kms_key_arn = null
kms_alias = "fleet-software-installers"
extra_kms_policies = []
tags = {}
})
}) | {
"autoscaling": {
"cpu_tracking_target_value": 80,
"max_capacity": 5,
"memory_tracking_target_value": 80,
"min_capacity": 1
},
"awslogs": {
"create": true,
"kms": {
"cmk_enabled": null,
"enabled": null,
"extra_kms_policies": [],
"kms_alias": "fleet-application-logs",
"kms_key_arn": null
},
"name": null,
"prefix": "fleet",
"region": null,
"retention": 5
},
"cpu": 256,
"database": {
"address": null,
"database": null,
"password_secret_arn": null,
"rr_address": null,
"user": null
},
"depends_on": [],
"ephemeral_storage": null,
"extra_environment_variables": {},
"extra_execution_iam_policies": [],
"extra_iam_policies": [],
"extra_load_balancers": [],
"extra_secrets": {},
"family": "fleet",
"iam": {
"execution": {
"name": "fleet-execution-role",
"policy_name": "fleet-iam-policy-execution"
},
"role": {
"name": "fleet-role",
"policy_name": "fleet-iam-policy"
}
},
"iam_role_arn": null,
"image": "fleetdm/fleet:v4.82.1",
"loadbalancer": {
"arn": null
},
"mem": 512,
"mount_points": [],
"networking": {
"assign_public_ip": false,
"ingress_sources": {
"cidr_blocks": [],
"ipv6_cidr_blocks": [],
"prefix_list_ids": [],
"security_groups": []
},
"security_groups": null,
"subnets": null
},
"pid_mode": null,
"private_key_secret_kms": {
"cmk_enabled": null,
"enabled": null,
"extra_kms_policies": [],
"kms_alias": "fleet-server-private-key",
"kms_key_arn": null
},
"private_key_secret_name": "fleet-server-private-key",
"redis": {
"address": null,
"use_tls": true
},
"repository_credentials": "",
"security_group_name": "fleet",
"server_tls_enabled": false,
"service": {
"name": "fleet"
},
"sidecars": [],
"software_installers": {
"bucket_name": null,
"bucket_prefix": "fleet-software-installers-",
"cloudfront_distribution_arn": null,
"create_bucket": true,
"create_kms_key": false,
"enable_bucket_versioning": false,
"expire_noncurrent_versions": true,
"extra_kms_policies": [],
"kms_alias": "fleet-software-installers",
"kms_key_arn": null,
"noncurrent_version_expiration_days": 30,
"s3_object_prefix": "",
"tags": {}
},
"task_cpu": null,
"task_mem": null,
"volumes": []
} | no |
| [kms\_base\_policy](#input\_kms\_base\_policy) | Optional base KMS key-policy statements to apply to module-created CMKs before module-required service access statements are merged in. If null, the module defaults to the historical root `kms:*` statement. | list(object({
sid = string
effect = string
principals = object({
type = string
identifiers = list(string)
})
actions = list(string)
resources = list(string)
conditions = optional(list(object({
test = string
variable = string
values = list(string)
})), [])
})) | `null` | no |
| [migration\_config](#input\_migration\_config) | The configuration object for Fleet's migration task. | object({
mem = number
cpu = number
}) | {
"cpu": 1024,
"mem": 2048
} | no |
| [vpc\_id](#input\_vpc\_id) | n/a | `string` | `null` | no |
diff --git a/byo-vpc/byo-db/byo-ecs/iam.tf b/byo-vpc/byo-db/byo-ecs/iam.tf
index f7b11f1..f4c1fc6 100644
--- a/byo-vpc/byo-db/byo-ecs/iam.tf
+++ b/byo-vpc/byo-db/byo-ecs/iam.tf
@@ -6,19 +6,19 @@ locals {
task_role_name = var.fleet_config.iam_role_arn == null ? var.fleet_config.iam.role.name : (
can(split(":", var.fleet_config.iam_role_arn)[4]) && split(":", var.fleet_config.iam_role_arn)[4] == data.aws_caller_identity.current.account_id && can(split("role/", var.fleet_config.iam_role_arn)[1]) ? split("role/", var.fleet_config.iam_role_arn)[1] : null
)
- software_installers_kms_policy = local.software_installers_kms_key_arn != null ? [{
+ software_installers_kms_policy = local.software_installers_kms_key_arn != null && local.software_installers_create_kms_key == false ? [{
sid = "AllowSoftwareInstallersKMSAccess"
actions = [
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
- "kms:Encrypt*",
- "kms:Describe*",
- "kms:Decrypt*"
+ "kms:Encrypt",
+ "kms:DescribeKey",
+ "kms:Decrypt"
]
resources = [local.software_installers_kms_key_arn]
effect = "Allow"
}] : []
- private_key_secret_kms_policy = local.private_key_secret_kms_key_arn != null ? [{
+ private_key_secret_kms_policy = local.private_key_secret_kms_key_arn != null && local.private_key_secret_create_kms_key == false ? [{
sid = "AllowFleetPrivateKeySecretKMSAccess"
actions = [
"kms:Decrypt",
@@ -55,6 +55,7 @@ data "aws_iam_policy_document" "software_installers" {
]
resources = [aws_s3_bucket.software_installers[0].arn, "${aws_s3_bucket.software_installers[0].arn}/*"]
}
+
dynamic "statement" {
for_each = local.software_installers_kms_policy
content {
@@ -62,21 +63,6 @@ data "aws_iam_policy_document" "software_installers" {
actions = try(statement.value.actions, [])
resources = try(statement.value.resources, [])
effect = try(statement.value.effect, null)
- dynamic "principals" {
- for_each = try(statement.value.principals, [])
- content {
- type = principals.value.type
- identifiers = principals.value.identifiers
- }
- }
- dynamic "condition" {
- for_each = try(statement.value.conditions, [])
- content {
- test = condition.value.test
- variable = condition.value.variable
- values = condition.value.values
- }
- }
}
}
}
@@ -122,6 +108,7 @@ data "aws_iam_policy_document" "fleet-execution" {
aws_secretsmanager_secret.fleet_server_private_key.arn
]
}
+
dynamic "statement" {
for_each = local.execution_kms_policy
content {
diff --git a/byo-vpc/byo-db/byo-ecs/main.tf b/byo-vpc/byo-db/byo-ecs/main.tf
index ebc9dcf..e42ea06 100644
--- a/byo-vpc/byo-db/byo-ecs/main.tf
+++ b/byo-vpc/byo-db/byo-ecs/main.tf
@@ -160,6 +160,17 @@ check "deprecated_fleet_config_awslogs_kms_enabled" {
}
}
+check "kms_base_policy_requires_module_managed_cmk" {
+ assert {
+ condition = var.kms_base_policy == null || (
+ local.application_logs_create_kms_key == true ||
+ local.private_key_secret_create_kms_key == true ||
+ local.software_installers_create_kms_key == true
+ )
+ error_message = "kms_base_policy is not used by byo-ecs unless this module is creating at least one CMK. When kms_key_arn is provided, external key policies remain caller-managed."
+ }
+}
+
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
@@ -377,15 +388,57 @@ resource "aws_appautoscaling_policy" "ecs_policy_cpu" {
}
}
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "application_logs_kms" {
count = local.application_logs_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.fleet_config.awslogs.kms.extra_kms_policies,
- [local.kms_service_statements.cloudwatch_logs]
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.fleet_config.awslogs.kms.extra_kms_policies
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.cloudwatch_logs]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -496,16 +549,79 @@ resource "aws_kms_key" "private_key_secret" {
policy = data.aws_iam_policy_document.private_key_secret_kms[0].json
}
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "private_key_secret_kms" {
count = local.private_key_secret_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.fleet_config.private_key_secret_kms.extra_kms_policies,
- [local.kms_service_statements.secretsmanager],
- [local.kms_service_statements.execution_role]
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.fleet_config.private_key_secret_kms.extra_kms_policies
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.secretsmanager]
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.execution_role]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -527,16 +643,79 @@ data "aws_iam_policy_document" "private_key_secret_kms" {
}
}
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "software_installers_kms" {
count = local.software_installers_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.fleet_config.software_installers.extra_kms_policies,
- local.software_installers_kms_task_role_statements,
- local.software_installers_kms_service_statements
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = try(statement.value.sid, "")
+ effect = try(statement.value.effect, null)
+ actions = try(statement.value.actions, [])
+ resources = try(statement.value.resources, [])
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.fleet_config.software_installers.extra_kms_policies
+ content {
+ sid = try(statement.value.sid, "")
+ effect = try(statement.value.effect, null)
+ actions = try(statement.value.actions, [])
+ resources = try(statement.value.resources, [])
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = local.software_installers_kms_task_role_statements
+ content {
+ sid = try(statement.value.sid, "")
+ effect = try(statement.value.effect, null)
+ actions = try(statement.value.actions, [])
+ resources = try(statement.value.resources, [])
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = local.software_installers_kms_service_statements
content {
sid = try(statement.value.sid, "")
effect = try(statement.value.effect, null)
diff --git a/byo-vpc/byo-db/byo-ecs/variables.tf b/byo-vpc/byo-db/byo-ecs/variables.tf
index d875008..a330358 100644
--- a/byo-vpc/byo-db/byo-ecs/variables.tf
+++ b/byo-vpc/byo-db/byo-ecs/variables.tf
@@ -301,6 +301,64 @@ variable "fleet_config" {
condition = var.fleet_config.ephemeral_storage == null ? true : (var.fleet_config.ephemeral_storage.size_in_gib >= 21 && var.fleet_config.ephemeral_storage.size_in_gib <= 200)
error_message = "fleet_config.ephemeral_storage.size_in_gib must be between 21 and 200 GiB when set."
}
+
+ validation {
+ condition = (
+ var.fleet_config.private_key_secret_kms.kms_key_arn == null ||
+ coalesce(var.fleet_config.private_key_secret_kms.cmk_enabled, var.fleet_config.private_key_secret_kms.enabled, false)
+ )
+ error_message = "fleet_config.private_key_secret_kms.kms_key_arn requires fleet_config.private_key_secret_kms.cmk_enabled = true (or legacy enabled = true)."
+ }
+
+ validation {
+ condition = (
+ length(var.fleet_config.private_key_secret_kms.extra_kms_policies) == 0 ||
+ (
+ coalesce(var.fleet_config.private_key_secret_kms.cmk_enabled, var.fleet_config.private_key_secret_kms.enabled, false) &&
+ var.fleet_config.private_key_secret_kms.kms_key_arn == null
+ )
+ )
+ error_message = "fleet_config.private_key_secret_kms.extra_kms_policies can be set only when byo-ecs is creating the private key secret CMK."
+ }
+
+ validation {
+ condition = (
+ var.fleet_config.awslogs.kms.kms_key_arn == null ||
+ (
+ var.fleet_config.awslogs.create == true &&
+ coalesce(var.fleet_config.awslogs.kms.cmk_enabled, var.fleet_config.awslogs.kms.enabled, false)
+ )
+ )
+ error_message = "fleet_config.awslogs.kms.kms_key_arn requires fleet_config.awslogs.create = true and fleet_config.awslogs.kms.cmk_enabled = true (or legacy enabled = true)."
+ }
+
+ validation {
+ condition = (
+ length(var.fleet_config.awslogs.kms.extra_kms_policies) == 0 ||
+ (
+ var.fleet_config.awslogs.create == true &&
+ coalesce(var.fleet_config.awslogs.kms.cmk_enabled, var.fleet_config.awslogs.kms.enabled, false) &&
+ var.fleet_config.awslogs.kms.kms_key_arn == null
+ )
+ )
+ error_message = "fleet_config.awslogs.kms.extra_kms_policies can be set only when byo-ecs is creating the application logs CMK."
+ }
+
+ validation {
+ condition = !(var.fleet_config.software_installers.kms_key_arn != null && var.fleet_config.software_installers.create_kms_key == true)
+ error_message = "fleet_config.software_installers.kms_key_arn and fleet_config.software_installers.create_kms_key are mutually exclusive; set one or the other, not both."
+ }
+
+ validation {
+ condition = (
+ length(var.fleet_config.software_installers.extra_kms_policies) == 0 ||
+ (
+ var.fleet_config.software_installers.create_kms_key == true &&
+ var.fleet_config.software_installers.kms_key_arn == null
+ )
+ )
+ error_message = "fleet_config.software_installers.extra_kms_policies can be set only when byo-ecs is creating the software installers CMK."
+ }
}
variable "migration_config" {
diff --git a/byo-vpc/byo-db/main.tf b/byo-vpc/byo-db/main.tf
index 49c852f..0d45973 100644
--- a/byo-vpc/byo-db/main.tf
+++ b/byo-vpc/byo-db/main.tf
@@ -206,6 +206,16 @@ check "deprecated_fleet_config_fargate_ephemeral_storage_kms_enabled" {
}
}
+check "kms_base_policy_requires_module_managed_cmk" {
+ assert {
+ condition = var.kms_base_policy == null || (
+ local.fargate_ephemeral_storage_create_kms_key == true ||
+ local.cluster_cloudwatch_log_group_create_kms_key == true
+ )
+ error_message = "kms_base_policy is not used by byo-db unless this module is creating at least one CMK. When kms_key_arn is provided, external key policies remain caller-managed."
+ }
+}
+
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
data "aws_region" "current" {}
@@ -237,18 +247,60 @@ module "cluster" {
tags = var.ecs_cluster.tags
}
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "fargate_ephemeral_storage_kms" {
count = local.fargate_ephemeral_storage_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.fleet_config.fargate_ephemeral_storage_kms.extra_kms_policies,
- [
- local.kms_service_statements.fargate_generate_data_key,
- local.kms_service_statements.fargate_create_grant,
- ]
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.fleet_config.fargate_ephemeral_storage_kms.extra_kms_policies
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [
+ local.kms_service_statements.fargate_generate_data_key,
+ local.kms_service_statements.fargate_create_grant,
+ ]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -283,15 +335,57 @@ resource "aws_kms_alias" "fargate_ephemeral_storage" {
name = "alias/${var.fleet_config.fargate_ephemeral_storage_kms.kms_alias}"
}
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "cluster_cloudwatch_log_group_kms" {
count = local.cluster_cloudwatch_log_group_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.ecs_cluster.cloudwatch_log_group.kms.extra_kms_policies,
- [local.kms_service_statements.cloudwatch_logs]
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.ecs_cluster.cloudwatch_log_group.kms.extra_kms_policies
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.cloudwatch_logs]
content {
sid = statement.value.sid
effect = statement.value.effect
diff --git a/byo-vpc/byo-db/variables.tf b/byo-vpc/byo-db/variables.tf
index 0da9b91..4006da6 100644
--- a/byo-vpc/byo-db/variables.tf
+++ b/byo-vpc/byo-db/variables.tf
@@ -124,6 +124,29 @@ variable "ecs_cluster" {
}
description = "The config for the terraform-aws-modules/ecs/aws module. For published KMS blocks, legacy `enabled` is deprecated and still accepted; prefer `cmk_enabled`."
nullable = false
+
+ validation {
+ condition = (
+ var.ecs_cluster.cloudwatch_log_group.kms.kms_key_arn == null ||
+ (
+ var.ecs_cluster.cloudwatch_log_group.create == true &&
+ coalesce(var.ecs_cluster.cloudwatch_log_group.kms.cmk_enabled, var.ecs_cluster.cloudwatch_log_group.kms.enabled, false)
+ )
+ )
+ error_message = "ecs_cluster.cloudwatch_log_group.kms.kms_key_arn requires ecs_cluster.cloudwatch_log_group.create = true and ecs_cluster.cloudwatch_log_group.kms.cmk_enabled = true (or legacy enabled = true)."
+ }
+
+ validation {
+ condition = (
+ length(var.ecs_cluster.cloudwatch_log_group.kms.extra_kms_policies) == 0 ||
+ (
+ var.ecs_cluster.cloudwatch_log_group.create == true &&
+ coalesce(var.ecs_cluster.cloudwatch_log_group.kms.cmk_enabled, var.ecs_cluster.cloudwatch_log_group.kms.enabled, false) &&
+ var.ecs_cluster.cloudwatch_log_group.kms.kms_key_arn == null
+ )
+ )
+ error_message = "ecs_cluster.cloudwatch_log_group.kms.extra_kms_policies can be set only when byo-db is creating the ECS cluster CloudWatch log group CMK."
+ }
}
@@ -443,6 +466,25 @@ variable "fleet_config" {
}
description = "The configuration object for Fleet itself. Fields that default to null will have their respective resources created if not specified. For published KMS blocks, legacy `enabled` is deprecated and still accepted; prefer `cmk_enabled`."
nullable = false
+
+ validation {
+ condition = (
+ var.fleet_config.fargate_ephemeral_storage_kms.kms_key_arn == null ||
+ coalesce(var.fleet_config.fargate_ephemeral_storage_kms.cmk_enabled, var.fleet_config.fargate_ephemeral_storage_kms.enabled, false)
+ )
+ error_message = "fleet_config.fargate_ephemeral_storage_kms.kms_key_arn requires fleet_config.fargate_ephemeral_storage_kms.cmk_enabled = true (or legacy enabled = true)."
+ }
+
+ validation {
+ condition = (
+ length(var.fleet_config.fargate_ephemeral_storage_kms.extra_kms_policies) == 0 ||
+ (
+ coalesce(var.fleet_config.fargate_ephemeral_storage_kms.cmk_enabled, var.fleet_config.fargate_ephemeral_storage_kms.enabled, false) &&
+ var.fleet_config.fargate_ephemeral_storage_kms.kms_key_arn == null
+ )
+ )
+ error_message = "fleet_config.fargate_ephemeral_storage_kms.extra_kms_policies can be set only when byo-db is creating the Fargate ephemeral storage CMK."
+ }
}
variable "migration_config" {
diff --git a/byo-vpc/main.tf b/byo-vpc/main.tf
index 80af27f..5760bd8 100644
--- a/byo-vpc/main.tf
+++ b/byo-vpc/main.tf
@@ -194,15 +194,71 @@ data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
data "aws_region" "current" {}
+check "kms_base_policy_requires_module_managed_cmk" {
+ assert {
+ condition = var.kms_base_policy == null || (
+ local.rds_storage_create_kms_key == true ||
+ local.rds_password_secret_create_kms_key == true ||
+ local.rds_observability_create_kms_key == true ||
+ local.rds_cloudwatch_log_group_create_kms_key == true ||
+ local.redis_at_rest_create_kms_key == true ||
+ local.redis_cloudwatch_log_group_create_kms_key == true
+ )
+ error_message = "kms_base_policy is not used by byo-vpc unless this module is creating at least one CMK. When kms_key_arn is provided, external key policies remain caller-managed."
+ }
+}
+
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "rds_storage_kms" {
count = local.rds_storage_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.rds_config.storage_kms.extra_kms_policies,
- [local.kms_service_statements.rds]
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.rds_config.storage_kms.extra_kms_policies
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.rds]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -237,16 +293,79 @@ resource "aws_kms_alias" "rds_storage" {
name = "alias/${var.rds_config.storage_kms.kms_alias}"
}
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "rds_password_secret_kms" {
count = local.rds_password_secret_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.rds_config.password_secret_kms.extra_kms_policies,
- [local.kms_service_statements.secretsmanager],
- [local.kms_service_statements.execution_role]
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.rds_config.password_secret_kms.extra_kms_policies
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.secretsmanager]
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.execution_role]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -281,15 +400,57 @@ resource "aws_kms_alias" "rds_password_secret" {
name = "alias/${var.rds_config.password_secret_kms.kms_alias}"
}
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "rds_observability_kms" {
count = local.rds_observability_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.rds_config.observability.kms.extra_kms_policies,
- [local.kms_service_statements.rds]
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.rds_config.observability.kms.extra_kms_policies
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.rds]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -324,15 +485,57 @@ resource "aws_kms_alias" "rds_observability" {
name = "alias/${var.rds_config.observability.kms.kms_alias}"
}
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "rds_cloudwatch_log_group_kms" {
count = local.rds_cloudwatch_log_group_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.rds_config.cloudwatch_log_group.kms.extra_kms_policies,
- [local.kms_service_statements.cloudwatch_logs]
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.rds_config.cloudwatch_log_group.kms.extra_kms_policies
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.cloudwatch_logs]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -367,15 +570,57 @@ resource "aws_kms_alias" "rds_cloudwatch_log_group" {
name = "alias/${var.rds_config.cloudwatch_log_group.kms.kms_alias}"
}
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "redis_at_rest_kms" {
count = local.redis_at_rest_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.redis_config.at_rest_kms.extra_kms_policies,
- [local.kms_service_statements.elasticache]
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.redis_config.at_rest_kms.extra_kms_policies
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.elasticache]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -410,15 +655,57 @@ resource "aws_kms_alias" "redis_at_rest" {
name = "alias/${var.redis_config.at_rest_kms.kms_alias}"
}
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "redis_cloudwatch_log_group_kms" {
count = local.redis_cloudwatch_log_group_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.redis_config.cloudwatch_log_group.kms.extra_kms_policies,
- [local.kms_service_statements.cloudwatch_logs]
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.redis_config.cloudwatch_log_group.kms.extra_kms_policies
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.cloudwatch_logs]
content {
sid = statement.value.sid
effect = statement.value.effect
diff --git a/main.tf b/main.tf
index 2cf46c8..057555d 100644
--- a/main.tf
+++ b/main.tf
@@ -46,15 +46,66 @@ data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
data "aws_region" "current" {}
+check "kms_base_policy_requires_module_managed_cmk" {
+ assert {
+ condition = var.kms_base_policy == null || (
+ local.vpc_flow_log_cloudwatch_log_group_create_kms_key == true
+ )
+ error_message = "kms_base_policy is not used by the root module unless this module is creating the VPC flow log CloudWatch log group CMK. When kms_key_arn is provided, external key policies remain caller-managed."
+ }
+}
+
+# Each source uses its own dynamic "statement" block to avoid Terraform type
+# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "vpc_flow_log_cloudwatch_log_group_kms" {
count = local.vpc_flow_log_cloudwatch_log_group_create_kms_key == true ? 1 : 0
dynamic "statement" {
- for_each = concat(
- local.kms_base_policy_statements,
- var.vpc.flow_log_cloudwatch_log_group_kms.extra_kms_policies,
- [local.kms_service_statements.cloudwatch_logs]
- )
+ for_each = local.kms_base_policy_statements
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = var.vpc.flow_log_cloudwatch_log_group_kms.extra_kms_policies
+ content {
+ sid = statement.value.sid
+ effect = statement.value.effect
+ actions = statement.value.actions
+ resources = statement.value.resources
+ principals {
+ type = statement.value.principals.type
+ identifiers = statement.value.principals.identifiers
+ }
+ dynamic "condition" {
+ for_each = try(statement.value.conditions, [])
+ content {
+ test = condition.value.test
+ variable = condition.value.variable
+ values = condition.value.values
+ }
+ }
+ }
+ }
+
+ dynamic "statement" {
+ for_each = [local.kms_service_statements.cloudwatch_logs]
content {
sid = statement.value.sid
effect = statement.value.effect
diff --git a/variables.tf b/variables.tf
index 9c7397a..caea6de 100644
--- a/variables.tf
+++ b/variables.tf
@@ -70,6 +70,29 @@ variable "vpc" {
}
vpc_flow_log_tags = {}
}
+ validation {
+ condition = (
+ var.vpc.flow_log_cloudwatch_log_group_kms.kms_key_arn == null ||
+ (
+ var.vpc.enable_flow_log == true &&
+ var.vpc.create_flow_log_cloudwatch_log_group == true &&
+ var.vpc.flow_log_cloudwatch_log_group_kms.cmk_enabled == true
+ )
+ )
+ error_message = "vpc.flow_log_cloudwatch_log_group_kms.kms_key_arn requires vpc.enable_flow_log = true, vpc.create_flow_log_cloudwatch_log_group = true, and vpc.flow_log_cloudwatch_log_group_kms.cmk_enabled = true."
+ }
+ validation {
+ condition = (
+ length(var.vpc.flow_log_cloudwatch_log_group_kms.extra_kms_policies) == 0 ||
+ (
+ var.vpc.enable_flow_log == true &&
+ var.vpc.create_flow_log_cloudwatch_log_group == true &&
+ var.vpc.flow_log_cloudwatch_log_group_kms.cmk_enabled == true &&
+ var.vpc.flow_log_cloudwatch_log_group_kms.kms_key_arn == null
+ )
+ )
+ error_message = "vpc.flow_log_cloudwatch_log_group_kms.extra_kms_policies can be set only when the root module is creating the VPC flow log CloudWatch log group CMK."
+ }
}
variable "certificate_arn" {