gruntwork-io / intro-to-terraform

Sample code for the blog post series "A Comprehensive Guide to Terraform."
https://blog.gruntwork.io/a-comprehensive-guide-to-terraform-b3d32832baca
Other
729 stars 530 forks source link

Syntax error in blog post? #9

Closed IshwarBhat closed 4 years ago

IshwarBhat commented 4 years ago

I was experimenting with workspaces in https://blog.gruntwork.io/how-to-manage-terraform-state-28f5697e68fa (great set of blog posts btw, thanks!) and tried this piece of code:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = 
    terraform.workspace == "default" 
    ? "t2.medium" 
    : "t2.micro"
}

which gave me an error:

Error: Argument or block definition required

on main.tf line 29, in resource "aws_instance" "example": 29: terraform.workspace == "default"

An argument or block definition is required here. To set an argument, use the equals sign "=" to introduce the argument value.

Modifying the above code to this fixed my issue:

resource "aws_instance" "example" {
    ami           = "ami-0c55b159cbfafe1f0"
    instance_type = "${
        terraform.workspace == "default"
        ? "t2.medium"
        : "t2.micro"
    }"
}
brikis98 commented 4 years ago

Thanks for the heads up! I wrapped that snippet so that it fit better into the blog post, but putting a new line after the equals sign turns out not to be valid HCL2 syntax. I just fixed it in the blog post as follows:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = (
    terraform.workspace == "default" 
    ? "t2.medium" 
    : "t2.micro"
  )
}