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. The same module-managed CMK also encrypts the Lambda functions and their CloudWatch log groups.
How it works
- ALB writes log objects to the S3 bucket using SSE-S3 (the only encryption ALB supports).
- Event-driven Lambda triggers on
s3:ObjectCreated:Putand re-encrypts each object in-place to SSE-KMS viaCopyObject. The Lambda'sCopyObjectemitss3:ObjectCreated:Copywhich does not re-trigger the notification (loop prevention). - 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
S3PutObjectCopyjob 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.
- Apply Terraform so the new Lambda functions and Batch Operations IAM role are in place.
- 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.
- Wait for the batch job to finish and validate that objects are now SSE-KMS encrypted.
- 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.37.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
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 |
| 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({ |
null |
no |
| lambda_log_retention_in_days | CloudWatch log retention in days for the re-encrypt and sweep Lambda functions | number |
365 |
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) |