terraform retrieve file from URL

Here my concrete problem but the question is more generic: I’m creating a VM through a Terraform file with a certain cloud-init template.

resource "openstack_compute_instance_v2" "VM" {
....
 
  user_data = file('cloud-init')

}

Now , I’d like to re-use a cloud-init file that is external to the repository, let’s say at https://site/cloud-init

  user_data = fromUrl('https://server/cloud-init')  #something like this

Is there a generic simple way to retrieve a file from an URL and uses it’s contents?
Googling got me nowhere and I feel like the problem is generic enough.

I’d want to avoid having to ‘curl it’ myself.

To get the file from a URL, you may use the http resource:

data "http" "my_file" {
  url = var.my_file_url

  request_headers = {
    "Authorization" = "Bearer TOKEN" # (if you need it)
  }
}

Then, you can do whatever you want with the data you get. For example, if your data is a JSON that you need to use as a Terraform object:

locals {
  jsondecode(data.http.my_file.response_body)
}

Leave a Comment