- Newest
- Most votes
- Most comments
While Terraform doesn't directly support updating an existing KMS key policy through its resources, there are a few approaches you can consider to achieve this:
- Use a data source to fetch the existing policy, then use a local variable to modify it:
First, use the aws_kms_key data source to get information about the existing key:
data "aws_kms_key" "existing_key" { key_id = "your-key-id-or-arn" }
Then, use the aws_kms_key_policy data source to fetch the current policy:
data "aws_kms_key_policy" "existing_policy" { key_id = data.aws_kms_key.existing_key.id }
Next, create a local variable to modify the policy:
locals { updated_policy = jsonencode({ Version = "2012-10-17" Statement = concat( jsondecode(data.aws_kms_key_policy.existing_policy.policy).Statement, [ { Sid = "NewStatement" Effect = "Allow" Principal = { AWS = "arn:aws:iam::123456789012:user/NewUser" } Action = ["kms:Encrypt", "kms:Decrypt"] Resource = "*" } ] ) }) }
Finally, use the aws_kms_key_policy resource to apply the updated policy:
resource "aws_kms_key_policy" "updated_policy" { key_id = data.aws_kms_key.existing_key.id policy = local.updated_policy }
- If you need more control or if the above method doesn't work for your use case, you can use the
aws_kms_keyresource with thepolicyargument, but you'll need to import the existing key first:
resource "aws_kms_key" "existing_key" { description = "Existing KMS key" policy = jsonencode({ Version = "2012-10-17" Statement = [ # Your existing policy statements here { Sid = "NewStatement" Effect = "Allow" Principal = { AWS = "arn:aws:iam::123456789012:user/NewUser" } Action = ["kms:Encrypt", "kms:Decrypt"] Resource = "*" } ] }) }
Then import the existing key:
terraform import aws_kms_key.existing_key your-key-id-or-arn
- As a last resort, you could use a null_resource with local-exec to run AWS CLI commands to update the policy. This is not ideal from a Terraform perspective but can work if other methods fail:
resource "null_resource" "update_kms_policy" { provisioner "local-exec" { command = <<EOF aws kms get-key-policy --key-id ${your_key_id} --policy-name default --query Policy --output text > existing_policy.json # Use jq or another tool to modify existing_policy.json aws kms put-key-policy --key-id ${your_key_id} --policy-name default --policy file://updated_policy.json EOF } }
Remember that these approaches may have limitations and might not be suitable for all scenarios. Always test thoroughly in a non-production environment before applying changes to your KMS key policies.
Sources
Use GetKeyPolicy with an AWS SDK or CLI - AWS Key Management Service
Use PutKeyPolicy with an AWS SDK or CLI - AWS Key Management Service
Use PutKeyPolicy with an AWS SDK or CLI - AWS SDK Code Examples
Use GetKeyPolicy with an AWS SDK or CLI - AWS SDK Code Examples
answered 2 years ago
Relevant content
asked 2 years ago
asked 2 years ago
asked 4 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 10 months ago

The data block on aws_kms_key_policy is not available so the above solution is not correct.
Bhanu T is correct. This doesn't work:
Error: Invalid data source │ │ │ The provider hashicorp/aws does not support data source │ "aws_kms_key_policy".