在AWS中使用Terraform创建ROLE并附加SAML提供程序

时间:2018-01-25 16:35:05

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

我目前正在尝试自动化AWS账户配置,其中一个步骤是使用Identity Provider创建IAM ROLE(用于联合用户访问)。 我搜索并检查了Terraform文档,但找不到有关创建此类角色或将提供程序附加到角色的任何信息。 我可以创造两个都很好,但他们是独立的。 这是代码的一部分:

resource "aws_iam_saml_provider" "default" {
  name                   = "ADFS-TEST"
  saml_metadata_document = "${file("../../FederationMetadata.xml")}"
}

resource "aws_iam_role" "role" {
    name = "test-Admins"
}

2 个答案:

答案 0 :(得分:2)

想通了。这是完整的块

resource "aws_iam_saml_provider" "test" {
  name                   = "ADFS-TEST"
  saml_metadata_document = "${file("../../FederationMetadata.xml")}"
}

resource "aws_iam_role" "role" {
    name = "ADFStest-Admins"
    assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Federated": "${aws_iam_saml_provider.test.arn}"
      },
      "Action": "sts:AssumeRoleWithSAML",
      "Condition": {
        "StringEquals": {
          "SAML:aud": "https://signin.aws.amazon.com/saml"
        }
      }
    }
  ]
}
EOF

}

resource "aws_iam_role_policy" "admins" {
    name        = "Admin-Policy"
    #description = "A test policy"
    role = "${aws_iam_role.role.id}"
    policy = <<EOF
{
  "Version": "2012-10-17",
    "Statement": [
      {
        "Effect": "Allow",
        "Action": "*",
        "Resource": "*"
      }
  ]
}
EOF
}

答案 1 :(得分:0)

谢谢!它对我有用。

我只是将aws_iam_role_policy更改为使用aws_iam_role_policy_attachment:

resource "aws_iam_role_policy_attachment" "attach" {
    role = "${aws_iam_role.role.name}"
    policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
}