// cheat sheets / iac-terraform-packer
Page 1 of 2 · Terraform
How they work together
Packer template→Bake the image→AMI ID→terraform plan→terraform apply
Intermediate#terraform#packer#iac#devops#aws
Terraform
Describe the cloud in code; plan the change; apply it
Concepts
- 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.
Open this diagram in archhy Read every plan: it is the only preview you get.
- 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.
Open this diagram in archhy- 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.
Open this diagram in archhy Never edit state by hand: use the terraform state commands.
- 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.
Open this diagram in archhy| Command | What it does |
|---|
terraform fmt -recursive | Formats every file in the standard style. |
terraform validate | Checks syntax and references without touching the cloud. |
terraform plan -out=tfplan | Saves the plan so apply does exactly that. |
terraform apply tfplan | Applies the saved plan. |
terraform state list | Lists the resources in state. |
Page 2 of 2 · Packer
Packer
Bake the machine image once; launch it everywhere
Concepts
- 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.
Open this diagram in archhy Bake slow, boot fast: install at build time, not at launch.
- 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.
- Launch — A temporary instance starts from the base image.
- Connect — Packer connects over SSH, or WinRM for Windows.
- Provision — Your scripts install and configure the software.
- Snapshot — The instance is stopped and saved as a new AMI.
- Clean up — The temporary instance, key pair and security group are deleted.
Open this diagram in archhy| Command | What 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. |
- 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.
Open this diagram in archhy A new image is a new plan: review it like any other change.