1
votes

I am new to terraform using vs code and the "Terraform" extension from Mikael Olenfalk.

I learned using string interpolation where I can ctrl+space (intellisense) and use resource references. However I am unsure if this is always required (to reference resources in my *.tf file)? If I dont need to interpolate but only reference a resource, using the "string-interpolation-way" is really noisy.

Lets say I have a azurerm_storage_account resource of the name static-site. I can do the following and vs code says all is okay.

name = "${azurerm_storage_account.static-site.name}"

or I can do this

name = azurerm_storage_account.static-site.name

and I am getting an error unexpected token while parsing list: IDENT

By contrast if I look around on the official docs site, there is clearly some cases where quotes are not used, e.g. see the example in this section 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,
  ]
}

Which would give errors for me. Or is this especially required for azure resources (which I use)? The example above was with aws.

Cheers

1
VSCode is still not HCL2/Terraform 0.12 compatible last time I checked. Atom is (TF 0.12; unsure HCL2) because I wrote it, but no reason to switch because I am sure it will come to VScode eventually.Matt Schuchard
I found the section in the docs clearly stating this is a tf 0.12 feature: terraform.io/upgrade-guides/0-12.html#first-class-expressionsbaouss
@baouss the plugin does not support terraform 12, the issue with not the terraform, its wtih pluginAdiii
And here is the exact Github issue for this github.com/mauve/vscode-terraform/issues/180baouss

1 Answers

1
votes

I believe the issue with JSON encode Function, not with interpolation syantx. To overcome this error you can heredoc syantx. A multi-line string value can be provided using heredoc syntax.

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
}

To verify the issue just comment out role = "${aws_iam_role.example.name}".

enter image description here

While the issue disappears using heredoc syntax. enter image description here

The reason behind this error is that "Terraform 0.12 support is still not available" by the plugin provider. enter image description here