2

每当我第一次运行以下 terraform 文件时,我都会收到错误消息:

创建 IAM 角色 SecurityMonkey 时出错:MalformedPolicyDocument:策略中的委托人无效:“AWS”。

但是,当我第二次执行代码时,成功创建了承担角色对象。对我来说,角色 A 和角色 B 之间的依赖关系似乎存在一些问题。作为一种补救措施,我什至在角色 A 上放置了一个 depends_on 语句,但没有运气。

SecurityMonkeyInstanceProfile

在这里你可以查看我的 TF 代码。

resource "aws_iam_role" "SecurityMonkey" {
name               = "SecurityMonkey"
depends_on = ["aws_iam_role.SecurityMonkeyInstanceProfile"]
path               = "/"
assume_role_policy = <<POLICY
{
"Version": "2008-10-17",
"Statement": [
{
  "Sid": "",
  "Effect": "Allow",
  "Principal": {
    "AWS": "arn:aws:iam::<AccountID>:role/SecurityMonkeyInstanceProfile"
  },
  "Action": "sts:AssumeRole"
  }
  ]
}
POLICY
}


resource "aws_iam_role" "SecurityMonkeyInstanceProfile" {
name               = "SecurityMonkeyInstanceProfile"
path               = "/"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
  "Effect": "Allow",
  "Principal": {
    "Service": "ec2.amazonaws.com"
  },
  "Action": "sts:AssumeRole"
 }
 ]
 }
 POLICY
 }

第一次运行出错。

aws_iam_role.SecurityMonkey: Error creating IAM Role SecurityMonkey: MalformedPolicyDocument: Invalid principal in policy: "AWS":"arn:aws:iam::<AccountID>:role/SecurityMonkeyInstanceProfile"
    status code: 400, request id: 0810c923-28dd-11e6-af5d-47689d50861a

第二次运行没有错误。

terraform apply  -var-file=../../aws.tfvars
aws_iam_role.SecurityMonkeyInstanceProfile: Refreshing state... (ID: SecurityMonkeyInstanceProfile)
aws_iam_role.SecurityMonkey: Creating...
  arn:                "" => "<computed>"
  assume_role_policy: "" => "{\n  \"Version\": \"2008-10-17\",\n  \"Statement\": [\n    {\n      \"Sid\": \"\",\n      \"Effect\": \"Allow\",\n      \"Principal\": {\n        \"AWS\": \"arn:aws:iam::<AccountID>:role/SecurityMonkeyInstanceProfile\"\n      },\n      \"Action\": \"sts:AssumeRole\"\n    }\n  ]\n}\n"
  name:               "" => "SecurityMonkey"
  path:               "" => "/"
  unique_id:          "" => "<computed>"
aws_iam_role.SecurityMonkey: Creation complete

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

显然,资源是以正确的顺序创建的,但似乎存在某种超时,使得SecurityMonkeyInstanceProfile角色无法被SecurityMonkey角色发现。几乎是鸡和蛋的问题。

有什么提示吗?

4

1 回答 1

0

您运行的是哪个 terraform 版本?

如果已经是最新版本,那么我会猜测两个资源之间的时间间隔太短,API系统没有足够的时间SecurityMonkeyInstanceProfile在第二个资源创建跟进时报告要创建的新资源。

尝试添加睡眠功能,让我知道这是否可以解决您的问题。

resource "aws_iam_role" "SecurityMonkeyInstanceProfile" {
  ...
  provisioner "local-exec" {
        command = "sleep 10"
    }
}

参考错误报告:https ://github.com/hashicorp/terraform/issues/1885

于 2016-06-09T02:27:37.780 回答