Packer and TerraForm are fundamental components of Infrastructure as Code. I recently gave a talk at a DevOps meetup, which allowed me the opportunity to discuss the basics of these two tools, and how DevOps teams should be using them
2. Today’s challenges
Increasingly complex infrastructure to setup
• Multiple environments for testing and production
• Evolution / Changing of infrastructure
• Documentation of infrastructure
• More than one server involved
3. Infrastructure as Code (IaC)
Infrastructure as Code (IaC) is the process of managing and provisioning computing
infrastructure (processes, bare-metal servers, virtual servers, etc.) and their configuration
through machine-processable definition files
• Cost (reduction)
• Speed (faster execution)
• and Risk (remove errors and security violations)
4. Introducing Packer and TerraForm
Packer
Packer is a tool for creating machine and container images
for multiple platforms from a single source configuration.
Treat server as immutable
Any configuration change results in a
completely new server
Allows for easier tools then Chef or Puppet
JSON configuration language
TerraForm
Terraform provides a common configuration to launch
infrastructure. Once launched, Terraform safely and
efficiently changes infrastructure as the configuration is
evolved
Describe infrastructure in a declarative way
Keep track of changes to the infrastructure
Changing infrastructure is accessible to entire
team
Rollback your infrastructure to a previous point
5. Why use Packer?
Standardise development environments and machine images.
Create near identical state infrastructure across multiple
environments
Why use TerraForm?
To orchestrate and create resources in your environments with ease
and simplicity
8. Create a template
: configuration file used to define what
image we want built and how
Notes
Define the builders
Define provisioners
Define post-processors
Define variables (access keys etc)
<NB/>: Parallel Builds
Example
{
"builders": [],
"description": "A packer example template",
"min_packer_version": "0.8.0",
"provisioners": [],
"post-processors": [],
"variables": []
}
11. TerraForm
: a tool for building, changing, and
versioning infrastructure safely and
efficiently.
Resources
Providers
terraform.tfstate: maps various resource
metadata to actual resource IDs so that Terraform
knows what it is managing
Input variables: variables.tf &
terraform.tfvars
Output variables
Example: main.tf
provider "aws" {
access_key = "ACCESS_KEY_HERE"
secret_key = "SECRET_KEY_HERE"
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0d729a60" #from packer build
instance_type = "t2.micro"
}
13. Build Steps
Planning (Packer ->
TerraForm)
Plan reqs: packer builders / provisioners
Plan TerraForm resources/providers
Remote build (Packer)
Packer inspect/fix/validate
Packer build (store build artifact)-> update TF to use this
artifact/ami as a source
TerraForm Plan
Terraform validate > graph > plan
Store plan output
TerraForm Apply
Run terraform apply (or terraform destroy)
Commit .tfstate to VCS or remote backend.