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

Template Files as User Data don't seem to render the same from plan to apply/from one apply to another #1998

Closed
mlrobinson opened this issue May 17, 2015 · 3 comments
Labels
bug provider/template waiting-response An issue/pull request is waiting for a response from the community

Comments

@mlrobinson
Copy link

I was testing using the new template resource with creating a user-data script for AWS, when I noticed that even when I don't change the template/vars from one run to the next, Terraform still wants to reprovision any instances I defined because the user_data hash/checksum appears to be different.

Here is my example.tf:

provider "aws" {
    access_key = "ACCESS_KEY"
    secret_key = "SECRET_KEY"
    region = "us-east-1"
}

variable "key" {
    default = "hello"
}

variable "filename" {
    default = "template.txt"
}

resource "template_file" "sample" {
    filename = "${var.filename}"
    vars {
        hello = "${var.key}"
        world = "${replace(var.key, "ello", "i")}"
    }
}

# AMI is for Debian 7.8, latest from Debian Wiki
resource "aws_instance" "example" {
    ami = "ami-e0efab88"
    instance_type = "t2.micro"
    user_data = "${template_file.sample.rendered}"
}

output "rendered" {
    value = "${template_file.sample.rendered}"
}

and my template.txt:

Hello: ${hello}
World: ${world}
Addition: ${1+2+3}

Here is the results of running plan, apply, and then another plan:

$ terraform plan
var.filename
  Default: template.txt
  Enter a value: 

var.key
  Default: hello
  Enter a value: 

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_instance.example
    ami:                      "" => "ami-e0efab88"
...
    user_data:                "" => "894115c5b5247d5425ec8090e4dd6ba9f15d9d49"

+ template_file.sample
    filename:   "" => "template.txt"
    rendered:   "" => "<computed>"
    vars.#:     "0" => "2"
    vars.hello: "" => "hello"
    vars.world: "" => "hi"


$ terraform apply
var.filename
  Default: template.txt
  Enter a value: 

var.key
  Default: hello
  Enter a value: 

template_file.sample: Refreshing state... (ID: 81d5caf6b8aee37211d2)
template_file.sample: Creating...
  filename:   "" => "template.txt"
  rendered:   "" => "<computed>"
  vars.#:     "0" => "2"
  vars.hello: "" => "hello"
  vars.world: "" => "hi"
template_file.sample: Creation complete
aws_instance.example: Creating...
  ami:                      "" => "ami-e0efab88"
...
  user_data:                "" => "e0fa0ca27ad2cc328d30df5a6dd0c736d45de147"
aws_instance.example: Creation complete

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate

Outputs:

  rendered = Hello: hello
World: hi
Addition: 6


$ terraform plan
var.filename
  Default: template.txt
  Enter a value: 

var.key
  Default: hello
  Enter a value: 

Refreshing Terraform state prior to plan...

template_file.sample: Refreshing state... (ID: 81d5caf6b8aee37211d2)
aws_instance.example: Refreshing state... (ID: i-1a404c35)

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_instance.example
    ami:                      "ami-e0efab88" => "ami-e0efab88"
...
    user_data:                "e0fa0ca27ad2cc328d30df5a6dd0c736d45de147" => "894115c5b5247d5425ec8090e4dd6ba9f15d9d49" (forces new resource)
    vpc_security_group_ids.#: "1" => "<computed>"

+ template_file.sample
    filename:   "" => "template.txt"
    rendered:   "" => "<computed>"
    vars.#:     "0" => "2"
    vars.hello: "" => "hello"
    vars.world: "" => "hi"


I haven't dug into the code too much, but I am thinking the plan command should render the templates just as the apply command does, which may be difficult if the template uses data from other resources that haven't been built yet. Perhaps when things are refreshed/read from the state file, that could be used, if it's not already?

@radeksimko
Copy link
Member

Which version of Terraform do you use to run this example.tf?

There's a related issue which has been fixed in the latest release (0.5.1+). Can you confirm that it's happening with the latest release?

@radeksimko radeksimko added bug waiting-response An issue/pull request is waiting for a response from the community provider/template labels May 17, 2015
@mlrobinson
Copy link
Author

This did fix my issue, after upgrading to 0.5.2. Thank you.

@ghost
Copy link

ghost commented May 2, 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 May 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug provider/template waiting-response An issue/pull request is waiting for a response from the community
Projects
None yet
Development

No branches or pull requests

2 participants