Files
fleet-terraform/addons/logging-alb
Robert Fairburn e7faee93de 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`
2026-03-20 12:07:38 -05:00
..
2025-02-04 10:07:51 -06:00
2026-03-20 12:07:38 -05:00
2026-03-20 12:07:38 -05:00

ALB Logging Addon

This addon creates an S3 bucket for ALB access logs with in-place SSE-KMS re-encryption via Lambda. ALB access logging requires SSE-S3 (AES256), so objects are written with SSE-S3 and immediately re-encrypted to SSE-KMS using a customer-managed KMS key.

How it works

  1. ALB writes log objects to the S3 bucket using SSE-S3 (the only encryption ALB supports).
  2. Event-driven Lambda triggers on s3:ObjectCreated:Put and re-encrypts each object in-place to SSE-KMS via CopyObject. The Lambda's CopyObject emits s3:ObjectCreated:Copy which does not re-trigger the notification (loop prevention).
  3. Daily sweep Lambda runs via EventBridge and scans the last 2 days of ALB log prefixes. If any SSE-S3 objects are found (missed by the event-driven Lambda), it generates a CSV manifest and submits an S3 Batch Operations S3PutObjectCopy job to re-encrypt them.

Optionally creates Athena resources for querying the logs.

Example Configuration

This assumes your fleet module is main and is configured with its default documentation.

See https://github.com/fleetdm/fleet/blob/main/terraform/example/main.tf for details.

module "main" {
  source          = "github.com/fleetdm/fleet-terraform/?ref=main"
  certificate_arn = module.acm.acm_certificate_arn
  vpc = {
    name = random_pet.main.id
  }
  fleet_config = {
    extra_environment_variables = module.firehose-logging.fleet_extra_environment_variables
    extra_iam_policies          = module.firehose-logging.fleet_extra_iam_policies
  }
  alb_config = {
    access_logs = {
      bucket  = module.logging_alb.log_s3_bucket_id
      prefix  = "fleet"
      enabled = true
    }
  }
}

module "logging_alb" {
  source        = "github.com/fleetdm/fleet-terraform//addons/logging-alb?ref=main"
  prefix        = "fleet"
  enable_athena = true
}

Additional Information

Once this terraform is applied, the Athena table will need to be created. See https://docs.aws.amazon.com/athena/latest/ug/application-load-balancer-logs.html for help with creating the table.

For this implementation, the S3 pattern for the CREATE TABLE query should look like this:

s3://your-alb-logs-bucket/<PREFIX>/AWSLogs/<ACCOUNT-ID>/elasticloadbalancing/<REGION>/

Migration from previous versions

If upgrading from a previous version that used a separate archive bucket, use the provided script to re-encrypt existing SSE-S3 objects in-place. The script uses S3 Batch Operations with S3PutObjectCopy to re-encrypt objects at scale.

  1. Apply Terraform so the new Lambda functions and Batch Operations IAM role are in place.
  2. Run the migration script to re-encrypt existing objects:
./scripts/start-batch-replication-migration.sh \
  --source-bucket fleet-alb-logs

The script auto-detects the KMS key by deriving a prefix from the bucket name (stripping the -alb-logs suffix) and looking up a KMS alias matching alias/<prefix>-logs. Use --kms-key-arn to specify the key explicitly. Use --batch-role-arn to pass the Terraform-managed Batch Operations role ARN.

  1. Wait for the batch job to finish and validate that objects are now SSE-KMS encrypted.
  2. Optionally clean up the script-created IAM role:
./scripts/start-batch-replication-migration.sh \
  --cleanup-role \
  --source-bucket fleet-alb-logs

Requirements

No requirements.

Providers

Name Version
archive 2.7.1
aws 6.34.0

Modules

Name Source Version
athena-s3-bucket terraform-aws-modules/s3-bucket/aws 5.0.0
s3_bucket_for_logs terraform-aws-modules/s3-bucket/aws 5.0.0

Resources

Name Type
aws_athena_database.logs resource
aws_athena_workgroup.logs resource
aws_cloudwatch_event_rule.sweep_reencrypt resource
aws_cloudwatch_event_target.sweep_reencrypt resource
aws_cloudwatch_log_group.lambda_reencrypt resource
aws_cloudwatch_log_group.lambda_sweep resource
aws_glue_catalog_table.partitioned_alb_logs resource
aws_iam_role.batch_reencrypt resource
aws_iam_role.lambda_reencrypt resource
aws_iam_role.lambda_sweep resource
aws_iam_role_policy.batch_reencrypt resource
aws_iam_role_policy.lambda_reencrypt resource
aws_iam_role_policy.lambda_sweep resource
aws_kms_alias.logs_alias resource
aws_kms_key.logs resource
aws_lambda_function.reencrypt resource
aws_lambda_function.sweep_reencrypt resource
aws_lambda_permission.eventbridge_invoke_sweep resource
aws_lambda_permission.s3_invoke_reencrypt resource
aws_s3_bucket_notification.reencrypt resource
archive_file.lambda_reencrypt data source
archive_file.lambda_sweep data source
aws_caller_identity.current data source
aws_iam_policy_document.batch_reencrypt data source
aws_iam_policy_document.batch_reencrypt_assume_role data source
aws_iam_policy_document.kms data source
aws_iam_policy_document.lambda_reencrypt data source
aws_iam_policy_document.lambda_reencrypt_assume_role data source
aws_iam_policy_document.lambda_sweep data source
aws_iam_policy_document.lambda_sweep_assume_role data source
aws_iam_policy_document.s3_athena_bucket data source
aws_iam_policy_document.s3_log_bucket data source
aws_region.current data source

Inputs

Name Description Type Default Required
alt_path_prefix Used if the prefix inside of the s3 bucket doesn't match the name of the bucket prefix string null no
enable_athena n/a bool true no
extra_kms_policies n/a list(any) [] no
extra_s3_athena_policies n/a list(any) [] no
extra_s3_log_policies n/a list(any) [] no
prefix n/a string "fleet" no
s3_expiration_days n/a number 90 no
s3_newer_noncurrent_versions n/a number 5 no
s3_noncurrent_version_expiration_days n/a number 30 no
s3_transition_days n/a number 30 no

Outputs

Name Description
log_s3_bucket_id S3 bucket used by ALB access logging (SSE-S3 on write, re-encrypted to SSE-KMS by Lambda)