如果您附加了帐户中已存在的策略,我将使用数据源进行查询。您必须知道ARN才能使用IAM策略数据源,因此它与在ARN中直接指定ARN没有太大区别。 aws_iam_role_policy_attachment 除了它允许的资源 terraform plan 命令在运行之前验证策略是否存在 apply ,这是对你的额外保障。数据源也为您提供 更多信息 关于你需要它的资源。
aws_iam_role_policy_attachment
terraform plan
apply
data "aws_iam_policy" "security_audit" { arn = "arn:aws:iam::${var.target_account_id}:policy/SecurityAudit" } # BEGIN 'foo' resource "aws_iam_role" "foo" { name = "${terraform.workspace}_Foo" path = "/" assume_role_policy = <<EOF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "automation.amazonaws.com", "events.amazonaws.com" ] }, "Action": "sts:AssumeRole" }, { "Effect": "Allow", "Principal": { "AWS": [ "arn:aws:iam::${var.other_aws_account_id}:role/your_role_name_and_path_here" ] }, "Action": "sts:AssumeRole" } ] } EOF } resource "aws_iam_role_policy_attachment" "foo" { policy_arn = "${data.aws_iam_policy.security_audit.arn}" role = "${aws_iam_role.foo.name}" }
`# BEGIN 'Foo' resource "aws_iam_role" "foo" { name = "${terraform.workspace}_Foo" path = "/" assume_role_policy = <<EOF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::INSERT_ACCOUNT_NUMBER:root" }, "Action": "sts:AssumeRole", "Condition": { "StringEquals": { "sts:ExternalId": "INSERT_EXTERNAL_ID" } } } ] } EOF } resource "aws_iam_role_policy_attachment" "foo" { policy_arn = "arn:aws:iam::aws:policy/SecurityAudit" role = "${aws_iam_role.foo.name}" } resource "aws_iam_instance_profile" "foo" { name = "${terraform.workspace}_Foo" role = "${aws_iam_role.foo.name}" } # END `