Skip to content

Instantly share code, notes, and snippets.

@deepbrook
Last active March 4, 2021 06:53
Show Gist options
  • Save deepbrook/af942192c6be3c2619920083bd51ab6b to your computer and use it in GitHub Desktop.
Save deepbrook/af942192c6be3c2619920083bd51ab6b to your computer and use it in GitHub Desktop.
Terraform Variable for Packer Manifest post-processor output
{
"builders": [
{
"type": "digitalocean",
...
}
],
"post-processors":[
{
"type": "manifest",
"output": "packer.auto.tfvars.json"
}
]
}
{
"builds": [
{
"name": "digitalocean",
"builder_type": "digitalocean",
"build_time": 1589911059,
"files": null,
"artifact_id": "ams3:63876948",
"packer_run_uuid": "41577b9e-4c85-ddef-a692-deaf6145783b",
"custom_data": null
},
{
"name": "digitalocean",
"builder_type": "digitalocean",
"build_time": 1589912902,
"files": null,
"artifact_id": "nyc1,nyc3,sgp1,lon1,fra1,tor1,sfo2,blr1,ams3:63877376",
"packer_run_uuid": "94e2e639-4e9b-e806-5248-7a8d7dc7eca4",
"custom_data": null
}
],
"last_run_uuid": "94e2e639-4e9b-e806-5248-7a8d7dc7eca4"
}
// Define Terraform input variables for packer variables to allow their usage
variable "builds" {
type = list(
object(
{
name=string,
builder_type=string,
build_time=number,
files=list(object({name=string, size=number})),
artifact_id=string,
packer_run_uuid=string
}
)
)
description = "List of images, as generated by Packer's 'Manifest' post-processor."
}
variable "last_run_uuid" {
type = string
}
// Accessing the artifact_id's image ID in a digitalocean_droplet resource
resource "digitalocean_droplet" "my-droplet" {
image = split(":", var.builds[length(var.builds)-1].artifact_id)[1]
...
}
@deepbrook
Copy link
Author

deepbrook commented May 20, 2020

Note that this does not read the custom_data field, as we do not use it.

Additonally, it makes sense to specify the post-processor's output file as <some-name>.auto.tfvars.json in the directory you want to execute terraform in, as that automatically loads the variables for you (no need for -var-file )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment