Working with openstack. I have a two steps process to build images with packer: (1) create infrastructure using terraform basically, just a network routed to the internet and some security group that allows SSH (2) build the image using packer
Problem is I need to provide the id of the network built by terraform to packer. I can do this manually by checking the state file but I was wondering what was the best practice to automate this?
You can use terraform output to read outputs from the state. You can pass these on as Packer variables, i.e.
packer build -var network=$(terraform output network_uuid) template.json
Another suggestion: you can call Packer from Terraform.
resource "null_resource" "packer_runner" {
triggers = {
install_script = "${sha1(file("${path.module}/scripts/app/install.sh"))}"
packer_file = "${sha1(file("${path.module}/packer/packer.json"))}"
}
provisioner "local-exec" {
working_dir = "${path.module}"
command = "packer build -var 'ami_name=${var.ami_name}' -var 'aws_region=${var.aws_region}' -var 'network_id=${var.network_id}' -var -parallel-builds=1 ./packer/packer.json"
interpreter = ["PowerShell", "-Command"]
}
}
Then, on packer.json
:
<...stuff...>
"provisioners": [
{
"type": "shell",
"inline": "/usr/bin/cloud-init status --wait"
},
{
"type": "shell",
"environment_vars": [
"NETWOR_ID={{user `network_id`}}"
],
"script": "./scripts/app/install.sh"
},
<...more stuff...>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With