运行terraform plan
时出现此错误:
错误:加载/Users/antkong/infrastructure/windows-server/main.tf时出错:读取时出错 aws_instance [worker]的配置:aws_iam_role.windows_server_role: 资源变量必须分为三个部分:TYPE.NAME.ATTR,位于:
这是相关代码
resource "aws_instance" "worker" {
ami = "${data.aws_ami.windows_server.image_id}"
instance_type = "${var.instance}"
iam_instance_profile = "${aws_iam_role.windows_server_role}" // <- ERROR here
get_password_data = "true"
我认为可以修复它,我可以使用
iam_instance_profile = "${aws_iam_role.windows_server_role.id}"
或
iam_instance_profile = "${aws_iam_role.windows_server_role.arn}"
但是在文档中并不明显。
除了在运行时遇到错误外,如何找到它?
答案 0 :(得分:1)
在terraform文档中达成一致可能会更好。我确实发现您需要引用aws_iam_instance_profile
而不是aws_iam_role
。这是doc-https://www.terraform.io/docs/providers/aws/r/iam_instance_profile.html的链接。
代码看起来像
resource "aws_instance" "worker" {
ami = "${data.aws_ami.windows_server.image_id}"
instance_type = "${var.instance}"
iam_instance_profile = "${aws_iam_instance_profile.windows_server.name}"
get_password_data = "true"
....
}
从上面共享的链接中复制实例配置文件def:
resource "aws_iam_instance_profile" "windows_server" {
name = "windows_server_instance_profile"
role = "${aws_iam_role.windows_server.name}"
}
resource "aws_iam_role" "windows_server" {
name = "windows_server_role"
path = "/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}