Terraform: Remote file/template

Created on 15 Sep 2017  ·  4Comments  ·  Source: hashicorp/terraform

Terraform Version

$ terraform -v
Terraform v0.10.3

Terraform Configuration Files

data "template_file" "vpn-cloudformation" {
  template = "${file("https://raw.githubusercontent.com/daveqr/amazon-web-services-in-action/master/modules/vpn/vpn-cloudformation.json")}"
}

resource "aws_cloudformation_stack" "vpn" {
  name = "vpn"

  parameters {
    KeyName           = "mykey"
    IPSecSharedSecret = "blahsharedsecret"
    VPNUser           = "vpn"
    VPNPassword       = "blahpassword"
  }

  template_body = "${data.template_file.vpn-cloudformation.rendered}"
}

Debug Output

1 error(s) occurred:

* data.template_file.aws-cloudformation: 1 error(s) occurred:

* data.template_file.aws-cloudformation: file: open https://raw.githubusercontent.com/daveqr/amazon-web-services-in-action/master/modules/vpn/vpn-cloudformation.json: no such file or directory in:

${file("https://raw.githubusercontent.com/daveqr/amazon-web-services-in-action/master/modules/vpn/vpn-cloudformation.json")}

Expected Behavior

Be able to read/fetch remote template files, allowing for a common repo amongst multiple terraform deployments

Actual Behavior

Error 😢

Steps to Reproduce

Please list the full steps required to reproduce the issue, for example:

  1. terraform init
  2. terraform apply
question

Most helpful comment

Hi @FrenchBen!

It's intentional that the file function doesn't support URLs, but you can use the http data source to retrieve data from remote URLs.

data "http" "template" {
  url = "https://raw.githubusercontent.com/daveqr/amazon-web-services-in-action/master/modules/vpn/vpn-cloudformation.json"
}

data "template_file" "vpn-cloudformation" {
  template = "${http.template.body)}"
}

Another option, which is maybe more conventional for this sort of use-case, is to put the vpn-cloudformation.json file in a Terraform module and reference it using a module block, which can then directly use git rather than accessing the git repo over HTTP.

In the shared module, you might write:

output "source" {
  # Assumes that the file is in the same directory as this config file
  value = "${file("${path.module}/vpn-cloudformation.json")}"
}

Then you can access it by declaring the child module:

module "template" {
  source = "github.com/daveqr/amazon-web-services-in-action//modules/vpn"
}

data "template_file" "vpn-cloudformation" {
  template = "${module.template.source)}"
}

All 4 comments

Hi @FrenchBen!

It's intentional that the file function doesn't support URLs, but you can use the http data source to retrieve data from remote URLs.

data "http" "template" {
  url = "https://raw.githubusercontent.com/daveqr/amazon-web-services-in-action/master/modules/vpn/vpn-cloudformation.json"
}

data "template_file" "vpn-cloudformation" {
  template = "${http.template.body)}"
}

Another option, which is maybe more conventional for this sort of use-case, is to put the vpn-cloudformation.json file in a Terraform module and reference it using a module block, which can then directly use git rather than accessing the git repo over HTTP.

In the shared module, you might write:

output "source" {
  # Assumes that the file is in the same directory as this config file
  value = "${file("${path.module}/vpn-cloudformation.json")}"
}

Then you can access it by declaring the child module:

module "template" {
  source = "github.com/daveqr/amazon-web-services-in-action//modules/vpn"
}

data "template_file" "vpn-cloudformation" {
  template = "${module.template.source)}"
}

Thanks for the details and code example. I would like to go the module way, but haven't figured out how to -target= a specific module aka allow multiple modules in a declaration but only apply 1 module.

Closing as resolved thanks to @apparentlymart

@FrenchBen does this solution work ?? rendering a file from remote git location using git protocol as @apparentlymart mentioned

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings