Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple user_data File use in Terraform

I am trying to have a common user_data file for common tasks such as folder creation and certain package install and a separate user_data file for application specific configuration

I am trying the below -

user_data = "${data.template_file.userdata_common.rendered}", "${data.template_file.userdata_master.rendered}"

With these configs -

Common User Data Template

data "template_file" "userdata_common" {
  template = "${file("${path.module}/userdata_common.sh")}"

  vars {
    "ALBTarget"          = "${var.ALBTarget}"
    "s3bucket"           = "${var.s3bucket}"
    "centrifydomain"     = "${lookup(var.centrifydomain, format("%s-%s", lower(var.env),var.region))}"
    "centrifyadgroup"    = "${lookup(var.centrifyadgroup, format("%s-%s", lower(var.env),var.region))}"
  }
}

Application Specific Config

data "template_file" "userdata_master" {
  template = "${file("${path.module}/userdata_master.sh")}"

  vars {
    "ALBTarget"          = "${var.ALBTarget}"
    "s3bucket"           = "${var.s3bucket}"
    "centrifydomain"     = "${lookup(var.centrifydomain, format("%s-%s", lower(var.env),var.region))}"
    "centrifyadgroup"    = "${lookup(var.centrifyadgroup, format("%s-%s", lower(var.env),var.region))}"
  }
}

I get the below Error when i do Plan -

Failed to load root config module: Error parsing /terraform/main.tf: key ${data.template_file.userdata_common.rendered}"' expected start of object ('{') or assignment ('=')

Is this possible using Terraform (0.9.3)?

If not, what's the best way to do this with Terraform?

like image 533
Rohit Sarkar Avatar asked Apr 26 '17 18:04

Rohit Sarkar


1 Answers

Did you try template_cloudinit_config?

Add below codes.

data "template_cloudinit_config" "master" {
  gzip          = true
  base64_encode = true

  # get common user_data
  part {
    filename     = "common.cfg"
    content_type = "text/part-handler"
    content      = "${data.template_file.userdata_common.rendered}"
  }

  # get master user_data
  part {
    filename     = "master.cfg"
    content_type = "text/part-handler"
    content      = "${data.template_file.userdata_master.rendered}"
  }

}

# sample code to use it.
resource "aws_instance" "web" {
  ami           = "ami-d05e75b8"
  instance_type = "t2.micro"
  user_data     = "${data.template_cloudinit_config.master.rendered}"
}

Let me know if it works.

like image 164
BMW Avatar answered Oct 17 '22 11:10

BMW