From e7faee93de8c5c6c76090d88849274240f1ad02c Mon Sep 17 00:00:00 2001 From: Robert Fairburn <8029478+rfairburn@users.noreply.github.com> Date: Fri, 20 Mar 2026 12:07:38 -0500 Subject: [PATCH] Kms cmk root to byo db (#193) ## Highlights - Upgrades Aurora from `terraform-aws-modules/rds-aurora/aws` `7.6.0` to `9.16.1` - Bumps the Terraform minimum version from `>= 1.3.8` to `>= 1.5.0` - Adds CMK support across newly exposed RDS, Redis, VPC, ECS, and Fleet surfaces - Adds additive per-key `extra_kms_policies` support across the root -> `byo-vpc` -> `byo-db` -> `byo-ecs` path - Moves software-installers KMS policy ownership out of `addons/cloudfront-software-installers` and back into `byo-ecs` - Extends monitoring and ECS integration to work with CMK-encrypted DB password secrets - Adds migration tooling and docs for Aurora storage KMS replacement and CloudWatch Logs KMS cutovers ## Aurora module upgrade: `7.6.0` -> `9.16.1` This PR keeps a single Aurora module path on upstream `9.16.1` and updates the Fleet wrapper modules to preserve compatibility where possible while exposing the new KMS and observability capabilities. Notable changes handled in the wrapper: - `security_group_rules` - upstream moved from older SG rule addressing to the newer `security_group_rules` model - added `moved` handling for the confirmed SG rule address rename to avoid unnecessary destroy/create churn - `manage_master_user_password` - the wrapper continues to own the DB password flow - Aurora is kept on `manage_master_user_password = false` - Secrets Manager integration remains wrapper-managed rather than switching to upstream-managed password behavior - `final_snapshot_identifier` - wrapper support was added so old behavior is preserved - if unset, the legacy generated naming pattern is still used - callers can now override it explicitly - Observability / Database Insights wiring - cluster-level Performance Insights / Database Insights configuration was added to match Aurora's current control surface - monitoring behavior was aligned to cluster-level management for consistency with the Aurora module and Database Insights requirements - `backtrack_window` - added optional pass-through support for Aurora MySQL backtracking - validated as `null` or `0..259200` ## Terraform version bump This PR raises the Terraform minimum version from `>= 1.3.8` to `>= 1.5.0`. Reason: - deprecation warnings for legacy KMS `enabled` flags now use Terraform `check` blocks - `check` blocks require Terraform `1.5+` This was done so published interfaces can remain backward-compatible for existing `enabled` users while still warning during plan/apply and preferring `cmk_enabled`. ## KMS surface expansion and pass-through This PR adds or expands CMK support for: - Aurora storage - Aurora password secret - Aurora observability / Performance Insights - Aurora exported log groups - Redis at-rest encryption - Redis log groups - VPC flow log CloudWatch log groups - ECS cluster log groups - Fleet private-key secret - Fleet Fargate ephemeral storage - Fleet application logs - Fleet software installers It also adds additive `extra_kms_policies` support across those module-owned CMK surfaces so callers can append narrowly scoped statements without replacing the inherited base policy. During validation of the cron-monitoring integration, I found that the root module schema was still dropping some nested KMS attributes before they reached `byo-vpc`. This PR now carries those fields all the way through to their final destinations, including: - `rds_config.*.extra_kms_policies` - `redis_config.*.extra_kms_policies` - `ecs_cluster.cloudwatch_log_group.kms.extra_kms_policies` - `fleet_config.private_key_secret_kms.extra_kms_policies` - `fleet_config.fargate_ephemeral_storage_kms.extra_kms_policies` - `fleet_config.awslogs.kms.extra_kms_policies` - `fleet_config.software_installers.extra_kms_policies` - `fleet_config.software_installers.cloudfront_distribution_arn` - `vpc.flow_log_cloudwatch_log_group_kms.extra_kms_policies` ## DB password secret KMS handoff to monitoring `byo-vpc` now exposes: - `rds_password_secret_kms_key_arn` The monitoring addon now accepts: - `cron_monitoring.mysql_password_secret_kms_key_arn` The monitoring addon also now exposes: - `cron_monitoring_lambda_arn` - `cron_monitoring_lambda_role_arn` This is needed so the cron-monitoring Lambda can decrypt the Fleet DB password secret when that secret is encrypted with a customer-managed KMS key. Expected wiring: ```hcl module "monitoring" { # ... cron_monitoring = { # ... mysql_password_secret_name = "${local.customer}-database-password" mysql_password_secret_kms_key_arn = module.main.byo-vpc.rds_password_secret_kms_key_arn } } ``` ## Software installers KMS ownership change This PR changes the software-installers KMS contract: - `addons/cloudfront-software-installers` no longer manages the software-installers KMS key policy - `byo-ecs` now owns the software-installers CMK policy, including required service / principal statements - `fleet_config.software_installers.cloudfront_distribution_arn` is the static input used to add the CloudFront KMS statement without introducing a module cycle - `byo-ecs` also adds the explicit Fleet task-role KMS statement required under the narrower external key-policy model - the CloudFront addon exposes `cloudfront_distribution_arn` for visibility, but that output should not be fed back into `byo-ecs` because that would create a dependency cycle Important compatibility note: - if software installers uses KMS at all, it must use a customer-managed key - CloudFront requires a key-policy statement on that key - the AWS-managed default key cannot be modified to attach that policy ## Other notable items - Added and expanded migration docs for RDS and Redis KMS adoption - Added `byo-vpc/scripts/rds_storage_kms_migration.sh` to automate Aurora storage-KMS migration workflows - Added `scripts/cloudwatch_logs_kms_migration.sh` to help identify and optionally remove pre-cutover log streams after associating a new KMS key with a CloudWatch Logs group - Normalized `cmk_enabled` usage while preserving deprecated `enabled` compatibility only for previously published interfaces - Refactored `addons/logging-alb` KMS policy handling to support `kms_base_policy` and align it with the structured KMS statement model used elsewhere - Reduced repeated KMS policy boilerplate within modules where it was materially duplicated ## Compatibility notes - Existing published KMS interfaces that previously used `enabled` still accept it, but now warn and prefer `cmk_enabled` - New KMS surfaces are `cmk_enabled`-only - Software-installers KMS policy ownership moved from `addons/cloudfront-software-installers` to `byo-ecs` ## Validation Validated module stacks after the refactors: - `root` - `byo-vpc` - `byo-db` - `byo-ecs` - `addons/monitoring` - `addons/cloudfront-software-installers` - `addons/logging-alb` --- .header.md | 164 ++- README.md | 190 ++- .../cloudfront-software-installers/.header.md | 16 +- .../cloudfront-software-installers/README.md | 23 +- addons/cloudfront-software-installers/main.tf | 34 - .../cloudfront-software-installers/outputs.tf | 4 + .../variables.tf | 6 - addons/logging-alb/main.tf | 72 +- addons/logging-alb/variables.tf | 20 + addons/monitoring/.header.md | 5 + addons/monitoring/README.md | 14 +- addons/monitoring/main.tf | 38 +- addons/monitoring/outputs.tf | 7 + addons/monitoring/variables.tf | 1 + byo-vpc/.header.md | 313 +++++ byo-vpc/.terraform-docs.yml | 1 + byo-vpc/README.md | 355 ++++- byo-vpc/byo-db/.header.md | 26 + byo-vpc/byo-db/README.md | 11 +- byo-vpc/byo-db/byo-ecs/.header.md | 27 +- byo-vpc/byo-db/byo-ecs/README.md | 36 +- byo-vpc/byo-db/byo-ecs/iam.tf | 11 +- byo-vpc/byo-db/byo-ecs/main.tf | 246 +++- byo-vpc/byo-db/byo-ecs/variables.tf | 97 +- byo-vpc/byo-db/byo-ecs/versions.tf | 3 + byo-vpc/byo-db/main.tf | 247 ++-- byo-vpc/byo-db/variables.tf | 159 ++- byo-vpc/byo-db/versions.tf | 3 + byo-vpc/example/main.tf | 2 +- byo-vpc/main.tf | 511 +++++++- byo-vpc/moved.tf | 9 + byo-vpc/outputs.tf | 4 + byo-vpc/scripts/rds_storage_kms_migration.sh | 1146 +++++++++++++++++ byo-vpc/variables.tf | 396 ++++-- byo-vpc/versions.tf | 3 +- main.tf | 93 +- .../cloudwatch_logs_kms_migration.sh | 43 +- variables.tf | 413 ++++-- 38 files changed, 4128 insertions(+), 621 deletions(-) create mode 100644 addons/monitoring/outputs.tf create mode 100644 byo-vpc/.header.md create mode 100644 byo-vpc/.terraform-docs.yml create mode 100644 byo-vpc/byo-db/.header.md create mode 100644 byo-vpc/byo-db/byo-ecs/versions.tf create mode 100644 byo-vpc/byo-db/versions.tf create mode 100644 byo-vpc/moved.tf create mode 100755 byo-vpc/scripts/rds_storage_kms_migration.sh rename {byo-vpc/byo-db/byo-ecs/scripts => scripts}/cloudwatch_logs_kms_migration.sh (74%) diff --git a/.header.md b/.header.md index 88fcc8c..75cf17d 100644 --- a/.header.md +++ b/.header.md @@ -1,20 +1,123 @@ -This module provides a basic [Fleet](https://fleetdm.com) with Terraform. This assumes that you bring nothing to the installation. -If you want to bring your own VPC/database/cache nodes/ECS cluster, then use one of the submodules provided. +This module provides a basic [Fleet](https://fleetdm.com) deployment on AWS with Terraform. It is the root module for the repo and creates the VPC, database, cache, ECS cluster, ALB, and Fleet service stack. If you want to bring part of that infrastructure yourself, use one of the nested submodules instead. + +To quickly list available released module tags: -To quickly list all available module versions you can run: ```shell -git tag |grep '^tf' +git tag | grep '^tf' ``` -The following is the module layout, so you can navigate to the module that you want: +Module layout: -* Root module (use this to get a Fleet instance ASAP with minimal setup) - * BYO-VPC (use this if you want to install Fleet inside an existing VPC) - * BYO-database (use this if you want to use an existing database and cache node) - * BYO-ECS (use this if you want to bring your own everything but Fleet ECS services) +* Root module +* `byo-vpc` +* `byo-vpc/byo-db` +* `byo-vpc/byo-db/byo-ecs` + +# KMS Coverage + +This root module now exposes optional customer-managed KMS key (CMK) support for every KMS-capable surface that this stack manages directly or passes through to child modules. + +At the root module level this includes: + +* VPC flow log CloudWatch log groups +* Aurora storage encryption +* Aurora database password secret in Secrets Manager +* Aurora observability encryption for Performance Insights / CloudWatch Database Insights +* Aurora exported CloudWatch log groups +* ElastiCache at-rest encryption +* ElastiCache CloudWatch log groups for `cloudwatch-logs` delivery targets +* ECS cluster exec log groups, Fleet app log groups, Fleet private-key secret, and other nested Fleet/ECS KMS features through child-module passthroughs + +Behavior rules are consistent across features: + +* `cmk_enabled = true` means "use a customer-managed KMS key here." +* Set `cmk_enabled = false` or omit it to keep using the service-managed key. +* For KMS options that existed in published releases before this change, legacy `enabled` is deprecated but still accepted. Terraform plan/apply warns when it is used, and `cmk_enabled` takes precedence if both are set. +* If CMK use is enabled and no key ARN is provided, the module creates a CMK and alias. +* If a key ARN is provided, the module uses that key and does not create one. +* For provided keys, required IAM is managed where this repo owns an IAM principal, but external key policies must already allow the relevant AWS service to use the key. + +# Aurora Database Insights + +AWS has announced that the Performance Insights console reaches end of life on **June 30, 2026**. Aurora observability is moving toward CloudWatch Database Insights. + +This module exposes Aurora observability through `rds_config.observability`: + +* `database_insights_mode = null` means Terraform does not force Standard vs Advanced mode. +* `database_insights_mode = "standard"` explicitly keeps the cluster in Standard Database Insights mode. +* `database_insights_mode = "advanced"` enables Advanced Database Insights and therefore requires: + * `performance_insights_enabled = true` + * `monitoring_interval > 0` + * `retention_period >= 465` + +The observability CMK applies through Aurora's underlying Performance Insights encryption plane, which Database Insights builds on. + +# Aurora Backtrack + +This module also exposes Aurora MySQL backtracking through `rds_config.backtrack_window`. + +* Set it to a value between `0` and `259200` seconds. +* Set `0` to disable backtracking explicitly. +* Leave it `null` to keep the default upstream behavior. + +# Migration Notes + +* Existing environments remain unchanged unless you opt into new KMS or Database Insights settings. +* For provided CMKs, ensure the key policy already allows the relevant AWS service. +* Enabling KMS on existing CloudWatch log groups may require old log streams to be purged so all retained data is under the new key. + +Use the repository-root helper script for CloudWatch Logs KMS cleanup: + +```bash +# Dry run +DELETE_OLD_STREAMS=false ./scripts/cloudwatch_logs_kms_migration.sh + +# Delete old streams +./scripts/cloudwatch_logs_kms_migration.sh +``` + +# Example + +```hcl +module "fleet" { + source = "github.com/fleetdm/fleet-terraform?depth=1&ref=tf-mod-root-v1.23.0" + + certificate_arn = module.acm.acm_certificate_arn + + vpc = { + enable_flow_log = true + create_flow_log_cloudwatch_log_group = true + flow_log_cloudwatch_log_group_kms = { + cmk_enabled = true + } + } + + rds_config = { + storage_kms = { + cmk_enabled = true + } + password_secret_kms = { + cmk_enabled = true + } + observability = { + database_insights_mode = "standard" + kms = { + cmk_enabled = true + } + } + } + + redis_config = { + at_rest_kms = { + cmk_enabled = true + } + } +} +``` # Migrating from existing Dogfood code -The below code describes how to migrate from existing Dogfood code + +The below code describes how to migrate from existing Dogfood code. ```hcl moved { @@ -24,39 +127,26 @@ moved { moved { from = module.aurora_mysql - to = module.main.module.byo-vpc.module.rds + to = module.main.module.byo-vpc.module.rds } moved { from = aws_elasticache_replication_group.default - to = module.main.module.byo-vpc.module.redis.aws_elasticache_replication_group.default + to = module.main.module.byo-vpc.module.redis.aws_elasticache_replication_group.default } ``` -This focuses on the resources that are "heavy" or store data. Note that the ALB cannot be moved like this because Dogfood uses the `aws_alb` resource and the module uses the `aws_lb` resource. The resources are aliases of eachother, but Terraform can't recognize that. +This focuses on the resources that are "heavy" or store data. The ALB cannot be moved the same way because Dogfood uses `aws_alb` while the module uses `aws_lb`. # Cache Engine: Redis vs Valkey -This module supports both Redis and [Valkey](https://valkey.io/) as the ElastiCache engine. Valkey is an open-source, Redis-compatible in-memory data store. +This module supports both Redis and [Valkey](https://valkey.io/) as the ElastiCache engine. ## Provider Requirements -When using this module, ensure your AWS provider version is >= 5.73.0: +When using this module, ensure your AWS provider version is `>= 5.73.0`. -```hcl -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = ">= 5.73.0" - } - } -} -``` - -## Using Valkey (Recommended for Cost Savings) - -To use Valkey instead of Redis, configure the `redis_config` variable: +## Using Valkey ```hcl redis_config = { @@ -68,13 +158,11 @@ redis_config = { } ``` -## Using Redis (Default) - -Redis remains the default engine for backwards compatibility: +## Using Redis ```hcl redis_config = { - engine = "redis" # Default + engine = "redis" engine_version = "7.1" family = "redis7" instance_type = "cache.m5.large" @@ -83,13 +171,9 @@ redis_config = { ``` # How to improve this module -If this module somehow doesn't fit your needs, feel free to contact us by -opening a ticket, or contacting your contact at Fleet. Our goal is to make this module -fit all needs within AWS, so we will try to find a solution so that this module fits your needs. -If you want to make the changes yourself, simply make a PR into main with your additions. -We would ask that you make sure that variables are defined as null if there is -no default that makes sense and that variable changes are reflected all the way up the stack. +If this module does not fit your needs, open a ticket or contact Fleet. Variable changes should stay nullable when no sensible default exists and should be reflected all the way up the stack. # How to update this readme -Edit .header.md and run `terraform-docs markdown . > README.md` + +Edit `.header.md`, run `terraform init`, then run `terraform-docs markdown . > README.md`. diff --git a/README.md b/README.md index f560e29..fe794a0 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,123 @@ -This module provides a basic [Fleet](https://fleetdm.com) with Terraform. This assumes that you bring nothing to the installation. -If you want to bring your own VPC/database/cache nodes/ECS cluster, then use one of the submodules provided. +This module provides a basic [Fleet](https://fleetdm.com) deployment on AWS with Terraform. It is the root module for the repo and creates the VPC, database, cache, ECS cluster, ALB, and Fleet service stack. If you want to bring part of that infrastructure yourself, use one of the nested submodules instead. + +To quickly list available released module tags: -To quickly list all available module versions you can run: ```shell -git tag |grep '^tf' +git tag | grep '^tf' ``` -The following is the module layout, so you can navigate to the module that you want: +Module layout: -* Root module (use this to get a Fleet instance ASAP with minimal setup) - * BYO-VPC (use this if you want to install Fleet inside an existing VPC) - * BYO-database (use this if you want to use an existing database and cache node) - * BYO-ECS (use this if you want to bring your own everything but Fleet ECS services) +* Root module +* `byo-vpc` +* `byo-vpc/byo-db` +* `byo-vpc/byo-db/byo-ecs` + +# KMS Coverage + +This root module now exposes optional customer-managed KMS key (CMK) support for every KMS-capable surface that this stack manages directly or passes through to child modules. + +At the root module level this includes: + +* VPC flow log CloudWatch log groups +* Aurora storage encryption +* Aurora database password secret in Secrets Manager +* Aurora observability encryption for Performance Insights / CloudWatch Database Insights +* Aurora exported CloudWatch log groups +* ElastiCache at-rest encryption +* ElastiCache CloudWatch log groups for `cloudwatch-logs` delivery targets +* ECS cluster exec log groups, Fleet app log groups, Fleet private-key secret, and other nested Fleet/ECS KMS features through child-module passthroughs + +Behavior rules are consistent across features: + +* `cmk_enabled = true` means "use a customer-managed KMS key here." +* Set `cmk_enabled = false` or omit it to keep using the service-managed key. +* For KMS options that existed in published releases before this change, legacy `enabled` is deprecated but still accepted. Terraform plan/apply warns when it is used, and `cmk_enabled` takes precedence if both are set. +* If CMK use is enabled and no key ARN is provided, the module creates a CMK and alias. +* If a key ARN is provided, the module uses that key and does not create one. +* For provided keys, required IAM is managed where this repo owns an IAM principal, but external key policies must already allow the relevant AWS service to use the key. + +# Aurora Database Insights + +AWS has announced that the Performance Insights console reaches end of life on **June 30, 2026**. Aurora observability is moving toward CloudWatch Database Insights. + +This module exposes Aurora observability through `rds_config.observability`: + +* `database_insights_mode = null` means Terraform does not force Standard vs Advanced mode. +* `database_insights_mode = "standard"` explicitly keeps the cluster in Standard Database Insights mode. +* `database_insights_mode = "advanced"` enables Advanced Database Insights and therefore requires: + * `performance_insights_enabled = true` + * `monitoring_interval > 0` + * `retention_period >= 465` + +The observability CMK applies through Aurora's underlying Performance Insights encryption plane, which Database Insights builds on. + +# Aurora Backtrack + +This module also exposes Aurora MySQL backtracking through `rds_config.backtrack_window`. + +* Set it to a value between `0` and `259200` seconds. +* Set `0` to disable backtracking explicitly. +* Leave it `null` to keep the default upstream behavior. + +# Migration Notes + +* Existing environments remain unchanged unless you opt into new KMS or Database Insights settings. +* For provided CMKs, ensure the key policy already allows the relevant AWS service. +* Enabling KMS on existing CloudWatch log groups may require old log streams to be purged so all retained data is under the new key. + +Use the repository-root helper script for CloudWatch Logs KMS cleanup: + +```bash +# Dry run +DELETE_OLD_STREAMS=false ./scripts/cloudwatch_logs_kms_migration.sh + +# Delete old streams +./scripts/cloudwatch_logs_kms_migration.sh +``` + +# Example + +```hcl +module "fleet" { + source = "github.com/fleetdm/fleet-terraform?depth=1&ref=tf-mod-root-v1.23.0" + + certificate_arn = module.acm.acm_certificate_arn + + vpc = { + enable_flow_log = true + create_flow_log_cloudwatch_log_group = true + flow_log_cloudwatch_log_group_kms = { + cmk_enabled = true + } + } + + rds_config = { + storage_kms = { + cmk_enabled = true + } + password_secret_kms = { + cmk_enabled = true + } + observability = { + database_insights_mode = "standard" + kms = { + cmk_enabled = true + } + } + } + + redis_config = { + at_rest_kms = { + cmk_enabled = true + } + } +} +``` # Migrating from existing Dogfood code -The below code describes how to migrate from existing Dogfood code + +The below code describes how to migrate from existing Dogfood code. ```hcl moved { @@ -24,39 +127,26 @@ moved { moved { from = module.aurora_mysql - to = module.main.module.byo-vpc.module.rds + to = module.main.module.byo-vpc.module.rds } moved { from = aws_elasticache_replication_group.default - to = module.main.module.byo-vpc.module.redis.aws_elasticache_replication_group.default + to = module.main.module.byo-vpc.module.redis.aws_elasticache_replication_group.default } ``` -This focuses on the resources that are "heavy" or store data. Note that the ALB cannot be moved like this because Dogfood uses the `aws_alb` resource and the module uses the `aws_lb` resource. The resources are aliases of eachother, but Terraform can't recognize that. +This focuses on the resources that are "heavy" or store data. The ALB cannot be moved the same way because Dogfood uses `aws_alb` while the module uses `aws_lb`. # Cache Engine: Redis vs Valkey -This module supports both Redis and [Valkey](https://valkey.io/) as the ElastiCache engine. Valkey is an open-source, Redis-compatible in-memory data store. +This module supports both Redis and [Valkey](https://valkey.io/) as the ElastiCache engine. ## Provider Requirements -When using this module, ensure your AWS provider version is >= 5.73.0: +When using this module, ensure your AWS provider version is `>= 5.73.0`. -```hcl -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = ">= 5.73.0" - } - } -} -``` - -## Using Valkey (Recommended for Cost Savings) - -To use Valkey instead of Redis, configure the `redis_config` variable: +## Using Valkey ```hcl redis_config = { @@ -68,13 +158,11 @@ redis_config = { } ``` -## Using Redis (Default) - -Redis remains the default engine for backwards compatibility: +## Using Redis ```hcl redis_config = { - engine = "redis" # Default + engine = "redis" engine_version = "7.1" family = "redis7" instance_type = "cache.m5.large" @@ -83,26 +171,24 @@ redis_config = { ``` # How to improve this module -If this module somehow doesn't fit your needs, feel free to contact us by -opening a ticket, or contacting your contact at Fleet. Our goal is to make this module -fit all needs within AWS, so we will try to find a solution so that this module fits your needs. -If you want to make the changes yourself, simply make a PR into main with your additions. -We would ask that you make sure that variables are defined as null if there is -no default that makes sense and that variable changes are reflected all the way up the stack. +If this module does not fit your needs, open a ticket or contact Fleet. Variable changes should stay nullable when no sensible default exists and should be reflected all the way up the stack. # How to update this readme -Edit .header.md and run `terraform-docs markdown . > README.md` + +Edit `.header.md`, run `terraform init`, then run `terraform-docs markdown . > README.md`. ## Requirements | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 1.3.8 | +| [terraform](#requirement\_terraform) | >= 1.5.0 | ## Providers -No providers. +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | 6.35.1 | ## Modules @@ -113,7 +199,14 @@ No providers. ## Resources -No resources. +| Name | Type | +|------|------| +| [aws_kms_alias.vpc_flow_log_cloudwatch_log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource | +| [aws_kms_key.vpc_flow_log_cloudwatch_log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | +| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | +| [aws_iam_policy_document.vpc_flow_log_cloudwatch_log_group_kms](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 @@ -121,12 +214,13 @@ No resources. |------|-------------|------|---------|:--------:| | [alb\_config](#input\_alb\_config) | n/a |
object({
name = optional(string, "fleet")
security_groups = optional(list(string), [])
access_logs = optional(map(string), {})
allowed_cidrs = optional(list(string), ["0.0.0.0/0"])
allowed_ipv6_cidrs = optional(list(string), ["::/0"])
egress_cidrs = optional(list(string), ["0.0.0.0/0"])
egress_ipv6_cidrs = optional(list(string), ["::/0"])
fleet_target_group = optional(object({
protocol = optional(string, "HTTP")
port = optional(number, 80)
target_type = optional(string, "ip")
create_attachment = optional(bool, false)
health_check = optional(object({
path = optional(string, "/healthz")
matcher = optional(string, "200")
port = optional(string)
timeout = optional(number, 10)
interval = optional(number, 15)
healthy_threshold = optional(number, 5)
unhealthy_threshold = optional(number, 5)
}), {})
}), {})
extra_target_groups = optional(any, [])
https_listener_rules = optional(any, [])
https_overrides = optional(any, {})
xff_header_processing_mode = optional(string, null)
tls_policy = optional(string, "ELBSecurityPolicy-TLS13-1-2-2021-06")
idle_timeout = optional(number, 905)
internal = optional(bool, false)
enable_deletion_protection = optional(bool, false)
})
| `{}` | no | | [certificate\_arn](#input\_certificate\_arn) | n/a | `string` | n/a | yes | -| [ecs\_cluster](#input\_ecs\_cluster) | The config for the terraform-aws-modules/ecs/aws module |
object({
autoscaling_capacity_providers = optional(any, {})
cluster_configuration = optional(any, {
execute_command_configuration = {
logging = "OVERRIDE"
log_configuration = {
cloud_watch_log_group_name = "/aws/ecs/aws-ec2"
}
}
})
cluster_name = optional(string, "fleet")
cloudwatch_log_group = optional(object({
create = optional(bool, true)
retention_in_days = optional(number, 90)
kms = optional(object({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-ecs-cluster-logs")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
})
}), {
create = true
retention_in_days = 90
kms = {
enabled = false
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
}
})
cluster_settings = optional(any, {
"name" : "containerInsights",
"value" : "enabled",
})
create = optional(bool, true)
default_capacity_provider_use_fargate = optional(bool, true)
fargate_capacity_providers = optional(any, {
FARGATE = {
default_capacity_provider_strategy = {
weight = 100
}
}
FARGATE_SPOT = {
default_capacity_provider_strategy = {
weight = 0
}
}
})
tags = optional(map(string))
})
|
{
"autoscaling_capacity_providers": {},
"cloudwatch_log_group": {
"create": true,
"kms": {
"enabled": false,
"kms_alias": "fleet-ecs-cluster-logs",
"kms_key_arn": null
},
"retention_in_days": 90
},
"cluster_configuration": {
"execute_command_configuration": {
"log_configuration": {
"cloud_watch_log_group_name": "/aws/ecs/aws-ec2"
},
"logging": "OVERRIDE"
}
},
"cluster_name": "fleet",
"cluster_settings": {
"name": "containerInsights",
"value": "enabled"
},
"create": true,
"default_capacity_provider_use_fargate": true,
"fargate_capacity_providers": {
"FARGATE": {
"default_capacity_provider_strategy": {
"weight": 100
}
},
"FARGATE_SPOT": {
"default_capacity_provider_strategy": {
"weight": 0
}
}
},
"tags": {}
}
| 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. |
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({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-server-private-key")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-server-private-key"
})
fargate_ephemeral_storage_kms = optional(object({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-fargate-ephemeral-storage")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-fargate-ephemeral-storage"
})
server_tls_enabled = optional(bool, false)
service = optional(object({
name = optional(string, "fleet")
}), {
name = "fleet"
})
database = optional(object({
password_secret_arn = string
user = string
database = string
address = string
rr_address = optional(string, null)
}), {
password_secret_arn = null
user = null
database = null
address = null
rr_address = null
})
redis = optional(object({
address = string
use_tls = optional(bool, true)
}), {
address = null
use_tls = 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({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-application-logs")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-application-logs"
})
}), {
name = null
region = null
create = true
prefix = "fleet"
retention = 5
kms = {
enabled = false
kms_key_arn = null
kms_alias = "fleet-application-logs"
}
})
loadbalancer = optional(object({
arn = string
}), {
arn = null
})
extra_load_balancers = optional(list(any), [])
networking = optional(object({
subnets = optional(list(string), null)
security_groups = optional(list(string), null)
ingress_sources = optional(object({
cidr_blocks = optional(list(string), [])
ipv6_cidr_blocks = optional(list(string), [])
security_groups = optional(list(string), [])
prefix_list_ids = optional(list(string), [])
}), {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
})
assign_public_ip = optional(bool, false)
}), {
subnets = null
security_groups = null
ingress_sources = {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
}
assign_public_ip = 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, "")
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")
tags = optional(map(string), {})
}), {
create_bucket = true
bucket_name = null
bucket_prefix = "fleet-software-installers-"
s3_object_prefix = ""
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"
tags = {}
})
})
|
{
"autoscaling": {
"cpu_tracking_target_value": 80,
"max_capacity": 5,
"memory_tracking_target_value": 80,
"min_capacity": 1
},
"awslogs": {
"create": true,
"kms": {
"enabled": false,
"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",
"fargate_ephemeral_storage_kms": {
"enabled": false,
"kms_alias": "fleet-fargate-ephemeral-storage",
"kms_key_arn": null
},
"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": {
"enabled": false,
"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",
"security_groups": null,
"server_tls_enabled": false,
"service": {
"name": "fleet"
},
"sidecars": [],
"software_installers": {
"bucket_name": null,
"bucket_prefix": "fleet-software-installers-",
"create_bucket": true,
"create_kms_key": false,
"enable_bucket_versioning": false,
"expire_noncurrent_versions": true,
"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 | +| [ecs\_cluster](#input\_ecs\_cluster) | The config for the terraform-aws-modules/ecs/aws module. For published KMS blocks, legacy `enabled` is deprecated and still accepted; prefer `cmk_enabled`. |
object({
autoscaling_capacity_providers = optional(any, {})
cluster_configuration = optional(any, {
execute_command_configuration = {
logging = "OVERRIDE"
log_configuration = {
cloud_watch_log_group_name = "/aws/ecs/aws-ec2"
}
}
})
cluster_name = optional(string, "fleet")
cloudwatch_log_group = optional(object({
create = optional(bool, true)
retention_in_days = optional(number, 90)
kms = optional(object({
cmk_enabled = optional(bool, null)
enabled = optional(bool, null)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-ecs-cluster-logs")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
extra_kms_policies = []
})
}), {
create = true
retention_in_days = 90
kms = {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
}
})
cluster_settings = optional(any, {
"name" : "containerInsights",
"value" : "enabled",
})
create = optional(bool, true)
default_capacity_provider_use_fargate = optional(bool, true)
fargate_capacity_providers = optional(any, {
FARGATE = {
default_capacity_provider_strategy = {
weight = 100
}
}
FARGATE_SPOT = {
default_capacity_provider_strategy = {
weight = 0
}
}
})
tags = optional(map(string))
})
|
{
"autoscaling_capacity_providers": {},
"cloudwatch_log_group": {
"create": true,
"kms": {
"cmk_enabled": null,
"enabled": null,
"extra_kms_policies": [],
"kms_alias": "fleet-ecs-cluster-logs",
"kms_key_arn": null
},
"retention_in_days": 90
},
"cluster_configuration": {
"execute_command_configuration": {
"log_configuration": {
"cloud_watch_log_group_name": "/aws/ecs/aws-ec2"
},
"logging": "OVERRIDE"
}
},
"cluster_name": "fleet",
"cluster_settings": {
"name": "containerInsights",
"value": "enabled"
},
"create": true,
"default_capacity_provider_use_fargate": true,
"fargate_capacity_providers": {
"FARGATE": {
"default_capacity_provider_strategy": {
"weight": 100
}
},
"FARGATE_SPOT": {
"default_capacity_provider_strategy": {
"weight": 0
}
}
},
"tags": {}
}
| 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.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 = []
})
fargate_ephemeral_storage_kms = optional(object({
cmk_enabled = optional(bool, null)
enabled = optional(bool, null)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-fargate-ephemeral-storage")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-fargate-ephemeral-storage"
extra_kms_policies = []
})
server_tls_enabled = optional(bool, false)
service = optional(object({
name = optional(string, "fleet")
}), {
name = "fleet"
})
database = optional(object({
password_secret_arn = string
password_secret_kms_key_arn = optional(string, null)
user = string
database = string
address = string
rr_address = optional(string, null)
}), {
password_secret_arn = null
password_secret_kms_key_arn = null
user = null
database = null
address = null
rr_address = null
})
redis = optional(object({
address = string
use_tls = optional(bool, true)
}), {
address = null
use_tls = 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 = optional(object({
arn = string
}), {
arn = null
})
extra_load_balancers = optional(list(any), [])
networking = optional(object({
subnets = optional(list(string), null)
security_groups = optional(list(string), null)
ingress_sources = optional(object({
cidr_blocks = optional(list(string), [])
ipv6_cidr_blocks = optional(list(string), [])
security_groups = optional(list(string), [])
prefix_list_ids = optional(list(string), [])
}), {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
})
assign_public_ip = optional(bool, false)
}), {
subnets = null
security_groups = null
ingress_sources = {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
}
assign_public_ip = 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": 512,
"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",
"fargate_ephemeral_storage_kms": {
"cmk_enabled": null,
"enabled": null,
"extra_kms_policies": [],
"kms_alias": "fleet-fargate-ephemeral-storage",
"kms_key_arn": null
},
"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": 4096,
"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",
"security_groups": null,
"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 | -| [rds\_config](#input\_rds\_config) | The config for the terraform-aws-modules/rds-aurora/aws module |
object({
name = optional(string, "fleet")
engine_version = optional(string, "8.0.mysql_aurora.3.08.2")
instance_class = optional(string, "db.t4g.large")
subnets = optional(list(string), [])
allowed_security_groups = optional(list(string), [])
allowed_cidr_blocks = optional(list(string), [])
apply_immediately = optional(bool, true)
monitoring_interval = optional(number, 10)
db_parameter_group_name = optional(string)
db_parameters = optional(map(string), {})
db_cluster_parameter_group_name = optional(string)
db_cluster_parameters = optional(map(string), {})
enabled_cloudwatch_logs_exports = optional(list(string), [])
master_username = optional(string, "fleet")
snapshot_identifier = optional(string)
cluster_tags = optional(map(string), {})
skip_final_snapshot = optional(bool, true)
backup_retention_period = optional(number, 7)
replicas = optional(number, 2)
serverless = optional(bool, false)
serverless_min_capacity = optional(number, 2)
serverless_max_capacity = optional(number, 10)
restore_to_point_in_time = optional(map(string), {})
})
|
{
"allowed_cidr_blocks": [],
"allowed_security_groups": [],
"apply_immediately": true,
"backup_retention_period": 7,
"cluster_tags": {},
"db_cluster_parameter_group_name": null,
"db_cluster_parameters": {},
"db_parameter_group_name": null,
"db_parameters": {},
"enabled_cloudwatch_logs_exports": [],
"engine_version": "8.0.mysql_aurora.3.08.2",
"instance_class": "db.t4g.large",
"master_username": "fleet",
"monitoring_interval": 10,
"name": "fleet",
"replicas": 2,
"restore_to_point_in_time": {},
"serverless": false,
"serverless_max_capacity": 10,
"serverless_min_capacity": 2,
"skip_final_snapshot": true,
"snapshot_identifier": null,
"subnets": []
}
| no | -| [redis\_config](#input\_redis\_config) | n/a |
object({
name = optional(string, "fleet")
replication_group_id = optional(string)
elasticache_subnet_group_name = optional(string)
allowed_security_group_ids = optional(list(string), [])
subnets = optional(list(string))
availability_zones = optional(list(string))
cluster_size = optional(number, 3)
instance_type = optional(string, "cache.m5.large")
apply_immediately = optional(bool, true)
automatic_failover_enabled = optional(bool, false)
engine = optional(string, "redis")
engine_version = optional(string, "7.1")
family = optional(string, "redis7")
at_rest_encryption_enabled = optional(bool, true)
transit_encryption_enabled = optional(bool, true)
parameter = optional(list(object({
name = string
value = string
})), [])
log_delivery_configuration = optional(list(map(any)), [])
tags = optional(map(string), {})
})
|
{
"allowed_security_group_ids": [],
"apply_immediately": true,
"at_rest_encryption_enabled": true,
"automatic_failover_enabled": false,
"availability_zones": null,
"cluster_size": 3,
"elasticache_subnet_group_name": null,
"engine": "redis",
"engine_version": "7.1",
"family": "redis7",
"instance_type": "cache.m5.large",
"log_delivery_configuration": [],
"name": "fleet",
"parameter": [],
"replication_group_id": null,
"subnets": null,
"tags": {},
"transit_encryption_enabled": true
}
| no | -| [vpc](#input\_vpc) | n/a |
object({
name = optional(string, "fleet")
cidr = optional(string, "10.10.0.0/16")
azs = optional(list(string), ["us-east-2a", "us-east-2b", "us-east-2c"])
private_subnets = optional(list(string), ["10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24"])
public_subnets = optional(list(string), ["10.10.11.0/24", "10.10.12.0/24", "10.10.13.0/24"])
database_subnets = optional(list(string), ["10.10.21.0/24", "10.10.22.0/24", "10.10.23.0/24"])
elasticache_subnets = optional(list(string), ["10.10.31.0/24", "10.10.32.0/24", "10.10.33.0/24"])

create_database_subnet_group = optional(bool, false)
create_database_subnet_route_table = optional(bool, true)
create_elasticache_subnet_group = optional(bool, true)
create_elasticache_subnet_route_table = optional(bool, true)
enable_vpn_gateway = optional(bool, false)
one_nat_gateway_per_az = optional(bool, false)
single_nat_gateway = optional(bool, true)
enable_nat_gateway = optional(bool, true)
enable_dns_hostnames = optional(bool, false)
enable_dns_support = optional(bool, true)
enable_flow_log = optional(bool, false)
create_flow_log_cloudwatch_log_group = optional(bool, false)
create_flow_log_cloudwatch_iam_role = optional(bool, false)
flow_log_max_aggregation_interval = optional(number, 600)
flow_log_cloudwatch_log_group_name_prefix = optional(string, "/aws/vpc-flow-log/")
flow_log_cloudwatch_log_group_name_suffix = optional(string, "")
vpc_flow_log_tags = optional(map(string), {})
})
|
{
"azs": [
"us-east-2a",
"us-east-2b",
"us-east-2c"
],
"cidr": "10.10.0.0/16",
"create_database_subnet_group": false,
"create_database_subnet_route_table": true,
"create_elasticache_subnet_group": true,
"create_elasticache_subnet_route_table": true,
"create_flow_log_cloudwatch_iam_role": false,
"create_flow_log_cloudwatch_log_group": false,
"database_subnets": [
"10.10.21.0/24",
"10.10.22.0/24",
"10.10.23.0/24"
],
"elasticache_subnets": [
"10.10.31.0/24",
"10.10.32.0/24",
"10.10.33.0/24"
],
"enable_dns_hostnames": false,
"enable_dns_support": true,
"enable_flow_log": false,
"enable_nat_gateway": true,
"enable_vpn_gateway": false,
"flow_log_cloudwatch_log_group_name_prefix": "/aws/vpc-flow-log/",
"flow_log_cloudwatch_log_group_name_suffix": "",
"flow_log_max_aggregation_interval": 600,
"name": "fleet",
"one_nat_gateway_per_az": false,
"private_subnets": [
"10.10.1.0/24",
"10.10.2.0/24",
"10.10.3.0/24"
],
"public_subnets": [
"10.10.11.0/24",
"10.10.12.0/24",
"10.10.13.0/24"
],
"single_nat_gateway": true,
"vpc_flow_log_tags": {}
}
| no | +| [rds\_config](#input\_rds\_config) | The config for the terraform-aws-modules/rds-aurora/aws module |
object({
name = optional(string, "fleet")
engine_version = optional(string, "8.0.mysql_aurora.3.08.2")
instance_class = optional(string, "db.t4g.large")
subnets = optional(list(string), [])
allowed_security_groups = optional(list(string), [])
allowed_cidr_blocks = optional(list(string), [])
apply_immediately = optional(bool, true)
monitoring_interval = optional(number, 10)
backtrack_window = optional(number, null)
db_parameter_group_name = optional(string)
db_parameters = optional(map(string), {})
db_cluster_parameter_group_name = optional(string)
db_cluster_parameters = optional(map(string), {})
enabled_cloudwatch_logs_exports = optional(list(string), [])
final_snapshot_identifier = optional(string, null)
password_secret_kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-rds-password-secret")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-password-secret"
extra_kms_policies = []
})
storage_kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-rds-storage")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-storage"
extra_kms_policies = []
})
observability = optional(object({
performance_insights_enabled = optional(bool, true)
retention_period = optional(number, null)
database_insights_mode = optional(string, null)
kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-rds-performance-insights")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-performance-insights"
extra_kms_policies = []
})
}), {
performance_insights_enabled = true
retention_period = null
database_insights_mode = null
kms = {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-performance-insights"
extra_kms_policies = []
}
})
cloudwatch_log_group = optional(object({
retention_in_days = optional(number, null)
skip_destroy = optional(bool, false)
kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-rds-logs")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-logs"
extra_kms_policies = []
})
}), {
retention_in_days = null
skip_destroy = false
kms = {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-logs"
extra_kms_policies = []
}
})
master_username = optional(string, "fleet")
snapshot_identifier = optional(string)
cluster_tags = optional(map(string), {})
skip_final_snapshot = optional(bool, true)
backup_retention_period = optional(number, 7)
replicas = optional(number, 2)
serverless = optional(bool, false)
serverless_min_capacity = optional(number, 2)
serverless_max_capacity = optional(number, 10)
restore_to_point_in_time = optional(map(string), {})
})
|
{
"allowed_cidr_blocks": [],
"allowed_security_groups": [],
"apply_immediately": true,
"backtrack_window": null,
"backup_retention_period": 7,
"cloudwatch_log_group": {
"kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-rds-logs",
"kms_key_arn": null
},
"retention_in_days": null,
"skip_destroy": false
},
"cluster_tags": {},
"db_cluster_parameter_group_name": null,
"db_cluster_parameters": {},
"db_parameter_group_name": null,
"db_parameters": {},
"enabled_cloudwatch_logs_exports": [],
"engine_version": "8.0.mysql_aurora.3.08.2",
"final_snapshot_identifier": null,
"instance_class": "db.t4g.large",
"master_username": "fleet",
"monitoring_interval": 10,
"name": "fleet",
"observability": {
"database_insights_mode": null,
"kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-rds-performance-insights",
"kms_key_arn": null
},
"performance_insights_enabled": true,
"retention_period": null
},
"password_secret_kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-rds-password-secret",
"kms_key_arn": null
},
"replicas": 2,
"restore_to_point_in_time": {},
"serverless": false,
"serverless_max_capacity": 10,
"serverless_min_capacity": 2,
"skip_final_snapshot": true,
"snapshot_identifier": null,
"storage_kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-rds-storage",
"kms_key_arn": null
},
"subnets": []
}
| no | +| [redis\_config](#input\_redis\_config) | n/a |
object({
name = optional(string, "fleet")
replication_group_id = optional(string)
elasticache_subnet_group_name = optional(string)
allowed_security_group_ids = optional(list(string), [])
subnets = optional(list(string))
availability_zones = optional(list(string))
cluster_size = optional(number, 3)
instance_type = optional(string, "cache.m5.large")
apply_immediately = optional(bool, true)
automatic_failover_enabled = optional(bool, false)
engine = optional(string, "redis")
engine_version = optional(string, "7.1")
family = optional(string, "redis7")
at_rest_encryption_enabled = optional(bool, true)
at_rest_kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-redis-at-rest")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-redis-at-rest"
extra_kms_policies = []
})
transit_encryption_enabled = optional(bool, true)
parameter = optional(list(object({
name = string
value = string
})), [])
cloudwatch_log_group = optional(object({
retention_in_days = optional(number, null)
skip_destroy = optional(bool, false)
kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-redis-logs")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-redis-logs"
extra_kms_policies = []
})
}), {
retention_in_days = null
skip_destroy = false
kms = {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-redis-logs"
}
})
log_delivery_configuration = optional(list(map(any)), [])
tags = optional(map(string), {})
})
|
{
"allowed_security_group_ids": [],
"apply_immediately": true,
"at_rest_encryption_enabled": true,
"at_rest_kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-redis-at-rest",
"kms_key_arn": null
},
"automatic_failover_enabled": false,
"availability_zones": null,
"cloudwatch_log_group": {
"kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-redis-logs",
"kms_key_arn": null
},
"retention_in_days": null,
"skip_destroy": false
},
"cluster_size": 3,
"elasticache_subnet_group_name": null,
"engine": "redis",
"engine_version": "7.1",
"family": "redis7",
"instance_type": "cache.m5.large",
"log_delivery_configuration": [],
"name": "fleet",
"parameter": [],
"replication_group_id": null,
"subnets": null,
"tags": {},
"transit_encryption_enabled": true
}
| no | +| [vpc](#input\_vpc) | n/a |
object({
name = optional(string, "fleet")
cidr = optional(string, "10.10.0.0/16")
azs = optional(list(string), ["us-east-2a", "us-east-2b", "us-east-2c"])
private_subnets = optional(list(string), ["10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24"])
public_subnets = optional(list(string), ["10.10.11.0/24", "10.10.12.0/24", "10.10.13.0/24"])
database_subnets = optional(list(string), ["10.10.21.0/24", "10.10.22.0/24", "10.10.23.0/24"])
elasticache_subnets = optional(list(string), ["10.10.31.0/24", "10.10.32.0/24", "10.10.33.0/24"])

create_database_subnet_group = optional(bool, false)
create_database_subnet_route_table = optional(bool, true)
create_elasticache_subnet_group = optional(bool, true)
create_elasticache_subnet_route_table = optional(bool, true)
enable_vpn_gateway = optional(bool, false)
one_nat_gateway_per_az = optional(bool, false)
single_nat_gateway = optional(bool, true)
enable_nat_gateway = optional(bool, true)
enable_dns_hostnames = optional(bool, false)
enable_dns_support = optional(bool, true)
enable_flow_log = optional(bool, false)
create_flow_log_cloudwatch_log_group = optional(bool, false)
create_flow_log_cloudwatch_iam_role = optional(bool, false)
flow_log_max_aggregation_interval = optional(number, 600)
flow_log_cloudwatch_log_group_name_prefix = optional(string, "/aws/vpc-flow-log/")
flow_log_cloudwatch_log_group_name_suffix = optional(string, "")
flow_log_cloudwatch_log_group_kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-vpc-flow-logs")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-vpc-flow-logs"
extra_kms_policies = []
})
vpc_flow_log_tags = optional(map(string), {})
})
|
{
"azs": [
"us-east-2a",
"us-east-2b",
"us-east-2c"
],
"cidr": "10.10.0.0/16",
"create_database_subnet_group": false,
"create_database_subnet_route_table": true,
"create_elasticache_subnet_group": true,
"create_elasticache_subnet_route_table": true,
"create_flow_log_cloudwatch_iam_role": false,
"create_flow_log_cloudwatch_log_group": false,
"database_subnets": [
"10.10.21.0/24",
"10.10.22.0/24",
"10.10.23.0/24"
],
"elasticache_subnets": [
"10.10.31.0/24",
"10.10.32.0/24",
"10.10.33.0/24"
],
"enable_dns_hostnames": false,
"enable_dns_support": true,
"enable_flow_log": false,
"enable_nat_gateway": true,
"enable_vpn_gateway": false,
"flow_log_cloudwatch_log_group_kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-vpc-flow-logs",
"kms_key_arn": null
},
"flow_log_cloudwatch_log_group_name_prefix": "/aws/vpc-flow-log/",
"flow_log_cloudwatch_log_group_name_suffix": "",
"flow_log_max_aggregation_interval": 600,
"name": "fleet",
"one_nat_gateway_per_az": false,
"private_subnets": [
"10.10.1.0/24",
"10.10.2.0/24",
"10.10.3.0/24"
],
"public_subnets": [
"10.10.11.0/24",
"10.10.12.0/24",
"10.10.13.0/24"
],
"single_nat_gateway": true,
"vpc_flow_log_tags": {}
}
| no | ## Outputs diff --git a/addons/cloudfront-software-installers/.header.md b/addons/cloudfront-software-installers/.header.md index 4ae3474..284959b 100644 --- a/addons/cloudfront-software-installers/.header.md +++ b/addons/cloudfront-software-installers/.header.md @@ -26,20 +26,23 @@ Previous versions do not allow for proper interaction with both the software ins ### tf-mod-root/tf-mod-byo-vpc/tf-mod-byo-db/tf-mod-byo-ecs -For any of these modules, the software installers configuration will require a KMS key created in order to be able to set a key policy. +For any of these modules, software installers requires a customer-managed KMS key whenever KMS encryption is used at all. CloudFront access requires a key policy statement on that key, and the AWS-managed default key cannot be modified to accept that policy. + +Breaking change: this addon no longer manages the software installers KMS key policy. That policy now lives in `tf-mod-byo-ecs`, which means CloudFront access to the CMK must be configured there by setting `software_installers.cloudfront_distribution_arn` to the static distribution ARN. This is the relevant configuration starting at the software installers configuration block: ``` software_installers = { - bucket_prefix = "fleet-software-installers-" - create_kms_key = true - kms_alias = "fleet-software-installers" + bucket_prefix = "fleet-software-installers-" + create_kms_key = true + kms_alias = "fleet-software-installers" + cloudfront_distribution_arn = "arn:aws:cloudfront:::distribution/" } ``` -The new configuration items are `create_kms_key` and `kms_alias`. +The required configuration items here are `create_kms_key`, `kms_alias`, and `cloudfront_distribution_arn` when CloudFront must read from an SSE-KMS bucket. ### tf-mod-addon-logging-alb @@ -117,7 +120,6 @@ module "cloudfront-software-installers" { source = "github.com/fleetdm/fleet-terraform/addons/cloudfront-software-installers?ref=tf-mod-addon-cloudfront-software-installers-v1.2.0" customer = "fleet" s3_bucket = module.main.byo-vpc.byo-db.byo-ecs.fleet_s3_software_installers_config.bucket_name - s3_kms_key_id = module.main.byo-vpc.byo-db.byo-ecs.fleet_s3_software_installers_config.kms_key_id # OPTIONAL # If you'd like to use an existing key_group_id for your new Cloudfront distribution, uncomment key_group_id and supply the value for the key_group_id # If you're using an existing key_group_id, public_key_id is required. The public_key_id is the value of a public_key that is also part of the key group that you define. @@ -145,6 +147,8 @@ data "aws_kms_secrets" "cloudfront" { Then we need to include outputs from this module once applied back into the main fleet-config under the `extra_secrets` and `extra_execution_roles`: +This addon also outputs `cloudfront_distribution_arn`. Do not feed that output back into `tf-mod-byo-ecs`, because that creates a Terraform cycle. Instead, set the same static ARN directly in `software_installers.cloudfront_distribution_arn`. + Under the `fleet_config` section. If not using the mdm module, that could be omitted but was included to show how to include multiple extra items: diff --git a/addons/cloudfront-software-installers/README.md b/addons/cloudfront-software-installers/README.md index 1cc767e..ada3dd3 100644 --- a/addons/cloudfront-software-installers/README.md +++ b/addons/cloudfront-software-installers/README.md @@ -25,20 +25,23 @@ Previous versions do not allow for proper interaction with both the software ins ### tf-mod-root/tf-mod-byo-vpc/tf-mod-byo-db/tf-mod-byo-ecs -For any of these modules, the software installers configuration will require a KMS key created in order to be able to set a key policy. +For any of these modules, software installers requires a customer-managed KMS key whenever KMS encryption is used at all. CloudFront access requires a key policy statement on that key, and the AWS-managed default key cannot be modified to accept that policy. + +Breaking change: this addon no longer manages the software installers KMS key policy. That policy now lives in `tf-mod-byo-ecs`, which means CloudFront access to the CMK must be configured there by setting `software_installers.cloudfront_distribution_arn` to the static distribution ARN. This is the relevant configuration starting at the software installers configuration block: ``` software_installers = { - bucket_prefix = "fleet-software-installers-" - create_kms_key = true - kms_alias = "fleet-software-installers" + bucket_prefix = "fleet-software-installers-" + create_kms_key = true + kms_alias = "fleet-software-installers" + cloudfront_distribution_arn = "arn:aws:cloudfront:::distribution/" } ``` -The new configuration items are `create_kms_key` and `kms_alias`. +The required configuration items here are `create_kms_key`, `kms_alias`, and `cloudfront_distribution_arn` when CloudFront must read from an SSE-KMS bucket. ### tf-mod-addon-logging-alb @@ -116,7 +119,6 @@ module "cloudfront-software-installers" { source = "github.com/fleetdm/fleet-terraform/addons/cloudfront-software-installers?ref=tf-mod-addon-cloudfront-software-installers-v1.2.0" customer = "fleet" s3_bucket = module.main.byo-vpc.byo-db.byo-ecs.fleet_s3_software_installers_config.bucket_name - s3_kms_key_id = module.main.byo-vpc.byo-db.byo-ecs.fleet_s3_software_installers_config.kms_key_id # OPTIONAL # If you'd like to use an existing key_group_id for your new Cloudfront distribution, uncomment key_group_id and supply the value for the key_group_id # If you're using an existing key_group_id, public_key_id is required. The public_key_id is the value of a public_key that is also part of the key group that you define. @@ -144,6 +146,8 @@ data "aws_kms_secrets" "cloudfront" { Then we need to include outputs from this module once applied back into the main fleet-config under the `extra_secrets` and `extra_execution_roles`: +This addon also outputs `cloudfront_distribution_arn`. Do not feed that output back into `tf-mod-byo-ecs`, because that creates a Terraform cycle. Instead, set the same static ARN directly in `software_installers.cloudfront_distribution_arn`. + Under the `fleet_config` section. If not using the mdm module, that could be omitted but was included to show how to include multiple extra items: ``` @@ -169,7 +173,7 @@ No requirements. | Name | Version | |------|---------| -| [aws](#provider\_aws) | n/a | +| [aws](#provider\_aws) | 6.36.0 | ## Modules @@ -184,13 +188,10 @@ No requirements. | [aws_cloudfront_key_group.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_key_group) | resource | | [aws_cloudfront_public_key.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_public_key) | resource | | [aws_iam_policy.software_installers_secret](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource | -| [aws_kms_key_policy.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key_policy) | resource | | [aws_s3_bucket_policy.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource | | [aws_secretsmanager_secret.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret) | resource | | [aws_secretsmanager_secret_version.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret_version) | resource | -| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | | [aws_iam_policy_document.software_installers_bucket](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_iam_policy_document.software_installers_secret](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | | [aws_s3_bucket.logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/s3_bucket) | data source | | [aws_s3_bucket.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/s3_bucket) | data source | @@ -208,12 +209,12 @@ No requirements. | [public\_key](#input\_public\_key) | Public key used for signed URLs | `string` | n/a | yes | | [public\_key\_id](#input\_public\_key\_id) | Cloudfront public key id. Required when passing in a key\_group\_id | `string` | `null` | no | | [s3\_bucket](#input\_s3\_bucket) | Name of the S3 bucket that Cloudfront will point to | `string` | n/a | yes | -| [s3\_kms\_key\_id](#input\_s3\_kms\_key\_id) | KMS key id used to encrypt the s3 bucket | `string` | `null` | no | ## Outputs | Name | Description | |------|-------------| | [cloudfront\_arn](#output\_cloudfront\_arn) | n/a | +| [cloudfront\_distribution\_arn](#output\_cloudfront\_distribution\_arn) | n/a | | [extra\_execution\_iam\_policies](#output\_extra\_execution\_iam\_policies) | n/a | | [extra\_secrets](#output\_extra\_secrets) | n/a | diff --git a/addons/cloudfront-software-installers/main.tf b/addons/cloudfront-software-installers/main.tf index afb5ce8..666a1cb 100644 --- a/addons/cloudfront-software-installers/main.tf +++ b/addons/cloudfront-software-installers/main.tf @@ -1,5 +1,3 @@ -data "aws_caller_identity" "current" {} - data "aws_iam_policy_document" "software_installers_bucket" { statement { actions = ["s3:GetObject"] @@ -26,38 +24,6 @@ resource "aws_s3_bucket_policy" "software_installers" { policy = data.aws_iam_policy_document.software_installers_bucket.json } -data "aws_iam_policy_document" "software_installers_kms" { - statement { - actions = ["kms:*"] - principals { - type = "AWS" - identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"] - } - resources = ["*"] - } - statement { - sid = "AllowOriginAccessIdentity" - effect = "Allow" - principals { - type = "Service" - identifiers = ["cloudfront.amazonaws.com"] - } - actions = ["kms:Decrypt"] - resources = ["*"] - condition { - test = "StringEquals" - variable = "AWS:SourceArn" - values = [module.cloudfront_software_installers.cloudfront_distribution_arn] - } - } -} - -resource "aws_kms_key_policy" "software_installers" { - count = var.s3_kms_key_id != null ? 1 : 0 - key_id = var.s3_kms_key_id - policy = data.aws_iam_policy_document.software_installers_kms.json -} - data "aws_iam_policy_document" "software_installers_secret" { statement { effect = "Allow" diff --git a/addons/cloudfront-software-installers/outputs.tf b/addons/cloudfront-software-installers/outputs.tf index ac50599..48f53c4 100644 --- a/addons/cloudfront-software-installers/outputs.tf +++ b/addons/cloudfront-software-installers/outputs.tf @@ -13,3 +13,7 @@ output "extra_execution_iam_policies" { output "cloudfront_arn" { value = module.cloudfront_software_installers.cloudfront_distribution_arn } + +output "cloudfront_distribution_arn" { + value = module.cloudfront_software_installers.cloudfront_distribution_arn +} diff --git a/addons/cloudfront-software-installers/variables.tf b/addons/cloudfront-software-installers/variables.tf index 055c41b..b1a51ac 100644 --- a/addons/cloudfront-software-installers/variables.tf +++ b/addons/cloudfront-software-installers/variables.tf @@ -41,12 +41,6 @@ variable "s3_bucket" { type = string } -variable "s3_kms_key_id" { - description = "KMS key id used to encrypt the s3 bucket" - type = string - default = null -} - variable "enable_logging" { description = "Enable optional logging to s3" type = bool diff --git a/addons/logging-alb/main.tf b/addons/logging-alb/main.tf index 218d82a..dda2f57 100644 --- a/addons/logging-alb/main.tf +++ b/addons/logging-alb/main.tf @@ -1,59 +1,63 @@ data "aws_caller_identity" "current" {} +data "aws_partition" "current" {} + data "aws_region" "current" {} locals { landing_bucket_name = "${var.prefix}-alb-logs" - kms_policies = concat([ + kms_base_policy_statements = var.kms_base_policy != null ? var.kms_base_policy : [ { - actions = ["kms:*"] - principals = [{ + sid = "EnableRootPermissions" + effect = "Allow" + actions = ["kms:*"] + resources = ["*"] + principals = { type = "AWS" - identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"] - }] - resources = ["*"] - }, + identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"] + } + conditions = [] + } + ] + + kms_service_statements = [ { - actions = [ - "kms:Encrypt", - "kms:Decrypt", - "kms:GenerateDataKey*", - "kms:DescribeKey", - ] + sid = "AllowLambdaReencryptUseOfTheKey" + effect = "Allow" + actions = ["kms:Encrypt", "kms:Decrypt", "kms:GenerateDataKey*", "kms:DescribeKey"] resources = ["*"] - principals = [{ + principals = { type = "AWS" identifiers = [aws_iam_role.lambda_reencrypt.arn] - }] + } + conditions = [] }, { - actions = [ - "kms:Encrypt", - "kms:Decrypt", - "kms:GenerateDataKey*", - "kms:DescribeKey", - ] + sid = "AllowLambdaSweepUseOfTheKey" + effect = "Allow" + actions = ["kms:Encrypt", "kms:Decrypt", "kms:GenerateDataKey*", "kms:DescribeKey"] resources = ["*"] - principals = [{ + principals = { type = "AWS" identifiers = [aws_iam_role.lambda_sweep.arn] - }] + } + conditions = [] }, { - actions = [ - "kms:Encrypt", - "kms:GenerateDataKey", - "kms:GenerateDataKeyWithoutPlaintext", - "kms:DescribeKey", - ] + sid = "AllowBatchReencryptUseOfTheKey" + effect = "Allow" + actions = ["kms:Encrypt", "kms:GenerateDataKey", "kms:GenerateDataKeyWithoutPlaintext", "kms:DescribeKey"] resources = ["*"] - principals = [{ + principals = { type = "AWS" identifiers = [aws_iam_role.batch_reencrypt.arn] - }] - }, - ], var.extra_kms_policies) + } + conditions = [] + } + ] + + 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) } @@ -68,7 +72,7 @@ data "aws_iam_policy_document" "kms" { resources = try(statement.value.resources, []) effect = try(statement.value.effect, null) dynamic "principals" { - for_each = try(statement.value.principals, []) + for_each = try([statement.value.principals], []) content { type = principals.value.type identifiers = principals.value.identifiers diff --git a/addons/logging-alb/variables.tf b/addons/logging-alb/variables.tf index 48c1b75..5ca557c 100644 --- a/addons/logging-alb/variables.tf +++ b/addons/logging-alb/variables.tf @@ -45,6 +45,26 @@ variable "extra_kms_policies" { default = [] } +variable "kms_base_policy" { + type = 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) + })), []) + })) + default = null + description = "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." +} + variable "extra_s3_log_policies" { type = list(any) default = [] diff --git a/addons/monitoring/.header.md b/addons/monitoring/.header.md index ec24d94..e37a361 100644 --- a/addons/monitoring/.header.md +++ b/addons/monitoring/.header.md @@ -29,6 +29,10 @@ https://github.com/fleetdm/fleet-terraform/blob/main/example/main.tf for details > Note: If you haven't specified defined `local.customer` or customized service names, the default is "fleet" for anywhere that `local.customer` is specified below. +If the Fleet database password secret is encrypted with a CMK, also pass `mysql_password_secret_kms_key_arn` so the cron-monitoring Lambda can decrypt it. When using the `byo-vpc` module, wire this from `module..byo-vpc.rds_password_secret_kms_key_arn`. + +**Important:** If you supply a custom `kms_base_policy` that does not grant `kms:*` to the account root (for example, a least-privilege policy that only allows specific principals), you must also add the cron-monitoring Lambda role to `rds_config.password_secret_kms.extra_kms_policies` in the `byo-vpc` module. AWS KMS requires both the key policy and the IAM policy to allow access. The Lambda role name is predictable (`-cron-monitoring-lambda`), so you can construct the ARN before the role exists. See the `byo-vpc` module README for a full example. + ``` module "monitoring" { source = "github.com/fleetdm/fleet-terraform//addons/monitoring?ref=tf-mod-addon-monitoring-v1.9.0" @@ -69,6 +73,7 @@ module "monitoring" { mysql_database = module.fleet.byo-vpc.rds.cluster_database_name mysql_user = module.fleet.byo-vpc.rds.cluster_master_username mysql_password_secret_name = "${local.customer}-database-password" + mysql_password_secret_kms_key_arn = module.fleet.byo-vpc.rds_password_secret_kms_key_arn mysql_tls_config = "true" rds_security_group_id = module.fleet.byo-vpc.rds.security_group_id subnet_ids = module.fleet.vpc.private_subnets diff --git a/addons/monitoring/README.md b/addons/monitoring/README.md index 076ea05..d93f345 100644 --- a/addons/monitoring/README.md +++ b/addons/monitoring/README.md @@ -29,6 +29,10 @@ https://github.com/fleetdm/fleet-terraform/blob/main/example/main.tf for details > Note: If you haven't specified defined `local.customer` or customized service names, the default is "fleet" for anywhere that `local.customer` is specified below. +If the Fleet database password secret is encrypted with a CMK, also pass `mysql_password_secret_kms_key_arn` so the cron-monitoring Lambda can decrypt it. When using the `byo-vpc` module, wire this from `module..byo-vpc.rds_password_secret_kms_key_arn`. + +**Important:** If you supply a custom `kms_base_policy` that does not grant `kms:*` to the account root (for example, a least-privilege policy that only allows specific principals), you must also add the cron-monitoring Lambda role to `rds_config.password_secret_kms.extra_kms_policies` in the `byo-vpc` module. AWS KMS requires both the key policy and the IAM policy to allow access. The Lambda role name is predictable (`-cron-monitoring-lambda`), so you can construct the ARN before the role exists. See the `byo-vpc` module README for a full example. + ``` module "monitoring" { source = "github.com/fleetdm/fleet-terraform//addons/monitoring?ref=tf-mod-addon-monitoring-v1.9.0" @@ -69,6 +73,7 @@ module "monitoring" { mysql_database = module.fleet.byo-vpc.rds.cluster_database_name mysql_user = module.fleet.byo-vpc.rds.cluster_master_username mysql_password_secret_name = "${local.customer}-database-password" + mysql_password_secret_kms_key_arn = module.fleet.byo-vpc.rds_password_secret_kms_key_arn mysql_tls_config = "true" rds_security_group_id = module.fleet.byo-vpc.rds.security_group_id subnet_ids = module.fleet.vpc.private_subnets @@ -158,7 +163,7 @@ No requirements. | Name | Version | |------|---------| | [archive](#provider\_archive) | 2.7.1 | -| [aws](#provider\_aws) | 6.33.0 | +| [aws](#provider\_aws) | 6.20.0 | | [null](#provider\_null) | 3.2.4 | ## Modules @@ -207,7 +212,7 @@ No modules. |------|-------------|------|---------|:--------:| | [acm\_certificate\_arn](#input\_acm\_certificate\_arn) | n/a | `string` | `null` | no | | [albs](#input\_albs) | n/a |
list(object({
name = string
arn_suffix = string
target_group_name = string
target_group_arn_suffix = string
min_containers = optional(string, 1)
ecs_service_name = string
alert_thresholds = optional(
object({
HTTPCode_ELB_5XX_Count = object({
period = number
threshold = number
})
HTTPCode_Target_5XX_Count = object({
period = number
threshold = number
})
}),
{
HTTPCode_ELB_5XX_Count = {
period = 120
threshold = 0
},
HTTPCode_Target_5XX_Count = {
period = 120
threshold = 0
}
}
)
}))
| `[]` | no | -| [cron\_monitoring](#input\_cron\_monitoring) | n/a |
object({
mysql_host = string
mysql_database = string
mysql_user = string
mysql_password_secret_name = string
mysql_tls_config = optional(string, "true")
vpc_id = string
subnet_ids = list(string)
rds_security_group_id = string
delay_tolerance = string
run_interval = string
log_retention_in_days = optional(number, 7)
ignore_list = optional(list(string), [])
})
| `null` | no | +| [cron\_monitoring](#input\_cron\_monitoring) | n/a |
object({
mysql_host = string
mysql_database = string
mysql_user = string
mysql_password_secret_name = string
mysql_password_secret_kms_key_arn = optional(string, null)
mysql_tls_config = optional(string, "true")
vpc_id = string
subnet_ids = list(string)
rds_security_group_id = string
delay_tolerance = string
run_interval = string
log_retention_in_days = optional(number, 7)
ignore_list = optional(list(string), [])
})
| `null` | no | | [customer\_prefix](#input\_customer\_prefix) | n/a | `string` | `"fleet"` | no | | [default\_sns\_topic\_arns](#input\_default\_sns\_topic\_arns) | n/a | `list(string)` | `[]` | no | | [fleet\_ecs\_service\_name](#input\_fleet\_ecs\_service\_name) | n/a | `string` | `null` | no | @@ -218,4 +223,7 @@ No modules. ## Outputs -No outputs. +| Name | Description | +|------|-------------| +| [cron\_monitoring\_lambda\_arn](#output\_cron\_monitoring\_lambda\_arn) | n/a | +| [cron\_monitoring\_lambda\_role\_arn](#output\_cron\_monitoring\_lambda\_role\_arn) | n/a | diff --git a/addons/monitoring/main.tf b/addons/monitoring/main.tf index 1a52676..b8dbfec 100644 --- a/addons/monitoring/main.tf +++ b/addons/monitoring/main.tf @@ -403,17 +403,17 @@ resource "aws_lambda_function" "cron_monitoring" { environment { variables = { - MYSQL_HOST = var.cron_monitoring.mysql_host - MYSQL_DATABASE = var.cron_monitoring.mysql_database - MYSQL_USER = var.cron_monitoring.mysql_user - MYSQL_SECRETSMANAGER_SECRET = data.aws_secretsmanager_secret.mysql_database_password[0].name - MYSQL_TLS_CONFIG = var.cron_monitoring.mysql_tls_config - CRON_SYSTEM_MONITOR_SNS_TOPIC_ARNS = join(",", lookup(var.sns_topic_arns_map, "cron_monitoring", var.default_sns_topic_arns)) - CRON_JOB_FAILURE_MONITOR_SNS_TOPIC_ARNS = join(",", lookup(var.sns_topic_arns_map, "cron_job_failure_monitoring", var.default_sns_topic_arns)) - FLEET_ENV = var.customer_prefix - CRON_DELAY_TOLERANCE = var.cron_monitoring.delay_tolerance - CRON_MONITOR_RUN_INTERVAL = var.cron_monitoring.run_interval - CRON_IGNORE_LIST = join(",", var.cron_monitoring.ignore_list) + MYSQL_HOST = var.cron_monitoring.mysql_host + MYSQL_DATABASE = var.cron_monitoring.mysql_database + MYSQL_USER = var.cron_monitoring.mysql_user + MYSQL_SECRETSMANAGER_SECRET = data.aws_secretsmanager_secret.mysql_database_password[0].name + MYSQL_TLS_CONFIG = var.cron_monitoring.mysql_tls_config + CRON_SYSTEM_MONITOR_SNS_TOPIC_ARNS = join(",", lookup(var.sns_topic_arns_map, "cron_monitoring", var.default_sns_topic_arns)) + CRON_JOB_FAILURE_MONITOR_SNS_TOPIC_ARNS = join(",", lookup(var.sns_topic_arns_map, "cron_job_failure_monitoring", var.default_sns_topic_arns)) + FLEET_ENV = var.customer_prefix + CRON_DELAY_TOLERANCE = var.cron_monitoring.delay_tolerance + CRON_MONITOR_RUN_INTERVAL = var.cron_monitoring.run_interval + CRON_IGNORE_LIST = join(",", var.cron_monitoring.ignore_list) } } @@ -474,6 +474,22 @@ data "aws_iam_policy_document" "cron_monitoring_lambda" { } + dynamic "statement" { + for_each = var.cron_monitoring != null && try(var.cron_monitoring.mysql_password_secret_kms_key_arn, null) != null ? [var.cron_monitoring.mysql_password_secret_kms_key_arn] : [] + content { + sid = "KMSDecryptMysqlPasswordSecret" + + actions = [ + "kms:Decrypt", + "kms:DescribeKey" + ] + + resources = [statement.value] + + effect = "Allow" + } + } + statement { sid = "SNSPublish" diff --git a/addons/monitoring/outputs.tf b/addons/monitoring/outputs.tf new file mode 100644 index 0000000..5a48a6d --- /dev/null +++ b/addons/monitoring/outputs.tf @@ -0,0 +1,7 @@ +output "cron_monitoring_lambda_arn" { + value = try(aws_lambda_function.cron_monitoring[0].arn, null) +} + +output "cron_monitoring_lambda_role_arn" { + value = try(aws_iam_role.cron_monitoring_lambda[0].arn, null) +} diff --git a/addons/monitoring/variables.tf b/addons/monitoring/variables.tf index 2e0e494..ebd2031 100644 --- a/addons/monitoring/variables.tf +++ b/addons/monitoring/variables.tf @@ -86,6 +86,7 @@ variable "cron_monitoring" { mysql_database = string mysql_user = string mysql_password_secret_name = string + mysql_password_secret_kms_key_arn = optional(string, null) mysql_tls_config = optional(string, "true") vpc_id = string subnet_ids = list(string) diff --git a/byo-vpc/.header.md b/byo-vpc/.header.md new file mode 100644 index 0000000..ba02392 --- /dev/null +++ b/byo-vpc/.header.md @@ -0,0 +1,313 @@ +This module deploys Fleet into an existing VPC while still provisioning the supporting AWS services that Fleet needs, including Aurora, ElastiCache, ECS, and the ALB. + +# KMS Coverage + +This module adds optional CMK support for: + +* Aurora storage encryption +* Aurora database password secret encryption in Secrets Manager +* Aurora observability encryption for Performance Insights / Database Insights +* Aurora exported CloudWatch log groups +* ElastiCache at-rest encryption +* ElastiCache CloudWatch log groups for `cloudwatch-logs` delivery targets +* Nested ECS cluster, Fleet service, application logs, and Fleet secrets through child-module passthroughs + +For each feature: + +* `cmk_enabled = true` means "use a customer-managed KMS key here." +* Set `cmk_enabled = false` or omit it to keep using the service-managed key. +* For KMS options that existed in published releases before this change, legacy `enabled` is deprecated but still accepted. Terraform plan/apply warns when it is used, and `cmk_enabled` takes precedence if both are set. +* Set `cmk_enabled = true` without a key ARN to have the module create a CMK and alias. +* Set `kms_key_arn` to use an existing CMK. + +Provided CMKs must already allow the relevant AWS service in their key policy. + +# Aurora Database Insights + +`rds_config.observability` manages Aurora observability behavior: + +* `database_insights_mode = null` leaves Standard vs Advanced unmanaged. +* `database_insights_mode = "standard"` enforces Standard Database Insights. +* `database_insights_mode = "advanced"` requires: + * `performance_insights_enabled = true` + * `monitoring_interval > 0` + * `retention_period >= 465` + +AWS has announced the Performance Insights console end of life for **June 30, 2026**. Standard Database Insights is a clean forward path for existing clusters that are already using Standard outside Terraform. + +# Aurora Backtrack + +`rds_config.backtrack_window` is optional and passes Aurora MySQL backtracking through to the upstream `rds-aurora` module. + +* Set it to a value between `0` and `259200` seconds. +* Set `0` to disable backtracking explicitly. +* Leave it `null` to keep the default upstream behavior. + +# Aurora Final Snapshot Naming + +`rds_config.final_snapshot_identifier` is optional. + +* Set it explicitly if you want a fixed final snapshot name. +* Leave it `null` to preserve the legacy generated naming pattern: `final--<8-digit-hex>`. + +# Monitoring Addon Secret KMS Wiring + +If you enable `rds_config.password_secret_kms`, the `byo-vpc` module also exposes `rds_password_secret_kms_key_arn`. + +That output is intended to be passed through to the monitoring addon so the cron-monitoring Lambda can decrypt the Fleet database password secret when it is encrypted with a CMK. + +When the module creates the password secret CMK, the key policy automatically grants +`kms:Decrypt` and `kms:DescribeKey` to the Fleet ECS execution role. However, the +cron-monitoring Lambda role is created by the **monitoring addon**, which is applied +after the `byo-vpc` module. If you supply a custom `kms_base_policy` that does not +grant `kms:*` to the account root (for example, a least-privilege policy that only +allows specific principals), you must also add the Lambda role to +`rds_config.password_secret_kms.extra_kms_policies` so the key policy permits the +Lambda to decrypt. Because the Lambda role name is predictable, you can construct the +ARN before the role exists. + +Example: + +```hcl +locals { + cron_monitoring_lambda_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${local.customer}-cron-monitoring-lambda" +} + +module "fleet_byo_vpc" { + source = "github.com/fleetdm/fleet-terraform//byo-vpc?depth=1&ref=tf-mod-byo-vpc-v1.24.0" + + kms_base_policy = local.kms_base_policy_statements # your restrictive policy + + rds_config = { + # ... + password_secret_kms = { + cmk_enabled = true + extra_kms_policies = [ + { + sid = "AllowCronMonitoringLambdaDecrypt" + effect = "Allow" + principals = { + type = "AWS" + identifiers = [local.cron_monitoring_lambda_role_arn] + } + actions = ["kms:Decrypt", "kms:DescribeKey"] + resources = ["*"] + conditions = [] + } + ] + } + } + + # ... +} + +module "monitoring" { + source = "github.com/fleetdm/fleet-terraform//addons/monitoring?ref=tf-mod-addon-monitoring-v1.9.0" + + # ... + cron_monitoring = { + # ... + mysql_password_secret_name = "${local.customer}-database-password" + mysql_password_secret_kms_key_arn = module.fleet_byo_vpc.rds_password_secret_kms_key_arn + } +} +``` + +If your `kms_base_policy` grants `kms:*` to the account root (the default), the +Lambda's IAM policy alone is sufficient and `extra_kms_policies` is not needed. + +# Example + +```hcl +module "fleet_byo_vpc" { + source = "github.com/fleetdm/fleet-terraform//byo-vpc?depth=1&ref=tf-mod-byo-vpc-v1.24.0" + + vpc_config = { + vpc_id = "vpc-1234567890abcdef0" + networking = { + subnets = ["subnet-aaa", "subnet-bbb"] + } + } + + rds_config = { + observability = { + database_insights_mode = "standard" + kms = { + cmk_enabled = true + } + } + storage_kms = { + cmk_enabled = true + } + password_secret_kms = { + cmk_enabled = true + } + } + + redis_config = { + at_rest_kms = { + cmk_enabled = true + } + } +} +``` + +# Migration Notes + +* Existing deployments are unchanged until you enable new KMS settings. +* Upgrading from `tf-mod-byo-vpc-v1.24.0` to any newer version may plan an in-place Aurora cluster update setting cluster-level `performance_insights_enabled = true`. Older versions already enabled Performance Insights at the instance level; newer versions also manage it at the cluster level to align with Aurora Database Insights support. +* Aurora storage and some observability changes may still be sensitive changes; review your maintenance and restore strategy before applying them in production. +* If your goal is routine key rotation, prefer AWS KMS automatic rotation on the existing CMK when possible. Switching a resource to a different CMK is a separate migration with service-specific behavior. + +## KMS Migration Guidance + +### Aurora storage encryption (`rds_config.storage_kms`) + +Changing the Aurora storage CMK is **not** an in-place re-key. + +AWS documents that you can't change the KMS key of an existing encrypted Aurora DB cluster in place. To move to a different CMK, treat the change as a backup-and-restore migration: + +1. Confirm you have a recent automated backup or create a fresh manual DB cluster snapshot before making changes. +2. Keep the current CMK enabled for the entire migration. Aurora and old snapshots still need it for decryption. +3. If the source cluster is already encrypted, create a **copy** of the manual snapshot using the target CMK. +4. Restore the copied snapshot as a **new** Aurora cluster. Aurora restores snapshots into a new cluster, not into the existing one. +5. Recreate or verify the expected instances, parameter groups, subnet/security settings, and any cluster endpoints on the restored cluster. +6. Validate Fleet connectivity and application behavior against the restored cluster before cutover. +7. Cut traffic over during a maintenance window, then keep the old cluster and old CMK until you are satisfied with rollback and retention requirements. + +Operationally, this means enabling a new storage CMK should be planned like a cluster replacement, not a normal in-place Terraform apply. + +If you want to automate the snapshot/copy/restore cutover for a byo-vpc deployment that defines `rds_config` inline in its Terraform, use the byo-vpc helper: + +```bash +./byo-vpc/scripts/rds_storage_kms_migration.sh \ + --terraform-dir . \ + --config-file main.tf \ + --storage-kms-alias fleet-rds-storage-2026 +``` + +or: + +```bash +./byo-vpc/scripts/rds_storage_kms_migration.sh \ + --terraform-dir . \ + --config-file main.tf \ + --storage-kms-key-arn arn:aws:kms:us-east-2:123456789012:key/00000000-0000-0000-0000-000000000000 +``` + +The helper script: + +1. edits the caller's inline `rds_config` object in `main.tf` by default +2. pre-creates the wrapper-managed storage CMK with a targeted Terraform apply when `--storage-kms-alias` is used +3. creates a manual Aurora cluster snapshot and an encrypted copy under the target CMK +4. removes the currently managed Aurora resources from Terraform state +5. temporarily disables Performance Insights and Enhanced Monitoring in the config, then applies Terraform to restore a new Aurora cluster from the copied snapshot (AWS `RestoreDBClusterFromSnapshot` rejects these parameters for non-Limitless Aurora clusters) +6. restores the original config from backup and re-applies restore-specific changes (plus any `--include-performance-insights` overrides), then runs a reconcile apply that re-enables PI and monitoring via `ModifyDBCluster` (in-place, no downtime) +7. deletes the old Aurora cluster, secret, parameter groups, subnet group, and security group after the new cluster is managed + +Important operational notes for the helper: + +* The restored cluster uses a new `rds_config.name`. The helper generates one automatically unless you pass `--restored-name`. +* The helper leaves `rds_config.snapshot_identifier` pinned to the copied snapshot after the migration. Removing it later would force Terraform to replace the restored cluster. +* If you also want the recreated cluster to adopt a Performance Insights / Database Insights CMK, add `--include-performance-insights`. This updates `rds_config.observability` in the post-restore config edit so the reconcile apply enables Performance Insights CMK configuration via `ModifyDBCluster`. +* If you want the post-restore config edit to write a specific observability CMK alias, also pass `--performance-insights-kms-alias `. +* Run the helper during a maintenance window. It automates the infrastructure steps, but you still need to plan for application cutover timing and validation. +* Use `--dry-run` first to inspect the exact names, snapshots, and Terraform state addresses it will touch. +* `--dry-run` also writes a `manifest.json` artifact with the exact `state_remove_addresses` and AWS-side identifiers that cleanup will use later. +* If you do not want to delete the old Aurora resources immediately after cutover, use `--keep-old-resources` during the main migration run. You can perform cleanup later from the saved manifest. +* If you want an interactive safety rail during AWS cleanup, add `--confirm`. The helper will print each AWS CLI command and prompt before it runs. + +To defer old-resource cleanup until after you have validated the restored cluster, run the migration with `--keep-old-resources`, then later run: + +```bash +./byo-vpc/scripts/rds_storage_kms_migration.sh \ + --cleanup-only \ + --manifest ./.rds-storage-kms-migration-/manifest.json \ + --region us-east-2 \ + --confirm +``` + +`--cleanup-only` requires `--manifest` and skips the snapshot, Terraform, and state-migration steps. It only deletes the old AWS resources recorded in the manifest, such as the retired cluster, instances, secret, enhanced monitoring IAM role, parameter groups, subnet group, and security group. + +### Aurora database password secret encryption (`rds_config.password_secret_kms`) + +Changing the Secrets Manager CMK for the Fleet database password secret is an in-place metadata update. It does **not** require an Aurora backup/restore. + +Before applying: + +1. Ensure the applying identity can decrypt with the old key and encrypt with the new key. +2. Keep the old key enabled until you verify the secret can be read everywhere that uses it. + +After the key change: + +1. Secrets Manager re-encrypts the `AWSCURRENT`, `AWSPENDING`, and `AWSPREVIOUS` versions if it can decrypt them with the previous key. +2. Older versions without those staging labels can remain encrypted under the previous key. +3. If you want the current secret value to depend only on the new CMK, create a fresh secret version or rotate the secret after the CMK change. + +### Aurora observability encryption (`rds_config.observability.kms`) + +Changing the CMK for Performance Insights / Database Insights is an in-place observability change. AWS documents cluster and instance modifications for Performance Insights and Database Insights as no-downtime changes, but the modification can still take time to complete. + +Before applying: + +1. Make sure the new CMK policy allows RDS to use the key and allows the principals that need to read observability data. +2. Keep the old CMK enabled until you confirm observability data is readable after the update. +3. If enabling Advanced Database Insights at the same time, also satisfy the module requirements for `performance_insights_enabled`, `monitoring_interval`, and retention. + +This path does **not** require an Aurora snapshot/restore migration. + +### Aurora exported CloudWatch log groups (`rds_config.cloudwatch_log_group.kms`) + +Associating a new CMK with an existing CloudWatch log group only affects **newly ingested** log events. Historical log events remain encrypted with the previous key, so the old key must remain available until that data ages out or is removed. + +* For CloudWatch log group re-keying, use the repository-root script: + +```bash +DELETE_OLD_STREAMS=false ./scripts/cloudwatch_logs_kms_migration.sh +``` + +### ElastiCache at-rest encryption (`redis_config.at_rest_kms`) + +Changing the ElastiCache at-rest CMK is **not** an in-place re-key. + +AWS documents that ElastiCache at-rest encryption can only be set when a replication group is created. Existing replication groups do not support enabling at-rest encryption later, and encrypted caches do not support manual key rotation to a different CMK. Treat a move to a new Redis/Valkey CMK as a backup-and-restore migration to a new replication group: + +1. Create a fresh manual backup of the existing replication group before making changes. +2. Keep the current CMK enabled for the entire migration and retention window. Existing encrypted backups and the source replication group still depend on it. +3. Restore the backup into a **new** replication group with at-rest encryption enabled and the target CMK selected. +4. Recreate or verify parameter groups, subnet groups, security groups, maintenance settings, log delivery settings, and any auth/token settings on the restored replication group. +5. Validate Fleet cache connectivity and application behavior against the restored replication group before cutover. +6. Cut application traffic over to the new primary endpoint during a maintenance window. +7. Keep the old replication group and old CMK until rollback is no longer needed and backup retention requirements are satisfied. + +Operationally, this should be planned as a replication group replacement, not a normal in-place Terraform apply. + +For the default Fleet usage in this module, Redis is used as a cache rather than a durable system of record. Replacing the replication group without backup/restore is therefore not expected to be data-loss breaking for Fleet, though you should expect cache cold-start behavior after cutover. Use backup/restore if you specifically want to preserve warm cache state during the migration. + +If you do **not** need to preserve warm cache state, the simplest Terraform migration is usually: + +1. Enable the new Redis CMK settings. +2. Set a temporary new `redis_config.replication_group_id`, for example by appending a suffix such as `-1`. +3. Apply Terraform to create a new encrypted replication group alongside the old one. +4. Validate Fleet against the new replication group and allow the cache to warm naturally. +5. Terraform will then destroy the old replication group as part of the same replacement once the new one is ready. + +This avoids the `ReplicationGroupAlreadyExists` conflict that happens when Terraform tries to replace an ElastiCache replication group in place while reusing the same replication group ID. + +### ElastiCache CloudWatch log groups (`redis_config.cloudwatch_log_group.kms`) + +If Redis/Valkey log delivery targets a CloudWatch log group, changing that log group's CMK behaves the same way as any other CloudWatch Logs re-key: + +* only newly ingested log events use the new key +* historical log events remain encrypted with the previous key +* the previous key must remain available until old data ages out or is removed + +Use the same repository-root helper for CloudWatch Logs re-keying: + +```bash +DELETE_OLD_STREAMS=false ./scripts/cloudwatch_logs_kms_migration.sh +``` + +# How to update this readme + +Edit `.header.md`, run `terraform init`, then run `terraform-docs markdown --header-from .header.md . > README.md`. diff --git a/byo-vpc/.terraform-docs.yml b/byo-vpc/.terraform-docs.yml new file mode 100644 index 0000000..1d139dd --- /dev/null +++ b/byo-vpc/.terraform-docs.yml @@ -0,0 +1 @@ +header-from: .header.md diff --git a/byo-vpc/README.md b/byo-vpc/README.md index c21ef19..1ffbb85 100644 --- a/byo-vpc/README.md +++ b/byo-vpc/README.md @@ -1,23 +1,337 @@ +This module deploys Fleet into an existing VPC while still provisioning the supporting AWS services that Fleet needs, including Aurora, ElastiCache, ECS, and the ALB. + +# KMS Coverage + +This module adds optional CMK support for: + +* Aurora storage encryption +* Aurora database password secret encryption in Secrets Manager +* Aurora observability encryption for Performance Insights / Database Insights +* Aurora exported CloudWatch log groups +* ElastiCache at-rest encryption +* ElastiCache CloudWatch log groups for `cloudwatch-logs` delivery targets +* Nested ECS cluster, Fleet service, application logs, and Fleet secrets through child-module passthroughs + +For each feature: + +* `cmk_enabled = true` means "use a customer-managed KMS key here." +* Set `cmk_enabled = false` or omit it to keep using the service-managed key. +* For KMS options that existed in published releases before this change, legacy `enabled` is deprecated but still accepted. Terraform plan/apply warns when it is used, and `cmk_enabled` takes precedence if both are set. +* Set `cmk_enabled = true` without a key ARN to have the module create a CMK and alias. +* Set `kms_key_arn` to use an existing CMK. + +Provided CMKs must already allow the relevant AWS service in their key policy. + +# Aurora Database Insights + +`rds_config.observability` manages Aurora observability behavior: + +* `database_insights_mode = null` leaves Standard vs Advanced unmanaged. +* `database_insights_mode = "standard"` enforces Standard Database Insights. +* `database_insights_mode = "advanced"` requires: + * `performance_insights_enabled = true` + * `monitoring_interval > 0` + * `retention_period >= 465` + +AWS has announced the Performance Insights console end of life for **June 30, 2026**. Standard Database Insights is a clean forward path for existing clusters that are already using Standard outside Terraform. + +# Aurora Backtrack + +`rds_config.backtrack_window` is optional and passes Aurora MySQL backtracking through to the upstream `rds-aurora` module. + +* Set it to a value between `0` and `259200` seconds. +* Set `0` to disable backtracking explicitly. +* Leave it `null` to keep the default upstream behavior. + +# Aurora Final Snapshot Naming + +`rds_config.final_snapshot_identifier` is optional. + +* Set it explicitly if you want a fixed final snapshot name. +* Leave it `null` to preserve the legacy generated naming pattern: `final--<8-digit-hex>`. + +# Monitoring Addon Secret KMS Wiring + +If you enable `rds_config.password_secret_kms`, the `byo-vpc` module also exposes `rds_password_secret_kms_key_arn`. + +That output is intended to be passed through to the monitoring addon so the cron-monitoring Lambda can decrypt the Fleet database password secret when it is encrypted with a CMK. + +When the module creates the password secret CMK, the key policy automatically grants +`kms:Decrypt` and `kms:DescribeKey` to the Fleet ECS execution role. However, the +cron-monitoring Lambda role is created by the **monitoring addon**, which is applied +after the `byo-vpc` module. If you supply a custom `kms_base_policy` that does not +grant `kms:*` to the account root (for example, a least-privilege policy that only +allows specific principals), you must also add the Lambda role to +`rds_config.password_secret_kms.extra_kms_policies` so the key policy permits the +Lambda to decrypt. Because the Lambda role name is predictable, you can construct the +ARN before the role exists. + +Example: + +```hcl +locals { + cron_monitoring_lambda_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${local.customer}-cron-monitoring-lambda" +} + +module "fleet_byo_vpc" { + source = "github.com/fleetdm/fleet-terraform//byo-vpc?depth=1&ref=tf-mod-byo-vpc-v1.24.0" + + kms_base_policy = local.kms_base_policy_statements # your restrictive policy + + rds_config = { + # ... + password_secret_kms = { + cmk_enabled = true + extra_kms_policies = [ + { + sid = "AllowCronMonitoringLambdaDecrypt" + effect = "Allow" + principals = { + type = "AWS" + identifiers = [local.cron_monitoring_lambda_role_arn] + } + actions = ["kms:Decrypt", "kms:DescribeKey"] + resources = ["*"] + conditions = [] + } + ] + } + } + + # ... +} + +module "monitoring" { + source = "github.com/fleetdm/fleet-terraform//addons/monitoring?ref=tf-mod-addon-monitoring-v1.9.0" + + # ... + cron_monitoring = { + # ... + mysql_password_secret_name = "${local.customer}-database-password" + mysql_password_secret_kms_key_arn = module.fleet_byo_vpc.rds_password_secret_kms_key_arn + } +} +``` + +If your `kms_base_policy` grants `kms:*` to the account root (the default), the +Lambda's IAM policy alone is sufficient and `extra_kms_policies` is not needed. + +# Example + +```hcl +module "fleet_byo_vpc" { + source = "github.com/fleetdm/fleet-terraform//byo-vpc?depth=1&ref=tf-mod-byo-vpc-v1.24.0" + + vpc_config = { + vpc_id = "vpc-1234567890abcdef0" + networking = { + subnets = ["subnet-aaa", "subnet-bbb"] + } + } + + rds_config = { + observability = { + database_insights_mode = "standard" + kms = { + cmk_enabled = true + } + } + storage_kms = { + cmk_enabled = true + } + password_secret_kms = { + cmk_enabled = true + } + } + + redis_config = { + at_rest_kms = { + cmk_enabled = true + } + } +} +``` + +# Migration Notes + +* Existing deployments are unchanged until you enable new KMS settings. +* Upgrading from `tf-mod-byo-vpc-v1.24.0` to any newer version may plan an in-place Aurora cluster update setting cluster-level `performance_insights_enabled = true`. Older versions already enabled Performance Insights at the instance level; newer versions also manage it at the cluster level to align with Aurora Database Insights support. +* Aurora storage and some observability changes may still be sensitive changes; review your maintenance and restore strategy before applying them in production. +* If your goal is routine key rotation, prefer AWS KMS automatic rotation on the existing CMK when possible. Switching a resource to a different CMK is a separate migration with service-specific behavior. + +## KMS Migration Guidance + +### Aurora storage encryption (`rds_config.storage_kms`) + +Changing the Aurora storage CMK is **not** an in-place re-key. + +AWS documents that you can't change the KMS key of an existing encrypted Aurora DB cluster in place. To move to a different CMK, treat the change as a backup-and-restore migration: + +1. Confirm you have a recent automated backup or create a fresh manual DB cluster snapshot before making changes. +2. Keep the current CMK enabled for the entire migration. Aurora and old snapshots still need it for decryption. +3. If the source cluster is already encrypted, create a **copy** of the manual snapshot using the target CMK. +4. Restore the copied snapshot as a **new** Aurora cluster. Aurora restores snapshots into a new cluster, not into the existing one. +5. Recreate or verify the expected instances, parameter groups, subnet/security settings, and any cluster endpoints on the restored cluster. +6. Validate Fleet connectivity and application behavior against the restored cluster before cutover. +7. Cut traffic over during a maintenance window, then keep the old cluster and old CMK until you are satisfied with rollback and retention requirements. + +Operationally, this means enabling a new storage CMK should be planned like a cluster replacement, not a normal in-place Terraform apply. + +If you want to automate the snapshot/copy/restore cutover for a byo-vpc deployment that defines `rds_config` inline in its Terraform, use the byo-vpc helper: + +```bash +./byo-vpc/scripts/rds_storage_kms_migration.sh \ + --terraform-dir . \ + --config-file main.tf \ + --storage-kms-alias fleet-rds-storage-2026 +``` + +or: + +```bash +./byo-vpc/scripts/rds_storage_kms_migration.sh \ + --terraform-dir . \ + --config-file main.tf \ + --storage-kms-key-arn arn:aws:kms:us-east-2:123456789012:key/00000000-0000-0000-0000-000000000000 +``` + +The helper script: + +1. edits the caller's inline `rds_config` object in `main.tf` by default +2. pre-creates the wrapper-managed storage CMK with a targeted Terraform apply when `--storage-kms-alias` is used +3. creates a manual Aurora cluster snapshot and an encrypted copy under the target CMK +4. removes the currently managed Aurora resources from Terraform state +5. temporarily disables Performance Insights and Enhanced Monitoring in the config, then applies Terraform to restore a new Aurora cluster from the copied snapshot (AWS `RestoreDBClusterFromSnapshot` rejects these parameters for non-Limitless Aurora clusters) +6. restores the original config from backup and re-applies restore-specific changes (plus any `--include-performance-insights` overrides), then runs a reconcile apply that re-enables PI and monitoring via `ModifyDBCluster` (in-place, no downtime) +7. deletes the old Aurora cluster, secret, parameter groups, subnet group, and security group after the new cluster is managed + +Important operational notes for the helper: + +* The restored cluster uses a new `rds_config.name`. The helper generates one automatically unless you pass `--restored-name`. +* The helper leaves `rds_config.snapshot_identifier` pinned to the copied snapshot after the migration. Removing it later would force Terraform to replace the restored cluster. +* If you also want the recreated cluster to adopt a Performance Insights / Database Insights CMK, add `--include-performance-insights`. This updates `rds_config.observability` in the post-restore config edit so the reconcile apply enables Performance Insights CMK configuration via `ModifyDBCluster`. +* If you want the post-restore config edit to write a specific observability CMK alias, also pass `--performance-insights-kms-alias `. +* Run the helper during a maintenance window. It automates the infrastructure steps, but you still need to plan for application cutover timing and validation. +* Use `--dry-run` first to inspect the exact names, snapshots, and Terraform state addresses it will touch. +* `--dry-run` also writes a `manifest.json` artifact with the exact `state_remove_addresses` and AWS-side identifiers that cleanup will use later. +* If you do not want to delete the old Aurora resources immediately after cutover, use `--keep-old-resources` during the main migration run. You can perform cleanup later from the saved manifest. +* If you want an interactive safety rail during AWS cleanup, add `--confirm`. The helper will print each AWS CLI command and prompt before it runs. + +To defer old-resource cleanup until after you have validated the restored cluster, run the migration with `--keep-old-resources`, then later run: + +```bash +./byo-vpc/scripts/rds_storage_kms_migration.sh \ + --cleanup-only \ + --manifest ./.rds-storage-kms-migration-/manifest.json \ + --region us-east-2 \ + --confirm +``` + +`--cleanup-only` requires `--manifest` and skips the snapshot, Terraform, and state-migration steps. It only deletes the old AWS resources recorded in the manifest, such as the retired cluster, instances, secret, enhanced monitoring IAM role, parameter groups, subnet group, and security group. + +### Aurora database password secret encryption (`rds_config.password_secret_kms`) + +Changing the Secrets Manager CMK for the Fleet database password secret is an in-place metadata update. It does **not** require an Aurora backup/restore. + +Before applying: + +1. Ensure the applying identity can decrypt with the old key and encrypt with the new key. +2. Keep the old key enabled until you verify the secret can be read everywhere that uses it. + +After the key change: + +1. Secrets Manager re-encrypts the `AWSCURRENT`, `AWSPENDING`, and `AWSPREVIOUS` versions if it can decrypt them with the previous key. +2. Older versions without those staging labels can remain encrypted under the previous key. +3. If you want the current secret value to depend only on the new CMK, create a fresh secret version or rotate the secret after the CMK change. + +### Aurora observability encryption (`rds_config.observability.kms`) + +Changing the CMK for Performance Insights / Database Insights is an in-place observability change. AWS documents cluster and instance modifications for Performance Insights and Database Insights as no-downtime changes, but the modification can still take time to complete. + +Before applying: + +1. Make sure the new CMK policy allows RDS to use the key and allows the principals that need to read observability data. +2. Keep the old CMK enabled until you confirm observability data is readable after the update. +3. If enabling Advanced Database Insights at the same time, also satisfy the module requirements for `performance_insights_enabled`, `monitoring_interval`, and retention. + +This path does **not** require an Aurora snapshot/restore migration. + +### Aurora exported CloudWatch log groups (`rds_config.cloudwatch_log_group.kms`) + +Associating a new CMK with an existing CloudWatch log group only affects **newly ingested** log events. Historical log events remain encrypted with the previous key, so the old key must remain available until that data ages out or is removed. + +* For CloudWatch log group re-keying, use the repository-root script: + +```bash +DELETE_OLD_STREAMS=false ./scripts/cloudwatch_logs_kms_migration.sh +``` + +### ElastiCache at-rest encryption (`redis_config.at_rest_kms`) + +Changing the ElastiCache at-rest CMK is **not** an in-place re-key. + +AWS documents that ElastiCache at-rest encryption can only be set when a replication group is created. Existing replication groups do not support enabling at-rest encryption later, and encrypted caches do not support manual key rotation to a different CMK. Treat a move to a new Redis/Valkey CMK as a backup-and-restore migration to a new replication group: + +1. Create a fresh manual backup of the existing replication group before making changes. +2. Keep the current CMK enabled for the entire migration and retention window. Existing encrypted backups and the source replication group still depend on it. +3. Restore the backup into a **new** replication group with at-rest encryption enabled and the target CMK selected. +4. Recreate or verify parameter groups, subnet groups, security groups, maintenance settings, log delivery settings, and any auth/token settings on the restored replication group. +5. Validate Fleet cache connectivity and application behavior against the restored replication group before cutover. +6. Cut application traffic over to the new primary endpoint during a maintenance window. +7. Keep the old replication group and old CMK until rollback is no longer needed and backup retention requirements are satisfied. + +Operationally, this should be planned as a replication group replacement, not a normal in-place Terraform apply. + +For the default Fleet usage in this module, Redis is used as a cache rather than a durable system of record. Replacing the replication group without backup/restore is therefore not expected to be data-loss breaking for Fleet, though you should expect cache cold-start behavior after cutover. Use backup/restore if you specifically want to preserve warm cache state during the migration. + +If you do **not** need to preserve warm cache state, the simplest Terraform migration is usually: + +1. Enable the new Redis CMK settings. +2. Set a temporary new `redis_config.replication_group_id`, for example by appending a suffix such as `-1`. +3. Apply Terraform to create a new encrypted replication group alongside the old one. +4. Validate Fleet against the new replication group and allow the cache to warm naturally. +5. Terraform will then destroy the old replication group as part of the same replacement once the new one is ready. + +This avoids the `ReplicationGroupAlreadyExists` conflict that happens when Terraform tries to replace an ElastiCache replication group in place while reusing the same replication group ID. + +### ElastiCache CloudWatch log groups (`redis_config.cloudwatch_log_group.kms`) + +If Redis/Valkey log delivery targets a CloudWatch log group, changing that log group's CMK behaves the same way as any other CloudWatch Logs re-key: + +* only newly ingested log events use the new key +* historical log events remain encrypted with the previous key +* the previous key must remain available until old data ages out or is removed + +Use the same repository-root helper for CloudWatch Logs re-keying: + +```bash +DELETE_OLD_STREAMS=false ./scripts/cloudwatch_logs_kms_migration.sh +``` + +# How to update this readme + +Edit `.header.md`, run `terraform init`, then run `terraform-docs markdown --header-from .header.md . > README.md`. + ## Requirements | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 1.3.8 | +| [terraform](#requirement\_terraform) | >= 1.5.0 | | [aws](#requirement\_aws) | >= 5.73.0 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | 6.25.0 | -| [random](#provider\_random) | 3.7.2 | +| [aws](#provider\_aws) | 6.35.1 | +| [random](#provider\_random) | 3.8.1 | ## Modules | Name | Source | Version | |------|--------|---------| | [byo-db](#module\_byo-db) | ./byo-db | n/a | -| [rds](#module\_rds) | terraform-aws-modules/rds-aurora/aws | 7.6.0 | +| [rds](#module\_rds) | terraform-aws-modules/rds-aurora/aws | 9.16.1 | | [redis](#module\_redis) | cloudposse/elasticache-redis/aws | >= 1.9.1 | | [secrets-manager-1](#module\_secrets-manager-1) | lgallard/secrets-manager/aws | 0.6.1 | @@ -25,20 +339,44 @@ | Name | Type | |------|------| +| [aws_cloudwatch_log_group.redis](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource | | [aws_db_parameter_group.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_parameter_group) | resource | +| [aws_kms_alias.rds_cloudwatch_log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource | +| [aws_kms_alias.rds_observability](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource | +| [aws_kms_alias.rds_password_secret](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource | +| [aws_kms_alias.rds_storage](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource | +| [aws_kms_alias.redis_at_rest](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource | +| [aws_kms_alias.redis_cloudwatch_log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource | +| [aws_kms_key.rds_cloudwatch_log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | +| [aws_kms_key.rds_observability](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | +| [aws_kms_key.rds_password_secret](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | +| [aws_kms_key.rds_storage](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | +| [aws_kms_key.redis_at_rest](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | +| [aws_kms_key.redis_cloudwatch_log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | | [aws_rds_cluster_parameter_group.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/rds_cluster_parameter_group) | resource | +| [random_id.rds_final_snapshot_identifier](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource | | [random_password.rds](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource | +| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | +| [aws_iam_policy_document.rds_cloudwatch_log_group_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_iam_policy_document.rds_observability_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_iam_policy_document.rds_password_secret_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_iam_policy_document.rds_storage_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_iam_policy_document.redis_at_rest_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | +| [aws_iam_policy_document.redis_cloudwatch_log_group_kms](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 | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | [alb\_config](#input\_alb\_config) | n/a |
object({
name = optional(string, "fleet")
subnets = list(string)
security_groups = optional(list(string), [])
access_logs = optional(map(string), {})
certificate_arn = string
allowed_cidrs = optional(list(string), ["0.0.0.0/0"])
allowed_ipv6_cidrs = optional(list(string), ["::/0"])
egress_cidrs = optional(list(string), ["0.0.0.0/0"])
egress_ipv6_cidrs = optional(list(string), ["::/0"])
fleet_target_group = optional(object({
protocol = optional(string, "HTTP")
port = optional(number, 80)
target_type = optional(string, "ip")
create_attachment = optional(bool, false)
health_check = optional(object({
path = optional(string, "/healthz")
matcher = optional(string, "200")
port = optional(string)
timeout = optional(number, 10)
interval = optional(number, 15)
healthy_threshold = optional(number, 5)
unhealthy_threshold = optional(number, 5)
}), {})
}), {})
extra_target_groups = optional(any, [])
https_listener_rules = optional(any, [])
https_overrides = optional(any, {})
xff_header_processing_mode = optional(string, null)
tls_policy = optional(string, "ELBSecurityPolicy-TLS13-1-2-2021-06")
idle_timeout = optional(number, 905)
internal = optional(bool, false)
enable_deletion_protection = optional(bool, false)
})
| n/a | yes | -| [ecs\_cluster](#input\_ecs\_cluster) | The config for the terraform-aws-modules/ecs/aws module |
object({
autoscaling_capacity_providers = optional(any, {})
cluster_configuration = optional(any, {
execute_command_configuration = {
logging = "OVERRIDE"
log_configuration = {
cloud_watch_log_group_name = "/aws/ecs/aws-ec2"
}
}
})
cluster_name = optional(string, "fleet")
cloudwatch_log_group = optional(object({
create = optional(bool, true)
retention_in_days = optional(number, 90)
kms = optional(object({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-ecs-cluster-logs")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
})
}), {
create = true
retention_in_days = 90
kms = {
enabled = false
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
}
})
cluster_settings = optional(any, {
"name" : "containerInsights",
"value" : "enabled",
})
create = optional(bool, true)
default_capacity_provider_use_fargate = optional(bool, true)
fargate_capacity_providers = optional(any, {
FARGATE = {
default_capacity_provider_strategy = {
weight = 100
}
}
FARGATE_SPOT = {
default_capacity_provider_strategy = {
weight = 0
}
}
})
tags = optional(map(string))
})
|
{
"autoscaling_capacity_providers": {},
"cloudwatch_log_group": {
"create": true,
"kms": {
"enabled": false,
"kms_alias": "fleet-ecs-cluster-logs",
"kms_key_arn": null
},
"retention_in_days": 90
},
"cluster_configuration": {
"execute_command_configuration": {
"log_configuration": {
"cloud_watch_log_group_name": "/aws/ecs/aws-ec2"
},
"logging": "OVERRIDE"
}
},
"cluster_name": "fleet",
"cluster_settings": {
"name": "containerInsights",
"value": "enabled"
},
"create": true,
"default_capacity_provider_use_fargate": true,
"fargate_capacity_providers": {
"FARGATE": {
"default_capacity_provider_strategy": {
"weight": 100
}
},
"FARGATE_SPOT": {
"default_capacity_provider_strategy": {
"weight": 0
}
}
},
"tags": {}
}
| 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. |
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({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-server-private-key")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-server-private-key"
})
fargate_ephemeral_storage_kms = optional(object({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-fargate-ephemeral-storage")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-fargate-ephemeral-storage"
})
server_tls_enabled = optional(bool, false)
service = optional(object({
name = optional(string, "fleet")
}), {
name = "fleet"
})
database = optional(object({
password_secret_arn = string
user = string
database = string
address = string
rr_address = optional(string, null)
}), {
password_secret_arn = null
user = null
database = null
address = null
rr_address = null
})
redis = optional(object({
address = string
use_tls = optional(bool, true)
}), {
address = null
use_tls = 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({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-application-logs")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-application-logs"
})
}), {
name = null
region = null
create = true
prefix = "fleet"
retention = 5
kms = {
enabled = false
kms_key_arn = null
kms_alias = "fleet-application-logs"
}
})
loadbalancer = optional(object({
arn = string
}), {
arn = null
})
extra_load_balancers = optional(list(any), [])
networking = optional(object({
subnets = optional(list(string), null)
security_groups = optional(list(string), null)
ingress_sources = optional(object({
cidr_blocks = optional(list(string), [])
ipv6_cidr_blocks = optional(list(string), [])
security_groups = optional(list(string), [])
prefix_list_ids = optional(list(string), [])
}), {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
})
assign_public_ip = optional(bool, false)
}), {
subnets = null
security_groups = null
ingress_sources = {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
}
assign_public_ip = 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, "")
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")
tags = optional(map(string), {})
}), {
create_bucket = true
bucket_name = null
bucket_prefix = "fleet-software-installers-"
s3_object_prefix = ""
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"
tags = {}
})
})
|
{
"autoscaling": {
"cpu_tracking_target_value": 80,
"max_capacity": 5,
"memory_tracking_target_value": 80,
"min_capacity": 1
},
"awslogs": {
"create": true,
"kms": {
"enabled": false,
"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",
"fargate_ephemeral_storage_kms": {
"enabled": false,
"kms_alias": "fleet-fargate-ephemeral-storage",
"kms_key_arn": null
},
"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": {
"enabled": false,
"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",
"security_groups": null,
"server_tls_enabled": false,
"service": {
"name": "fleet"
},
"sidecars": [],
"software_installers": {
"bucket_name": null,
"bucket_prefix": "fleet-software-installers-",
"create_bucket": true,
"create_kms_key": false,
"enable_bucket_versioning": false,
"expire_noncurrent_versions": true,
"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 | +| [ecs\_cluster](#input\_ecs\_cluster) | The config for the terraform-aws-modules/ecs/aws module. For published KMS blocks, legacy `enabled` is deprecated and still accepted; prefer `cmk_enabled`. |
object({
autoscaling_capacity_providers = optional(any, {})
cluster_configuration = optional(any, {
execute_command_configuration = {
logging = "OVERRIDE"
log_configuration = {
cloud_watch_log_group_name = "/aws/ecs/aws-ec2"
}
}
})
cluster_name = optional(string, "fleet")
cloudwatch_log_group = optional(object({
create = optional(bool, true)
retention_in_days = optional(number, 90)
kms = optional(object({
cmk_enabled = optional(bool, null)
enabled = optional(bool, null)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-ecs-cluster-logs")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
extra_kms_policies = []
})
}), {
create = true
retention_in_days = 90
kms = {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
extra_kms_policies = []
}
})
cluster_settings = optional(any, {
"name" : "containerInsights",
"value" : "enabled",
})
create = optional(bool, true)
default_capacity_provider_use_fargate = optional(bool, true)
fargate_capacity_providers = optional(any, {
FARGATE = {
default_capacity_provider_strategy = {
weight = 100
}
}
FARGATE_SPOT = {
default_capacity_provider_strategy = {
weight = 0
}
}
})
tags = optional(map(string))
})
|
{
"autoscaling_capacity_providers": {},
"cloudwatch_log_group": {
"create": true,
"kms": {
"cmk_enabled": false,
"kms_alias": "fleet-ecs-cluster-logs",
"kms_key_arn": null
},
"retention_in_days": 90
},
"cluster_configuration": {
"execute_command_configuration": {
"log_configuration": {
"cloud_watch_log_group_name": "/aws/ecs/aws-ec2"
},
"logging": "OVERRIDE"
}
},
"cluster_name": "fleet",
"cluster_settings": {
"name": "containerInsights",
"value": "enabled"
},
"create": true,
"default_capacity_provider_use_fargate": true,
"fargate_capacity_providers": {
"FARGATE": {
"default_capacity_provider_strategy": {
"weight": 100
}
},
"FARGATE_SPOT": {
"default_capacity_provider_strategy": {
"weight": 0
}
}
},
"tags": {}
}
| 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.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 = []
})
fargate_ephemeral_storage_kms = optional(object({
cmk_enabled = optional(bool, null)
enabled = optional(bool, null)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-fargate-ephemeral-storage")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-fargate-ephemeral-storage"
extra_kms_policies = []
})
server_tls_enabled = optional(bool, false)
service = optional(object({
name = optional(string, "fleet")
}), {
name = "fleet"
})
database = optional(object({
password_secret_arn = string
password_secret_kms_key_arn = optional(string, null)
user = string
database = string
address = string
rr_address = optional(string, null)
}), {
password_secret_arn = null
password_secret_kms_key_arn = null
user = null
database = null
address = null
rr_address = null
})
redis = optional(object({
address = string
use_tls = optional(bool, true)
}), {
address = null
use_tls = 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 = optional(object({
arn = string
}), {
arn = null
})
extra_load_balancers = optional(list(any), [])
networking = optional(object({
subnets = optional(list(string), null)
security_groups = optional(list(string), null)
ingress_sources = optional(object({
cidr_blocks = optional(list(string), [])
ipv6_cidr_blocks = optional(list(string), [])
security_groups = optional(list(string), [])
prefix_list_ids = optional(list(string), [])
}), {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
})
assign_public_ip = optional(bool, false)
}), {
subnets = null
security_groups = null
ingress_sources = {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
}
assign_public_ip = 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": 512,
"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",
"fargate_ephemeral_storage_kms": {
"cmk_enabled": null,
"enabled": null,
"extra_kms_policies": [],
"kms_alias": "fleet-fargate-ephemeral-storage",
"kms_key_arn": null
},
"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": 4096,
"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",
"security_groups": null,
"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 | -| [rds\_config](#input\_rds\_config) | The config for the terraform-aws-modules/rds-aurora/aws module |
object({
name = optional(string, "fleet")
engine_version = optional(string, "8.0.mysql_aurora.3.07.1")
instance_class = optional(string, "db.t4g.large")
subnets = optional(list(string), [])
allowed_security_groups = optional(list(string), [])
allowed_cidr_blocks = optional(list(string), [])
apply_immediately = optional(bool, true)
monitoring_interval = optional(number, 10)
db_parameter_group_name = optional(string)
db_parameters = optional(map(string), {})
db_cluster_parameter_group_name = optional(string)
db_cluster_parameters = optional(map(string), {})
enabled_cloudwatch_logs_exports = optional(list(string), [])
master_username = optional(string, "fleet")
snapshot_identifier = optional(string)
cluster_tags = optional(map(string), {})
preferred_maintenance_window = optional(string, "thu:23:00-fri:00:00")
skip_final_snapshot = optional(bool, true)
backup_retention_period = optional(number, 7)
replicas = optional(number, 2)
serverless = optional(bool, false)
serverless_min_capacity = optional(number, 2)
serverless_max_capacity = optional(number, 10)
restore_to_point_in_time = optional(map(string), {})
})
|
{
"allowed_cidr_blocks": [],
"allowed_security_groups": [],
"apply_immediately": true,
"backup_retention_period": 7,
"cluster_tags": {},
"db_cluster_parameter_group_name": null,
"db_cluster_parameters": {},
"db_parameter_group_name": null,
"db_parameters": {},
"enabled_cloudwatch_logs_exports": [],
"engine_version": "8.0.mysql_aurora.3.07.1",
"instance_class": "db.t4g.large",
"master_username": "fleet",
"monitoring_interval": 10,
"name": "fleet",
"preferred_maintenance_window": "thu:23:00-fri:00:00",
"replicas": 2,
"restore_to_point_in_time": {},
"serverless": false,
"serverless_max_capacity": 10,
"serverless_min_capacity": 2,
"skip_final_snapshot": true,
"snapshot_identifier": null,
"subnets": []
}
| no | -| [redis\_config](#input\_redis\_config) | n/a |
object({
name = optional(string, "fleet")
replication_group_id = optional(string)
elasticache_subnet_group_name = optional(string, "")
allowed_security_group_ids = optional(list(string), [])
subnets = list(string)
allowed_cidrs = list(string)
availability_zones = optional(list(string), [])
cluster_size = optional(number, 3)
instance_type = optional(string, "cache.m5.large")
apply_immediately = optional(bool, true)
automatic_failover_enabled = optional(bool, false)
engine = optional(string, "redis")
engine_version = optional(string, "7.1")
family = optional(string, "redis7")
at_rest_encryption_enabled = optional(bool, true)
transit_encryption_enabled = optional(bool, true)
parameter = optional(list(object({
name = string
value = string
})), [])
log_delivery_configuration = optional(list(map(any)), [])
tags = optional(map(string), {})
})
|
{
"allowed_cidrs": null,
"allowed_security_group_ids": [],
"apply_immediately": true,
"at_rest_encryption_enabled": true,
"automatic_failover_enabled": false,
"availability_zones": [],
"cluster_size": 3,
"elasticache_subnet_group_name": "",
"engine": "redis",
"engine_version": "7.1",
"family": "redis7",
"instance_type": "cache.m5.large",
"log_delivery_configuration": [],
"name": "fleet",
"parameter": [],
"replication_group_id": null,
"subnets": null,
"tags": {},
"transit_encryption_enabled": true
}
| no | +| [rds\_config](#input\_rds\_config) | The config for the terraform-aws-modules/rds-aurora/aws module |
object({
name = optional(string, "fleet")
engine_version = optional(string, "8.0.mysql_aurora.3.07.1")
instance_class = optional(string, "db.t4g.large")
subnets = optional(list(string), [])
allowed_security_groups = optional(list(string), [])
allowed_cidr_blocks = optional(list(string), [])
apply_immediately = optional(bool, true)
monitoring_interval = optional(number, 10)
backtrack_window = optional(number, null)
db_parameter_group_name = optional(string)
db_parameters = optional(map(string), {})
db_cluster_parameter_group_name = optional(string)
db_cluster_parameters = optional(map(string), {})
enabled_cloudwatch_logs_exports = optional(list(string), [])
final_snapshot_identifier = optional(string, null)
password_secret_kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-rds-password-secret")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-password-secret"
extra_kms_policies = []
})
storage_kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-rds-storage")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-storage"
extra_kms_policies = []
})
observability = optional(object({
performance_insights_enabled = optional(bool, true)
retention_period = optional(number, null)
database_insights_mode = optional(string, null)
kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-rds-performance-insights")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-performance-insights"
extra_kms_policies = []
})
}), {
performance_insights_enabled = true
retention_period = null
database_insights_mode = null
kms = {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-performance-insights"
extra_kms_policies = []
}
})
cloudwatch_log_group = optional(object({
retention_in_days = optional(number, null)
skip_destroy = optional(bool, false)
kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-rds-logs")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-logs"
extra_kms_policies = []
})
}), {
retention_in_days = null
skip_destroy = false
kms = {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-rds-logs"
extra_kms_policies = []
}
})
master_username = optional(string, "fleet")
snapshot_identifier = optional(string)
cluster_tags = optional(map(string), {})
preferred_maintenance_window = optional(string, "thu:23:00-fri:00:00")
skip_final_snapshot = optional(bool, true)
backup_retention_period = optional(number, 7)
replicas = optional(number, 2)
serverless = optional(bool, false)
serverless_min_capacity = optional(number, 2)
serverless_max_capacity = optional(number, 10)
restore_to_point_in_time = optional(map(string), {})
})
|
{
"allowed_cidr_blocks": [],
"allowed_security_groups": [],
"apply_immediately": true,
"backtrack_window": null,
"backup_retention_period": 7,
"cloudwatch_log_group": {
"kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-rds-logs",
"kms_key_arn": null
},
"retention_in_days": null,
"skip_destroy": false
},
"cluster_tags": {},
"db_cluster_parameter_group_name": null,
"db_cluster_parameters": {},
"db_parameter_group_name": null,
"db_parameters": {},
"enabled_cloudwatch_logs_exports": [],
"engine_version": "8.0.mysql_aurora.3.07.1",
"final_snapshot_identifier": null,
"instance_class": "db.t4g.large",
"master_username": "fleet",
"monitoring_interval": 10,
"name": "fleet",
"observability": {
"database_insights_mode": null,
"kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-rds-performance-insights",
"kms_key_arn": null
},
"performance_insights_enabled": true,
"retention_period": null
},
"password_secret_kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-rds-password-secret",
"kms_key_arn": null
},
"preferred_maintenance_window": "thu:23:00-fri:00:00",
"replicas": 2,
"restore_to_point_in_time": {},
"serverless": false,
"serverless_max_capacity": 10,
"serverless_min_capacity": 2,
"skip_final_snapshot": true,
"snapshot_identifier": null,
"storage_kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-rds-storage",
"kms_key_arn": null
},
"subnets": []
}
| no | +| [redis\_config](#input\_redis\_config) | n/a |
object({
name = optional(string, "fleet")
replication_group_id = optional(string)
elasticache_subnet_group_name = optional(string, "")
allowed_security_group_ids = optional(list(string), [])
subnets = list(string)
allowed_cidrs = list(string)
availability_zones = optional(list(string), [])
cluster_size = optional(number, 3)
instance_type = optional(string, "cache.m5.large")
apply_immediately = optional(bool, true)
automatic_failover_enabled = optional(bool, false)
engine = optional(string, "redis")
engine_version = optional(string, "7.1")
family = optional(string, "redis7")
at_rest_encryption_enabled = optional(bool, true)
at_rest_kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-redis-at-rest")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-redis-at-rest"
extra_kms_policies = []
})
transit_encryption_enabled = optional(bool, true)
parameter = optional(list(object({
name = string
value = string
})), [])
cloudwatch_log_group = optional(object({
retention_in_days = optional(number, null)
skip_destroy = optional(bool, false)
kms = optional(object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-redis-logs")
extra_kms_policies = optional(list(any), [])
}), {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-redis-logs"
extra_kms_policies = []
})
}), {
retention_in_days = null
skip_destroy = false
kms = {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-redis-logs"
extra_kms_policies = []
}
})
log_delivery_configuration = optional(list(map(any)), [])
tags = optional(map(string), {})
})
|
{
"allowed_cidrs": null,
"allowed_security_group_ids": [],
"apply_immediately": true,
"at_rest_encryption_enabled": true,
"at_rest_kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-redis-at-rest",
"kms_key_arn": null
},
"automatic_failover_enabled": false,
"availability_zones": [],
"cloudwatch_log_group": {
"kms": {
"cmk_enabled": false,
"extra_kms_policies": [],
"kms_alias": "fleet-redis-logs",
"kms_key_arn": null
},
"retention_in_days": null,
"skip_destroy": false
},
"cluster_size": 3,
"elasticache_subnet_group_name": "",
"engine": "redis",
"engine_version": "7.1",
"family": "redis7",
"instance_type": "cache.m5.large",
"log_delivery_configuration": [],
"name": "fleet",
"parameter": [],
"replication_group_id": null,
"subnets": null,
"tags": {},
"transit_encryption_enabled": true
}
| no | | [vpc\_config](#input\_vpc\_config) | n/a |
object({
vpc_id = string
networking = object({
subnets = list(string)
})
})
| n/a | yes | ## Outputs @@ -47,5 +385,6 @@ |------|-------------| | [byo-db](#output\_byo-db) | n/a | | [rds](#output\_rds) | n/a | +| [rds\_password\_secret\_kms\_key\_arn](#output\_rds\_password\_secret\_kms\_key\_arn) | n/a | | [redis](#output\_redis) | n/a | | [secrets](#output\_secrets) | n/a | diff --git a/byo-vpc/byo-db/.header.md b/byo-vpc/byo-db/.header.md new file mode 100644 index 0000000..d2df2d8 --- /dev/null +++ b/byo-vpc/byo-db/.header.md @@ -0,0 +1,26 @@ +This module deploys the Fleet ECS cluster, ALB, and Fleet services while using an existing database and cache. In the full repo stack it is normally called by `byo-vpc`, but it can also be used independently when those backing services already exist. + +# KMS Coverage + +This module directly manages optional CMK support for: + +* ECS cluster execute-command CloudWatch log groups +* ECS Fargate ephemeral storage + +It also passes through nested `byo-ecs` KMS settings for: + +* Fleet private-key secret +* Fleet application log groups +* Fleet software installer bucket encryption + +# CloudWatch Logs Migration + +If you enable KMS for existing ECS cluster or Fleet log groups and need to purge pre-KMS streams, use the repository-root helper: + +```bash +DELETE_OLD_STREAMS=false ../../../scripts/cloudwatch_logs_kms_migration.sh +``` + +# How to update this readme + +Edit `.header.md`, run `terraform init`, then run `terraform-docs markdown . > README.md`. diff --git a/byo-vpc/byo-db/README.md b/byo-vpc/byo-db/README.md index f7c0123..0f7bfbb 100644 --- a/byo-vpc/byo-db/README.md +++ b/byo-vpc/byo-db/README.md @@ -1,12 +1,14 @@ ## Requirements -No requirements. +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.5.0 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | 6.21.0 | +| [aws](#provider\_aws) | 6.35.1 | ## Modules @@ -36,8 +38,9 @@ No requirements. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | [alb\_config](#input\_alb\_config) | n/a |
object({
name = optional(string, "fleet")
subnets = list(string)
security_groups = optional(list(string), [])
access_logs = optional(map(string), {})
certificate_arn = string
allowed_cidrs = optional(list(string), ["0.0.0.0/0"])
allowed_ipv6_cidrs = optional(list(string), ["::/0"])
egress_cidrs = optional(list(string), ["0.0.0.0/0"])
egress_ipv6_cidrs = optional(list(string), ["::/0"])
fleet_target_group = optional(object({
protocol = optional(string, "HTTP")
port = optional(number, 80)
target_type = optional(string, "ip")
create_attachment = optional(bool, false)
health_check = optional(object({
path = optional(string, "/healthz")
matcher = optional(string, "200")
port = optional(string)
timeout = optional(number, 10)
interval = optional(number, 15)
healthy_threshold = optional(number, 5)
unhealthy_threshold = optional(number, 5)
}), {})
}), {})
extra_target_groups = optional(any, [])
https_listener_rules = optional(any, [])
https_overrides = optional(any, {})
xff_header_processing_mode = optional(string, null)
tls_policy = optional(string, "ELBSecurityPolicy-TLS13-1-2-2021-06")
idle_timeout = optional(number, 905)
internal = optional(bool, false)
enable_deletion_protection = optional(bool, false)
})
| n/a | yes | -| [ecs\_cluster](#input\_ecs\_cluster) | The config for the terraform-aws-modules/ecs/aws module |
object({
autoscaling_capacity_providers = optional(any, {})
cluster_configuration = optional(any, {
execute_command_configuration = {
logging = "OVERRIDE"
log_configuration = {
cloud_watch_log_group_name = "/aws/ecs/aws-ec2"
}
}
})
cluster_name = optional(string, "fleet")
cloudwatch_log_group = optional(object({
create = optional(bool, true)
retention_in_days = optional(number, 90)
kms = optional(object({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-ecs-cluster-logs")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
})
}), {
create = true
retention_in_days = 90
kms = {
enabled = false
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
}
})
cluster_settings = optional(any, {
"name" : "containerInsights",
"value" : "enabled",
})
create = optional(bool, true)
default_capacity_provider_use_fargate = optional(bool, true)
fargate_capacity_providers = optional(any, {
FARGATE = {
default_capacity_provider_strategy = {
weight = 100
}
}
FARGATE_SPOT = {
default_capacity_provider_strategy = {
weight = 0
}
}
})
tags = optional(map(string))
})
|
{
"autoscaling_capacity_providers": {},
"cloudwatch_log_group": {
"create": true,
"kms": {
"enabled": false,
"kms_alias": "fleet-ecs-cluster-logs",
"kms_key_arn": null
},
"retention_in_days": 90
},
"cluster_configuration": {
"execute_command_configuration": {
"log_configuration": {
"cloud_watch_log_group_name": "/aws/ecs/aws-ec2"
},
"logging": "OVERRIDE"
}
},
"cluster_name": "fleet",
"cluster_settings": {
"name": "containerInsights",
"value": "enabled"
},
"create": true,
"default_capacity_provider_use_fargate": true,
"fargate_capacity_providers": {
"FARGATE": {
"default_capacity_provider_strategy": {
"weight": 100
}
},
"FARGATE_SPOT": {
"default_capacity_provider_strategy": {
"weight": 0
}
}
},
"tags": {}
}
| 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. |
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({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-server-private-key")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-server-private-key"
})
fargate_ephemeral_storage_kms = optional(object({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-fargate-ephemeral-storage")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-fargate-ephemeral-storage"
})
server_tls_enabled = optional(bool, false)
service = optional(object({
name = optional(string, "fleet")
}), {
name = "fleet"
})
database = optional(object({
password_secret_arn = string
user = string
database = string
address = string
rr_address = optional(string, null)
}), {
password_secret_arn = null
user = null
database = null
address = null
rr_address = null
})
redis = optional(object({
address = string
use_tls = optional(bool, true)
}), {
address = null
use_tls = 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({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-application-logs")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-application-logs"
})
}), {
name = null
region = null
create = true
prefix = "fleet"
retention = 5
kms = {
enabled = false
kms_key_arn = null
kms_alias = "fleet-application-logs"
}
})
loadbalancer = optional(object({
arn = string
}), {
arn = null
})
extra_load_balancers = optional(list(any), [])
networking = optional(object({
subnets = optional(list(string), null)
security_groups = optional(list(string), null)
ingress_sources = optional(object({
cidr_blocks = optional(list(string), [])
ipv6_cidr_blocks = optional(list(string), [])
security_groups = optional(list(string), [])
prefix_list_ids = optional(list(string), [])
}), {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
})
assign_public_ip = optional(bool, false)
}), {
subnets = null
security_groups = null
ingress_sources = {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
}
assign_public_ip = 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, "")
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")
tags = optional(map(string), {})
}), {
create_bucket = true
bucket_name = null
bucket_prefix = "fleet-software-installers-"
s3_object_prefix = ""
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"
tags = {}
})
})
|
{
"autoscaling": {
"cpu_tracking_target_value": 80,
"max_capacity": 5,
"memory_tracking_target_value": 80,
"min_capacity": 1
},
"awslogs": {
"create": true,
"kms": {
"enabled": false,
"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",
"fargate_ephemeral_storage_kms": {
"enabled": false,
"kms_alias": "fleet-fargate-ephemeral-storage",
"kms_key_arn": null
},
"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": {
"enabled": false,
"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",
"security_groups": null,
"server_tls_enabled": false,
"service": {
"name": "fleet"
},
"sidecars": [],
"software_installers": {
"bucket_name": null,
"bucket_prefix": "fleet-software-installers-",
"create_bucket": true,
"create_kms_key": false,
"enable_bucket_versioning": false,
"expire_noncurrent_versions": true,
"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 | +| [ecs\_cluster](#input\_ecs\_cluster) | The config for the terraform-aws-modules/ecs/aws module. For published KMS blocks, legacy `enabled` is deprecated and still accepted; prefer `cmk_enabled`. |
object({
autoscaling_capacity_providers = optional(any, {})
cluster_configuration = optional(any, {
execute_command_configuration = {
logging = "OVERRIDE"
log_configuration = {
cloud_watch_log_group_name = "/aws/ecs/aws-ec2"
}
}
})
cluster_name = optional(string, "fleet")
cloudwatch_log_group = optional(object({
create = optional(bool, true)
retention_in_days = optional(number, 90)
kms = optional(object({
cmk_enabled = optional(bool, null)
enabled = optional(bool, null)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-ecs-cluster-logs")
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
})
}), {
create = true
retention_in_days = 90
kms = {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-ecs-cluster-logs"
}
})
cluster_settings = optional(any, {
"name" : "containerInsights",
"value" : "enabled",
})
create = optional(bool, true)
default_capacity_provider_use_fargate = optional(bool, true)
fargate_capacity_providers = optional(any, {
FARGATE = {
default_capacity_provider_strategy = {
weight = 100
}
}
FARGATE_SPOT = {
default_capacity_provider_strategy = {
weight = 0
}
}
})
tags = optional(map(string))
})
|
{
"autoscaling_capacity_providers": {},
"cloudwatch_log_group": {
"create": true,
"kms": {
"cmk_enabled": null,
"enabled": null,
"kms_alias": "fleet-ecs-cluster-logs",
"kms_key_arn": null
},
"retention_in_days": 90
},
"cluster_configuration": {
"execute_command_configuration": {
"log_configuration": {
"cloud_watch_log_group_name": "/aws/ecs/aws-ec2"
},
"logging": "OVERRIDE"
}
},
"cluster_name": "fleet",
"cluster_settings": {
"name": "containerInsights",
"value": "enabled"
},
"create": true,
"default_capacity_provider_use_fargate": true,
"fargate_capacity_providers": {
"FARGATE": {
"default_capacity_provider_strategy": {
"weight": 100
}
},
"FARGATE_SPOT": {
"default_capacity_provider_strategy": {
"weight": 0
}
}
},
"tags": {}
}
| 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.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")
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-server-private-key"
})
fargate_ephemeral_storage_kms = optional(object({
cmk_enabled = optional(bool, null)
enabled = optional(bool, null)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-fargate-ephemeral-storage")
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-fargate-ephemeral-storage"
})
server_tls_enabled = optional(bool, false)
service = optional(object({
name = optional(string, "fleet")
}), {
name = "fleet"
})
database = optional(object({
password_secret_arn = string
password_secret_kms_key_arn = optional(string, null)
user = string
database = string
address = string
rr_address = optional(string, null)
}), {
password_secret_arn = null
password_secret_kms_key_arn = null
user = null
database = null
address = null
rr_address = null
})
redis = optional(object({
address = string
use_tls = optional(bool, true)
}), {
address = null
use_tls = 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")
}), {
cmk_enabled = null
enabled = null
kms_key_arn = null
kms_alias = "fleet-application-logs"
})
}), {
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"
}
})
loadbalancer = optional(object({
arn = string
}), {
arn = null
})
extra_load_balancers = optional(list(any), [])
networking = optional(object({
subnets = optional(list(string), null)
security_groups = optional(list(string), null)
ingress_sources = optional(object({
cidr_blocks = optional(list(string), [])
ipv6_cidr_blocks = optional(list(string), [])
security_groups = optional(list(string), [])
prefix_list_ids = optional(list(string), [])
}), {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
})
assign_public_ip = optional(bool, false)
}), {
subnets = null
security_groups = null
ingress_sources = {
cidr_blocks = []
ipv6_cidr_blocks = []
security_groups = []
prefix_list_ids = []
}
assign_public_ip = 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, "")
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")
tags = optional(map(string), {})
}), {
create_bucket = true
bucket_name = null
bucket_prefix = "fleet-software-installers-"
s3_object_prefix = ""
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"
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,
"kms_alias": "fleet-application-logs",
"kms_key_arn": null
},
"name": null,
"prefix": "fleet",
"region": null,
"retention": 5
},
"cpu": 512,
"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",
"fargate_ephemeral_storage_kms": {
"cmk_enabled": null,
"enabled": null,
"kms_alias": "fleet-fargate-ephemeral-storage",
"kms_key_arn": null
},
"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": 4096,
"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,
"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",
"security_groups": null,
"server_tls_enabled": false,
"service": {
"name": "fleet"
},
"sidecars": [],
"software_installers": {
"bucket_name": null,
"bucket_prefix": "fleet-software-installers-",
"create_bucket": true,
"create_kms_key": false,
"enable_bucket_versioning": false,
"expire_noncurrent_versions": true,
"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` | n/a | yes | diff --git a/byo-vpc/byo-db/byo-ecs/.header.md b/byo-vpc/byo-db/byo-ecs/.header.md index 7df1e1a..7b36ada 100644 --- a/byo-vpc/byo-db/byo-ecs/.header.md +++ b/byo-vpc/byo-db/byo-ecs/.header.md @@ -10,13 +10,15 @@ When no KMS inputs are provided, behavior remains unchanged from prior versions. ## KMS Behavior -- `fleet_config.private_key_secret_kms.enabled = true` enables CMK encryption for the Fleet private key secret. -- `fleet_config.awslogs.kms.enabled = true` enables CMK encryption for the Fleet application log group when `fleet_config.awslogs.create = true`. +- `fleet_config.private_key_secret_kms.cmk_enabled = true` enables CMK encryption for the Fleet private key secret. +- `fleet_config.awslogs.kms.cmk_enabled = true` enables CMK encryption for the Fleet application log group when `fleet_config.awslogs.create = true`. - `fleet_config.software_installers.create_kms_key = true` enables CMK encryption for software installers S3 objects. For each feature: -- If KMS is enabled and no key ARN is provided, this module creates a CMK and alias. +- Set `cmk_enabled = false` or omit it to keep using the service-managed key. +- For KMS options that existed in published releases before this change, legacy `enabled` is deprecated but still accepted. Terraform plan/apply warns when it is used, and `cmk_enabled` takes precedence if both are set. +- If CMK use is enabled and no key ARN is provided, this module creates a CMK and alias. - 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`. @@ -26,7 +28,7 @@ IAM permissions for using these keys are managed in-module where possible. If a ```hcl module "fleet_byo_ecs" { - source = "github.com/fleetdm/fleet-terraform//byo-vpc/byo-db/byo-ecs?ref=tf-mod-byo-ecs-v1.14.0" + source = "github.com/fleetdm/fleet-terraform//byo-vpc/byo-db/byo-ecs?ref=tf-mod-byo-ecs-v1.15.0" ecs_cluster = "fleet" vpc_id = "vpc-1234567890abcdef0" @@ -54,13 +56,13 @@ module "fleet_byo_ecs" { } } private_key_secret_kms = { - enabled = true + cmk_enabled = true } awslogs = { create = true name = "/aws/ecs/fleet" kms = { - enabled = true + cmk_enabled = true } } software_installers = { @@ -76,14 +78,14 @@ module "fleet_byo_ecs" { ```hcl fleet_config = { private_key_secret_kms = { - enabled = true + cmk_enabled = true kms_key_arn = "arn:aws:kms:us-east-1:123456789012:key/11111111-2222-3333-4444-555555555555" } awslogs = { create = true name = "/aws/ecs/fleet" kms = { - enabled = true + cmk_enabled = true kms_key_arn = "arn:aws:kms:us-east-1:123456789012:key/99999999-aaaa-bbbb-cccc-dddddddddddd" } } @@ -99,6 +101,9 @@ fleet_config = { - Existing deployments are unchanged unless new KMS options are enabled. - Enabling KMS for application logs sets `kms_key_id` on the module-managed CloudWatch log group. - Enabling KMS for software installers updates S3 default encryption configuration. +- Breaking change: software installers CMK policy ownership now lives in `byo-ecs`. The CloudFront addon no longer manages that key policy. +- If CloudFront should access a module-managed or caller-provided software installers CMK, set `fleet_config.software_installers.cloudfront_distribution_arn` to the static distribution ARN so `byo-ecs` can add the required `cloudfront.amazonaws.com` key-policy statement. +- Under narrow external `kms_base_policy` configurations, this module now adds an explicit Fleet task-role statement to the software installers CMK policy so Fleet itself can continue using the key. - If you enable software installers KMS after objects already exist in the bucket, re-encrypt existing objects by copying them onto themselves: `aws s3 cp s3:// s3:// --recursive` - Enabling KMS for the private key secret updates the secret to use the configured CMK. @@ -106,12 +111,12 @@ fleet_config = { ### CloudWatch Logs KMS Migration Script -Use the helper script at `scripts/cloudwatch_logs_kms_migration.sh`. It is generic for any log group (for example Fleet app logs and ECS cluster exec logs), uses JSON output with `jq`, auto-discovers the active `kmsKeyId` and `AssociateKmsKey` cutoff, and deletes whole old streams by default. +Use the helper script at `../../../scripts/cloudwatch_logs_kms_migration.sh`. It is generic for any log group (for example Fleet app logs and ECS cluster exec logs), uses JSON output with `jq`, auto-discovers the active `kmsKeyId` and `AssociateKmsKey` cutoff, and deletes whole old streams by default. ```bash # Dry run: list old streams only -DELETE_OLD_STREAMS=false ./scripts/cloudwatch_logs_kms_migration.sh +DELETE_OLD_STREAMS=false ../../../scripts/cloudwatch_logs_kms_migration.sh # Delete old streams (default behavior) -./scripts/cloudwatch_logs_kms_migration.sh +../../../scripts/cloudwatch_logs_kms_migration.sh ``` diff --git a/byo-vpc/byo-db/byo-ecs/README.md b/byo-vpc/byo-db/byo-ecs/README.md index 25dcb23..90af9f6 100644 --- a/byo-vpc/byo-db/byo-ecs/README.md +++ b/byo-vpc/byo-db/byo-ecs/README.md @@ -10,13 +10,15 @@ When no KMS inputs are provided, behavior remains unchanged from prior versions. ## KMS Behavior -- `fleet_config.private_key_secret_kms.enabled = true` enables CMK encryption for the Fleet private key secret. -- `fleet_config.awslogs.kms.enabled = true` enables CMK encryption for the Fleet application log group when `fleet_config.awslogs.create = true`. +- `fleet_config.private_key_secret_kms.cmk_enabled = true` enables CMK encryption for the Fleet private key secret. +- `fleet_config.awslogs.kms.cmk_enabled = true` enables CMK encryption for the Fleet application log group when `fleet_config.awslogs.create = true`. - `fleet_config.software_installers.create_kms_key = true` enables CMK encryption for software installers S3 objects. For each feature: -- If KMS is enabled and no key ARN is provided, this module creates a CMK and alias. +- Set `cmk_enabled = false` or omit it to keep using the service-managed key. +- For KMS options that existed in published releases before this change, legacy `enabled` is deprecated but still accepted. Terraform plan/apply warns when it is used, and `cmk_enabled` takes precedence if both are set. +- If CMK use is enabled and no key ARN is provided, this module creates a CMK and alias. - 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`. @@ -26,7 +28,7 @@ IAM permissions for using these keys are managed in-module where possible. If a ```hcl module "fleet_byo_ecs" { - source = "github.com/fleetdm/fleet-terraform//byo-vpc/byo-db/byo-ecs?ref=tf-mod-byo-ecs-v1.14.0" + source = "github.com/fleetdm/fleet-terraform//byo-vpc/byo-db/byo-ecs?ref=tf-mod-byo-ecs-v1.15.0" ecs_cluster = "fleet" vpc_id = "vpc-1234567890abcdef0" @@ -54,13 +56,13 @@ module "fleet_byo_ecs" { } } private_key_secret_kms = { - enabled = true + cmk_enabled = true } awslogs = { create = true name = "/aws/ecs/fleet" kms = { - enabled = true + cmk_enabled = true } } software_installers = { @@ -76,14 +78,14 @@ module "fleet_byo_ecs" { ```hcl fleet_config = { private_key_secret_kms = { - enabled = true + cmk_enabled = true kms_key_arn = "arn:aws:kms:us-east-1:123456789012:key/11111111-2222-3333-4444-555555555555" } awslogs = { create = true name = "/aws/ecs/fleet" kms = { - enabled = true + cmk_enabled = true kms_key_arn = "arn:aws:kms:us-east-1:123456789012:key/99999999-aaaa-bbbb-cccc-dddddddddddd" } } @@ -99,6 +101,9 @@ fleet_config = { - Existing deployments are unchanged unless new KMS options are enabled. - Enabling KMS for application logs sets `kms_key_id` on the module-managed CloudWatch log group. - Enabling KMS for software installers updates S3 default encryption configuration. +- Breaking change: software installers CMK policy ownership now lives in `byo-ecs`. The CloudFront addon no longer manages that key policy. +- If CloudFront should access a module-managed or caller-provided software installers CMK, set `fleet_config.software_installers.cloudfront_distribution_arn` to the static distribution ARN so `byo-ecs` can add the required `cloudfront.amazonaws.com` key-policy statement. +- Under narrow external `kms_base_policy` configurations, this module now adds an explicit Fleet task-role statement to the software installers CMK policy so Fleet itself can continue using the key. - If you enable software installers KMS after objects already exist in the bucket, re-encrypt existing objects by copying them onto themselves: `aws s3 cp s3:// s3:// --recursive` - Enabling KMS for the private key secret updates the secret to use the configured CMK. @@ -106,19 +111,21 @@ fleet_config = { ### CloudWatch Logs KMS Migration Script -Use the helper script at `scripts/cloudwatch_logs_kms_migration.sh`. It is generic for any log group (for example Fleet app logs and ECS cluster exec logs), uses JSON output with `jq`, auto-discovers the active `kmsKeyId` and `AssociateKmsKey` cutoff, and deletes whole old streams by default. +Use the helper script at `../../../scripts/cloudwatch_logs_kms_migration.sh`. It is generic for any log group (for example Fleet app logs and ECS cluster exec logs), uses JSON output with `jq`, auto-discovers the active `kmsKeyId` and `AssociateKmsKey` cutoff, and deletes whole old streams by default. ```bash # Dry run: list old streams only -DELETE_OLD_STREAMS=false ./scripts/cloudwatch_logs_kms_migration.sh +DELETE_OLD_STREAMS=false ../../../scripts/cloudwatch_logs_kms_migration.sh # Delete old streams (default behavior) -./scripts/cloudwatch_logs_kms_migration.sh +../../../scripts/cloudwatch_logs_kms_migration.sh ``` ## Requirements -No requirements. +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.5.0 | ## Providers @@ -172,7 +179,9 @@ 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.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_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 | | [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source | @@ -182,7 +191,8 @@ 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. |
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({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-server-private-key")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-server-private-key"
})
server_tls_enabled = optional(bool, false)
service = optional(object({
name = optional(string, "fleet")
}), {
name = "fleet"
})
database = object({
password_secret_arn = string
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({
enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-application-logs")
}), {
enabled = false
kms_key_arn = null
kms_alias = "fleet-application-logs"
})
}), {
name = null
region = null
create = true
prefix = "fleet"
retention = 5
kms = {
enabled = false
kms_key_arn = null
kms_alias = "fleet-application-logs"
}
})
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, "")
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")
tags = optional(map(string), {})
}), {
create_bucket = true
bucket_name = null
bucket_prefix = "fleet-software-installers-"
s3_object_prefix = ""
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"
tags = {}
})
})
|
{
"autoscaling": {
"cpu_tracking_target_value": 80,
"max_capacity": 5,
"memory_tracking_target_value": 80,
"min_capacity": 1
},
"awslogs": {
"create": true,
"kms": {
"enabled": false,
"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": {
"enabled": false,
"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-",
"create_bucket": true,
"create_kms_key": false,
"enable_bucket_versioning": false,
"expire_noncurrent_versions": true,
"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.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 | +| [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 6deb521..f7b11f1 100644 --- a/byo-vpc/byo-db/byo-ecs/iam.tf +++ b/byo-vpc/byo-db/byo-ecs/iam.tf @@ -27,7 +27,16 @@ locals { resources = [local.private_key_secret_kms_key_arn] effect = "Allow" }] : [] - execution_kms_policy = local.private_key_secret_kms_policy + database_password_secret_kms_policy = var.fleet_config.database.password_secret_kms_key_arn != null ? [{ + sid = "AllowFleetDatabasePasswordSecretKMSAccess" + actions = [ + "kms:Decrypt", + "kms:DescribeKey" + ] + resources = [var.fleet_config.database.password_secret_kms_key_arn] + effect = "Allow" + }] : [] + execution_kms_policy = concat(local.private_key_secret_kms_policy, local.database_password_secret_kms_policy) } data "aws_iam_policy_document" "software_installers" { diff --git a/byo-vpc/byo-db/byo-ecs/main.tf b/byo-vpc/byo-db/byo-ecs/main.tf index 06a70b6..ebc9dcf 100644 --- a/byo-vpc/byo-db/byo-ecs/main.tf +++ b/byo-vpc/byo-db/byo-ecs/main.tf @@ -19,12 +19,15 @@ locals { credentialsParameter = var.fleet_config.repository_credentials } } : null - private_key_secret_create_kms_key = var.fleet_config.private_key_secret_kms.enabled == true && var.fleet_config.private_key_secret_kms.kms_key_arn == null - private_key_secret_kms_key_arn = var.fleet_config.private_key_secret_kms.enabled == true ? ( + private_key_secret_cmk_enabled = coalesce(var.fleet_config.private_key_secret_kms.cmk_enabled, var.fleet_config.private_key_secret_kms.enabled, false) + private_key_secret_create_kms_key = local.private_key_secret_cmk_enabled == true && var.fleet_config.private_key_secret_kms.kms_key_arn == null + private_key_secret_kms_key_arn = local.private_key_secret_cmk_enabled == true ? ( var.fleet_config.private_key_secret_kms.kms_key_arn != null ? var.fleet_config.private_key_secret_kms.kms_key_arn : aws_kms_key.private_key_secret[0].arn ) : null - application_logs_create_kms_key = var.fleet_config.awslogs.create == true && var.fleet_config.awslogs.kms.enabled == true && var.fleet_config.awslogs.kms.kms_key_arn == null - application_logs_kms_key_arn = var.fleet_config.awslogs.create == true && var.fleet_config.awslogs.kms.enabled == true ? ( + + application_logs_cmk_enabled = coalesce(var.fleet_config.awslogs.kms.cmk_enabled, var.fleet_config.awslogs.kms.enabled, false) + application_logs_create_kms_key = var.fleet_config.awslogs.create == true && local.application_logs_cmk_enabled == true && var.fleet_config.awslogs.kms.kms_key_arn == null + application_logs_kms_key_arn = var.fleet_config.awslogs.create == true && local.application_logs_cmk_enabled == true ? ( var.fleet_config.awslogs.kms.kms_key_arn != null ? var.fleet_config.awslogs.kms.kms_key_arn : aws_kms_key.application_logs[0].arn ) : null software_installers_create_kms_key = var.fleet_config.software_installers.create_kms_key == true && var.fleet_config.software_installers.kms_key_arn == null @@ -34,6 +37,127 @@ locals { software_installers_kms_key_id = var.fleet_config.software_installers.create_kms_key == true || var.fleet_config.software_installers.kms_key_arn != null ? ( var.fleet_config.software_installers.kms_key_arn != null ? data.aws_kms_key.software_installers_provided[0].key_id : aws_kms_key.software_installers[0].id ) : null + task_role_kms_principal_arn = var.fleet_config.iam_role_arn != null ? var.fleet_config.iam_role_arn : aws_iam_role.main[0].arn + kms_base_policy_statements = var.kms_base_policy != null ? var.kms_base_policy : [ + { + sid = "EnableRootPermissions" + effect = "Allow" + principals = { + type = "AWS" + identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"] + } + actions = ["kms:*"] + resources = ["*"] + conditions = [] + } + ] + kms_service_statements = { + cloudwatch_logs = { + sid = "AllowCloudWatchLogsUseOfTheKey" + effect = "Allow" + principals = { + type = "Service" + identifiers = ["logs.${data.aws_region.current.id}.amazonaws.com"] + } + actions = [ + "kms:Encrypt*", + "kms:Decrypt*", + "kms:ReEncrypt*", + "kms:GenerateDataKey*", + "kms:Describe*" + ] + resources = ["*"] + conditions = [] + } + secretsmanager = { + sid = "AllowSecretsManagerUseOfTheKey" + effect = "Allow" + principals = { + type = "Service" + identifiers = ["secretsmanager.amazonaws.com"] + } + actions = [ + "kms:Encrypt", + "kms:Decrypt", + "kms:ReEncrypt*", + "kms:GenerateDataKey*", + "kms:CreateGrant", + "kms:DescribeKey" + ] + resources = ["*"] + conditions = [] + } + execution_role = { + sid = "AllowExecutionRoleDecrypt" + effect = "Allow" + principals = { + type = "AWS" + identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role/${var.fleet_config.iam.execution.name}"] + } + actions = [ + "kms:Decrypt", + "kms:DescribeKey" + ] + resources = ["*"] + conditions = [] + } + } + software_installers_kms_service_statements = var.fleet_config.software_installers.cloudfront_distribution_arn != null ? [ + { + sid = "AllowCloudFrontDistributionUseOfTheKey" + effect = "Allow" + principals = { + type = "Service" + identifiers = ["cloudfront.amazonaws.com"] + } + actions = [ + "kms:Decrypt", + "kms:Encrypt", + "kms:GenerateDataKey*" + ] + resources = ["*"] + conditions = [ + { + test = "StringEquals" + variable = "AWS:SourceArn" + values = [var.fleet_config.software_installers.cloudfront_distribution_arn] + } + ] + } + ] : [] + software_installers_kms_task_role_statements = [ + { + sid = "AllowFleetTaskRoleUseOfTheKey" + effect = "Allow" + principals = { + type = "AWS" + identifiers = [local.task_role_kms_principal_arn] + } + actions = [ + "kms:ReEncrypt*", + "kms:GenerateDataKey*", + "kms:Encrypt*", + "kms:Describe*", + "kms:Decrypt*" + ] + resources = ["*"] + conditions = [] + } + ] +} + +check "deprecated_fleet_config_private_key_secret_kms_enabled" { + assert { + condition = var.fleet_config.private_key_secret_kms.enabled == null + error_message = "fleet_config.private_key_secret_kms.enabled is deprecated; use fleet_config.private_key_secret_kms.cmk_enabled instead." + } +} + +check "deprecated_fleet_config_awslogs_kms_enabled" { + assert { + condition = var.fleet_config.awslogs.kms.enabled == null + error_message = "fleet_config.awslogs.kms.enabled is deprecated; use fleet_config.awslogs.kms.cmk_enabled instead." + } } data "aws_region" "current" {} @@ -256,34 +380,30 @@ resource "aws_appautoscaling_policy" "ecs_policy_cpu" { data "aws_iam_policy_document" "application_logs_kms" { count = local.application_logs_create_kms_key == true ? 1 : 0 - statement { - sid = "EnableRootPermissions" - effect = "Allow" - actions = [ - "kms:*" - ] - principals { - type = "AWS" - identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"] + dynamic "statement" { + for_each = concat( + local.kms_base_policy_statements, + var.fleet_config.awslogs.kms.extra_kms_policies, + [local.kms_service_statements.cloudwatch_logs] + ) + 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 + } + } } - resources = ["*"] - } - - statement { - sid = "AllowCloudWatchLogsUseOfTheKey" - effect = "Allow" - actions = [ - "kms:Encrypt*", - "kms:Decrypt*", - "kms:ReEncrypt*", - "kms:GenerateDataKey*", - "kms:Describe*" - ] - principals { - type = "Service" - identifiers = ["logs.${data.aws_region.current.id}.amazonaws.com"] - } - resources = ["*"] } } @@ -360,6 +480,7 @@ resource "aws_kms_key" "software_installers" { count = local.software_installers_create_kms_key == true ? 1 : 0 description = "CMK for Fleet software installers S3 bucket object encryption." enable_key_rotation = true + policy = data.aws_iam_policy_document.software_installers_kms[0].json } resource "aws_kms_alias" "software_installers" { @@ -372,6 +493,69 @@ resource "aws_kms_key" "private_key_secret" { count = local.private_key_secret_create_kms_key == true ? 1 : 0 description = "CMK for Fleet server private key secret encryption in Secrets Manager." enable_key_rotation = true + policy = data.aws_iam_policy_document.private_key_secret_kms[0].json +} + +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] + ) + 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 + } + } + } + } +} + +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 + ) + 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_alias" "private_key_secret" { diff --git a/byo-vpc/byo-db/byo-ecs/variables.tf b/byo-vpc/byo-db/byo-ecs/variables.tf index 2e31f16..d875008 100644 --- a/byo-vpc/byo-db/byo-ecs/variables.tf +++ b/byo-vpc/byo-db/byo-ecs/variables.tf @@ -9,6 +9,26 @@ variable "vpc_id" { default = null } +variable "kms_base_policy" { + type = 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) + })), []) + })) + default = null + description = "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." +} + variable "fleet_config" { type = object({ task_mem = optional(number, null) @@ -34,13 +54,17 @@ variable "fleet_config" { repository_credentials = optional(string, "") private_key_secret_name = optional(string, "fleet-server-private-key") private_key_secret_kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-server-private-key") + 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), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-server-private-key" + 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({ @@ -49,11 +73,12 @@ variable "fleet_config" { name = "fleet" }) database = object({ - password_secret_arn = string - user = string - database = string - address = string - rr_address = optional(string, null) + 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 @@ -66,13 +91,17 @@ variable "fleet_config" { prefix = optional(string, "fleet") retention = optional(number, 5) kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-application-logs") + 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), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] }) }), { name = null @@ -81,9 +110,11 @@ variable "fleet_config" { prefix = "fleet" retention = 5 kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] } }) loadbalancer = object({ @@ -135,24 +166,28 @@ variable "fleet_config" { 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 = {} }) }) @@ -178,9 +213,11 @@ variable "fleet_config" { repository_credentials = "" private_key_secret_name = "fleet-server-private-key" private_key_secret_kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-server-private-key" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-server-private-key" + extra_kms_policies = [] } server_tls_enabled = false service = { @@ -204,9 +241,11 @@ variable "fleet_config" { prefix = "fleet" retention = 5 kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] } } loadbalancer = { @@ -245,19 +284,21 @@ variable "fleet_config" { 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 = {} } } - description = "The configuration object for Fleet itself. Fields that default to null will have their respective resources created if not specified." + 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.ephemeral_storage == null || (var.fleet_config.ephemeral_storage.size_in_gib >= 21 && var.fleet_config.ephemeral_storage.size_in_gib <= 200) + 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." } } diff --git a/byo-vpc/byo-db/byo-ecs/versions.tf b/byo-vpc/byo-db/byo-ecs/versions.tf new file mode 100644 index 0000000..fd126d1 --- /dev/null +++ b/byo-vpc/byo-db/byo-ecs/versions.tf @@ -0,0 +1,3 @@ +terraform { + required_version = ">= 1.5.0" +} diff --git a/byo-vpc/byo-db/main.tf b/byo-vpc/byo-db/main.tf index 8c82dcf..49c852f 100644 --- a/byo-vpc/byo-db/main.tf +++ b/byo-vpc/byo-db/main.tf @@ -70,13 +70,15 @@ locals { } # Accept both legacy map input and list input for cluster settings. normalized_cluster_settings = var.ecs_cluster.cluster_settings == null ? [] : flatten([var.ecs_cluster.cluster_settings]) - fargate_ephemeral_storage_create_kms_key = var.fleet_config.fargate_ephemeral_storage_kms.enabled == true && var.fleet_config.fargate_ephemeral_storage_kms.kms_key_arn == null - fargate_ephemeral_storage_kms_key_arn = var.fleet_config.fargate_ephemeral_storage_kms.enabled == true ? ( + fargate_ephemeral_storage_cmk_enabled = coalesce(var.fleet_config.fargate_ephemeral_storage_kms.cmk_enabled, var.fleet_config.fargate_ephemeral_storage_kms.enabled, false) + fargate_ephemeral_storage_create_kms_key = local.fargate_ephemeral_storage_cmk_enabled == true && var.fleet_config.fargate_ephemeral_storage_kms.kms_key_arn == null + fargate_ephemeral_storage_kms_key_arn = local.fargate_ephemeral_storage_cmk_enabled == true ? ( var.fleet_config.fargate_ephemeral_storage_kms.kms_key_arn != null ? var.fleet_config.fargate_ephemeral_storage_kms.kms_key_arn : aws_kms_key.fargate_ephemeral_storage[0].arn ) : null cluster_cloudwatch_log_group_name = coalesce(try(var.ecs_cluster.cluster_configuration.execute_command_configuration.log_configuration.cloud_watch_log_group_name, null), "/aws/ecs/${var.ecs_cluster.cluster_name}") - cluster_cloudwatch_log_group_create_kms_key = var.ecs_cluster.cloudwatch_log_group.create == true && var.ecs_cluster.cloudwatch_log_group.kms.enabled == true && var.ecs_cluster.cloudwatch_log_group.kms.kms_key_arn == null - cluster_cloudwatch_log_group_kms_key_arn = var.ecs_cluster.cloudwatch_log_group.create == true && var.ecs_cluster.cloudwatch_log_group.kms.enabled == true ? ( + cluster_cloudwatch_log_group_cmk_enabled = coalesce(var.ecs_cluster.cloudwatch_log_group.kms.cmk_enabled, var.ecs_cluster.cloudwatch_log_group.kms.enabled, false) + cluster_cloudwatch_log_group_create_kms_key = var.ecs_cluster.cloudwatch_log_group.create == true && local.cluster_cloudwatch_log_group_cmk_enabled == true && var.ecs_cluster.cloudwatch_log_group.kms.kms_key_arn == null + cluster_cloudwatch_log_group_kms_key_arn = var.ecs_cluster.cloudwatch_log_group.create == true && local.cluster_cloudwatch_log_group_cmk_enabled == true ? ( var.ecs_cluster.cloudwatch_log_group.kms.kms_key_arn != null ? var.ecs_cluster.cloudwatch_log_group.kms.kms_key_arn : aws_kms_key.cluster_cloudwatch_log_group[0].arn ) : null ecs_cluster_configuration = merge( @@ -103,6 +105,105 @@ locals { ) } : {} ) + kms_base_policy_statements = var.kms_base_policy != null ? var.kms_base_policy : [ + { + sid = "EnableRootPermissions" + effect = "Allow" + principals = { + type = "AWS" + identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"] + } + actions = ["kms:*"] + resources = ["*"] + conditions = [] + } + ] + kms_service_statements = { + cloudwatch_logs = { + sid = "AllowCloudWatchLogsUseOfTheKey" + effect = "Allow" + principals = { + type = "Service" + identifiers = ["logs.${data.aws_region.current.id}.amazonaws.com"] + } + actions = [ + "kms:Encrypt*", + "kms:Decrypt*", + "kms:ReEncrypt*", + "kms:GenerateDataKey*", + "kms:Describe*" + ] + resources = ["*"] + conditions = [] + } + fargate_generate_data_key = { + sid = "AllowGenerateDataKeyWithoutPlaintextForFargateTasks" + effect = "Allow" + principals = { + type = "Service" + identifiers = ["fargate.amazonaws.com"] + } + actions = [ + "kms:GenerateDataKeyWithoutPlaintext" + ] + resources = ["*"] + conditions = [ + { + test = "StringEquals" + variable = "kms:EncryptionContext:aws:ecs:clusterAccount" + values = [data.aws_caller_identity.current.account_id] + }, + { + test = "StringEquals" + variable = "kms:EncryptionContext:aws:ecs:clusterName" + values = [var.ecs_cluster.cluster_name] + } + ] + } + fargate_create_grant = { + sid = "AllowCreateGrantForFargateTasks" + effect = "Allow" + principals = { + type = "Service" + identifiers = ["fargate.amazonaws.com"] + } + actions = [ + "kms:CreateGrant" + ] + resources = ["*"] + conditions = [ + { + test = "StringEquals" + variable = "kms:EncryptionContext:aws:ecs:clusterAccount" + values = [data.aws_caller_identity.current.account_id] + }, + { + test = "StringEquals" + variable = "kms:EncryptionContext:aws:ecs:clusterName" + values = [var.ecs_cluster.cluster_name] + }, + { + test = "ForAllValues:StringEquals" + variable = "kms:GrantOperations" + values = ["Decrypt"] + } + ] + } + } +} + +check "deprecated_ecs_cluster_cloudwatch_log_group_kms_enabled" { + assert { + condition = var.ecs_cluster.cloudwatch_log_group.kms.enabled == null + error_message = "ecs_cluster.cloudwatch_log_group.kms.enabled is deprecated; use ecs_cluster.cloudwatch_log_group.kms.cmk_enabled instead." + } +} + +check "deprecated_fleet_config_fargate_ephemeral_storage_kms_enabled" { + assert { + condition = var.fleet_config.fargate_ephemeral_storage_kms.enabled == null + error_message = "fleet_config.fargate_ephemeral_storage_kms.enabled is deprecated; use fleet_config.fargate_ephemeral_storage_kms.cmk_enabled instead." + } } data "aws_caller_identity" "current" {} @@ -112,6 +213,7 @@ data "aws_region" "current" {} module "ecs" { source = "./byo-ecs" ecs_cluster = module.cluster.cluster_name + kms_base_policy = var.kms_base_policy fleet_config = local.fleet_config migration_config = var.migration_config vpc_id = var.vpc_id @@ -138,67 +240,32 @@ module "cluster" { data "aws_iam_policy_document" "fargate_ephemeral_storage_kms" { count = local.fargate_ephemeral_storage_create_kms_key == true ? 1 : 0 - statement { - sid = "EnableRootPermissions" - effect = "Allow" - actions = [ - "kms:*" - ] - principals { - type = "AWS" - identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"] - } - resources = ["*"] - } - - statement { - sid = "AllowGenerateDataKeyWithoutPlaintextForFargateTasks" - effect = "Allow" - actions = [ - "kms:GenerateDataKeyWithoutPlaintext" - ] - principals { - type = "Service" - identifiers = ["fargate.amazonaws.com"] - } - resources = ["*"] - condition { - test = "StringEquals" - variable = "kms:EncryptionContext:aws:ecs:clusterAccount" - values = [data.aws_caller_identity.current.account_id] - } - condition { - test = "StringEquals" - variable = "kms:EncryptionContext:aws:ecs:clusterName" - values = [var.ecs_cluster.cluster_name] - } - } - - statement { - sid = "AllowCreateGrantForFargateTasks" - effect = "Allow" - actions = [ - "kms:CreateGrant" - ] - principals { - type = "Service" - identifiers = ["fargate.amazonaws.com"] - } - resources = ["*"] - condition { - test = "StringEquals" - variable = "kms:EncryptionContext:aws:ecs:clusterAccount" - values = [data.aws_caller_identity.current.account_id] - } - condition { - test = "StringEquals" - variable = "kms:EncryptionContext:aws:ecs:clusterName" - values = [var.ecs_cluster.cluster_name] - } - condition { - test = "ForAllValues:StringEquals" - variable = "kms:GrantOperations" - values = ["Decrypt"] + 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, + ] + ) + 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 + } + } } } } @@ -219,34 +286,30 @@ resource "aws_kms_alias" "fargate_ephemeral_storage" { data "aws_iam_policy_document" "cluster_cloudwatch_log_group_kms" { count = local.cluster_cloudwatch_log_group_create_kms_key == true ? 1 : 0 - statement { - sid = "EnableRootPermissions" - effect = "Allow" - actions = [ - "kms:*" - ] - principals { - type = "AWS" - identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"] + 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] + ) + 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 + } + } } - resources = ["*"] - } - - statement { - sid = "AllowCloudWatchLogsUseOfTheKey" - effect = "Allow" - actions = [ - "kms:Encrypt*", - "kms:Decrypt*", - "kms:ReEncrypt*", - "kms:GenerateDataKey*", - "kms:Describe*" - ] - principals { - type = "Service" - identifiers = ["logs.${data.aws_region.current.id}.amazonaws.com"] - } - resources = ["*"] } } diff --git a/byo-vpc/byo-db/variables.tf b/byo-vpc/byo-db/variables.tf index f42e184..0da9b91 100644 --- a/byo-vpc/byo-db/variables.tf +++ b/byo-vpc/byo-db/variables.tf @@ -2,6 +2,26 @@ variable "vpc_id" { type = string } +variable "kms_base_policy" { + type = 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) + })), []) + })) + default = null + description = "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." +} + variable "ecs_cluster" { type = object({ autoscaling_capacity_providers = optional(any, {}) @@ -18,21 +38,27 @@ variable "ecs_cluster" { create = optional(bool, true) retention_in_days = optional(number, 90) kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-ecs-cluster-logs") + cmk_enabled = optional(bool, null) + enabled = optional(bool, null) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-ecs-cluster-logs") + extra_kms_policies = optional(list(any), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-ecs-cluster-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-ecs-cluster-logs" + extra_kms_policies = [] }) }), { create = true retention_in_days = 90 kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-ecs-cluster-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-ecs-cluster-logs" + extra_kms_policies = [] } }) cluster_settings = optional(any, { @@ -70,7 +96,8 @@ variable "ecs_cluster" { create = true retention_in_days = 90 kms = { - enabled = false + cmk_enabled = null + enabled = null kms_key_arn = null kms_alias = "fleet-ecs-cluster-logs" } @@ -95,7 +122,7 @@ variable "ecs_cluster" { } tags = {} } - description = "The config for the terraform-aws-modules/ecs/aws module" + 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 } @@ -125,22 +152,30 @@ variable "fleet_config" { repository_credentials = optional(string, "") private_key_secret_name = optional(string, "fleet-server-private-key") private_key_secret_kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-server-private-key") + 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), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-server-private-key" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-server-private-key" + extra_kms_policies = [] }) fargate_ephemeral_storage_kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-fargate-ephemeral-storage") + cmk_enabled = optional(bool, null) + enabled = optional(bool, null) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-fargate-ephemeral-storage") + extra_kms_policies = optional(list(any), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-fargate-ephemeral-storage" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-fargate-ephemeral-storage" + extra_kms_policies = [] }) server_tls_enabled = optional(bool, false) service = optional(object({ @@ -149,17 +184,19 @@ variable "fleet_config" { name = "fleet" }) database = optional(object({ - password_secret_arn = string - user = string - database = string - address = string - rr_address = optional(string, null) + password_secret_arn = string + password_secret_kms_key_arn = optional(string, null) + user = string + database = string + address = string + rr_address = optional(string, null) }), { - password_secret_arn = null - user = null - database = null - address = null - rr_address = null + password_secret_arn = null + password_secret_kms_key_arn = null + user = null + database = null + address = null + rr_address = null }) redis = optional(object({ address = string @@ -175,13 +212,17 @@ variable "fleet_config" { prefix = optional(string, "fleet") retention = optional(number, 5) kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-application-logs") + 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), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] }) }), { name = null @@ -190,9 +231,11 @@ variable "fleet_config" { prefix = "fleet" retention = 5 kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] } }) loadbalancer = optional(object({ @@ -261,24 +304,28 @@ variable "fleet_config" { 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 = {} }) }) @@ -305,14 +352,18 @@ variable "fleet_config" { repository_credentials = "" private_key_secret_name = "fleet-server-private-key" private_key_secret_kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-server-private-key" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-server-private-key" + extra_kms_policies = [] } fargate_ephemeral_storage_kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-fargate-ephemeral-storage" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-fargate-ephemeral-storage" + extra_kms_policies = [] } server_tls_enabled = false service = { @@ -336,9 +387,11 @@ variable "fleet_config" { prefix = "fleet" retention = 5 kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] } } loadbalancer = { @@ -377,16 +430,18 @@ variable "fleet_config" { 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 = {} } } - description = "The configuration object for Fleet itself. Fields that default to null will have their respective resources created if not specified." + 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 } diff --git a/byo-vpc/byo-db/versions.tf b/byo-vpc/byo-db/versions.tf new file mode 100644 index 0000000..fd126d1 --- /dev/null +++ b/byo-vpc/byo-db/versions.tf @@ -0,0 +1,3 @@ +terraform { + required_version = ">= 1.5.0" +} diff --git a/byo-vpc/example/main.tf b/byo-vpc/example/main.tf index 021e6ab..319886f 100644 --- a/byo-vpc/example/main.tf +++ b/byo-vpc/example/main.tf @@ -1,5 +1,5 @@ terraform { - required_version = ">= 1.3.8" + required_version = ">= 1.5.0" required_providers { aws = { source = "hashicorp/aws" diff --git a/byo-vpc/main.tf b/byo-vpc/main.tf index c3e8b9b..80af27f 100644 --- a/byo-vpc/main.tf +++ b/byo-vpc/main.tf @@ -9,26 +9,470 @@ locals { for index, replica_number in local.replica_numbers : replica_number => {} if index < var.rds_config.replicas } + rds_final_snapshot_identifier = var.rds_config.skip_final_snapshot ? null : coalesce( + var.rds_config.final_snapshot_identifier, + format("final-%s-%s", var.rds_config.name, random_id.rds_final_snapshot_identifier[0].hex) + ) + rds_security_group_rules = merge( + { + for idx, sg_id in concat(tolist(module.byo-db.byo-ecs.non_circular.security_groups), var.rds_config.allowed_security_groups) : + "allowed_security_group_${idx}" => { + type = "ingress" + source_security_group_id = sg_id + description = "Ingress from allowed security group ${sg_id}" + } + }, + length(var.rds_config.allowed_cidr_blocks) > 0 ? { + allowed_cidr_blocks = { + type = "ingress" + cidr_blocks = var.rds_config.allowed_cidr_blocks + description = "Ingress from allowed CIDR blocks" + } + } : {} + ) + + rds_storage_cmk_enabled = var.rds_config.storage_kms.cmk_enabled + rds_storage_create_kms_key = local.rds_storage_cmk_enabled == true && var.rds_config.storage_kms.kms_key_arn == null + rds_storage_kms_key_arn = local.rds_storage_cmk_enabled == true ? ( + var.rds_config.storage_kms.kms_key_arn != null ? var.rds_config.storage_kms.kms_key_arn : aws_kms_key.rds_storage[0].arn + ) : null + + rds_password_secret_cmk_enabled = var.rds_config.password_secret_kms.cmk_enabled + rds_password_secret_create_kms_key = local.rds_password_secret_cmk_enabled == true && var.rds_config.password_secret_kms.kms_key_arn == null + rds_password_secret_kms_key_arn = local.rds_password_secret_cmk_enabled == true ? ( + var.rds_config.password_secret_kms.kms_key_arn != null ? var.rds_config.password_secret_kms.kms_key_arn : aws_kms_key.rds_password_secret[0].arn + ) : null + + rds_observability_cmk_enabled = var.rds_config.observability.kms.cmk_enabled + rds_observability_create_kms_key = var.rds_config.observability.performance_insights_enabled == true && local.rds_observability_cmk_enabled == true && var.rds_config.observability.kms.kms_key_arn == null + rds_observability_kms_key_arn = var.rds_config.observability.performance_insights_enabled == true && local.rds_observability_cmk_enabled == true ? ( + var.rds_config.observability.kms.kms_key_arn != null ? var.rds_config.observability.kms.kms_key_arn : aws_kms_key.rds_observability[0].arn + ) : null + + rds_performance_insights_enabled = var.rds_config.observability.performance_insights_enabled + rds_performance_insights_retention_period = local.rds_performance_insights_enabled == true ? ( + var.rds_config.observability.database_insights_mode == "advanced" ? coalesce(var.rds_config.observability.retention_period, 465) : var.rds_config.observability.retention_period + ) : null + rds_cluster_monitoring_interval = var.rds_config.monitoring_interval + + rds_cloudwatch_log_group_cmk_enabled = var.rds_config.cloudwatch_log_group.kms.cmk_enabled + rds_cloudwatch_log_group_create_kms_key = length(var.rds_config.enabled_cloudwatch_logs_exports) > 0 && local.rds_cloudwatch_log_group_cmk_enabled == true && var.rds_config.cloudwatch_log_group.kms.kms_key_arn == null + rds_cloudwatch_log_group_kms_key_arn = length(var.rds_config.enabled_cloudwatch_logs_exports) > 0 && local.rds_cloudwatch_log_group_cmk_enabled == true ? ( + var.rds_config.cloudwatch_log_group.kms.kms_key_arn != null ? var.rds_config.cloudwatch_log_group.kms.kms_key_arn : aws_kms_key.rds_cloudwatch_log_group[0].arn + ) : null + rds_manage_cloudwatch_log_groups = length(var.rds_config.enabled_cloudwatch_logs_exports) > 0 && ( + var.rds_config.cloudwatch_log_group.retention_in_days != null || + var.rds_config.cloudwatch_log_group.skip_destroy == true || + local.rds_cloudwatch_log_group_cmk_enabled == true + ) + + redis_at_rest_cmk_enabled = var.redis_config.at_rest_kms.cmk_enabled + redis_at_rest_create_kms_key = var.redis_config.at_rest_encryption_enabled == true && local.redis_at_rest_cmk_enabled == true && var.redis_config.at_rest_kms.kms_key_arn == null + redis_at_rest_kms_key_arn = var.redis_config.at_rest_encryption_enabled == true && local.redis_at_rest_cmk_enabled == true ? ( + var.redis_config.at_rest_kms.kms_key_arn != null ? var.redis_config.at_rest_kms.kms_key_arn : aws_kms_key.redis_at_rest[0].arn + ) : null + + redis_cloudwatch_log_group_destinations = { + for idx, config in var.redis_config.log_delivery_configuration : + tostring(idx) => config if try(lower(config.destination_type), "") == "cloudwatch-logs" && try(config.destination, null) != null + } + redis_cloudwatch_log_group_cmk_enabled = var.redis_config.cloudwatch_log_group.kms.cmk_enabled + redis_cloudwatch_log_group_create_kms_key = length(local.redis_cloudwatch_log_group_destinations) > 0 && local.redis_cloudwatch_log_group_cmk_enabled == true && var.redis_config.cloudwatch_log_group.kms.kms_key_arn == null + redis_cloudwatch_log_group_kms_key_arn = length(local.redis_cloudwatch_log_group_destinations) > 0 && local.redis_cloudwatch_log_group_cmk_enabled == true ? ( + var.redis_config.cloudwatch_log_group.kms.kms_key_arn != null ? var.redis_config.cloudwatch_log_group.kms.kms_key_arn : aws_kms_key.redis_cloudwatch_log_group[0].arn + ) : null + # Keep the decision to manage Redis log groups based only on input-known values + # so it can be used safely in for_each. + redis_manage_cloudwatch_log_groups = length(local.redis_cloudwatch_log_group_destinations) > 0 && ( + var.redis_config.cloudwatch_log_group.retention_in_days != null || + var.redis_config.cloudwatch_log_group.skip_destroy == true || + local.redis_cloudwatch_log_group_cmk_enabled == true + ) + kms_base_policy_statements = var.kms_base_policy != null ? var.kms_base_policy : [ + { + sid = "EnableRootPermissions" + effect = "Allow" + principals = { + type = "AWS" + identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"] + } + actions = ["kms:*"] + resources = ["*"] + conditions = [] + } + ] + kms_service_statements = { + rds = { + sid = "AllowRDSUseOfTheKey" + effect = "Allow" + principals = { + type = "Service" + identifiers = ["rds.amazonaws.com"] + } + actions = [ + "kms:Encrypt", + "kms:Decrypt", + "kms:ReEncrypt*", + "kms:GenerateDataKey*", + "kms:CreateGrant", + "kms:DescribeKey" + ] + resources = ["*"] + conditions = [] + } + secretsmanager = { + sid = "AllowSecretsManagerUseOfTheKey" + effect = "Allow" + principals = { + type = "Service" + identifiers = ["secretsmanager.amazonaws.com"] + } + actions = [ + "kms:Encrypt", + "kms:Decrypt", + "kms:ReEncrypt*", + "kms:GenerateDataKey*", + "kms:CreateGrant", + "kms:DescribeKey" + ] + resources = ["*"] + conditions = [] + } + cloudwatch_logs = { + sid = "AllowCloudWatchLogsUseOfTheKey" + effect = "Allow" + principals = { + type = "Service" + identifiers = ["logs.${data.aws_region.current.id}.amazonaws.com"] + } + actions = [ + "kms:Encrypt*", + "kms:Decrypt*", + "kms:ReEncrypt*", + "kms:GenerateDataKey*", + "kms:Describe*" + ] + resources = ["*"] + conditions = [] + } + elasticache = { + sid = "AllowElastiCacheUseOfTheKey" + effect = "Allow" + principals = { + type = "Service" + identifiers = ["elasticache.amazonaws.com"] + } + actions = [ + "kms:Encrypt", + "kms:Decrypt", + "kms:ReEncrypt*", + "kms:GenerateDataKey*", + "kms:CreateGrant", + "kms:DescribeKey" + ] + resources = ["*"] + conditions = [] + } + execution_role = { + sid = "AllowExecutionRoleDecrypt" + effect = "Allow" + principals = { + type = "AWS" + identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role/${var.fleet_config.iam.execution.name}"] + } + actions = [ + "kms:Decrypt", + "kms:DescribeKey" + ] + resources = ["*"] + conditions = [] + } + } +} + +data "aws_caller_identity" "current" {} +data "aws_partition" "current" {} +data "aws_region" "current" {} + +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] + ) + 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 + } + } + } + } +} + +resource "aws_kms_key" "rds_storage" { + count = local.rds_storage_create_kms_key == true ? 1 : 0 + description = "CMK for Aurora storage encryption." + enable_key_rotation = true + policy = data.aws_iam_policy_document.rds_storage_kms[0].json +} + +resource "aws_kms_alias" "rds_storage" { + count = local.rds_storage_create_kms_key == true ? 1 : 0 + target_key_id = aws_kms_key.rds_storage[0].id + name = "alias/${var.rds_config.storage_kms.kms_alias}" +} + +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] + ) + 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 + } + } + } + } +} + +resource "aws_kms_key" "rds_password_secret" { + count = local.rds_password_secret_create_kms_key == true ? 1 : 0 + description = "CMK for Aurora database password secret encryption." + enable_key_rotation = true + policy = data.aws_iam_policy_document.rds_password_secret_kms[0].json +} + +resource "aws_kms_alias" "rds_password_secret" { + count = local.rds_password_secret_create_kms_key == true ? 1 : 0 + target_key_id = aws_kms_key.rds_password_secret[0].id + name = "alias/${var.rds_config.password_secret_kms.kms_alias}" +} + +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] + ) + 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 + } + } + } + } +} + +resource "aws_kms_key" "rds_observability" { + count = local.rds_observability_create_kms_key == true ? 1 : 0 + description = "CMK for Aurora Performance Insights and Database Insights encryption." + enable_key_rotation = true + policy = data.aws_iam_policy_document.rds_observability_kms[0].json +} + +resource "aws_kms_alias" "rds_observability" { + count = local.rds_observability_create_kms_key == true ? 1 : 0 + target_key_id = aws_kms_key.rds_observability[0].id + name = "alias/${var.rds_config.observability.kms.kms_alias}" +} + +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] + ) + 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 + } + } + } + } +} + +resource "aws_kms_key" "rds_cloudwatch_log_group" { + count = local.rds_cloudwatch_log_group_create_kms_key == true ? 1 : 0 + description = "CMK for Aurora exported CloudWatch Logs encryption." + enable_key_rotation = true + policy = data.aws_iam_policy_document.rds_cloudwatch_log_group_kms[0].json +} + +resource "aws_kms_alias" "rds_cloudwatch_log_group" { + count = local.rds_cloudwatch_log_group_create_kms_key == true ? 1 : 0 + target_key_id = aws_kms_key.rds_cloudwatch_log_group[0].id + name = "alias/${var.rds_config.cloudwatch_log_group.kms.kms_alias}" +} + +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] + ) + 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 + } + } + } + } +} + +resource "aws_kms_key" "redis_at_rest" { + count = local.redis_at_rest_create_kms_key == true ? 1 : 0 + description = "CMK for ElastiCache at-rest encryption." + enable_key_rotation = true + policy = data.aws_iam_policy_document.redis_at_rest_kms[0].json +} + +resource "aws_kms_alias" "redis_at_rest" { + count = local.redis_at_rest_create_kms_key == true ? 1 : 0 + target_key_id = aws_kms_key.redis_at_rest[0].id + name = "alias/${var.redis_config.at_rest_kms.kms_alias}" +} + +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] + ) + 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 + } + } + } + } +} + +resource "aws_kms_key" "redis_cloudwatch_log_group" { + count = local.redis_cloudwatch_log_group_create_kms_key == true ? 1 : 0 + description = "CMK for ElastiCache CloudWatch Logs encryption." + enable_key_rotation = true + policy = data.aws_iam_policy_document.redis_cloudwatch_log_group_kms[0].json +} + +resource "aws_kms_alias" "redis_cloudwatch_log_group" { + count = local.redis_cloudwatch_log_group_create_kms_key == true ? 1 : 0 + target_key_id = aws_kms_key.redis_cloudwatch_log_group[0].id + name = "alias/${var.redis_config.cloudwatch_log_group.kms.kms_alias}" } module "byo-db" { - source = "./byo-db" - vpc_id = var.vpc_config.vpc_id + source = "./byo-db" + vpc_id = var.vpc_config.vpc_id + kms_base_policy = var.kms_base_policy fleet_config = merge(var.fleet_config, { database = { - address = module.rds.cluster_endpoint - rr_address = module.rds.cluster_reader_endpoint - database = "fleet" - user = "fleet" - password_secret_arn = module.secrets-manager-1.secret_arns["${var.rds_config.name}-database-password"] + address = module.rds.cluster_endpoint + rr_address = module.rds.cluster_reader_endpoint + database = "fleet" + user = "fleet" + password_secret_arn = module.secrets-manager-1.secret_arns["${var.rds_config.name}-database-password"] + password_secret_kms_key_arn = local.rds_password_secret_kms_key_arn } redis = { address = "${module.redis.endpoint}:${module.redis.port}" } networking = { - subnets = var.vpc_config.networking.subnets - security_groups = var.fleet_config.networking.security_groups - ingress_sources = var.fleet_config.networking.ingress_sources + subnets = var.vpc_config.networking.subnets + security_groups = var.fleet_config.networking.security_groups + ingress_sources = var.fleet_config.networking.ingress_sources assign_public_ip = var.fleet_config.networking.assign_public_ip } }) @@ -43,9 +487,19 @@ resource "random_password" "rds" { override_special = "!#$%&*()-_=+[]{}<>:?" } +resource "random_id" "rds_final_snapshot_identifier" { + count = var.rds_config.skip_final_snapshot ? 0 : 1 + + keepers = { + id = var.rds_config.name + } + + byte_length = 4 +} + module "rds" { source = "terraform-aws-modules/rds-aurora/aws" - version = "7.6.0" + version = "9.16.1" name = var.rds_config.name engine = "aurora-mysql" @@ -54,6 +508,7 @@ module "rds" { instances = local.rds_replica_instances + create_db_subnet_group = true serverlessv2_scaling_configuration = var.rds_config.serverless ? { min_capacity = var.rds_config.serverless_min_capacity max_capacity = var.rds_config.serverless_max_capacity @@ -62,13 +517,22 @@ module "rds" { vpc_id = var.vpc_config.vpc_id subnets = var.rds_config.subnets - allowed_security_groups = concat(tolist(module.byo-db.byo-ecs.non_circular.security_groups), var.rds_config.allowed_security_groups) - allowed_cidr_blocks = var.rds_config.allowed_cidr_blocks + security_group_rules = local.rds_security_group_rules - performance_insights_enabled = true - storage_encrypted = true - apply_immediately = var.rds_config.apply_immediately - monitoring_interval = var.rds_config.monitoring_interval + cluster_monitoring_interval = local.rds_cluster_monitoring_interval + cluster_performance_insights_enabled = local.rds_performance_insights_enabled + cluster_performance_insights_kms_key_id = local.rds_observability_kms_key_arn + cluster_performance_insights_retention_period = local.rds_performance_insights_retention_period + create_cloudwatch_log_group = local.rds_manage_cloudwatch_log_groups + cloudwatch_log_group_kms_key_id = local.rds_manage_cloudwatch_log_groups ? local.rds_cloudwatch_log_group_kms_key_arn : null + cloudwatch_log_group_retention_in_days = local.rds_manage_cloudwatch_log_groups ? var.rds_config.cloudwatch_log_group.retention_in_days : null + cloudwatch_log_group_skip_destroy = local.rds_manage_cloudwatch_log_groups ? var.rds_config.cloudwatch_log_group.skip_destroy : null + database_insights_mode = var.rds_config.observability.database_insights_mode + manage_master_user_password = false + storage_encrypted = true + kms_key_id = local.rds_storage_kms_key_arn + apply_immediately = var.rds_config.apply_immediately + backtrack_window = var.rds_config.backtrack_window db_parameter_group_name = var.rds_config.db_parameter_group_name == null ? aws_db_parameter_group.main[0].id : var.rds_config.db_parameter_group_name db_cluster_parameter_group_name = var.rds_config.db_cluster_parameter_group_name == null ? aws_rds_cluster_parameter_group.main[0].id : var.rds_config.db_cluster_parameter_group_name @@ -78,6 +542,7 @@ module "rds" { master_password = random_password.rds.result database_name = "fleet" skip_final_snapshot = var.rds_config.skip_final_snapshot + final_snapshot_identifier = local.rds_final_snapshot_identifier snapshot_identifier = var.rds_config.snapshot_identifier backup_retention_period = var.rds_config.backup_retention_period restore_to_point_in_time = var.rds_config.restore_to_point_in_time @@ -107,6 +572,7 @@ module "redis" { engine_version = var.redis_config.engine_version family = var.redis_config.family at_rest_encryption_enabled = var.redis_config.at_rest_encryption_enabled + kms_key_id = local.redis_at_rest_kms_key_arn transit_encryption_enabled = var.redis_config.transit_encryption_enabled parameter = var.redis_config.parameter log_delivery_configuration = var.redis_config.log_delivery_configuration @@ -127,12 +593,23 @@ module "secrets-manager-1" { secrets = { "${var.rds_config.name}-database-password" = { description = "fleet-database-password" + kms_key_id = local.rds_password_secret_kms_key_arn recovery_window_in_days = 0 secret_string = module.rds.cluster_master_password }, } } +resource "aws_cloudwatch_log_group" "redis" { + for_each = local.redis_manage_cloudwatch_log_groups ? local.redis_cloudwatch_log_group_destinations : {} + + name = each.value.destination + retention_in_days = var.redis_config.cloudwatch_log_group.retention_in_days + kms_key_id = local.redis_cloudwatch_log_group_kms_key_arn + skip_destroy = var.redis_config.cloudwatch_log_group.skip_destroy + tags = var.redis_config.tags +} + resource "aws_db_parameter_group" "main" { count = var.rds_config.db_parameter_group_name == null ? 1 : 0 name = var.rds_config.name diff --git a/byo-vpc/moved.tf b/byo-vpc/moved.tf new file mode 100644 index 0000000..5653cb5 --- /dev/null +++ b/byo-vpc/moved.tf @@ -0,0 +1,9 @@ +moved { + from = module.rds.aws_security_group_rule.default_ingress[0] + to = module.rds.aws_security_group_rule.this["allowed_security_group_0"] +} + +moved { + from = module.rds.random_id.snapshot_identifier[0] + to = random_id.rds_final_snapshot_identifier[0] +} diff --git a/byo-vpc/outputs.tf b/byo-vpc/outputs.tf index 4473d0d..dfee116 100644 --- a/byo-vpc/outputs.tf +++ b/byo-vpc/outputs.tf @@ -13,3 +13,7 @@ output "redis" { output "secrets" { value = module.secrets-manager-1 } + +output "rds_password_secret_kms_key_arn" { + value = local.rds_password_secret_kms_key_arn +} diff --git a/byo-vpc/scripts/rds_storage_kms_migration.sh b/byo-vpc/scripts/rds_storage_kms_migration.sh new file mode 100755 index 0000000..f6673a7 --- /dev/null +++ b/byo-vpc/scripts/rds_storage_kms_migration.sh @@ -0,0 +1,1146 @@ +#!/usr/bin/env bash +set -euo pipefail + +usage() { + cat <<'EOF' +Usage: + rds_storage_kms_migration.sh --storage-kms-key-arn [options] + rds_storage_kms_migration.sh --storage-kms-alias [options] + +This helper automates an Aurora storage KMS migration for the byo-vpc module by: +1. Editing the caller's inline rds_config object. +2. Pre-creating a wrapper-managed storage CMK when an alias is requested. +3. Creating a source snapshot and an encrypted copy with the target CMK. +4. Removing the current Aurora resources from Terraform state. +5. Applying Terraform to restore a new Aurora cluster from the encrypted snapshot. +6. Deleting the old Aurora resources from AWS after the new cluster is managed. + +Required options: + --storage-kms-key-arn Use an existing CMK ARN in rds_config.storage_kms.kms_key_arn. + --storage-kms-alias Have byo-vpc create/manage the target CMK with this alias. + +Options: + --terraform-dir Terraform working directory. Default: . + --config-file Terraform file that contains the inline rds_config object. + Default: main.tf (relative to --terraform-dir if not absolute) + --manifest Existing manifest.json to use with --cleanup-only. + --module-address Full Terraform address for the byo-vpc module instance. + Auto-detected when exactly one match exists. + --region AWS region. Default: AWS_REGION / AWS_DEFAULT_REGION + --restored-name New Aurora cluster identifier/name. Default: -kms- + --source-snapshot-id Manual DB cluster snapshot identifier to create. + --copied-snapshot-id Encrypted DB cluster snapshot copy identifier to create. + --old-final-snapshot-id Final snapshot identifier when deleting the old cluster. + --skip-old-final-snapshot Delete the old cluster without a final snapshot. + --include-performance-insights + Also enable Performance Insights CMK configuration after restore. + --performance-insights-kms-alias + Set rds_config.observability.kms.kms_alias during restore. + --keep-old-resources Leave the old Aurora resources in AWS after cutover. + --cleanup-only Skip migration steps and only run AWS cleanup from --manifest. + --confirm Prompt before each AWS cleanup command executes. + --dry-run Show the planned actions without mutating Terraform, state, or AWS. + --help Show this help text. + +Notes: +* This assumes the caller passes rds_config as a direct inline object literal in the config file. +* The helper intentionally leaves rds_config.snapshot_identifier pinned to the copied snapshot. + Clearing it later would force Terraform to replace the restored cluster. +* PI and Enhanced Monitoring are temporarily disabled during the snapshot restore because + AWS RestoreDBClusterFromSnapshot rejects these parameters for non-Limitless Aurora clusters. + The original settings (plus any --include-performance-insights changes) are re-applied + in a post-restore config edit before the reconcile apply, which enables them via + ModifyDBCluster (in-place, no downtime). +* --include-performance-insights updates rds_config.observability in the post-restore + config edit so the reconcile apply adopts Performance Insights CMK settings. +* --performance-insights-kms-alias controls the alias written to + rds_config.observability.kms.kms_alias in the post-restore config edit. +* Run this during a maintenance window. The snapshot/copy/restore path is not an in-place re-key. +EOF +} + +log() { + printf '[rds-storage-kms-migration] %s\n' "$*" >&2 +} + +die() { + log "ERROR: $*" + exit 1 +} + +require_cmd() { + command -v "$1" >/dev/null 2>&1 || die "required command not found: $1" +} + +sanitize_identifier() { + local value="$1" + local max_length="${2:-63}" + value="$(printf '%s' "$value" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9-]+/-/g; s/^-+//; s/-+$//; s/-{2,}/-/g')" + if [[ -z "$value" ]]; then + value="fleet" + fi + value="${value:0:$max_length}" + value="$(printf '%s' "$value" | sed -E 's/-+$//')" + if [[ ! "$value" =~ ^[a-z] ]]; then + value="f${value}" + value="${value:0:$max_length}" + value="$(printf '%s' "$value" | sed -E 's/-+$//')" + fi + printf '%s\n' "$value" +} + +join_by() { + local separator="$1" + shift + local first=1 + for value in "$@"; do + if [[ "$first" -eq 1 ]]; then + printf '%s' "$value" + first=0 + else + printf '%s%s' "$separator" "$value" + fi + done +} + +json_array_from_lines() { + jq -Rsc 'split("\n") | map(select(length > 0))' +} + +append_array_value() { + local array_name="$1" + local value="$2" + local quoted_value="" + printf -v quoted_value '%q' "$value" + eval "$array_name+=( $quoted_value )" +} + +array_from_newline_text() { + local array_name="$1" + local text="$2" + local line="" + + eval "$array_name=()" + while IFS= read -r line; do + [[ -n "$line" ]] || continue + append_array_value "$array_name" "$line" + done <&2 + printf '[rds-storage-kms-migration] continue? [y/N] ' >/dev/tty + read -r reply "$STATE_JSON_FILE" + jq "$state_resources_query" "$STATE_JSON_FILE" >"$STATE_RESOURCES_FILE" +} + +state_value() { + local address="$1" + local jq_filter="$2" + jq -r --arg addr "$address" ".[] | select(.address == \$addr) | ${jq_filter}" "$STATE_RESOURCES_FILE" +} + +resolve_target_storage_kms_id() { + if [[ -n "$TARGET_STORAGE_KMS_KEY_ARN" ]]; then + RESOLVED_TARGET_STORAGE_KMS_ID="$TARGET_STORAGE_KMS_KEY_ARN" + return + fi + + local key_address="${MODULE_ADDRESS}.aws_kms_key.rds_storage[0]" + RESOLVED_TARGET_STORAGE_KMS_ID="$(state_value "$key_address" '.values.arn')" + RESOLVED_TARGET_STORAGE_KMS_ID="${RESOLVED_TARGET_STORAGE_KMS_ID//$'\r'/}" + if [[ -z "$RESOLVED_TARGET_STORAGE_KMS_ID" || "$RESOLVED_TARGET_STORAGE_KMS_ID" == "null" ]]; then + die "could not determine the created storage CMK ARN from ${key_address}" + fi +} + +collect_state_addresses_for_removal() { + local pattern + local matches_text="" + pattern="^${MODULE_ADDRESS//./\\.}\\.(module\\.rds\\.|module\\.secrets-manager-1\\.|random_id\\.rds_final_snapshot_identifier(\\[0\\])?$|aws_db_parameter_group\\.main(\\[0\\])?$|aws_rds_cluster_parameter_group\\.main(\\[0\\])?$)" + matches_text="$(terraform_cmd state list | grep -E "$pattern" | sort || true)" + array_from_newline_text STATE_REMOVE_ADDRESSES "$matches_text" + if [[ "${#STATE_REMOVE_ADDRESSES[@]}" -eq 0 ]]; then + die "did not find any RDS-related Terraform addresses to remove under ${MODULE_ADDRESS}" + fi +} + +write_manifest() { + jq -n \ + --arg module_address "$MODULE_ADDRESS" \ + --arg config_file "$CONFIG_FILE" \ + --arg current_cluster_identifier "$CURRENT_CLUSTER_IDENTIFIER" \ + --arg restored_name "$RESTORED_NAME" \ + --arg source_snapshot_id "$SOURCE_SNAPSHOT_ID" \ + --arg copied_snapshot_id "$COPIED_SNAPSHOT_ID" \ + --arg old_final_snapshot_id "$OLD_FINAL_SNAPSHOT_ID" \ + --arg storage_kms_key_arn "$TARGET_STORAGE_KMS_KEY_ARN" \ + --arg storage_kms_alias "$TARGET_STORAGE_KMS_ALIAS" \ + --arg performance_insights_kms_alias "$PERFORMANCE_INSIGHTS_KMS_ALIAS" \ + --argjson include_performance_insights "$INCLUDE_PERFORMANCE_INSIGHTS_JSON" \ + --argjson old_instance_identifiers "$OLD_INSTANCE_IDENTIFIERS_JSON" \ + --argjson old_security_group_ids "$OLD_SECURITY_GROUP_IDS_JSON" \ + --argjson old_secrets "$OLD_SECRETS_JSON" \ + --argjson old_monitoring_roles "$OLD_MONITORING_ROLES_JSON" \ + --argjson old_parameter_groups "$OLD_PARAMETER_GROUPS_JSON" \ + --argjson old_db_subnet_groups "$OLD_DB_SUBNET_GROUPS_JSON" \ + --argjson state_remove_addresses "$STATE_REMOVE_ADDRESSES_JSON" \ + '{ + module_address: $module_address, + config_file: $config_file, + current_cluster_identifier: $current_cluster_identifier, + restored_name: $restored_name, + source_snapshot_id: $source_snapshot_id, + copied_snapshot_id: $copied_snapshot_id, + old_final_snapshot_id: ($old_final_snapshot_id | if length > 0 then . else null end), + storage_kms_key_arn: ($storage_kms_key_arn | if length > 0 then . else null end), + storage_kms_alias: ($storage_kms_alias | if length > 0 then . else null end), + performance_insights_kms_alias: ($performance_insights_kms_alias | if length > 0 then . else null end), + include_performance_insights: $include_performance_insights, + old_instance_identifiers: $old_instance_identifiers, + old_security_group_ids: $old_security_group_ids, + old_secrets: $old_secrets, + old_monitoring_roles: $old_monitoring_roles, + old_parameter_groups: $old_parameter_groups, + old_db_subnet_groups: $old_db_subnet_groups, + state_remove_addresses: $state_remove_addresses + } | with_entries(select(.value != null))' >"$ARTIFACT_DIR/manifest.json" +} + +update_rds_config_file() { + local file="$1" + local mode="$2" + + CONFIG_EDIT_MODE="$mode" \ + CONFIG_EDIT_RESTORED_NAME="${RESTORED_NAME:-}" \ + CONFIG_EDIT_SOURCE_SNAPSHOT_ID="${COPIED_SNAPSHOT_ID:-}" \ + CONFIG_EDIT_TARGET_STORAGE_KMS_KEY_ARN="${TARGET_STORAGE_KMS_KEY_ARN:-}" \ + CONFIG_EDIT_TARGET_STORAGE_KMS_ALIAS="${TARGET_STORAGE_KMS_ALIAS:-}" \ + CONFIG_EDIT_INCLUDE_PERFORMANCE_INSIGHTS="${INCLUDE_PERFORMANCE_INSIGHTS:-false}" \ + CONFIG_EDIT_PERFORMANCE_INSIGHTS_KMS_ALIAS="${PERFORMANCE_INSIGHTS_KMS_ALIAS:-}" \ + perl -i -0pe ' +use strict; +use warnings; + +my $mode = $ENV{CONFIG_EDIT_MODE} // ""; +my $restored_name = $ENV{CONFIG_EDIT_RESTORED_NAME} // ""; +my $snapshot_id = $ENV{CONFIG_EDIT_SOURCE_SNAPSHOT_ID} // ""; +my $kms_key_arn = $ENV{CONFIG_EDIT_TARGET_STORAGE_KMS_KEY_ARN} // ""; +my $kms_alias = $ENV{CONFIG_EDIT_TARGET_STORAGE_KMS_ALIAS} // ""; +my $include_performance_insights = $ENV{CONFIG_EDIT_INCLUDE_PERFORMANCE_INSIGHTS} // "false"; +my $performance_insights_kms_alias = $ENV{CONFIG_EDIT_PERFORMANCE_INSIGHTS_KMS_ALIAS} // ""; + +sub quote_hcl { + my ($value) = @_; + $value =~ s/\\/\\\\/g; + $value =~ s/"/\\"/g; + return qq("$value"); +} + +sub find_matching_brace { + my ($text, $start_index) = @_; + my $depth = 0; + my $in_string = 0; + my $in_line_comment = 0; + my $in_block_comment = 0; + my $escaped = 0; + + for (my $i = $start_index; $i < length($text); $i++) { + my $char = substr($text, $i, 1); + my $next = $i + 1 < length($text) ? substr($text, $i + 1, 1) : ""; + + if ($in_line_comment) { + if ($char eq "\n") { + $in_line_comment = 0; + } + next; + } + + if ($in_block_comment) { + if ($char eq "*" && $next eq "/") { + $in_block_comment = 0; + $i++; + } + next; + } + + if ($in_string) { + if ($escaped) { + $escaped = 0; + next; + } + if ($char eq "\\") { + $escaped = 1; + next; + } + if ($char eq "\"") { + $in_string = 0; + } + next; + } + + if ($char eq "/" && $next eq "/") { + $in_line_comment = 1; + $i++; + next; + } + + if ($char eq "#") { + $in_line_comment = 1; + next; + } + + if ($char eq "/" && $next eq "*") { + $in_block_comment = 1; + $i++; + next; + } + + if ($char eq "\"") { + $in_string = 1; + next; + } + + if ($char eq "{") { + $depth++; + next; + } + + if ($char eq "}") { + $depth--; + if ($depth == 0) { + return $i; + } + } + } + + die "failed to find matching closing brace for rds_config\n"; +} + +sub find_rds_config_span { + my ($text) = @_; + my @matches = ($text =~ /(^[ \t]*rds_config[ \t]*=[ \t]*\{)/mg); + my @positions; + while ($text =~ /(^[ \t]*rds_config[ \t]*=[ \t]*\{)/mg) { + push @positions, pos($text) - 1; + } + die "did not find an inline rds_config object\n" if @positions == 0; + die "found multiple inline rds_config objects; pass a narrower --config-file\n" if @positions > 1; + + my $line_start = rindex($text, "\n", $positions[0]); + $line_start = $line_start == -1 ? 0 : $line_start + 1; + my $brace_index = index($text, "{", $positions[0]); + my $end_index = find_matching_brace($text, $brace_index); + return ($line_start, $end_index); +} + +sub object_indent { + my ($text) = @_; + if ($text =~ /\{\n([ \t]+)\S/m) { + return $1; + } + return " "; +} + +sub upsert_simple_attribute { + my ($object_text, $key, $value_text) = @_; + if ($object_text =~ /^([ \t]*)\Q$key\E[ \t]*=.*$/m) { + $object_text =~ s/^([ \t]*)\Q$key\E[ \t]*=.*$/$1$key = $value_text/m; + return $object_text; + } + + my $indent = object_indent($object_text); + $object_text =~ s/\n([ \t]*)\}$/\n$indent$key = $value_text\n$1}/s; + return $object_text; +} + +sub upsert_storage_kms_block { + my ($object_text, $kms_key_arn, $kms_alias) = @_; + my $indent = object_indent($object_text); + my $nested_indent = $indent . " "; + + my $replacement = "${indent}storage_kms = {\n"; + $replacement .= "${nested_indent}cmk_enabled = true\n"; + if (length($kms_key_arn)) { + $replacement .= "${nested_indent}kms_key_arn = " . quote_hcl($kms_key_arn) . "\n"; + } else { + $replacement .= "${nested_indent}kms_key_arn = null\n"; + } + if (length($kms_alias)) { + $replacement .= "${nested_indent}kms_alias = " . quote_hcl($kms_alias) . "\n"; + } + $replacement .= "${indent}}"; + + if ($object_text =~ /^([ \t]*)storage_kms[ \t]*=[ \t]*\{/m) { + my $storage_start = $-[0]; + my $storage_brace = index($object_text, "{", $storage_start); + my $storage_end = find_matching_brace($object_text, $storage_brace); + substr($object_text, $storage_start, $storage_end - $storage_start + 1, $replacement); + return $object_text; + } + + $object_text =~ s/\n([ \t]*)\}$/\n$replacement\n$1}/s; + return $object_text; +} + +sub upsert_observability_kms_block { + my ($observability_text, $kms_alias) = @_; + my $indent = object_indent($observability_text); + my $nested_indent = $indent . " "; + + my $replacement = "${indent}kms = {\n"; + $replacement .= "${nested_indent}cmk_enabled = true\n"; + if (length($kms_alias)) { + $replacement .= "${nested_indent}kms_alias = " . quote_hcl($kms_alias) . "\n"; + } + $replacement .= "${indent}}"; + + if ($observability_text =~ /^([ \t]*)kms[ \t]*=[ \t]*\{/m) { + my $kms_start = $-[0]; + my $kms_brace = index($observability_text, "{", $kms_start); + my $kms_end = find_matching_brace($observability_text, $kms_brace); + my $existing = substr($observability_text, $kms_start, $kms_end - $kms_start + 1); + $existing = upsert_simple_attribute($existing, "cmk_enabled", "true"); + if (length($kms_alias)) { + $existing = upsert_simple_attribute($existing, "kms_alias", quote_hcl($kms_alias)); + } + substr($observability_text, $kms_start, $kms_end - $kms_start + 1, $existing); + return $observability_text; + } + + $observability_text =~ s/\n([ \t]*)\}$/\n$replacement\n$1}/s; + return $observability_text; +} + +sub upsert_observability_block { + my ($object_text, $kms_alias) = @_; + my $indent = object_indent($object_text); + my $nested_indent = $indent . " "; + + if ($object_text =~ /^([ \t]*)observability[ \t]*=[ \t]*\{/m) { + my $observability_start = $-[0]; + my $observability_brace = index($object_text, "{", $observability_start); + my $observability_end = find_matching_brace($object_text, $observability_brace); + my $existing = substr($object_text, $observability_start, $observability_end - $observability_start + 1); + $existing = upsert_simple_attribute($existing, "performance_insights_enabled", "true"); + $existing = upsert_observability_kms_block($existing, $kms_alias); + substr($object_text, $observability_start, $observability_end - $observability_start + 1, $existing); + return $object_text; + } + + my $replacement = "${indent}observability = {\n"; + $replacement .= "${nested_indent}performance_insights_enabled = true\n"; + $replacement .= "${nested_indent}kms = {\n"; + $replacement .= "${nested_indent} cmk_enabled = true\n"; + if (length($kms_alias)) { + $replacement .= "${nested_indent} kms_alias = " . quote_hcl($kms_alias) . "\n"; + } + $replacement .= "${nested_indent}}\n"; + $replacement .= "${indent}}"; + + $object_text =~ s/\n([ \t]*)\}$/\n$replacement\n$1}/s; + return $object_text; +} + +sub disable_observability_for_restore { + my ($object_text) = @_; + my $indent = object_indent($object_text); + my $nested_indent = $indent . " "; + + my $replacement = "${indent}observability = {\n"; + $replacement .= "${nested_indent}performance_insights_enabled = false\n"; + $replacement .= "${nested_indent}database_insights_mode = null\n"; + $replacement .= "${nested_indent}kms = {\n"; + $replacement .= "${nested_indent} cmk_enabled = false\n"; + $replacement .= "${nested_indent}}\n"; + $replacement .= "${indent}}"; + + if ($object_text =~ /^([ \t]*)observability[ \t]*=[ \t]*\{/m) { + my $observability_start = $-[0]; + my $observability_brace = index($object_text, "{", $observability_start); + my $observability_end = find_matching_brace($object_text, $observability_brace); + substr($object_text, $observability_start, $observability_end - $observability_start + 1, $replacement); + return $object_text; + } + + $object_text =~ s/\n([ \t]*)\}$/\n$replacement\n$1}/s; + return $object_text; +} + +sub replace_optional_mysql_password_secret_name { + my ($text, $restored_name) = @_; + my $replacement = quote_hcl($restored_name . "-database-password"); + $text =~ s/^([ \t]*mysql_password_secret_name[ \t]*=[ \t]*).*$/$1$replacement/mg; + return $text; +} + +my ($start_index, $end_index) = find_rds_config_span($_); +my $object_text = substr($_, $start_index, $end_index - $start_index + 1); + +if ($mode eq "kms-bootstrap") { + $object_text = upsert_storage_kms_block($object_text, $kms_key_arn, $kms_alias); +} elsif ($mode eq "restore") { + die "restore mode requires CONFIG_EDIT_RESTORED_NAME\n" if !length($restored_name); + die "restore mode requires CONFIG_EDIT_SOURCE_SNAPSHOT_ID\n" if !length($snapshot_id); + $object_text = upsert_simple_attribute($object_text, "name", quote_hcl($restored_name)); + $object_text = upsert_simple_attribute($object_text, "snapshot_identifier", quote_hcl($snapshot_id)); + $object_text = upsert_simple_attribute($object_text, "restore_to_point_in_time", "{}"); + $object_text = upsert_storage_kms_block($object_text, $kms_key_arn, $kms_alias); + # Force-disable cluster-level PI and Enhanced Monitoring for the initial + # restore-from-snapshot apply. AWS RestoreDBClusterFromSnapshot rejects + # these parameters for non-Limitless Aurora clusters. They are re-enabled + # by the post-restore config edit before the reconcile apply. + $object_text = upsert_simple_attribute($object_text, "monitoring_interval", "0"); + $object_text = disable_observability_for_restore($object_text); +} elsif ($mode eq "post-restore") { + die "post-restore mode requires CONFIG_EDIT_RESTORED_NAME\n" if !length($restored_name); + die "post-restore mode requires CONFIG_EDIT_SOURCE_SNAPSHOT_ID\n" if !length($snapshot_id); + $object_text = upsert_simple_attribute($object_text, "name", quote_hcl($restored_name)); + $object_text = upsert_simple_attribute($object_text, "snapshot_identifier", quote_hcl($snapshot_id)); + $object_text = upsert_simple_attribute($object_text, "restore_to_point_in_time", "{}"); + $object_text = upsert_storage_kms_block($object_text, $kms_key_arn, $kms_alias); + if ($include_performance_insights eq "true") { + $object_text = upsert_observability_block($object_text, $performance_insights_kms_alias); + } +} else { + die "unexpected CONFIG_EDIT_MODE=$mode\n"; +} + +substr($_, $start_index, $end_index - $start_index + 1, $object_text); +if ($mode eq "restore" || $mode eq "post-restore") { + $_ = replace_optional_mysql_password_secret_name($_, $restored_name); +} + ' "$file" +} + +create_source_snapshot() { + log "creating source DB cluster snapshot ${SOURCE_SNAPSHOT_ID}" + aws_cmd rds create-db-cluster-snapshot \ + --db-cluster-identifier "$CURRENT_CLUSTER_IDENTIFIER" \ + --db-cluster-snapshot-identifier "$SOURCE_SNAPSHOT_ID" \ + >/dev/null + aws_cmd rds wait db-cluster-snapshot-available \ + --db-cluster-snapshot-identifier "$SOURCE_SNAPSHOT_ID" +} + +copy_snapshot_with_target_kms() { + local kms_id="$RESOLVED_TARGET_STORAGE_KMS_ID" + + log "copying snapshot ${SOURCE_SNAPSHOT_ID} to ${COPIED_SNAPSHOT_ID} with ${kms_id}" + aws_cmd rds copy-db-cluster-snapshot \ + --source-db-cluster-snapshot-identifier "$SOURCE_SNAPSHOT_ID" \ + --target-db-cluster-snapshot-identifier "$COPIED_SNAPSHOT_ID" \ + --kms-key-id "$kms_id" \ + --copy-tags \ + >/dev/null + aws_cmd rds wait db-cluster-snapshot-available \ + --db-cluster-snapshot-identifier "$COPIED_SNAPSHOT_ID" +} + +terraform_apply_storage_kms_only() { + local targets=( + "-target=${MODULE_ADDRESS}.aws_kms_key.rds_storage[0]" + "-target=${MODULE_ADDRESS}.aws_kms_alias.rds_storage[0]" + ) + + log "running targeted terraform apply to pre-create the storage CMK" + terraform_cmd apply -auto-approve "${targets[@]}" +} + +terraform_apply_byo_vpc_only() { + log "running targeted terraform apply for ${MODULE_ADDRESS}" + terraform_cmd apply -auto-approve "-target=${MODULE_ADDRESS}" +} + +terraform_reconcile_restored_byo_vpc() { + log "running a second targeted terraform apply for ${MODULE_ADDRESS} to reconcile post-restore in-place updates" + terraform_cmd apply -auto-approve "-target=${MODULE_ADDRESS}" +} + +remove_old_rds_state() { + log "removing old Aurora resources from Terraform state" + terraform_cmd state rm "${STATE_REMOVE_ADDRESSES[@]}" +} + +load_cleanup_manifest() { + [[ -f "$MANIFEST_FILE" ]] || die "manifest file does not exist: $MANIFEST_FILE" + + CURRENT_CLUSTER_IDENTIFIER="$(jq -r '.current_cluster_identifier // empty' "$MANIFEST_FILE")" + CURRENT_CLUSTER_IDENTIFIER="${CURRENT_CLUSTER_IDENTIFIER//$'\r'/}" + [[ -n "$CURRENT_CLUSTER_IDENTIFIER" ]] || die "manifest is missing current_cluster_identifier" + + OLD_FINAL_SNAPSHOT_ID="$(jq -r '.old_final_snapshot_id // empty' "$MANIFEST_FILE")" + + array_from_newline_text OLD_INSTANCE_IDENTIFIERS "$(jq -r '.old_instance_identifiers[]? // empty' "$MANIFEST_FILE")" + array_from_newline_text OLD_SECURITY_GROUP_IDS "$(jq -r '.old_security_group_ids[]? // empty' "$MANIFEST_FILE")" + array_from_newline_text OLD_SECRET_IDS "$(jq -r '.old_secrets[]? | .arn // .name // empty' "$MANIFEST_FILE")" + array_from_newline_text OLD_DB_PARAMETER_GROUP_NAMES "$(jq -r '.old_parameter_groups.db_parameter_groups[]? // empty' "$MANIFEST_FILE")" + array_from_newline_text OLD_DB_CLUSTER_PARAMETER_GROUP_NAMES "$(jq -r '.old_parameter_groups.db_cluster_parameter_groups[]? // empty' "$MANIFEST_FILE")" + array_from_newline_text OLD_DB_SUBNET_GROUP_NAMES "$(jq -r '.old_db_subnet_groups[]? // empty' "$MANIFEST_FILE")" + OLD_MONITORING_ROLES_JSON="$(jq -c '.old_monitoring_roles // []' "$MANIFEST_FILE")" +} + +delete_old_cluster_resources() { + if [[ "$KEEP_OLD_RESOURCES" == "true" ]]; then + log "keeping the old Aurora resources in AWS because --keep-old-resources was set" + return + fi + + if [[ "$DRY_RUN" == "true" ]]; then + log "dry run: would delete old Aurora instances, cluster, secret, enhanced monitoring IAM role, parameter groups, subnet groups, and security groups" + return + fi + + local instance_id + for instance_id in "${OLD_INSTANCE_IDENTIFIERS[@]}"; do + log "deleting old Aurora instance ${instance_id}" + aws_cleanup_cmd rds delete-db-instance \ + --db-instance-identifier "$instance_id" \ + --skip-final-snapshot \ + >/dev/null || true + done + + for instance_id in "${OLD_INSTANCE_IDENTIFIERS[@]}"; do + log "waiting for old Aurora instance ${instance_id} to be deleted" + aws_cleanup_cmd rds wait db-instance-deleted --db-instance-identifier "$instance_id" || true + done + + if [[ "$SKIP_OLD_FINAL_SNAPSHOT" == "true" ]]; then + log "deleting old Aurora cluster ${CURRENT_CLUSTER_IDENTIFIER} without a final snapshot" + aws_cleanup_cmd rds delete-db-cluster \ + --db-cluster-identifier "$CURRENT_CLUSTER_IDENTIFIER" \ + --skip-final-snapshot \ + >/dev/null || true + else + log "deleting old Aurora cluster ${CURRENT_CLUSTER_IDENTIFIER} with final snapshot ${OLD_FINAL_SNAPSHOT_ID}" + aws_cleanup_cmd rds delete-db-cluster \ + --db-cluster-identifier "$CURRENT_CLUSTER_IDENTIFIER" \ + --final-db-snapshot-identifier "$OLD_FINAL_SNAPSHOT_ID" \ + >/dev/null || true + fi + + log "waiting for old Aurora cluster ${CURRENT_CLUSTER_IDENTIFIER} to be deleted" + aws_cleanup_cmd rds wait db-cluster-deleted --db-cluster-identifier "$CURRENT_CLUSTER_IDENTIFIER" || true + + local secret_id + for secret_id in "${OLD_SECRET_IDS[@]}"; do + log "deleting old secret ${secret_id}" + aws_cleanup_cmd secretsmanager delete-secret \ + --secret-id "$secret_id" \ + --force-delete-without-recovery \ + >/dev/null || true + done + + local role_name + local policy_arn + while IFS= read -r role_name; do + [[ -n "$role_name" ]] || continue + + while IFS= read -r policy_arn; do + [[ -n "$policy_arn" ]] || continue + log "detaching managed policy ${policy_arn} from IAM role ${role_name}" + aws_cleanup_cmd iam detach-role-policy \ + --role-name "$role_name" \ + --policy-arn "$policy_arn" \ + >/dev/null || true + done < <(jq -r --arg role_name "$role_name" '.[] | select(.name == $role_name) | .attached_policy_arns[]?' <<<"$OLD_MONITORING_ROLES_JSON") + + log "deleting old enhanced monitoring IAM role ${role_name}" + aws_cleanup_cmd iam delete-role --role-name "$role_name" >/dev/null || true + done < <(jq -r '.[].name // empty' <<<"$OLD_MONITORING_ROLES_JSON") + + local group_name + for group_name in "${OLD_DB_PARAMETER_GROUP_NAMES[@]}"; do + log "deleting old DB parameter group ${group_name}" + aws_cleanup_cmd rds delete-db-parameter-group --db-parameter-group-name "$group_name" >/dev/null || true + done + + for group_name in "${OLD_DB_CLUSTER_PARAMETER_GROUP_NAMES[@]}"; do + log "deleting old DB cluster parameter group ${group_name}" + aws_cleanup_cmd rds delete-db-cluster-parameter-group --db-cluster-parameter-group-name "$group_name" >/dev/null || true + done + + for group_name in "${OLD_DB_SUBNET_GROUP_NAMES[@]}"; do + log "deleting old DB subnet group ${group_name}" + aws_cleanup_cmd rds delete-db-subnet-group --db-subnet-group-name "$group_name" >/dev/null || true + done + + local sg_id + for sg_id in "${OLD_SECURITY_GROUP_IDS[@]}"; do + log "deleting old security group ${sg_id}" + aws_cleanup_cmd ec2 delete-security-group --group-id "$sg_id" >/dev/null || true + done +} + +copy_file() { + local src="$1" + local dest="$2" + cp "$src" "$dest" +} + +CONFIG_FILE="" +MANIFEST_FILE="" +TERRAFORM_DIR="." +MODULE_ADDRESS="" +AWS_REGION_ARG="${AWS_REGION:-${AWS_DEFAULT_REGION:-}}" +TARGET_STORAGE_KMS_KEY_ARN="" +TARGET_STORAGE_KMS_ALIAS="" +PERFORMANCE_INSIGHTS_KMS_ALIAS="" +RESOLVED_TARGET_STORAGE_KMS_ID="" +RESTORED_NAME="" +SOURCE_SNAPSHOT_ID="" +COPIED_SNAPSHOT_ID="" +OLD_FINAL_SNAPSHOT_ID="" +SKIP_OLD_FINAL_SNAPSHOT="false" +INCLUDE_PERFORMANCE_INSIGHTS="false" +KEEP_OLD_RESOURCES="false" +CONFIRM_CLEANUP="false" +CLEANUP_ONLY="false" +DRY_RUN="false" + +while [[ $# -gt 0 ]]; do + case "$1" in + --terraform-dir) + TERRAFORM_DIR="$2" + shift 2 + ;; + --config-file) + CONFIG_FILE="$2" + shift 2 + ;; + --manifest) + MANIFEST_FILE="$2" + shift 2 + ;; + --module-address) + MODULE_ADDRESS="$2" + shift 2 + ;; + --region) + AWS_REGION_ARG="$2" + shift 2 + ;; + --storage-kms-key-arn) + TARGET_STORAGE_KMS_KEY_ARN="$2" + shift 2 + ;; + --storage-kms-alias) + TARGET_STORAGE_KMS_ALIAS="${2#alias/}" + shift 2 + ;; + --restored-name) + RESTORED_NAME="$2" + shift 2 + ;; + --source-snapshot-id) + SOURCE_SNAPSHOT_ID="$2" + shift 2 + ;; + --copied-snapshot-id) + COPIED_SNAPSHOT_ID="$2" + shift 2 + ;; + --old-final-snapshot-id) + OLD_FINAL_SNAPSHOT_ID="$2" + shift 2 + ;; + --skip-old-final-snapshot) + SKIP_OLD_FINAL_SNAPSHOT="true" + shift + ;; + --include-performance-insights) + INCLUDE_PERFORMANCE_INSIGHTS="true" + shift + ;; + --performance-insights-kms-alias) + PERFORMANCE_INSIGHTS_KMS_ALIAS="${2#alias/}" + shift 2 + ;; + --keep-old-resources) + KEEP_OLD_RESOURCES="true" + shift + ;; + --confirm) + CONFIRM_CLEANUP="true" + shift + ;; + --cleanup-only) + CLEANUP_ONLY="true" + shift + ;; + --dry-run) + DRY_RUN="true" + shift + ;; + --help|-h) + usage + exit 0 + ;; + *) + die "unknown argument: $1" + ;; + esac +done + +require_cmd aws +require_cmd jq +if [[ "$CLEANUP_ONLY" != "true" ]]; then + require_cmd perl + require_cmd terraform +fi + +if [[ "$CLEANUP_ONLY" == "true" ]]; then + [[ -n "$MANIFEST_FILE" ]] || die "--cleanup-only requires --manifest" +else + if [[ -z "$TARGET_STORAGE_KMS_KEY_ARN" && -z "$TARGET_STORAGE_KMS_ALIAS" ]]; then + die "set either --storage-kms-key-arn or --storage-kms-alias" + fi + + if [[ -n "$TARGET_STORAGE_KMS_KEY_ARN" && -n "$TARGET_STORAGE_KMS_ALIAS" ]]; then + die "use either --storage-kms-key-arn or --storage-kms-alias, not both" + fi +fi + +if [[ -z "$AWS_REGION_ARG" ]]; then + die "set --region or AWS_REGION/AWS_DEFAULT_REGION" +fi + +if [[ ! -d "$TERRAFORM_DIR" ]]; then + die "terraform directory does not exist: $TERRAFORM_DIR" +fi + +if [[ -n "$PERFORMANCE_INSIGHTS_KMS_ALIAS" && "$INCLUDE_PERFORMANCE_INSIGHTS" != "true" ]]; then + die "--performance-insights-kms-alias requires --include-performance-insights" +fi + +INCLUDE_PERFORMANCE_INSIGHTS_JSON="false" +if [[ "$INCLUDE_PERFORMANCE_INSIGHTS" == "true" ]]; then + INCLUDE_PERFORMANCE_INSIGHTS_JSON="true" +fi + +STAMP="$(date +%Y%m%d%H%M%S)" +if [[ "$CLEANUP_ONLY" != "true" ]]; then + ARTIFACT_DIR="${TERRAFORM_DIR%/}/.rds-storage-kms-migration-${STAMP}" + mkdir -p "$ARTIFACT_DIR" + if [[ -z "$CONFIG_FILE" ]]; then + CONFIG_FILE="main.tf" + fi + + if [[ "$CONFIG_FILE" != /* ]]; then + CONFIG_FILE="${TERRAFORM_DIR%/}/$CONFIG_FILE" + fi + + if [[ ! -f "$CONFIG_FILE" ]]; then + die "config file does not exist: $CONFIG_FILE" + fi + + terraform_cmd init -no-color >/dev/null + + if [[ -z "$MODULE_ADDRESS" ]]; then + auto_detect_module_address + fi + + copy_file "$CONFIG_FILE" "$ARTIFACT_DIR/$(basename "$CONFIG_FILE").original" + load_state_snapshot + + CURRENT_CLUSTER_ADDRESS="${MODULE_ADDRESS}.module.rds.aws_rds_cluster.this[0]" + CURRENT_CLUSTER_IDENTIFIER="$(state_value "$CURRENT_CLUSTER_ADDRESS" '.values.cluster_identifier // .values.id')" + CURRENT_CLUSTER_IDENTIFIER="${CURRENT_CLUSTER_IDENTIFIER//$'\r'/}" + if [[ -z "$CURRENT_CLUSTER_IDENTIFIER" || "$CURRENT_CLUSTER_IDENTIFIER" == "null" ]]; then + die "could not determine the current Aurora cluster identifier from ${CURRENT_CLUSTER_ADDRESS}" + fi + + if [[ -z "$RESTORED_NAME" ]]; then + RESTORED_NAME="$(sanitize_identifier "${CURRENT_CLUSTER_IDENTIFIER}-kms-${STAMP}" 63)" + fi + if [[ -z "$SOURCE_SNAPSHOT_ID" ]]; then + SOURCE_SNAPSHOT_ID="$(sanitize_identifier "${CURRENT_CLUSTER_IDENTIFIER}-kms-source-${STAMP}" 63)" + fi + if [[ -z "$COPIED_SNAPSHOT_ID" ]]; then + COPIED_SNAPSHOT_ID="$(sanitize_identifier "${CURRENT_CLUSTER_IDENTIFIER}-kms-copy-${STAMP}" 63)" + fi + if [[ -z "$OLD_FINAL_SNAPSHOT_ID" && "$SKIP_OLD_FINAL_SNAPSHOT" != "true" ]]; then + OLD_FINAL_SNAPSHOT_ID="$(sanitize_identifier "${CURRENT_CLUSTER_IDENTIFIER}-pre-kms-retirement-${STAMP}" 63)" + fi + + array_from_newline_text OLD_INSTANCE_IDENTIFIERS "$(jq -r --arg prefix "${MODULE_ADDRESS}.module.rds.aws_rds_cluster_instance." ' + .[] + | select(.address | startswith($prefix)) + | .values.identifier +' "$STATE_RESOURCES_FILE" | sed '/^null$/d')" + OLD_INSTANCE_IDENTIFIERS_JSON="$(printf '%s\n' "${OLD_INSTANCE_IDENTIFIERS[@]:-}" | json_array_from_lines)" + + array_from_newline_text OLD_SECURITY_GROUP_IDS "$(jq -r --arg prefix "${MODULE_ADDRESS}.module.rds.aws_security_group." ' + .[] + | select(.address | startswith($prefix)) + | .values.id +' "$STATE_RESOURCES_FILE" | sed '/^null$/d')" + OLD_SECURITY_GROUP_IDS_JSON="$(printf '%s\n' "${OLD_SECURITY_GROUP_IDS[@]:-}" | json_array_from_lines)" + + OLD_SECRETS_JSON="$(jq -c --arg prefix "${MODULE_ADDRESS}.module.secrets-manager-1." ' + [ + .[] + | select(.address | startswith($prefix)) + | select(.type == "aws_secretsmanager_secret") + | {name: .values.name, arn: .values.arn} + ] +' "$STATE_RESOURCES_FILE")" + array_from_newline_text OLD_SECRET_IDS "$(jq -r '.[] | .arn // .name' <<<"$OLD_SECRETS_JSON")" + + OLD_MONITORING_ROLES_JSON="$(jq -c --arg role_prefix "${MODULE_ADDRESS}.module.rds.aws_iam_role.rds_enhanced_monitoring" --arg attachment_prefix "${MODULE_ADDRESS}.module.rds.aws_iam_role_policy_attachment.rds_enhanced_monitoring" ' + . as $all + | [ + $all[] as $resource + | select($resource.address | startswith($role_prefix)) + | { + name: $resource.values.name, + attached_policy_arns: [ + $all[] + | select(.address | startswith($attachment_prefix)) + | select(.values.role == $resource.values.name) + | .values.policy_arn + ] + } + ] +' "$STATE_RESOURCES_FILE")" + + OLD_PARAMETER_GROUPS_JSON="$(jq -c --arg db_addr "${MODULE_ADDRESS}.aws_db_parameter_group.main[0]" --arg cluster_addr "${MODULE_ADDRESS}.aws_rds_cluster_parameter_group.main[0]" ' + { + db_parameter_groups: [ + .[] + | select(.address == $db_addr) + | .values.name + ], + db_cluster_parameter_groups: [ + .[] + | select(.address == $cluster_addr) + | .values.name + ] + } +' "$STATE_RESOURCES_FILE")" + array_from_newline_text OLD_DB_PARAMETER_GROUP_NAMES "$(jq -r '.db_parameter_groups[]?' <<<"$OLD_PARAMETER_GROUPS_JSON")" + array_from_newline_text OLD_DB_CLUSTER_PARAMETER_GROUP_NAMES "$(jq -r '.db_cluster_parameter_groups[]?' <<<"$OLD_PARAMETER_GROUPS_JSON")" + + OLD_DB_SUBNET_GROUPS_JSON="$(jq -c --arg prefix "${MODULE_ADDRESS}.module.rds.aws_db_subnet_group." ' + [ + .[] + | select(.address | startswith($prefix)) + | .values.name + ] +' "$STATE_RESOURCES_FILE")" + array_from_newline_text OLD_DB_SUBNET_GROUP_NAMES "$(jq -r '.[]?' <<<"$OLD_DB_SUBNET_GROUPS_JSON")" + + collect_state_addresses_for_removal + STATE_REMOVE_ADDRESSES_JSON="$(printf '%s\n' "${STATE_REMOVE_ADDRESSES[@]}" | json_array_from_lines)" + write_manifest + + log "artifact directory: $ARTIFACT_DIR" + log "module address: $MODULE_ADDRESS" + log "current cluster identifier: $CURRENT_CLUSTER_IDENTIFIER" + log "restored cluster name: $RESTORED_NAME" + log "source snapshot id: $SOURCE_SNAPSHOT_ID" + log "copied snapshot id: $COPIED_SNAPSHOT_ID" + if [[ "$SKIP_OLD_FINAL_SNAPSHOT" != "true" ]]; then + log "old final snapshot id: $OLD_FINAL_SNAPSHOT_ID" + fi + + if [[ "$DRY_RUN" == "true" ]]; then + log "dry run: would update ${CONFIG_FILE} in kms-bootstrap mode" + else + update_rds_config_file "$CONFIG_FILE" "kms-bootstrap" + copy_file "$CONFIG_FILE" "$ARTIFACT_DIR/$(basename "$CONFIG_FILE").after-kms-bootstrap" + terraform fmt "$CONFIG_FILE" >/dev/null + fi + + if [[ -n "$TARGET_STORAGE_KMS_ALIAS" ]]; then + if [[ "$DRY_RUN" == "true" ]]; then + log "dry run: would target apply ${MODULE_ADDRESS}.aws_kms_key.rds_storage[0] and ${MODULE_ADDRESS}.aws_kms_alias.rds_storage[0]" + else + terraform_apply_storage_kms_only + load_state_snapshot + resolve_target_storage_kms_id + fi + else + resolve_target_storage_kms_id + fi + + if [[ "$DRY_RUN" == "true" ]]; then + log "dry run: would create source snapshot ${SOURCE_SNAPSHOT_ID}" + if [[ -n "$TARGET_STORAGE_KMS_KEY_ARN" ]]; then + log "dry run: would create encrypted snapshot copy ${COPIED_SNAPSHOT_ID} with ${TARGET_STORAGE_KMS_KEY_ARN}" + else + log "dry run: would create encrypted snapshot copy ${COPIED_SNAPSHOT_ID} with the created storage CMK resolved from Terraform state" + fi + else + create_source_snapshot + copy_snapshot_with_target_kms + fi + + if [[ "$DRY_RUN" == "true" ]]; then + log "dry run: would update ${CONFIG_FILE} in restore mode (PI and Enhanced Monitoring temporarily disabled for snapshot restore)" + else + update_rds_config_file "$CONFIG_FILE" "restore" + copy_file "$CONFIG_FILE" "$ARTIFACT_DIR/$(basename "$CONFIG_FILE").after-restore" + terraform fmt "$CONFIG_FILE" >/dev/null + fi + + if [[ "$DRY_RUN" == "true" ]]; then + log "dry run: would remove these Terraform addresses from state:" + printf '%s\n' "${STATE_REMOVE_ADDRESSES[@]}" >&2 + else + remove_old_rds_state + fi + + if [[ "$DRY_RUN" == "true" ]]; then + log "dry run: would run a targeted terraform apply for ${MODULE_ADDRESS} to restore the new Aurora cluster from ${COPIED_SNAPSHOT_ID}" + else + terraform_apply_byo_vpc_only + fi + + if [[ "$DRY_RUN" == "true" ]]; then + log "dry run: would restore original config and re-apply in post-restore mode (re-enables PI and Enhanced Monitoring)" + if [[ "$INCLUDE_PERFORMANCE_INSIGHTS" == "true" ]]; then + log "dry run: would also enable Performance Insights CMK configuration" + if [[ -n "$PERFORMANCE_INSIGHTS_KMS_ALIAS" ]]; then + log "dry run: would set observability.kms.kms_alias to ${PERFORMANCE_INSIGHTS_KMS_ALIAS}" + fi + fi + else + log "restoring original config for post-restore edit (re-enables PI and Enhanced Monitoring)" + copy_file "$ARTIFACT_DIR/$(basename "$CONFIG_FILE").original" "$CONFIG_FILE" + update_rds_config_file "$CONFIG_FILE" "post-restore" + copy_file "$CONFIG_FILE" "$ARTIFACT_DIR/$(basename "$CONFIG_FILE").after-post-restore" + terraform fmt "$CONFIG_FILE" >/dev/null + fi + + if [[ "$DRY_RUN" == "true" ]]; then + log "dry run: would run a second targeted terraform apply for ${MODULE_ADDRESS} to reconcile any post-restore in-place updates before cleanup" + else + terraform_reconcile_restored_byo_vpc + fi +else + load_cleanup_manifest + log "cleanup-only mode using manifest: $MANIFEST_FILE" + log "current cluster identifier: $CURRENT_CLUSTER_IDENTIFIER" + if [[ "$SKIP_OLD_FINAL_SNAPSHOT" != "true" && -n "$OLD_FINAL_SNAPSHOT_ID" ]]; then + log "old final snapshot id: $OLD_FINAL_SNAPSHOT_ID" + fi +fi + +delete_old_cluster_resources + +if [[ "$CLEANUP_ONLY" == "true" ]]; then + log "cleanup-only mode complete" +elif [[ "$DRY_RUN" == "true" ]]; then + log "dry run: would run a full terraform apply after the restored cluster is in place" +else + log "running final full terraform apply after the restored cluster is in place" + terraform_cmd apply -auto-approve +fi + +if [[ "$CLEANUP_ONLY" == "true" ]]; then + log "cleanup complete" +else + log "migration complete" + log "caller config now points at the restored cluster and keeps snapshot_identifier pinned to ${COPIED_SNAPSHOT_ID}" + log "artifacts saved under ${ARTIFACT_DIR}" +fi diff --git a/byo-vpc/variables.tf b/byo-vpc/variables.tf index fb99f04..e09f040 100644 --- a/byo-vpc/variables.tf +++ b/byo-vpc/variables.tf @@ -7,6 +7,26 @@ variable "vpc_config" { }) } +variable "kms_base_policy" { + type = 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) + })), []) + })) + default = null + description = "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." +} + variable "rds_config" { type = object({ name = optional(string, "fleet") @@ -17,22 +37,96 @@ variable "rds_config" { allowed_cidr_blocks = optional(list(string), []) apply_immediately = optional(bool, true) monitoring_interval = optional(number, 10) + backtrack_window = optional(number, null) db_parameter_group_name = optional(string) db_parameters = optional(map(string), {}) db_cluster_parameter_group_name = optional(string) db_cluster_parameters = optional(map(string), {}) enabled_cloudwatch_logs_exports = optional(list(string), []) - master_username = optional(string, "fleet") - snapshot_identifier = optional(string) - cluster_tags = optional(map(string), {}) - preferred_maintenance_window = optional(string, "thu:23:00-fri:00:00") - skip_final_snapshot = optional(bool, true) - backup_retention_period = optional(number, 7) - replicas = optional(number, 2) - serverless = optional(bool, false) - serverless_min_capacity = optional(number, 2) - serverless_max_capacity = optional(number, 10) - restore_to_point_in_time = optional(map(string), {}) + final_snapshot_identifier = optional(string, null) + password_secret_kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-rds-password-secret") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-password-secret" + extra_kms_policies = [] + }) + storage_kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-rds-storage") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-storage" + extra_kms_policies = [] + }) + observability = optional(object({ + performance_insights_enabled = optional(bool, true) + retention_period = optional(number, null) + database_insights_mode = optional(string, null) + kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-rds-performance-insights") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-performance-insights" + extra_kms_policies = [] + }) + }), { + performance_insights_enabled = true + retention_period = null + database_insights_mode = null + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-performance-insights" + extra_kms_policies = [] + } + }) + cloudwatch_log_group = optional(object({ + retention_in_days = optional(number, null) + skip_destroy = optional(bool, false) + kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-rds-logs") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-logs" + extra_kms_policies = [] + }) + }), { + retention_in_days = null + skip_destroy = false + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-logs" + extra_kms_policies = [] + } + }) + master_username = optional(string, "fleet") + snapshot_identifier = optional(string) + cluster_tags = optional(map(string), {}) + preferred_maintenance_window = optional(string, "thu:23:00-fri:00:00") + skip_final_snapshot = optional(bool, true) + backup_retention_period = optional(number, 7) + replicas = optional(number, 2) + serverless = optional(bool, false) + serverless_min_capacity = optional(number, 2) + serverless_max_capacity = optional(number, 10) + restore_to_point_in_time = optional(map(string), {}) }) default = { name = "fleet" @@ -43,25 +137,84 @@ variable "rds_config" { allowed_cidr_blocks = [] apply_immediately = true monitoring_interval = 10 + backtrack_window = null db_parameter_group_name = null db_parameters = {} db_cluster_parameter_group_name = null db_cluster_parameters = {} enabled_cloudwatch_logs_exports = [] - master_username = "fleet" - snapshot_identifier = null - cluster_tags = {} - preferred_maintenance_window = "thu:23:00-fri:00:00" - skip_final_snapshot = true - backup_retention_period = 7 - replicas = 2 - serverless = false - serverless_min_capacity = 2 - serverless_max_capacity = 10 - restore_to_point_in_time = {} + final_snapshot_identifier = null + password_secret_kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-password-secret" + extra_kms_policies = [] + } + storage_kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-storage" + extra_kms_policies = [] + } + observability = { + performance_insights_enabled = true + retention_period = null + database_insights_mode = null + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-performance-insights" + extra_kms_policies = [] + } + } + cloudwatch_log_group = { + retention_in_days = null + skip_destroy = false + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-logs" + extra_kms_policies = [] + } + } + master_username = "fleet" + snapshot_identifier = null + cluster_tags = {} + preferred_maintenance_window = "thu:23:00-fri:00:00" + skip_final_snapshot = true + backup_retention_period = 7 + replicas = 2 + serverless = false + serverless_min_capacity = 2 + serverless_max_capacity = 10 + restore_to_point_in_time = {} } description = "The config for the terraform-aws-modules/rds-aurora/aws module" nullable = false + validation { + condition = var.rds_config.backtrack_window == null || (var.rds_config.backtrack_window >= 0 && var.rds_config.backtrack_window <= 259200) + error_message = "rds_config.backtrack_window must be null or between 0 and 259200 seconds." + } + validation { + condition = var.rds_config.observability.database_insights_mode == null || contains(["standard", "advanced"], var.rds_config.observability.database_insights_mode) + error_message = "rds_config.observability.database_insights_mode must be null, \"standard\", or \"advanced\"." + } + validation { + condition = var.rds_config.observability.database_insights_mode != "advanced" || var.rds_config.observability.performance_insights_enabled == true + error_message = "rds_config.observability.performance_insights_enabled must be true when database_insights_mode is \"advanced\"." + } + validation { + condition = var.rds_config.observability.database_insights_mode != "advanced" || var.rds_config.monitoring_interval > 0 + error_message = "rds_config.monitoring_interval must be greater than 0 when database_insights_mode is \"advanced\"." + } + validation { + condition = var.rds_config.observability.database_insights_mode != "advanced" || var.rds_config.observability.retention_period == null || var.rds_config.observability.retention_period >= 465 + error_message = "rds_config.observability.retention_period must be at least 465 when database_insights_mode is \"advanced\"." + } + validation { + condition = var.rds_config.observability.performance_insights_enabled == true || (var.rds_config.observability.database_insights_mode == null && var.rds_config.observability.kms.cmk_enabled == false && var.rds_config.observability.kms.kms_key_arn == null) + error_message = "When performance_insights_enabled is false, database_insights_mode must be null and observability KMS must be disabled." + } } variable "redis_config" { @@ -81,11 +234,46 @@ variable "redis_config" { engine_version = optional(string, "7.1") family = optional(string, "redis7") at_rest_encryption_enabled = optional(bool, true) - transit_encryption_enabled = optional(bool, true) + at_rest_kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-redis-at-rest") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-redis-at-rest" + extra_kms_policies = [] + }) + transit_encryption_enabled = optional(bool, true) parameter = optional(list(object({ name = string value = string })), []) + cloudwatch_log_group = optional(object({ + retention_in_days = optional(number, null) + skip_destroy = optional(bool, false) + kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-redis-logs") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-redis-logs" + extra_kms_policies = [] + }) + }), { + retention_in_days = null + skip_destroy = false + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-redis-logs" + extra_kms_policies = [] + } + }) log_delivery_configuration = optional(list(map(any)), []) tags = optional(map(string), {}) }) @@ -105,10 +293,26 @@ variable "redis_config" { engine_version = "7.1" family = "redis7" at_rest_encryption_enabled = true - transit_encryption_enabled = true - parameter = [] - log_delivery_configuration = [] - tags = {} + at_rest_kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-redis-at-rest" + extra_kms_policies = [] + } + transit_encryption_enabled = true + parameter = [] + cloudwatch_log_group = { + retention_in_days = null + skip_destroy = false + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-redis-logs" + extra_kms_policies = [] + } + } + log_delivery_configuration = [] + tags = {} } } @@ -128,21 +332,27 @@ variable "ecs_cluster" { create = optional(bool, true) retention_in_days = optional(number, 90) kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-ecs-cluster-logs") + cmk_enabled = optional(bool, null) + enabled = optional(bool, null) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-ecs-cluster-logs") + extra_kms_policies = optional(list(any), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-ecs-cluster-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-ecs-cluster-logs" + extra_kms_policies = [] }) }), { create = true retention_in_days = 90 kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-ecs-cluster-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-ecs-cluster-logs" + extra_kms_policies = [] } }) cluster_settings = optional(any, { @@ -180,7 +390,7 @@ variable "ecs_cluster" { create = true retention_in_days = 90 kms = { - enabled = false + cmk_enabled = false kms_key_arn = null kms_alias = "fleet-ecs-cluster-logs" } @@ -205,7 +415,7 @@ variable "ecs_cluster" { } tags = {} } - description = "The config for the terraform-aws-modules/ecs/aws module" + 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 } @@ -234,22 +444,30 @@ variable "fleet_config" { repository_credentials = optional(string, "") private_key_secret_name = optional(string, "fleet-server-private-key") private_key_secret_kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-server-private-key") + 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), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-server-private-key" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-server-private-key" + extra_kms_policies = [] }) fargate_ephemeral_storage_kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-fargate-ephemeral-storage") + cmk_enabled = optional(bool, null) + enabled = optional(bool, null) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-fargate-ephemeral-storage") + extra_kms_policies = optional(list(any), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-fargate-ephemeral-storage" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-fargate-ephemeral-storage" + extra_kms_policies = [] }) server_tls_enabled = optional(bool, false) service = optional(object({ @@ -258,17 +476,19 @@ variable "fleet_config" { name = "fleet" }) database = optional(object({ - password_secret_arn = string - user = string - database = string - address = string - rr_address = optional(string, null) + password_secret_arn = string + password_secret_kms_key_arn = optional(string, null) + user = string + database = string + address = string + rr_address = optional(string, null) }), { - password_secret_arn = null - user = null - database = null - address = null - rr_address = null + password_secret_arn = null + password_secret_kms_key_arn = null + user = null + database = null + address = null + rr_address = null }) redis = optional(object({ address = string @@ -284,13 +504,17 @@ variable "fleet_config" { prefix = optional(string, "fleet") retention = optional(number, 5) kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-application-logs") + 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), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] }) }), { name = null @@ -299,9 +523,11 @@ variable "fleet_config" { prefix = "fleet" retention = 5 kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] } }) loadbalancer = optional(object({ @@ -370,24 +596,28 @@ variable "fleet_config" { 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 = {} }) }) @@ -414,14 +644,18 @@ variable "fleet_config" { repository_credentials = "" private_key_secret_name = "fleet-server-private-key" private_key_secret_kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-server-private-key" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-server-private-key" + extra_kms_policies = [] } fargate_ephemeral_storage_kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-fargate-ephemeral-storage" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-fargate-ephemeral-storage" + extra_kms_policies = [] } server_tls_enabled = false service = { @@ -445,9 +679,11 @@ variable "fleet_config" { prefix = "fleet" retention = 5 kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] } } loadbalancer = { @@ -486,16 +722,18 @@ variable "fleet_config" { 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 = {} } } - description = "The configuration object for Fleet itself. Fields that default to null will have their respective resources created if not specified." + 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 } diff --git a/byo-vpc/versions.tf b/byo-vpc/versions.tf index 4a2fa17..3bd7c2f 100644 --- a/byo-vpc/versions.tf +++ b/byo-vpc/versions.tf @@ -1,5 +1,5 @@ terraform { - required_version = ">= 1.3.8" + required_version = ">= 1.5.0" required_providers { aws = { @@ -8,4 +8,3 @@ terraform { } } } - diff --git a/main.tf b/main.tf index 7c43593..2cf46c8 100644 --- a/main.tf +++ b/main.tf @@ -1,5 +1,92 @@ terraform { - required_version = ">= 1.3.8" + required_version = ">= 1.5.0" +} + +locals { + vpc_flow_log_cloudwatch_log_group_cmk_enabled = var.vpc.flow_log_cloudwatch_log_group_kms.cmk_enabled + vpc_flow_log_cloudwatch_log_group_create_kms_key = var.vpc.enable_flow_log == true && var.vpc.create_flow_log_cloudwatch_log_group == true && local.vpc_flow_log_cloudwatch_log_group_cmk_enabled == true && var.vpc.flow_log_cloudwatch_log_group_kms.kms_key_arn == null + vpc_flow_log_cloudwatch_log_group_kms_key_arn = var.vpc.enable_flow_log == true && var.vpc.create_flow_log_cloudwatch_log_group == true && local.vpc_flow_log_cloudwatch_log_group_cmk_enabled == true ? ( + var.vpc.flow_log_cloudwatch_log_group_kms.kms_key_arn != null ? var.vpc.flow_log_cloudwatch_log_group_kms.kms_key_arn : aws_kms_key.vpc_flow_log_cloudwatch_log_group[0].arn + ) : null + kms_base_policy_statements = var.kms_base_policy != null ? var.kms_base_policy : [ + { + sid = "EnableRootPermissions" + effect = "Allow" + principals = { + type = "AWS" + identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"] + } + actions = ["kms:*"] + resources = ["*"] + conditions = [] + } + ] + kms_service_statements = { + cloudwatch_logs = { + sid = "AllowCloudWatchLogsUseOfTheKey" + effect = "Allow" + principals = { + type = "Service" + identifiers = ["logs.${data.aws_region.current.id}.amazonaws.com"] + } + actions = [ + "kms:Encrypt*", + "kms:Decrypt*", + "kms:ReEncrypt*", + "kms:GenerateDataKey*", + "kms:Describe*" + ] + resources = ["*"] + conditions = [] + } + } +} + +data "aws_caller_identity" "current" {} +data "aws_partition" "current" {} +data "aws_region" "current" {} + +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] + ) + 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 + } + } + } + } +} + +resource "aws_kms_key" "vpc_flow_log_cloudwatch_log_group" { + count = local.vpc_flow_log_cloudwatch_log_group_create_kms_key == true ? 1 : 0 + description = "CMK for VPC flow log CloudWatch Logs encryption." + enable_key_rotation = true + policy = data.aws_iam_policy_document.vpc_flow_log_cloudwatch_log_group_kms[0].json +} + +resource "aws_kms_alias" "vpc_flow_log_cloudwatch_log_group" { + count = local.vpc_flow_log_cloudwatch_log_group_create_kms_key == true ? 1 : 0 + target_key_id = aws_kms_key.vpc_flow_log_cloudwatch_log_group[0].id + name = "alias/${var.vpc.flow_log_cloudwatch_log_group_kms.kms_alias}" } module "vpc" { @@ -28,13 +115,15 @@ module "vpc" { flow_log_max_aggregation_interval = var.vpc.flow_log_max_aggregation_interval flow_log_cloudwatch_log_group_name_prefix = var.vpc.flow_log_cloudwatch_log_group_name_prefix flow_log_cloudwatch_log_group_name_suffix = var.vpc.flow_log_cloudwatch_log_group_name_suffix + flow_log_cloudwatch_log_group_kms_key_id = local.vpc_flow_log_cloudwatch_log_group_kms_key_arn vpc_flow_log_tags = var.vpc.vpc_flow_log_tags enable_dns_hostnames = var.vpc.enable_dns_hostnames enable_dns_support = var.vpc.enable_dns_support } module "byo-vpc" { - source = "./byo-vpc" + source = "./byo-vpc" + kms_base_policy = var.kms_base_policy vpc_config = { vpc_id = module.vpc.vpc_id networking = { diff --git a/byo-vpc/byo-db/byo-ecs/scripts/cloudwatch_logs_kms_migration.sh b/scripts/cloudwatch_logs_kms_migration.sh similarity index 74% rename from byo-vpc/byo-db/byo-ecs/scripts/cloudwatch_logs_kms_migration.sh rename to scripts/cloudwatch_logs_kms_migration.sh index da4aa0b..b784be2 100755 --- a/byo-vpc/byo-db/byo-ecs/scripts/cloudwatch_logs_kms_migration.sh +++ b/scripts/cloudwatch_logs_kms_migration.sh @@ -37,10 +37,33 @@ if [[ -z "$NEW_KMS_KEY_ARN" ]]; then exit 1 fi -CLOUDTRAIL_JSON="$(aws cloudtrail lookup-events \ - --region "$REGION" \ - --lookup-attributes AttributeKey=EventName,AttributeValue=AssociateKmsKey \ - --output json)" +CLOUDTRAIL_JSON='{"Events":[]}' +CLOUDTRAIL_NEXT_TOKEN='' +while :; do + if [[ -n "$CLOUDTRAIL_NEXT_TOKEN" ]]; then + CLOUDTRAIL_PAGE_JSON="$(aws cloudtrail lookup-events \ + --region "$REGION" \ + --lookup-attributes AttributeKey=EventName,AttributeValue=AssociateKmsKey \ + --starting-token "$CLOUDTRAIL_NEXT_TOKEN" \ + --output json)" + else + CLOUDTRAIL_PAGE_JSON="$(aws cloudtrail lookup-events \ + --region "$REGION" \ + --lookup-attributes AttributeKey=EventName,AttributeValue=AssociateKmsKey \ + --output json)" + fi + + CLOUDTRAIL_JSON="$(jq -c --argjson page "$CLOUDTRAIL_PAGE_JSON" ' + { + Events: ((.Events // []) + ($page.Events // [])) + } + ' <<<"$CLOUDTRAIL_JSON")" + + CLOUDTRAIL_NEXT_TOKEN="$(jq -r '.NextToken // empty' <<<"$CLOUDTRAIL_PAGE_JSON")" + if [[ -z "$CLOUDTRAIL_NEXT_TOKEN" ]]; then + break + fi +done CUTOFF_ISO="$(jq -r --arg lg "$LOG_GROUP_NAME" --arg kms "$NEW_KMS_KEY_ARN" ' [ @@ -92,9 +115,13 @@ done OLD_STREAMS_JSON="$(jq --argjson cutoff "$CUTOFF_MS" ' [ .[] - | select((.lastEventTimestamp // 0) > 0) - | select(.lastEventTimestamp < $cutoff) - | {logStreamName, lastEventTimestamp} + | select((.creationTime // 0) > 0) + | select(.creationTime < $cutoff) + | { + logStreamName, + creationTime, + lastEventTimestamp + } ] ' <<<"$ALL_STREAMS_JSON")" @@ -106,7 +133,7 @@ echo "new_kms_key_arn: $NEW_KMS_KEY_ARN" echo "associate_kms_at: $CUTOFF_ISO" echo "old_stream_count: $OLD_COUNT" echo -echo "Old streams (lastEventTimestamp < associate_kms_at):" +echo "Streams created before associate_kms_at (creationTime < associate_kms_at):" jq '.' <<<"$OLD_STREAMS_JSON" echo diff --git a/variables.tf b/variables.tf index affb76c..9c7397a 100644 --- a/variables.tf +++ b/variables.tf @@ -24,7 +24,18 @@ variable "vpc" { flow_log_max_aggregation_interval = optional(number, 600) flow_log_cloudwatch_log_group_name_prefix = optional(string, "/aws/vpc-flow-log/") flow_log_cloudwatch_log_group_name_suffix = optional(string, "") - vpc_flow_log_tags = optional(map(string), {}) + flow_log_cloudwatch_log_group_kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-vpc-flow-logs") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-vpc-flow-logs" + extra_kms_policies = [] + }) + vpc_flow_log_tags = optional(map(string), {}) }) default = { name = "fleet" @@ -51,7 +62,13 @@ variable "vpc" { flow_log_max_aggregation_interval = 600 flow_log_cloudwatch_log_group_name_prefix = "/aws/vpc-flow-log/" flow_log_cloudwatch_log_group_name_suffix = "" - vpc_flow_log_tags = {} + flow_log_cloudwatch_log_group_kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-vpc-flow-logs" + extra_kms_policies = [] + } + vpc_flow_log_tags = {} } } @@ -59,6 +76,26 @@ variable "certificate_arn" { type = string } +variable "kms_base_policy" { + type = 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) + })), []) + })) + default = null + description = "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." +} + variable "rds_config" { type = object({ name = optional(string, "fleet") @@ -69,21 +106,95 @@ variable "rds_config" { allowed_cidr_blocks = optional(list(string), []) apply_immediately = optional(bool, true) monitoring_interval = optional(number, 10) + backtrack_window = optional(number, null) db_parameter_group_name = optional(string) db_parameters = optional(map(string), {}) db_cluster_parameter_group_name = optional(string) db_cluster_parameters = optional(map(string), {}) enabled_cloudwatch_logs_exports = optional(list(string), []) - master_username = optional(string, "fleet") - snapshot_identifier = optional(string) - cluster_tags = optional(map(string), {}) - skip_final_snapshot = optional(bool, true) - backup_retention_period = optional(number, 7) - replicas = optional(number, 2) - serverless = optional(bool, false) - serverless_min_capacity = optional(number, 2) - serverless_max_capacity = optional(number, 10) - restore_to_point_in_time = optional(map(string), {}) + final_snapshot_identifier = optional(string, null) + password_secret_kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-rds-password-secret") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-password-secret" + extra_kms_policies = [] + }) + storage_kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-rds-storage") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-storage" + extra_kms_policies = [] + }) + observability = optional(object({ + performance_insights_enabled = optional(bool, true) + retention_period = optional(number, null) + database_insights_mode = optional(string, null) + kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-rds-performance-insights") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-performance-insights" + extra_kms_policies = [] + }) + }), { + performance_insights_enabled = true + retention_period = null + database_insights_mode = null + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-performance-insights" + extra_kms_policies = [] + } + }) + cloudwatch_log_group = optional(object({ + retention_in_days = optional(number, null) + skip_destroy = optional(bool, false) + kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-rds-logs") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-logs" + extra_kms_policies = [] + }) + }), { + retention_in_days = null + skip_destroy = false + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-logs" + extra_kms_policies = [] + } + }) + master_username = optional(string, "fleet") + snapshot_identifier = optional(string) + cluster_tags = optional(map(string), {}) + skip_final_snapshot = optional(bool, true) + backup_retention_period = optional(number, 7) + replicas = optional(number, 2) + serverless = optional(bool, false) + serverless_min_capacity = optional(number, 2) + serverless_max_capacity = optional(number, 10) + restore_to_point_in_time = optional(map(string), {}) }) default = { name = "fleet" @@ -94,24 +205,83 @@ variable "rds_config" { allowed_cidr_blocks = [] apply_immediately = true monitoring_interval = 10 + backtrack_window = null db_parameter_group_name = null db_parameters = {} db_cluster_parameter_group_name = null db_cluster_parameters = {} enabled_cloudwatch_logs_exports = [] - master_username = "fleet" - snapshot_identifier = null - cluster_tags = {} - skip_final_snapshot = true - backup_retention_period = 7 - replicas = 2 - serverless = false - serverless_min_capacity = 2 - serverless_max_capacity = 10 - restore_to_point_in_time = {} + final_snapshot_identifier = null + password_secret_kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-password-secret" + extra_kms_policies = [] + } + storage_kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-storage" + extra_kms_policies = [] + } + observability = { + performance_insights_enabled = true + retention_period = null + database_insights_mode = null + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-performance-insights" + extra_kms_policies = [] + } + } + cloudwatch_log_group = { + retention_in_days = null + skip_destroy = false + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-rds-logs" + extra_kms_policies = [] + } + } + master_username = "fleet" + snapshot_identifier = null + cluster_tags = {} + skip_final_snapshot = true + backup_retention_period = 7 + replicas = 2 + serverless = false + serverless_min_capacity = 2 + serverless_max_capacity = 10 + restore_to_point_in_time = {} } description = "The config for the terraform-aws-modules/rds-aurora/aws module" nullable = false + validation { + condition = var.rds_config.backtrack_window == null || (var.rds_config.backtrack_window >= 0 && var.rds_config.backtrack_window <= 259200) + error_message = "rds_config.backtrack_window must be null or between 0 and 259200 seconds." + } + validation { + condition = var.rds_config.observability.database_insights_mode == null || contains(["standard", "advanced"], var.rds_config.observability.database_insights_mode) + error_message = "rds_config.observability.database_insights_mode must be null, \"standard\", or \"advanced\"." + } + validation { + condition = var.rds_config.observability.database_insights_mode != "advanced" || var.rds_config.observability.performance_insights_enabled == true + error_message = "rds_config.observability.performance_insights_enabled must be true when database_insights_mode is \"advanced\"." + } + validation { + condition = var.rds_config.observability.database_insights_mode != "advanced" || var.rds_config.monitoring_interval > 0 + error_message = "rds_config.monitoring_interval must be greater than 0 when database_insights_mode is \"advanced\"." + } + validation { + condition = var.rds_config.observability.database_insights_mode != "advanced" || var.rds_config.observability.retention_period == null || var.rds_config.observability.retention_period >= 465 + error_message = "rds_config.observability.retention_period must be at least 465 when database_insights_mode is \"advanced\"." + } + validation { + condition = var.rds_config.observability.performance_insights_enabled == true || (var.rds_config.observability.database_insights_mode == null && var.rds_config.observability.kms.cmk_enabled == false && var.rds_config.observability.kms.kms_key_arn == null) + error_message = "When performance_insights_enabled is false, database_insights_mode must be null and observability KMS must be disabled." + } } variable "redis_config" { @@ -130,11 +300,45 @@ variable "redis_config" { engine_version = optional(string, "7.1") family = optional(string, "redis7") at_rest_encryption_enabled = optional(bool, true) - transit_encryption_enabled = optional(bool, true) + at_rest_kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-redis-at-rest") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-redis-at-rest" + extra_kms_policies = [] + }) + transit_encryption_enabled = optional(bool, true) parameter = optional(list(object({ name = string value = string })), []) + cloudwatch_log_group = optional(object({ + retention_in_days = optional(number, null) + skip_destroy = optional(bool, false) + kms = optional(object({ + cmk_enabled = optional(bool, false) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-redis-logs") + extra_kms_policies = optional(list(any), []) + }), { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-redis-logs" + extra_kms_policies = [] + }) + }), { + retention_in_days = null + skip_destroy = false + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-redis-logs" + } + }) log_delivery_configuration = optional(list(map(any)), []) tags = optional(map(string), {}) }) @@ -153,10 +357,26 @@ variable "redis_config" { engine_version = "7.1" family = "redis7" at_rest_encryption_enabled = true - transit_encryption_enabled = true - parameter = [] - log_delivery_configuration = [] - tags = {} + at_rest_kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-redis-at-rest" + extra_kms_policies = [] + } + transit_encryption_enabled = true + parameter = [] + cloudwatch_log_group = { + retention_in_days = null + skip_destroy = false + kms = { + cmk_enabled = false + kms_key_arn = null + kms_alias = "fleet-redis-logs" + extra_kms_policies = [] + } + } + log_delivery_configuration = [] + tags = {} } } @@ -176,19 +396,24 @@ variable "ecs_cluster" { create = optional(bool, true) retention_in_days = optional(number, 90) kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-ecs-cluster-logs") + cmk_enabled = optional(bool, null) + enabled = optional(bool, null) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-ecs-cluster-logs") + extra_kms_policies = optional(list(any), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-ecs-cluster-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-ecs-cluster-logs" + extra_kms_policies = [] }) }), { create = true retention_in_days = 90 kms = { - enabled = false + cmk_enabled = null + enabled = null kms_key_arn = null kms_alias = "fleet-ecs-cluster-logs" } @@ -228,9 +453,11 @@ variable "ecs_cluster" { create = true retention_in_days = 90 kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-ecs-cluster-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-ecs-cluster-logs" + extra_kms_policies = [] } } cluster_settings = { @@ -253,7 +480,7 @@ variable "ecs_cluster" { } tags = {} } - description = "The config for the terraform-aws-modules/ecs/aws module" + 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 } @@ -282,22 +509,30 @@ variable "fleet_config" { repository_credentials = optional(string, "") private_key_secret_name = optional(string, "fleet-server-private-key") private_key_secret_kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-server-private-key") + 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), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-server-private-key" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-server-private-key" + extra_kms_policies = [] }) fargate_ephemeral_storage_kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-fargate-ephemeral-storage") + cmk_enabled = optional(bool, null) + enabled = optional(bool, null) + kms_key_arn = optional(string, null) + kms_alias = optional(string, "fleet-fargate-ephemeral-storage") + extra_kms_policies = optional(list(any), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-fargate-ephemeral-storage" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-fargate-ephemeral-storage" + extra_kms_policies = [] }) server_tls_enabled = optional(bool, false) service = optional(object({ @@ -306,17 +541,19 @@ variable "fleet_config" { name = "fleet" }) database = optional(object({ - password_secret_arn = string - user = string - database = string - address = string - rr_address = optional(string, null) + password_secret_arn = string + password_secret_kms_key_arn = optional(string, null) + user = string + database = string + address = string + rr_address = optional(string, null) }), { - password_secret_arn = null - user = null - database = null - address = null - rr_address = null + password_secret_arn = null + password_secret_kms_key_arn = null + user = null + database = null + address = null + rr_address = null }) redis = optional(object({ address = string @@ -332,13 +569,17 @@ variable "fleet_config" { prefix = optional(string, "fleet") retention = optional(number, 5) kms = optional(object({ - enabled = optional(bool, false) - kms_key_arn = optional(string, null) - kms_alias = optional(string, "fleet-application-logs") + 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), []) }), { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] }) }), { name = null @@ -347,9 +588,11 @@ variable "fleet_config" { prefix = "fleet" retention = 5 kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] } }) loadbalancer = optional(object({ @@ -418,24 +661,28 @@ variable "fleet_config" { 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 = {} }) }) @@ -462,14 +709,18 @@ variable "fleet_config" { repository_credentials = "" private_key_secret_name = "fleet-server-private-key" private_key_secret_kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-server-private-key" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-server-private-key" + extra_kms_policies = [] } fargate_ephemeral_storage_kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-fargate-ephemeral-storage" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-fargate-ephemeral-storage" + extra_kms_policies = [] } server_tls_enabled = false service = { @@ -493,9 +744,11 @@ variable "fleet_config" { prefix = "fleet" retention = 5 kms = { - enabled = false - kms_key_arn = null - kms_alias = "fleet-application-logs" + cmk_enabled = null + enabled = null + kms_key_arn = null + kms_alias = "fleet-application-logs" + extra_kms_policies = [] } } loadbalancer = { @@ -534,16 +787,18 @@ variable "fleet_config" { 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 = {} } } - description = "The configuration object for Fleet itself. Fields that default to null will have their respective resources created if not specified." + 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 }