-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Unnecessary template_file recreation when no template variables have changed #8871
Comments
Hi @lflux. Sorry for the issue here. The bug here is actually not what it first appears... what you're hitting is a known issue with Terraform's validation of interpolations. In your So the fix is to escape those variables, ensuring that Terraform core won't try to deal with them itself: data "template_file" "aws_config_bucket_name" {
template = "$${purpose}-$${region}-$${account_id}"
vars {
region = "${var.region}"
account_id = "${var.account_id}"
purpose = "aws-config"
}
} The correct behavior for your original config would be for Terraform to emit an error saying that If you add the extra dollar signs to escape as in my above example, you should find that the template gets rendered during the "refresh" phase of |
I'm going to close this issue to consolidate the discussion of this bug into #8077, but feel free to reopen it if my suggested config fix doesn't work, since that would indicate a new bug. |
Thanks. This is a regression to how inline template_file's worked in 0.6, I'll double-quote for now until #8077 is fixed. |
@lflux specifically it is an effect of it being a data source rather than a resource. Data sources have a different lifecycle where Terraform will deal with them very early on (during refresh) if the configuration doesn't contain any unknown variables, so you're triggering an unintended consequence of that behavior in conjunction with the validation bug. This was actually always a core bug, even in 0.6... it just wasn't observable in this precise way until we added a new branch in behavior for computed attributes. Note that even when #8077 it will still be required to double-dollar these. That issue is about making Terraform produce a suitable error message when you only have a single dollar sign. |
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. |
Hi there, I'm seeing an issue where a data.template_file thinks it's output has changed and the resource will be recreated on subsequent terraform runs, despite no variable inputs to the template_file resource changing.
In our case, we're using the template output as input to an S3 bucket name. Since
aws_s3_bucket
is usingdata.template_file.aws_config_bucket_name.rendered
as the bucket name, and terraform believes the template resource has changed, terraform wants to destroy and recreate the bucket even though the name is exactly the same.This only happens if template variables are interpolated - i e:
will not force recreation, but
will force recreation.
Terraform Version
0.7.3
Affected Resource(s)
Terraform Configuration Files
Expected Behavior
Subsequent plans/applies after first application don't want to recreate the bucket
Actual Behavior
Terraform plan output:
$ terraform apply
aws_s3_bucket.aws_config: Refreshing state... (ID: aws-config-us-west-2-4711)
data.template_file.aws_config_bucket_name: Refreshing state...
aws_s3_bucket.aws_config: Destroying...
aws_s3_bucket.aws_config: Destruction complete
aws_s3_bucket.aws_config: Creating...
acceleration_status: "" => ""
acl: "" => "private"
arn: "" => ""
bucket: "" => "aws-config-us-west-2-4711"
force_destroy: "" => "false"
hosted_zone_id: "" => ""
policy: "" => ""
region: "" => ""
request_payer: "" => ""
website_domain: "" => ""
website_endpoint: "" => ""
aws_s3_bucket.aws_config: Creation complete
Apply complete! Resources: 1 added, 0 changed, 1 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
Steps to Reproduce
terraform apply
- will create the bucketterraform apply
- will destroy and recreate the bucketThe text was updated successfully, but these errors were encountered: