Search code examples
terraformterraform-provider-awsterraform0.12+terraform-template-file

Reference Terraform variable in shell script using templatefile function


I have a shell script to download a http link using wget:

wget -qO - "${mojang_server_url}"

I want to enter this link in a tfvars file, and then reference this in the shell script using Terraform.

I found a good solution here using template_file data source (it works)

data "template_file" "setup_script" {
 template = file("setup.sh")
 vars = {
  mojang_server_url = "${var.mojang_server_url}"
 }
}

However, the official Terraform documentation for template_file tells me I should be using the templatefile function instead?

I can't seem to figure out the syntax for this function, here's what I've got:

templatefile("setup.sh",{"mojang_server_url"="${var.mojang_server_url}")

it highlights templatefile, saying:

Argument or block definition required: An argument or block definition is required here. To set an argument, use the equals sign "=" to introduce the argument value.

Is there a way to make it so this can reference the tfvars variable ina shell script?

thank you!


Solution

  • You just simply have to provide a map as an input for the second argument:

    data "template_file" "setup_script" {
      template = templatefile("setup.sh",
        {
          mojang_server_url = var.mojang_server_url
        }
      )
    }