hashicorp / terraform-provider-template

Terraform template provider
https://www.terraform.io/docs/providers/template/
Mozilla Public License 2.0
131 stars 89 forks source link

template_file resource unknown variable accessed #41

Closed ghost closed 5 years ago

ghost commented 5 years ago

This issue was originally opened by @ewilde as hashicorp/terraform#12520. It was migrated here as a result of the provider split. The original body of the issue is below.


Part of a larger terraform script is failing when rendering a simple template

Terraform Version

Terraform v0.8.8

Affected Resource(s)

Terraform Configuration Files

variable "name" {
}

variable "environment" {
}

variable "account_id" {
}

data "template_file" "policy" {
  template = "${file("${path.module}/policy.json")}"

  vars = {
    bucket_name = "${var.name}-${var.environment}-logs"
    account_id  = "${var.account_id}"
  }
}

resource "aws_s3_bucket" "logs" {
  bucket = "${var.name}-${var.environment}-logs"

  tags {
    Name        = "${var.name}-${var.environment}-logs"
    Environment = "${var.environment}"
    Stack       = "${var.name}"
  }

  policy = "${data.template_file.policy.rendered}"
}

output "id" {
  value = "${aws_s3_bucket.logs.id}"
}

Policy json

{
  "Id": "log-bucket-policy",
  "Statement": [
    {
      "Sid": "AWSCloudTrailAclCheck20150319",
      "Effect": "Allow",
      "Principal": {
        "Service": "cloudtrail.amazonaws.com"
      },
      "Action": "s3:GetBucketAcl",
      "Resource": "arn:aws:s3:::${bucket_name}"
    },
    {
      "Action": "s3:PutObject",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::${account_id}:root",
        "Service": "cloudtrail.amazonaws.com"
        },
      "Resource": "arn:aws:s3:::${bucket_name}/*",
      "Sid": "log-bucket-policy"
    }
  ],
  "Version": "2012-10-17"
}

Debug Output

https://www.dropbox.com/s/iwt5h3xxf8w1ot6/debug.log.gpg?dl=0

Expected Behavior

Template resource should render

Actual Behavior

Error message:

Error applying plan:

1 error(s) occurred:

* data.template_file.policy: failed to render : 11:35: unknown variable accessed: bucket_name

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply
mildwonkey commented 5 years ago

This issue is from an older version of terraform, and I was not able to reproduce locally with the given file, so I am going to close it. Please open a new issue if you are still experiencing an issue with the latest version of terraform and the the template provider.