Search code examples
devopsterraformvspherepackeresxi

Workflow sample for Packer and Terraform integration


I'm trying to create a lab for virtual machines in the VMWare Hypervisor to install a cluster in RHEL; I've seen that Packer and Terraform are very interesting but I can't find clear and /or detailed samples about their workflow, specifically how to create the image in Packer and then let that Terraform consume it. I have seen that Packer has build capabilities but even some type of deploy ones and I don't understand if these overlaps Terraform; I've read that some type of automation is possible via another HashiCorp product, Atlas, but I don't want to use it, at least at this stage of study and trial of the software. So what I'd like to do is create a VMWare compatible virtual machine images with Packer (RHEL base plus other capabilities), pass them to a Terraform artifact that creates the vm in my esxi.

Hope to find guidance.


Solution

  • I am not sure of your specific use case, but Terraform does have data-sources that make it easier to connect images built by Packer to Terraform: https://www.terraform.io/docs/configuration/data-sources.html

    Here is a partial example of using a Packer Built AMI for an AWS EC2 instance:

    data "aws_ami" "bastion" {
      filter {
        name   = "state"
        values = ["available"]
      }
    
      filter {
        name   = "tag:Name"
        values = ["Bastion"]
      }
    
      most_recent = true
    }
    
    resource "aws_instance" "bastion" {
      ami = "${data.aws_ami.bastion.id}"
      # ...
    }
    

    I have also used bash scripts to parse out Packer generated values and dump them into tfvars files, which Terraform consumed.