Here is a sample that we use. This is a bit more involved than your request. but i have a folder, in my module folder called templates. Thats where my 'userdata' scripts live. There are 2. If 'var.set_password_on_launch' is set to true, we use the first user data, if its set to false we use the second user data. Notice for the first template_file, I am declaring a variable, and passing that variable into my file.This variable will get interpolated inside the template by terraform, replacing the variable with whatever value I defined for 'administrator_password'.
This line `user_data = "${element(concat(data.template_file.windows_password.*.rendered, data.template_file.windows.*.rendered), 0)}"` is a fancy way of saying, if set_password_long == true, use the first userdata, if false, use the second userdata.
data "template_file" "windows_password" {
count = "${var.set_password_on_launch}"
template = "${file("${path.module}/templates/windows_set_password.tpl")}"
vars {
administrator_password = "${var.administrator_password}"
}
}
data "template_file" "windows" {
count = "${1 - var.set_password_on_launch}"
template = "${file("${path.module}/templates/windows.tpl")}"
}
resource "aws_instance" "dc" {
ami = "${var.ami != "" ? var.ami : data.aws_ami.dc.id}"
instance_type = "${var.instance_type}"
key_name = "${var.ec2_key}"
subnet_id = "${data.aws_subnet.subnet.id}"
vpc_security_group_ids = ["${aws_security_group.dc.id}"]
private_ip = "${var.private_ip}"
iam_instance_profile = "role_WindowsEC2SSM_Access"
user_data = "${element(concat(data.template_file.windows_password.*.rendered, data.template_file.windows.*.rendered), 0)}"
root_block_device {
volume_size = "${var.ebs_size}"
}
lifecycle {
create_before_destroy = true
prevent_destroy = true
ignore_changes = ["user_data"]
}
tags {
Name = "${format("ec2-%s-%s-%s-%s", var.customer, var.name, var.project, var.environment)}"
Customer = "${var.customer}"
Environment = "${var.environment}"
Project = "${var.project}"
Owner = "${var.owner}"
Terraform = true
AZ = "${var.az}"
AMI = "true"
"Patch Group" = "${format("%s-%s-%s", var.customer, var.project, var.environment)}"
startInstance = "${var.start_time_tag}"
stopInstance = "${var.stop_time_tag}"
}
volume_tags {
Name = "${format("ebs-%s-%s-%s-%s", var.customer, var.name, var.project, var.environment)}"
Customer = "${var.customer}"
Environment = "${var.environment}"
Project = "${var.project}"
Owner = "${var.owner}"
Terraform = true
AZ = "${var.az}"
}
}
Here is a sample of the template file. Notice `"${administrator_password}"` is in the file, just like it was defined in my vars above. When this file is actually created, `"${administrator_password}"` would be
replaced with the value I defined for that variable. If I wanted to have a literal ${bash_variable} in this file. I would do this `\$${bash_variable}. Which tells terraform I really want the string ${bash_variable}
to be in the userdata file used for my launch configuration.
To your question about having diff. variables defined for prd/dev. You could do that a few ways. If you were using .tfvars, and defining variables as inputs to your template file, the value of those variables would change depending on what
you specify in the tfvars file for those environments. If you wanted to have separate files for dev and prod, you could do like I did above, and choose one based on some input value.