Skip to content
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 · 15 comments
Closed

aws_cloudformation_stack: template_body not interpolated? #5462

alsonkemp opened this issue Mar 4, 2016 · 15 comments

Comments

@alsonkemp
Copy link

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
Copy link
Contributor

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
Copy link
Author

@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
Copy link
Author

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

@alsonkemp
Copy link
Author

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
Copy link
Member

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
Copy link
Author

@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
Copy link
Author

@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
Copy link
Contributor

phinze commented Mar 25, 2016

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

@alsonkemp
Copy link
Author

@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
Copy link
Author

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
Copy link
Member

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

@comerford
Copy link

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
Copy link
Contributor

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!

@puttagunta
Copy link

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

@ghost
Copy link

ghost commented Apr 4, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants