我对使用vs代码和Mikael Olenfalk的“ Terraform”扩展名是Terraform还是陌生的。
我学习了使用字符串插值的方法,其中可以
可以说我有一个名称为static-site的azurerm_storage_account资源。我可以执行以下操作,并且vs代码表明一切正常。
name = "${azurerm_storage_account.static-site.name}"
或者我可以做到
name = azurerm_storage_account.static-site.name
我收到一个错误unexpected token while parsing list: IDENT
相比之下,如果我在官方docs网站上四处看看,显然有些情况下不使用引号,例如请参阅本节https://www.terraform.io/docs/configuration/resources.html#depends_on-explicit-resource-dependencies
中的示例
resource "aws_iam_role_policy" "example" {
name = "example"
role = aws_iam_role.example.name
policy = jsonencode({
"Statement" = [{
# This policy allows software running on the EC2 instance to
# access the S3 API.
"Action" = "s3:*",
"Effect" = "Allow",
}],
})
}
resource "aws_instance" "example" {
ami = "ami-a1b2c3d4"
instance_type = "t2.micro"
iam_instance_profile = aws_iam_instance_profile.example <--------------- !!!
# However, if software running in this EC2 instance needs access
# to the S3 API in order to boot properly, there is also a "hidden"
# dependency on the aws_iam_role_policy that Terraform cannot
# automatically infer, so it must be declared explicitly:
depends_on = [
aws_iam_role_policy.example,
]
}
谁会给我错误。还是对天蓝色资源(我使用的资源)特别要求?上面的示例是aws。
欢呼
答案 0 :(得分:1)
我相信JSON encode Function
的问题,而不是插值syantx的问题。要克服此错误,您可以heredoc syantx
。可以使用heredoc语法提供多行字符串值。
resource "aws_iam_role_policy" "example" {
name = "example"
role = "${aws_iam_role.example.name}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement" = [
{
"Sid": "VisualEditor5",
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
}
]
}
EOF
}
要验证该问题,只需将role = "${aws_iam_role.example.name}"
注释掉。