brikis98 / terraform-up-and-running-code

Code samples for the book "Terraform: Up & Running" by Yevgeniy Brikman
http://www.terraformupandrunning.com/
MIT License
2.92k stars 1.93k forks source link

Parsing error: heredoc not terminated #11

Closed syl20bnr closed 7 years ago

syl20bnr commented 7 years ago

Hi Yevgeniy,

Thank you for the great book.

While trying to run the Single Web Server example from chapter 2, I've got a parsing error regarding heredoc <<-EOF ... EOF with terraform version 0.9.3:

Failed to load root config module: Error parsing ./TerraformBook/chapter2/single_server/main.tf: At 30:1: heredoc not terminated

I have to delete the hyphen in <<-EOF to make it pass which seems to be confirmed by the documentation of HCL: https://github.com/hashicorp/hcl#syntax

I'm on macOS.

brikis98 commented 7 years ago

Which example are you actually running? Could you post the full code? There is no chapter2/single_server/main.tf in this repo.

Also, the examples in this repo have not yet been updated to work with Terraform 0.9, which is enforced via the required_version setting. Even if I remove that check from code/terraform/02-intro-to-terraform-syntax/one-webserver/main.tf, the plan command works just fine with Terraform 0.9.3. Is it possible you have a copy/paste error?

syl20bnr commented 7 years ago

I type the code myself without relying on the code in this repo so sorry if the issue is not about the provided code itself.

This issue is related to #12 where you can find the code I use. The reason why there is no hyphen in #12 is because of this parsing error.

The parsing disappear if the closing statement is -EOF instead of EOF. I'll try it as soon as possible when I have a computer available.

brikis98 commented 7 years ago

Sounds good. Keep me posted.

syl20bnr commented 7 years ago

I tried with the following code:

# Single Server Deployment

provider "aws" {
  region = "us-east-1"
}

resource "aws_security_group" "webserver" {
  name = "terraform-example-webserver"
  ingress {
    from_port = 8080
    to_port = 8080
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "example" {
  tags {
    Name = "terraform-example"
  }
  ami = "ami-f4cc1de2"
  instance_type = "t2.micro"
  vpc_security_group_ids = ["${aws_security_group.webserver.id}"]
  user_data = <<-EOF
    #!/bin/bash
    echo "Hello, Terraform!" > index.html
    nohup busybox httpd -f -p 8080 &
    -EOF
}

It is parsed just fine by terraform but the web server does not kick in.

That's weird, I just downloaded the binary for macOS from the Terraform website which gave me the version 0.9.3.

I looked into the doc and I could only find this url https://www.terraform.io/docs/configuration/syntax.html where it is mentioned:

Multiline strings can use shell-style "here doc" syntax, with the string starting with a marker like <<EOF and then the string ending with EOF on a line of its own. The lines of the string and the end marker must not be indented.

brikis98 commented 7 years ago

The syntax is <<-EOF...EOF as shown in this example.

syl20bnr commented 7 years ago

There are something really weird with what happened. I was able to reproduce the parsing error 100% of the time then I copy pasted the example from the repo, no parsing error! Then I undo the copy paste to go back to the version with parsing error and....... impossible to reproduce it, it just works. I'm really puzzled, my best bet was a whitespace between <<-and EOF and when I fixed it I may have not saved the file so I thought it was broken, but still.... I tried several time.

Anyway let's close the issue, sorry for losing your time.

As I was not able to find it in the official documentation, it could be a good thing to add a mention of it for an update of the book. In the meantime I submitted a PR to add documentation for it: https://github.com/hashicorp/hcl/pull/195

brikis98 commented 7 years ago

Yea, the heredoc stuff is whitespace sensitive, so if some tab or space got caught in the mix, it might break things in a way that's hard to see.

Good call on the HCL PR!

Joshua-Rowe commented 7 years ago

I had the exact same issue where I got to the Deploy a Single Web Server example on page 39. After typing the updated program, I ran terraform graph to look at the dependencies and got the error: main.tf: At 29:1: heredoc not terminated. An inital search brought me here, but I was unable to resolve the problem with the information listed in this issue. However, a few more searches and I came across this issue in the Terraform repo: hashicorp/terraform/issues/4835 where phinze says: the consistent behavior I found was that the heredoc terminates only when a line contains the terminator with no trailing space.. After removing a trailing space after the second EOF, I did not have any more issues.

fr34k8 commented 6 years ago

this was also my error.. thanks @Joshua-Rowe

slysenko1 commented 5 years ago

trailing spaces it is - thank you

advissor commented 5 years ago

Indeed, the issue was caused by a trailing space after EOD

EOF EOD

After removing trailing space, the issue was solved