What is Packer and how does it relate to Terraform?

Answer

Packer is a HashiCorp tool for creating identical machine images (AMIs, VHDs, Docker images) from a single template across multiple platforms. It automates creating pre-baked images with all software pre-installed. Relationship with Terraform: Build time vs runtime: Packer builds the image with software; Terraform deploys instances using that image. Workflow: 1. Packer: starts a temporary VM, installs Nginx/Java/your app via a shell provisioner or Ansible, creates a snapshot (AMI), terminates the VM. 2. Terraform: use the AMI ID from Packer as the ami attribute. VMs launch instantly with everything pre-installed. Benefits: faster instance boot (no user_data scripts), immutable infrastructure (never modify running instances — build a new image and redeploy), consistent environments. The Packer AMI ID can be passed to Terraform via a data source: data "aws_ami" "my_app" { filter { name = "tag:Version"; values = ["1.2.3"] } }.