Template/Module iteration terraform style

981 views
Skip to first unread message

Noah

unread,
May 19, 2016, 3:23:23 PM5/19/16
to Terraform
Sourcing for ideas here in case I've overlooked any. 

I believe https://github.com/hashicorp/terraform/issues/953 would cover this use case.

Basically I have multiple AWS instances being created, for each of those instances I'm creating cloudwatch alarms. 

I've attempted using templates to iterate through: 

resource "template_file" "cloudwatch-ec2" {
  count = "${var.instance_count}"
  template = "${file("./cloudwatch-ec2.tf")}
  vars {
    name = "${element(aws_instance.web.*.tags.Name, count.index)}"
    instID = "${element(aws_instance.web.*.id, count.index)}"
    snsTopic = "${var.snsTopic}"
  }
}

This, while appearing to work, would eventually failed in the apply phase due to var.name (in the template) not being known. 

Exact Error: * template_file.cloudwatch-ec2: failed to render : 7:20: unknown variable accessed: var.name

I tried using modules, but I'm not sure how to get the count into the module. 

An example of the alarm resources: 

resource "aws_cloudwatch_metric_alarm" "ec2-statusCheck" {
  alarm_name = "${var.name}-StatusCheckSystem"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods = "2"
  metric_name = "StatusCheckFailed_System"
  namespace = "AWS/EC2"
  period = "300"
  statistic = "Maximum"
  threshold = "1"
  actions_enabled = true
  alarm_actions = ["${var.snsTopic}"]
  alarm_description = "${var.name} AWS availability degraded"
  ok_actions = ["${var.snsTopic}"]
  dimensions { InstanceID = "${var.instID}" }
}


I have 3 different modules for instance creation, but they all use the same EC2 alarms. Two of the modules have multiple instances (using count).

 The only way I can see to do this is to have all instance configurations use count, and then configure the cloudwatch alarm resources to use count? 

Any other ideas?

Please let me know if any additional info/clarity would be helpful.

Thank you

-Noah

Justin Nauman

unread,
May 20, 2016, 10:31:12 AM5/20/16
to Terraform
I believe the issue is in your template syntax.  The syntax there does not "prefix" var names with "var.".  Take a look at the interpolation example from https://www.terraform.io/docs/configuration/interpolation.html where it uses: 

resource "template_file" "example" {
  template = "${hello} ${world}!"
  vars {
    hello = "goodnight"
    world = "moon"
  }
}

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


NOTE: The "template" string does not use "var." in the interpolation 

JRN 

Noah

unread,
May 23, 2016, 2:00:33 PM5/23/16
to terrafo...@googlegroups.com
Right in front of me! Thanks for the review Justin; that was the issue with rendering the templates. 


Still haven't come up with a good way to use the rendered templates now, but they now render happily!



~~ One who has no where to go can not be lost.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/b97OFyNcNkQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/b0ab26df-6d3c-428b-a2e6-5d9223a424ec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

thiên chương

unread,
Nov 9, 2018, 12:47:27 AM11/9/18
to Terraform
Hi Noah,
Can you show me what exactly you fixed with your terraform code

Vào 23:30:33 UTC+5:30 Thứ Hai, ngày 23 tháng 5 năm 2016, Noah đã viết:

Jd Daniel

unread,
Nov 29, 2018, 12:52:29 PM11/29/18
to terrafo...@googlegroups.com
You need to use $$

bash-4.4$ tf plan

Warning: template_file.example: using template_file as a resource is deprecated; consider using the data source instead



Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + template_file.example
      id:         <computed>
      rendered:   <computed>
      template:   "${hello} ${world}!"
      vars.%:     "2"
      vars.hello: "goodnight"
      vars.world: "moon"


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

------------------------------------------------------------------------

Try....

resource "template_file" "example" {
  template = "$${hello} $${world}!"


  vars {
    hello = "goodnight"
    world = "moon"
  }
}

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

You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/2f05eb84-41ab-43c3-84c5-462c9ecd1a71%40googlegroups.com.

Jd Daniel

unread,
Nov 29, 2018, 12:54:02 PM11/29/18
to terrafo...@googlegroups.com
nvm i see the issue now

Jd Daniel

unread,
Nov 29, 2018, 12:55:43 PM11/29/18
to terrafo...@googlegroups.com
Here you go

bash-4.4$ tf plan -var hello=foo -var world=bar


Warning: template_file.example: using template_file as a resource is deprecated; consider using the data source instead



Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + template_file.example
      id:       <computed>
      rendered: <computed>
      template: "foo bar!"



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

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.


variable "hello" {}
variable "world" {}

resource "template_file" "example" {
  template = "${var.hello} ${var.world}!"

}

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

Reply all
Reply to author
Forward
0 new messages