Files
fleet-terraform/byo-vpc/byo-db/byo-ecs
Robert Fairburn e2681aea79 Add configurable Fleet private key delivery, optional command override, and caller-managed private key secret ARN support (#205)
## Summary

  This PR extends the Fleet ECS modules to support:

  - configurable Fleet server private key delivery via ecs or iam
  - optional fleet_config.command overrides for Fleet task definitions
- caller-managed Fleet server private key secrets via
fleet_config.private_key_secret_arn

It also updates the external vuln scans addon to follow the same
private-key delivery and command behavior, and refreshes the generated
module documentation.

  ## What changed

- Added fleet_config.private_key_delivery_method with supported values:
- ecs (default): inject the private key as FLEET_SERVER_PRIVATE_KEY
      - iam: inject the secret ARN as FLEET_SERVER_PRIVATE_KEY_ARN
  - Added validation so only ecs or iam are accepted
- Added fleet_config.command support so callers can override the Fleet
container command when needed
- Added fleet_config.private_key_secret_arn support so callers can
supply an existing Secrets Manager secret instead of having this module
create and populate one
- Updated IAM policy wiring so private-key secret access and private-key
KMS decrypt permissions are granted to the correct role based on the
selected delivery method
- Updated module outputs so fleet_server_private_key_secret_arn returns
the effective ARN whether the secret is module-managed or caller-managed
- Updated addons/external-vuln-scans to consume the same private-key
delivery mode and optional command behavior from the passed fleet_config
  - Updated byo-ecs documentation and regenerated README files

  ## Behavior

  ### Private key delivery

- ecs mode injects the secret value into the container and uses the ECS
execution role for private-key secret access
- iam mode injects the secret ARN into the container environment and
uses the Fleet task role for private-key secret access

  ### Command override

- When fleet_config.command is unset, the task definition omits command
- When fleet_config.command is set, the provided command is passed
through to the Fleet container
- This behavior now applies consistently to both the main ECS service
and external vuln scans

  ### Caller-managed private key secret

- When fleet_config.private_key_secret_arn is unset, the module creates
and populates the Fleet private key secret
- When fleet_config.private_key_secret_arn is set, the module uses the
provided secret ARN and does not create the secret, secret version, or
generated random value

  ## KMS note for external secrets

If a caller-managed private-key secret uses a CMK, the caller must also
provide the existing private-key KMS ARN input so the correct role
receives decrypt permissions for the selected delivery mode.

  ## Validation

  Validated successfully with:

  - terraform validate -no-color at repo root
  - terraform validate -no-color in byo-vpc
  - terraform validate -no-color in byo-vpc/byo-db
  - terraform validate -no-color in byo-vpc/byo-db/byo-ecs

Also verified that existing module-managed private-key resources plan as
non-destructive state-address moves when transitioning to the new
counted resources:

  - 0 to add
  - 0 to change
  - 0 to destroy
2026-04-06 19:25:17 -05:00
..

BYO ECS Module

This module deploys Fleet into an existing ECS/Fargate environment and supports optional customer-managed KMS keys (CMKs) for:

  • Fleet private key secret in Secrets Manager
  • Fleet application CloudWatch Logs log group encryption
  • Fleet software installers S3 bucket encryption

When no KMS inputs are provided, behavior remains unchanged from prior versions.

KMS Behavior

  • 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:

  • 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.

IAM permissions for using these keys are managed in-module where possible. For module-created keys, the module prefers granting usage in the KMS key policy itself. If a caller-provided external key still needs IAM authorization and the task role ARN belongs to the same AWS account, the module attaches a separate resource-scoped KMS IAM policy to that role instead of mixing KMS access into the broader S3 or secret-access policy. extra_iam_policies attachments continue to be applied to the same-account task role when provided.

Example: Use Module-Managed Keys

module "fleet_byo_ecs" {
  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"

  fleet_config = {
    database = {
      password_secret_arn = "arn:aws:secretsmanager:us-east-1:123456789012:secret:fleet-db"
      user                = "fleet"
      database            = "fleet"
      address             = "fleet.cluster-xyz.us-east-1.rds.amazonaws.com"
    }
    redis = {
      address = "fleet-redis.example.cache.amazonaws.com:6379"
    }
    loadbalancer = {
      arn = "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/fleet/abc123"
    }
    networking = {
      subnets = ["subnet-aaa", "subnet-bbb"]
      ingress_sources = {
        cidr_blocks      = []
        ipv6_cidr_blocks = []
        security_groups  = []
        prefix_list_ids  = []
      }
    }
    private_key_secret_kms = {
      cmk_enabled = true
    }
    awslogs = {
      create = true
      name   = "/aws/ecs/fleet"
      kms = {
        cmk_enabled = true
      }
    }
    software_installers = {
      create_bucket  = true
      create_kms_key = true
    }
  }
}

Example: Use Existing CMKs

fleet_config = {
  private_key_secret_kms = {
    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 = {
      cmk_enabled = true
      kms_key_arn = "arn:aws:kms:us-east-1:123456789012:key/99999999-aaaa-bbbb-cccc-dddddddddddd"
    }
  }
  software_installers = {
    create_bucket = true
    kms_key_arn   = "arn:aws:kms:us-east-1:123456789012:key/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
  }
}

Command Configuration

Optionally specify a custom command for the Fleet container:

fleet_config = {
  # ... other config ...
  command = ["/usr/bin/fleet", "agent", "--some-flag"]
}

Private Key Delivery Method

Control how the Fleet server private key is delivered to the container:

  • ecs (default): The secret value is injected as FLEET_SERVER_PRIVATE_KEY secret. The ECS Execution Role needs Secrets Manager permissions to decrypt the secret.
  • iam: The secret ARN is injected as FLEET_SERVER_PRIVATE_KEY_ARN environment variable. The Fleet task role needs Secrets Manager permissions to decrypt the secret.
fleet_config = {
  # ... other config ...
  private_key_delivery_method = "iam"  # or "ecs"
}

When private_key_delivery_method = "iam", the KMS key policy for the private key secret grants decrypt permissions to the Fleet task role instead of the ECS execution role.

Caller-Provided Private Key Secret

By default, this module creates and populates the Fleet server private key secret in Secrets Manager.

To use a caller-managed existing secret instead, set fleet_config.private_key_secret_arn. When this is set:

  • This module does not create the Fleet server private key secret, secret version, or generated random value.
  • fleet_config.private_key_secret_name is ignored.
  • The provided ARN is used in both delivery modes:
    • ecs: injected as FLEET_SERVER_PRIVATE_KEY
    • iam: injected as FLEET_SERVER_PRIVATE_KEY_ARN
  • The output fleet_server_private_key_secret_arn returns the effective ARN, whether the secret is module-managed or caller-managed.
  • Secret value population and lifecycle are entirely caller-managed.

If the caller-provided secret uses a CMK, the caller must also provide fleet_config.private_key_secret_kms.kms_key_arn so the correct ECS execution role or Fleet task role gets decrypt permissions for the selected delivery mode. This module does not inspect external secrets to discover their KMS configuration.

fleet_config = {
  private_key_secret_arn      = "arn:aws:secretsmanager:us-east-1:123456789012:secret:existing-fleet-private-key"
  private_key_delivery_method = "iam"

  private_key_secret_kms = {
    cmk_enabled = true
    kms_key_arn = "arn:aws:kms:us-east-1:123456789012:key/11111111-2222-3333-4444-555555555555"
  }
}

Migration Notes

  • Existing deployments are unchanged unless new KMS options are enabled.
  • Supplying fleet_config.private_key_secret_arn switches the module to use an existing caller-managed secret instead of creating one.
  • 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://<software-installers-bucket-name> s3://<software-installers-bucket-name> --recursive
  • Enabling KMS for the private key secret updates the secret to use the configured CMK.
  • If you enable KMS on an existing CloudWatch log group, older data remains encrypted with the previous keying context. To ensure only the new key is used, delete log streams whose last event predates the AssociateKmsKey event for the current kmsKeyId.

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.

# Dry run: list old streams only
DELETE_OLD_STREAMS=false ../../../scripts/cloudwatch_logs_kms_migration.sh <log-group-name> <region>

# Delete old streams (default behavior)
../../../scripts/cloudwatch_logs_kms_migration.sh <log-group-name> <region>

Requirements

Name Version
terraform >= 1.12.0
aws >= 6.37.0

Providers

Name Version
aws 6.38.0
random 3.8.1

Modules

No modules.

Resources

Name Type
aws_appautoscaling_policy.ecs_policy_cpu resource
aws_appautoscaling_policy.ecs_policy_memory resource
aws_appautoscaling_target.ecs_target resource
aws_cloudwatch_log_group.main resource
aws_ecs_service.fleet resource
aws_ecs_task_definition.backend resource
aws_iam_policy.execution resource
aws_iam_policy.main resource
aws_iam_policy.software_installers resource
aws_iam_role.execution resource
aws_iam_role.main resource
aws_iam_role_policy_attachment.execution resource
aws_iam_role_policy_attachment.execution_extras resource
aws_iam_role_policy_attachment.extras resource
aws_iam_role_policy_attachment.main resource
aws_iam_role_policy_attachment.role_attachment resource
aws_iam_role_policy_attachment.software_installers resource
aws_kms_alias.application_logs resource
aws_kms_alias.private_key_secret resource
aws_kms_alias.software_installers resource
aws_kms_key.application_logs resource
aws_kms_key.private_key_secret resource
aws_kms_key.software_installers resource
aws_s3_bucket.software_installers resource
aws_s3_bucket_lifecycle_configuration.software_installers resource
aws_s3_bucket_public_access_block.software_installers resource
aws_s3_bucket_server_side_encryption_configuration.software_installers resource
aws_s3_bucket_versioning.software_installers resource
aws_secretsmanager_secret.fleet_server_private_key resource
aws_secretsmanager_secret_version.fleet_server_private_key resource
aws_security_group.main resource
random_password.fleet_server_private_key resource
aws_caller_identity.current data source
aws_iam_policy_document.application_logs_kms data source
aws_iam_policy_document.assume_role data source
aws_iam_policy_document.fleet data source
aws_iam_policy_document.fleet-execution data source
aws_iam_policy_document.private_key_secret_kms data source
aws_iam_policy_document.software_installers data source
aws_iam_policy_document.software_installers_kms data source
aws_kms_key.software_installers_provided data source
aws_partition.current data source
aws_region.current data source

Inputs

Name Description Type Default Required
ecs_cluster The name of the ECS cluster to use string n/a yes
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)
command = optional(list(string), null)
private_key_delivery_method = optional(string, "ecs")
image = optional(string, "fleetdm/fleet:v4.83.0")
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_arn = optional(string, null)
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
},
"command": null,
"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.83.0",
"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_delivery_method": "ecs",
"private_key_secret_arn": 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 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 The configuration object for Fleet's migration task.
object({
mem = number
cpu = number
})
{
"cpu": 1024,
"mem": 2048
}
no
vpc_id n/a string null no

Outputs

Name Description
appautoscaling_target n/a
execution_iam_role_arn n/a
fleet_application_logs_kms_key_arn n/a
fleet_config n/a
fleet_s3_software_installers_config n/a
fleet_server_private_key_secret_arn n/a
fleet_server_private_key_secret_kms_key_arn n/a
iam_role_arn n/a
logging_config n/a
non_circular n/a
service n/a
task_definition n/a