Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Increment template #12

Open
hashibot opened this issue Jul 5, 2017 · 3 comments
Open

Increment template #12

hashibot opened this issue Jul 5, 2017 · 3 comments
Labels

Comments

@hashibot
Copy link

hashibot commented Jul 5, 2017

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


I'm using a template to provide initial configuration data for my EC2 instances. Part of that configuration is to set a host name using a defined prefix and the counter as a suffix.

The result is all instances are getting the same name 0, as 0 is the first counter index. I use the same variable to set apply tags to the EC2 instance and node in chef and they increment just fine. The only place it doesn't is with the template.

On face value it appears that the template does not not reevaluate the passed variables after they are first set.

The temple resource is configured as

data "template_file" "file_server" {
  template = "${file("userdata.txt")}"
  vars {
    admin_password="${var.administrator_pw}"
    hostname = "${var.file_server.["hostname_prefix"]}-${count.index}"
  }
}

It is applied against the AWS resource as

[user_data = "${data.template_file.file_server.rendered}"]

In the user data file I am referring to the variable with the following line

Rename-Computer -NewName "${hostname}" -Restart -Confirm:$False

I would expect that my servers are named fs-0, fs-1 ..... but the host name is set as fs-0 for every server.

This is on Terraform 0.9.6

@zefr-ericb-testing-only
Copy link

I ran into the same trouble you did with the template not rendering as expected. I needed to get the count.index to be a broker_id to uniquely ID each instance.

I found hashicorp/terraform#2167 which mentioned a solution that didn't work for me either.

What solved it was adding a 'count' to the template and referencing it in the aws_resource with the splat operator as below.

data "template_file" "kafka_user_data" {
  template = "${file("${path.module}/templates/kafka-user_data.sh")}"
  count    = "${var.count}"

  vars {
    environment       = "${var.env}"
    component         = "${var.component}"
    suffix            = "${var.suffix}"
    broker_id         = "${count.index}"
    data_disk_type    = "${var.data_disk_type}"
    git_branch_or_tag = "${var.git_branch_or_tag}"
  }
}

Here's how I'm calling it from my aws_instance resource.

resource aws_instance "kafka-broker-ebs"{
  count                = "${var.data_disk_type == "ebs" ? var.count : 0}"
  ami                  = "${var.ami}"
  instance_type        = "${var.instance_type}"
  subnet_id            = "${element(var.subnet_id, count.index)}"
  iam_instance_profile = "${aws_iam_instance_profile.kafka-profile.name}"
  monitoring           = true
  ebs_optimized        = true
  security_groups      = ["${var.security_group}"]
  user_data            = "${element(data.template_file.kafka_user_data.*.rendered, count.index)}"
  key_name             = "${var.key_name}"

---removed tags and volumes---
}

this is on Terraform v0.9.11

@SDBrett
Copy link

SDBrett commented Aug 7, 2017

I've updated my code to reflect what you posted. Though I get an error in the data resource if I do so.

  • resource 'data.template_file.file_server' count: unknown variable referenced: 'count'. define it with 'variable' blocks

data "template_file" "file_server" {
template = "${file("userdata.txt")}"
count = "${var.count}"

vars {
admin_password="${var.administrator_pw}"
hostname ="${var.file_server.["hostname_prefix"]}-"
hostname_increment = "${count.index}"
}

In the file for the EC2 instance I'm using

resource "aws_instance" "file_server" {

instance_type   = "${var.file_server["type"]}"
count           = "${var.file_server["number"]}"
ami             = "${data.aws_ami.server2016_ami.id}"
key_name        = "${var.awskeypair.["key_pair_name"]}"
vpc_security_group_ids = ["${aws_security_group.windows_lab.id}"]
subnet_id       = "${aws_subnet.windows_lab_public.id}"
user_data       = "${element(data.template_file.file_server.*.rendered, count.index)}"

@SDBrett
Copy link

SDBrett commented Sep 20, 2017

I have changed the way that I am incrementing the hostname when calling the template.

The template now looks like

`data "template_file" "file_server" {
template = "${file("userdata.txt")}"
count = "${var.file_server["number"]}"

vars {
admin_password = "${var.administrator_pw}"
hostname = "${var.file_server.["hostname_prefix"]}-"
hostname_increment = "${count.index}"
}
}`

"${var.file_server["number"]}" is the variable used to specify the number of AWS instances to create.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants