HBach
HBach

Reputation: 83

deploying multiple user_data ps scripts with Terraform

I've written a fair bit of Terraform and have deployed a single user-data script during boot of aws instances. I simply used the file function calling the location of the powershell script. This approach works well for single user-data scripts.

resource "aws_instance" "windows" {
     ...
     ...
     user_data  = file("./user-data/ConfigureWinRM.ps1")

Now, I want to have two scripts executed as the instances are stood up. I took a similar approach and tried the following but it appears the two scripts are not executed.

user_data = join("\n", [file("./user-data/ConfigureWinRM.ps1"), file("./user-data/admin-pass.ps1")])

I tried the template_file data source but this this also does not work.

data "template_file" "script1" {
  template = file("./user-data/ConfigureWinRM.ps1")
}

data "template_file" "script2" {
  template = file("./user-data/admin-pass.ps1")
}
user_data = join("\n", [data.template_file.script1.rendered, data.template_file.script2.rendered]) 

I also tried the template_cloud_init data source but it did not work.

data "template_cloudinit_config" "user-data" { 
  part { 
    filename = "ConfigureWinRM.ps1" 
    content  = file("./user-data/ConfigureWinRM.ps1") 
  } 
  part { 
    filename = "admin-pass.ps1" 
    content  = file("./user-data/admin-pass.ps1") 
  } 
} 
user_data = data.template_cloudinit_config.user-data.rendered 

Any suggestions would be most helpful. Thanks!

Upvotes: 5

Views: 2945

Answers (1)

Diego Velez
Diego Velez

Reputation: 1893

I think you're missing the content_type in the part section on template_cloudinit_config

sample

data "template_cloudinit_config" "user-data" { 
  part { 
    content_type = "text/x-shellscript"
    content  = file("./user-data/ConfigureWinRM.ps1") 
  } 
  part { 
    content_type = "text/x-shellscript" 
    content  = file("./user-data/admin-pass.ps1") 
  } 
} 

Upvotes: 1

Related Questions