尝试为AWS KMS编写Terraform代码

时间:2018-03-29 11:42:33

标签: amazon-web-services amazon-iam terraform aws-kms

  

目标:

     
    

我正在尝试创建以下东西A terraform模板     创建KMS密钥此模板应创建密钥和两个IAM角色。     这些角色将是一个用于管理功能和一个允许的角色     加密/解密我写了以下代码

  

我是否正确地实现了我的目标?

provider "aws"
{
access_key = "*****************"
secret_key = "4ZJaLh***********"
region     = "us-east-1"
}

resource "aws_kms_key" "test_key" {
  description             = "KMS Test key"
}

resource "aws_kms_alias" "alias" {
  name          = "alias/test_key"
  target_key_id = "${aws_kms_key.test_key.key_id}"
}

#IAM Role and Policy

resource "aws_iam_policy" "kms_user_policy" {
    name = "KMS-User-Policy"
    policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "kms:Decrypt",
                "kms:Encrypt",
                "kms:GenerateDataKey",
                "kms:ReEncryptTo",
                "kms:DescribeKey",
                "kms:ReEncryptFrom"
            ],
            "Resource": "*"
        }
    ]
}
EOF
}


resource "aws_iam_role" "kms_user_role" {
  name = "kms_user_role"
  path = "/"

  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": "sts:AssumeRole",
            "Principal": {
               "Service": "ec2.amazonaws.com"
            },
            "Effect": "Allow",
            "Sid": ""
        }
    ]
}
EOF
}

resource "aws_iam_policy_attachment" "test-attach" {
  name       = "test-attachment"
  roles      = ["${aws_iam_role.kms_user_role.name}"]
  policy_arn = "${aws_iam_policy.kms_user_policy.arn}"
}

0 个答案:

没有答案