This Terraform project provisions a customer managed AWS KMS key in every region you specify and configures the key policy so the CloudWatch Logs service in that same region can encrypt log groups. Each key receives its own alias, optional rotation, and granular policy statements limiting usage to CloudWatch Logs encryption contexts.
versions.tf– Terraform and provider constraintsproviders.tf– Default AWS provider plus one alias per account/region you plan to managevariables.tf– Inputs such as the account/region matrix and admin principalsmain.tf– Iterates over every account/region pair and instantiates the reusablekms-keymodulemodules/kms-key– Creates the CMK, alias, and CloudWatch-friendly policyterraform.tfvars.example– Example configuration you can copy toterraform.tfvars
- Terraform v1.6+ and AWS provider v5+
- AWS credentials capable of creating and managing KMS keys in each targeted region
- (Optional) An IAM principal list that should have admin access to every key
-
Copy
terraform.tfvars.exampletoterraform.tfvarsand adjust theaccount_region_configs,alias_prefix, andkey_admin_arnsvalues as needed. -
Make sure
providers.tfcontains oneprovider "aws" { alias = "..." }block for everyprovider_aliaslisted under each account'sregions. Each alias can configure a different AWS account (for example, by specifying distinct credentials or assume-role settings) and must target the region you pair it with. After adding a new alias, add an entry tolocal.provider_alias_mapinmain.tfso Terraform knows which provider instance to hand to each module (see the defaultuse1,use2, andusw2aliases for reference). -
Initialize Terraform:
terraform init
-
Review the planned changes:
terraform plan
-
Apply when satisfied:
terraform apply
- Add a new
provider "aws" { alias = "<alias>" ... }block toproviders.tf. Point it at the desired account/credentials and region. - Insert or update an entry in
account_region_configsso the account label references that provider alias under the appropriate region entry. - The root module automatically instantiates the
kms-keymodule for every account/region pair that you declare.
Out of the box, the configuration targets AWS account 111111111111111 in us-east-1, us-east-2, and us-west-2. If you supply your own account_region_configs, those defaults are replaced entirely.
kms_keys exposes a nested map keyed first by account label, then by region. Each entry contains the key ARN, key ID, and alias ARN so you can wire the CMKs into CloudWatch Log Group encryption settings or other services.