在使用 Terraform for AWS 时出现错误:“新的密钥策略将不允许您在未来更新密钥策略。”

Ale*_*lex 8 amazon-web-services terraform aws-kms

在 AWS KMS 中运行 terraform 以创建密钥策略我收到错误:

  • aws_kms_key.dyn_logs_server_side_cmk:MalformedPolicyDocumentException:新的密钥策略将不允许您在未来更新密钥策略。状态码:400,请求ID:e34567896780780

有很多关于这个问题的帖子,但没有任何帮助。所以,我的 kms.tf 文件如下:

provider "aws" {
    access_key = "${var.aws_access_key}"
    secret_key = "${var.aws_secret_key}"
    region     = "${var.aws_region}"
} 
resource "aws_kms_key" "dyn_logs_server_side_cmk" {
    description = "dyn-logs-sse-cmk-${var.environment}"
    enable_key_rotation = "true"
    policy = <<EOF
{
    "Version":"2015-11-17",
    "Statement":[
    {
        "Sid": "Enable IAM User Permissions",
        "Effect": "Allow",
        "Principal": {"AWS": "arn:aws:iam::${var.account_id}:root"},
        "Action": "kms:*",
        "Resource": "*"
    }
    ]
    }EOF
}
Run Code Online (Sandbox Code Playgroud)

这就是我在输出之后看到的

terraform 应用“dyn-vpc.plan”

aws_kms_key.dyn_logs_server_side_cmk: Creating...
arn:                 "" => "<computed>"
description:         "" => "dyn-logs-server-dyn"
enable_key_rotation: "" => "true"
is_enabled:          "" => "true"
key_id:              "" => "<computed>"
key_usage:           "" => "<computed>"
policy:              "" => "{\n   \"Version\":\"2015-11-17\",\n   \"Statement\":[\n      {\n         \"Sid\": \"Enable IAM User Permissions\",\n         \"Effect\": \"Allow\",\n         
\"Principal\": {\"AWS\": \"arn:aws:iam::12345678901234:root\"},\n         \"Action\": \"kms:*\",\n         \"Resource\": \"*\"\n      }\n   ]\n}\n"

aws_kms_key.dyn_logs_server_side_cmk: Still creating... (10s elapsed)
aws_kms_key.dyn_logs_server_side_cmk: Still creating... (20s elapsed)
Error applying plan:
1 error(s) occurred:
* aws_kms_key.dyn_logs_server_side_cmk: 1 error(s) occurred:
* aws_kms_key.dyn_logs_server_side_cmk:
MalformedPolicyDocumentException: The new key policy will not allow
you to update the key policy in the future.
Run Code Online (Sandbox Code Playgroud)

gar*_*y69 7

就我而言,帐户 ID 是正确的,但创建密钥的用户未包含在Enable IAM User Permissions声明中。我不得不这样做

resource "aws_kms_key" "dyn_logs_server_side_cmk" {
    description = "dyn-logs-sse-cmk-${var.environment}"
    enable_key_rotation = "true"
    policy = <<EOF
{
    "Version":"2015-11-17",
    "Statement":[
    {
        "Sid": "Enable IAM User Permissions",
        "Effect": "Allow",
        "Principal": {
            "AWS": [
                 "arn:aws:iam::${var.account_id}:root",
                 "arn:aws:iam::${var.account_id}:user/system/terraform-user" 
             ]
        },
        "Action": "kms:*",
        "Resource": "*"
    }
    ]
    }EOF
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*lex 2

基本上,@ydaetskcoR 的评论是正确的。策略中的 account_id 不正确,这导致了错误。MalformedPolicyDocumentException 并不能提供真正的信息,需要找到真正的原因