Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

Terraform: Interpolation could be replaced by HCL2 expression

When I try to use interpolation syntax like this:

vpc_id     = "${aws_vpc.prod-vpc.id}"

I get the suggestion in IntelliJ that "Interpolation could be replaced by HCL2 expression", so if I change the line into this:

 vpc_id     = "aws_vpc.prod-vpc.id"

and issue terraform apply, I get:

C:f_ptojectsawssubnet>terraform apply -auto-approve
aws_subnet.prod-subnet: Creating...
aws_vpc.prod-vpc: Creating...
aws_vpc.prod-vpc: Creation complete after 2s [id=vpc-0cfb27255522bdf15]

Error: error creating subnet: InvalidVpcID.NotFound: The vpc ID 'aws_vpc.prod-vpc.id' does not exist
        status code: 400, request id: dab3fb03-424d-4bf2-ace6-bef93a94ee9c

If I re-apply interpolation syntax and run terraform apply again, then the resources get deployed but I get the warning in Terraform, saying that interpolation-only expressions are deprecated:

 Warning: Interpolation-only expressions are deprecated

  on main.tf line 16, in resource "aws_subnet" "prod-subnet":
  16:   vpc_id     = "${aws_vpc.prod-vpc.id}"

So TF is discouraging the use of interpolation syntax, yet issues an error if it's not used. Is it some kind of bug or something?

C:f_ptojectsawssubnet>terraform -version
Terraform v0.14.4
+ provider registry.terraform.io/hashicorp/aws v3.25.0

Entire TF code for reference:

provider "aws" {
  region = "eu-central-1"
}


resource "aws_vpc" "prod-vpc" {
  cidr_block = "10.100.0.0/16"

  tags = {
    name = "production vpc"
  }
}

resource "aws_subnet" "prod-subnet" {
  cidr_block = "10.100.1.0/24"
  vpc_id     = "aws_vpc.prod-vpc.id"

  tags = {
    name = "prod-subnet"
  }
}
question from:https://stackoverflow.com/questions/65871914/terraform-interpolation-could-be-replaced-by-hcl2-expression

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You just have to get the id without using double quotes vpc_id = aws_vpc.prod-vpc.id because you are getting vpc id from the resource. If you use the double quotes, it will be considered as a string and no evaluation will be done, and terraform will consider "aws_vpc.prod-vpc.id" as the id.

This is the corrected code:

provider "aws" {
  region = "eu-central-1"
}


resource "aws_vpc" "prod-vpc" {
  cidr_block = "10.100.0.0/16"

  tags = {
    name = "production vpc"
  }
}

resource "aws_subnet" "prod-subnet" {
  cidr_block = "10.100.1.0/24"
  vpc_id     = aws_vpc.prod-vpc.id

  tags = {
    name = "prod-subnet"
  }
}

I had tested the above code snippet and it is working perfectly fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...