Skip to content

// cheat sheets / iac-terraform-packer

Page 1 of 2 · Terraform

Infrastructure as Code: Terraform and Packer

Packer bakes the machine image; Terraform builds the cloud around it

How they work together

Intermediate#terraform#packer#iac#devops#aws

Terraform

Describe the cloud in code; plan the change; apply it

Concepts

1. The core workflow

  • terraform init — Downloads the providers and modules, and sets up the state backend.
  • terraform plan — Compares your code with state and the real cloud, and shows what will change.
  • terraform apply — Makes the planned changes in dependency order, then records them in state.
  • terraform destroy — Removes everything this configuration manages.
Diagram for The core workflow: main.tf → terraform plan (diff); terraform plan → terraform apply (approve); terraform apply → EC2 instance (create); terraform apply → S3 bucket (create).
Open this diagram in archhy

Read every plan: it is the only preview you get.

2. Providers and resources

  • Provider — A plugin that talks to one API: aws, azurerm, google, kubernetes and many more.
  • Resource — One thing Terraform creates and manages, such as aws_instance.
  • Data source — Reads something that already exists, such as the newest AMI, without managing it.
  • Variables and outputs — Inputs make a configuration reusable; outputs pass values on.
Diagram for Providers and resources: Terraform → aws provider (API calls); aws provider → aws_instance; aws provider → aws_s3_bucket; data aws_ami → Terraform (reads).
Open this diagram in archhy

3. State

  • State file — Terraform's record of what it created, mapping code to real resource IDs.
  • Remote backend — Keep state in S3 or HCP Terraform so the whole team shares one copy.
  • Locking — One apply at a time: S3 native locking (use_lockfile) or a DynamoDB table.
  • Drift — Changes made outside Terraform; the next plan shows them.
Diagram for State: Engineer A → S3 state backend (plan / apply); CI pipeline → S3 state backend (plan / apply); S3 state backend → State lock (one at a time).
Open this diagram in archhy

Never edit state by hand: use the terraform state commands.

4. Modules

  • Module — A folder of Terraform files called with inputs, like a function.
  • Registry — Ready-made modules for common building blocks, such as a VPC or an EKS cluster.
  • Versions — Pin module and provider versions so a plan means the same thing tomorrow.
Diagram for Modules: Root module → module "vpc" (inputs); Root module → module "app" (inputs); module "vpc" → module "app" (subnet IDs); module "app" → Load balancer; module "app" → Auto Scaling group.
Open this diagram in archhy

5. Everyday commands

CommandWhat it does
terraform fmt -recursiveFormats every file in the standard style.
terraform validateChecks syntax and references without touching the cloud.
terraform plan -out=tfplanSaves the plan so apply does exactly that.
terraform apply tfplanApplies the saved plan.
terraform state listLists the resources in state.
archhyarchhy.com/cheat-sheets/iac-terraform-packerMake your own at archhy.com

Page 2 of 2 · Packer

Packer

Bake the machine image once; launch it everywhere

Concepts

6. What Packer does

  • Machine image — A ready-to-boot image: an AMI on AWS, an image on Azure or Google Cloud.
  • Golden image — Your base: OS updates, agents and hardening baked in once, used everywhere.
  • Immutable servers — Replace instances with a new image instead of patching them in place.
Diagram for What Packer does: Packer template → packer build; packer build → AMI (bakes); AMI → Instance (launch); AMI → Instance (launch).
Open this diagram in archhy

Bake slow, boot fast: install at build time, not at launch.

7. The template (HCL)

  • packer block — The plugins it needs and their versions, such as amazon.
  • source — Where to build: the builder, the base AMI, region and instance type.
  • build — Which sources to build, and the steps to run on them.
  • provisioner — Steps on the build machine: shell scripts, Ansible, files.
  • post-processor — Steps after the image exists, such as a manifest with the new AMI ID.

8. How a build runs

  1. Launch — A temporary instance starts from the base image.
  2. Connect — Packer connects over SSH, or WinRM for Windows.
  3. Provision — Your scripts install and configure the software.
  4. Snapshot — The instance is stopped and saved as a new AMI.
  5. Clean up — The temporary instance, key pair and security group are deleted.
Diagram for How a build runs: Base AMI → Temporary instance (launch); Provisioners → Temporary instance (SSH); Temporary instance → New AMI (snapshot).
Open this diagram in archhy

9. Everyday commands

CommandWhat it does
packer init .Installs the plugins the template needs.
packer fmt .Formats the HCL files.
packer validate .Checks the template before a build.
packer build .Runs every build in the template.
packer build -var 'version=1.2' .Sets a variable for this build.

10. Handing over to Terraform

  • Manifest — The manifest post-processor writes the new AMI ID to a JSON file.
  • Data source — Or Terraform finds the newest image with data "aws_ami" and most_recent.
  • Launch template — Point it at the new AMI; an instance refresh rolls it out.
Diagram for Handing over to Terraform: packer build → AMI (bakes); AMI → Launch template (image_id); terraform apply → Launch template (updates); Launch template → Auto Scaling group (instance refresh).
Open this diagram in archhy

A new image is a new plan: review it like any other change.

archhyarchhy.com/cheat-sheets/iac-terraform-packerMake your own at archhy.com

Draw your own, with real cloud icons.

Start drawing, free

No account needed to start.