New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_cloudformation_stack: template_body not interpolated? #5462

Closed
alsonkemp opened this Issue Mar 4, 2016 · 14 comments

Comments

Projects
None yet
6 participants
@alsonkemp

alsonkemp commented Mar 4, 2016

I'm on 0.6.12. I have the following resource:

resource "aws_cloudformation_stack" "app_cloudfront" { 
  name = "app_cloudfront" 
  template_body = "${template_file.web_app_cloudfront.rendered}" 
} 

I'm getting JSON errors, so I added a debug printf to terraform's normalizeJson to see how I had messed up the JSON. The printf yielded the following:

Results in: 2016/03/04 12:18:23 [DEBUG] terraform-provider-aws: 2016/03/04 12:18:23 [DEBUG] JSON STRING: ${template_file.web_app_cloudfront.rendered} 

So it appears that the template_body was not interpolated?

@phinze phinze added bug core labels Mar 4, 2016

@phinze

This comment has been minimized.

Member

phinze commented Mar 4, 2016

Hi @alsonkemp - sorry for the trouble here.

I just did a quick local test and was unable to reproduce the uninterpolated template_body on my first couple of tries. Here's the test config I'm working with:

resource "template_file" "networkstack" {
    template = <<STACK
{
  "Resources" : {
    "MyVPC": {
      "Type" : "AWS::EC2::VPC",
      "Properties" : {
        "CidrBlock" : "10.0.0.0/16",
        "Tags" : [
          {"Key": "Name", "Value": "Primary_CF_VPC"}
        ]
      }
    }
  },
  "Outputs" : {
    "DefaultSgId" : {
      "Description": "The ID of default security group",
      "Value" : { "Fn::GetAtt" : [ "MyVPC", "DefaultSecurityGroup" ]}
    },
    "VpcID" : {
      "Description": "The VPC ID",
      "Value" : { "Ref" : "MyVPC" }
    }
  }
}
STACK
}

resource "aws_cloudformation_stack" "network" {
  name          = "tf-networking-stack"
  template_body = "${template_file.networkstack.rendered}"
}

Do you see any differences between that test config and yours that might be key to reproducing this bug?

@alsonkemp

This comment has been minimized.

alsonkemp commented Mar 5, 2016

@phinze The thick plottens... I have the same error when running your sample. FWIW, I'm on Debian Testing (and fairly up-to-date). See below.

template_body: "" => "Error parsing JSON: invalid character '$' looking for beginning of value"

/tmp/terraform-test
alson@tortugita : 04:28 PM : $ cat test.tf 
# Configure the AWS Provider
provider "aws" {
    access_key = "access"
    secret_key = "secret"
    region = "us-east-1"
}


resource "template_file" "networkstack" {
    template = <<STACK
{
  "Resources" : {
    "MyVPC": {
      "Type" : "AWS::EC2::VPC",
      "Properties" : {
        "CidrBlock" : "10.0.0.0/16",
        "Tags" : [
          {"Key": "Name", "Value": "Primary_CF_VPC"}
        ]
      }
    }
  },
  "Outputs" : {
    "DefaultSgId" : {
      "Description": "The ID of default security group",
      "Value" : { "Fn::GetAtt" : [ "MyVPC", "DefaultSecurityGroup" ]}
    },
    "VpcID" : {
      "Description": "The VPC ID",
      "Value" : { "Ref" : "MyVPC" }
    }
  }
}
STACK
}

resource "aws_cloudformation_stack" "network" {
  name          = "tf-networking-stack"
  template_body = "${template_file.networkstack.rendered}"
}



/tmp/terraform-test
alson@tortugita : 04:28 PM : $ terraform plan
Refreshing Terraform state prior to plan...


The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ aws_cloudformation_stack.network
    name:          "" => "tf-networking-stack"
    outputs.#:     "" => "<computed>"
    parameters.#:  "" => "<computed>"
    policy_body:   "" => "<computed>"
    template_body: "" => "Error parsing JSON: invalid character '$' looking for beginning of value"

+ template_file.networkstack
    rendered: "" => "<computed>"
    template: "" => "{\n  \"Resources\" : {\n    \"MyVPC\": {\n      \"Type\" : \"AWS::EC2::VPC\",\n      \"Properties\" : {\n        \"CidrBlock\" : \"10.0.0.0/16\",\n        \"Tags\" : [\n          {\"Key\": \"Name\", \"Value\": \"Primary_CF_VPC\"}\n        ]\n      }\n    }\n  },\n  \"Outputs\" : {\n    \"DefaultSgId\" : {\n      \"Description\": \"The ID of default security group\",\n      \"Value\" : { \"Fn::GetAtt\" : [ \"MyVPC\", \"DefaultSecurityGroup\" ]}\n    },\n    \"VpcID\" : {\n      \"Description\": \"The VPC ID\",\n      \"Value\" : { \"Ref\" : \"MyVPC\" }\n    }\n  }\n}\n"


Plan: 2 to add, 0 to change, 0 to destroy.

/tmp/terraform-test
alson@tortugita : 04:28 PM : $ terraform version
Terraform v0.6.12

@alsonkemp

This comment has been minimized.

alsonkemp commented Mar 5, 2016

I'll try this in the Vagrant VM...

@alsonkemp

This comment has been minimized.

alsonkemp commented Mar 5, 2016

I had the same failure in the Vagrant VM.

vagrant@terraform:~$ cd /opt/gopath/src/github.com/hashicorp/terraform/
vagrant@terraform:/opt/gopath/src/github.com/hashicorp/terraform$ make quickdev
vagrant@terraform:/tmp/terraform-test$ vi test.tf
vagrant@terraform:/tmp/terraform-test$ terraform plan
Refreshing Terraform state prior to plan...


The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ aws_cloudformation_stack.network
    name:          "" => "tf-networking-stack"
    outputs.#:     "" => "<computed>"
    parameters.#:  "" => "<computed>"
    policy_body:   "" => "<computed>"
    template_body: "" => "Error parsing JSON: invalid character '$' looking for beginning of value"

+ template_file.networkstack
    rendered: "" => "<computed>"
    template: "" => "{\n  \"Resources\" : {\n    \"MyVPC\": {\n      \"Type\" : \"AWS::EC2::VPC\",\n      \"Properties\" : {\n        \"CidrBlock\" : \"10.0.0.0/16\",\n        \"Tags\" : [\n          {\"Key\": \"Name\", \"Value\": \"Primary_CF_VPC\"}\n        ]\n      }\n    }\n  },\n  \"Outputs\" : {\n    \"DefaultSgId\" : {\n      \"Description\": \"The ID of default security group\",\n      \"Value\" : { \"Fn::GetAtt\" : [ \"MyVPC\", \"DefaultSecurityGroup\" ]}\n    },\n    \"VpcID\" : {\n      \"Description\": \"The VPC ID\",\n      \"Value\" : { \"Ref\" : \"MyVPC\" }\n    }\n  }\n}\n"


Plan: 2 to add, 0 to change, 0 to destroy.
@radeksimko

This comment has been minimized.

Member

radeksimko commented Mar 6, 2016

I was able to reproduce this and I have a strong feeling (admittedly I can't confirm) it can be solved via #4961

It was reproduced with Terraform v0.6.12 and the latest master (Terraform v0.6.13-dev (38078fbfb893aab0bf7293e72c241f63acd4da44)), but I don't think the version really matters.

I reckon it's just race condition - if you're lucky (and ${template_file.networkstack.rendered} gets interpolated sooner) you don't run into it.

@alsonkemp

This comment has been minimized.

alsonkemp commented Mar 7, 2016

@radeksimko Might be a race condition, but, across 3 machines, I've hit it on 100% of runs. Do you know of any workarounds?

On a second note, why does aws_cloudformation_stack swallow the template error?

@alsonkemp

This comment has been minimized.

alsonkemp commented Mar 9, 2016

@phinze Can you provide any indications of where the bug might be? I'm not a golang-nut, but I might be able to help.

@phinze

This comment has been minimized.

Member

phinze commented Mar 25, 2016

Still no repro here - @alsonkemp can you confirm you hit this on 0.6.14 as well?

@alsonkemp

This comment has been minimized.

alsonkemp commented Mar 25, 2016

@phinze You were not able to get my repro from #5462 (comment) to work? I have not re-tried against 0.6.14. I will do so soon.

@alsonkemp

This comment has been minimized.

alsonkemp commented Mar 25, 2016

Still seeing the same error.

~/projects/terraform-test
alson@tortugita : 02:43 PM : $ cat test.tf 
# Configure the AWS Provider
provider "aws" {
    access_key = "###"
    secret_key = "###"
    region = "us-east-1"
}


resource "template_file" "networkstack" {
    template = <<STACK
{
  "Resources" : {
    "MyVPC": {
      "Type" : "AWS::EC2::VPC",
      "Properties" : {
        "CidrBlock" : "10.0.0.0/16",
        "Tags" : [
          {"Key": "Name", "Value": "Primary_CF_VPC"}
        ]
      }
    }
  },
  "Outputs" : {
    "DefaultSgId" : {
      "Description": "The ID of default security group",
      "Value" : { "Fn::GetAtt" : [ "MyVPC", "DefaultSecurityGroup" ]}
    },
    "VpcID" : {
      "Description": "The VPC ID",
      "Value" : { "Ref" : "MyVPC" }
    }
  }
}
STACK
}

resource "aws_cloudformation_stack" "network" {
  name          = "tf-networking-stack"
  template_body = "${template_file.networkstack.rendered}"
}


~/projects/terraform-test
alson@tortugita : 02:43 PM : $ terraform version
Terraform v0.6.14


~/projects/terraform-test
alson@tortugita : 02:43 PM : $ terraform plan
Refreshing Terraform state prior to plan...


The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ aws_cloudformation_stack.network
    name:          "" => "tf-networking-stack"
    outputs.#:     "" => "<computed>"
    parameters.#:  "" => "<computed>"
    policy_body:   "" => "<computed>"
    template_body: "" => "Error parsing JSON: invalid character '$' looking for beginning of value"

+ template_file.networkstack
    rendered: "" => "<computed>"
    template: "" => "{\n  \"Resources\" : {\n    \"MyVPC\": {\n      \"Type\" : \"AWS::EC2::VPC\",\n      \"Properties\" : {\n        \"CidrBlock\" : \"10.0.0.0/16\",\n        \"Tags\" : [\n          {\"Key\": \"Name\", \"Value\": \"Primary_CF_VPC\"}\n        ]\n      }\n    }\n  },\n  \"Outputs\" : {\n    \"DefaultSgId\" : {\n      \"Description\": \"The ID of default security group\",\n      \"Value\" : { \"Fn::GetAtt\" : [ \"MyVPC\", \"DefaultSecurityGroup\" ]}\n    },\n    \"VpcID\" : {\n      \"Description\": \"The VPC ID\",\n      \"Value\" : { \"Ref\" : \"MyVPC\" }\n    }\n  }\n}\n"


Plan: 2 to add, 0 to change, 0 to destroy.

template_body: "" => "Error parsing JSON: invalid character '$' looking for beginning of value"**

@radeksimko

This comment has been minimized.

Member

radeksimko commented Mar 25, 2016

See #3076 which I believe is caused by the same bug.

@comerford

This comment has been minimized.

comerford commented May 9, 2016

Can confirm that I am seeing this with 0.6.15 too - plan errors parsing the JSON but the apply succeeds and is using the correct policy

@mitchellh

This comment has been minimized.

Member

mitchellh commented Oct 28, 2016

This is fixed on master as I also can't reproduce the exact copy/paste example above (even using the deprecated template_file resource). Closing!

@mitchellh mitchellh closed this Oct 28, 2016

@puttagunta

This comment has been minimized.

puttagunta commented Mar 22, 2018

I found this issue today, when I'm trying to populate "template_body" of the resource, "azurerm_template_deployment", with the rendered content of a template. The plan failed with the message, "Error parsing JSON: invalid character '$' looking for beginning of value", but the apply succeded.
Terraform version: 0.11.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment