Skip to content

How to update an existing KMS key policy using Terraform?

0

Hi all

I'm trying to update (adding new policy statements) an existing KMS key policy using Terraform. I tried to follow the answers and comments in https://repost.aws/questions/QUFXg8npfcTySJauEPnpFslg/how-to-update-existing-s3-bucket-policy-using-terraform.

But, the terraform resource and data for aws_kms_key, aws_kms_key_policy do not support policy attribute (or existing policy read or update) - https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/kms_key. And use of "terraform import" requires more manual work than just adding statements to the existing policy from the console.

Any other better workarounds? Without using terraform import?

Thanks.

asked 2 years ago1.8K views

1 Answer
-2

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:

  1. 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
}
  1. If you need more control or if the above method doesn't work for your use case, you can use the aws_kms_key resource with the policy argument, 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
  1. 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

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

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.