# ALB Logging Addon This addon creates an S3 bucket for ALB access logs with optional in-place SSE-KMS re-encryption via Lambda. ALB access logging requires SSE-S3 (AES256), so objects are written with SSE-S3. When `enable_reencrypt_sweep` is enabled, objects are immediately re-encrypted to SSE-KMS using a customer-managed KMS key. The module-managed CMK also encrypts the Lambda functions and their CloudWatch log groups when the feature is active. ## How it works 1. **ALB writes** log objects to the S3 bucket using SSE-S3 (the only encryption ALB supports). 2. **Event-driven Lambda** (optional, requires `enable_reencrypt_sweep = true`) 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** (optional, requires `enable_reencrypt_sweep = true`) 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=tf-mod-addon-logging-alb-v2.3.0" prefix = "fleet" enable_athena = true # Optional: Enable the re-encrypt and sweep Lambda functions and associated resources. Default: false # enable_reencrypt_sweep = true # Optional: Override the prefix used in IAM role names when enable_reencrypt_sweep = true. # Must be 16 characters or fewer. # iam_role_name_prefix = "fleet" } ``` # 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//AWSLogs//elasticloadbalancing// ``` ## 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 with `enable_reencrypt_sweep = true` so the 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/-logs`. Use `--kms-key-arn` to specify the key explicitly. Use `--batch-role-arn` to pass the Terraform-managed Batch Operations role ARN. 3. Wait for the batch job to finish and validate that objects are now SSE-KMS encrypted. 4. Optionally clean up the script-created IAM role: ``` ./scripts/start-batch-replication-migration.sh \ --cleanup-role \ --source-bucket fleet-alb-logs ```