liveBook · Manning

Packer

As explained in Amazon Web Services in Action, Third Edition: An in-depth guide to AWS

Packer is an open-source tool for creating machine images from declarative templates across multiple platforms.

Packer is a HashiCorp tool for building Amazon Machine Images (AMIs). An AMI can contain an application and its dependencies before an EC2 instance starts. To deploy a change, build a new image and replace the old instance with one launched from it. This is an immutable-server deployment: the running server is replaced rather than modified. section 15.3 section 15.5

A practical build-and-rollout sequence is:

  • Get the application running manually on the chosen Linux distribution, then turn the installation steps into a shell script. section 15.3.1
  • Define a Packer template with an Amazon plugin, a source AMI, a temporary EC2 build instance, and a shell provisioner. The Etherpad example connects through Systems Manager rather than default SSH and requires an EC2 instance profile. section 15.3
  • Run `packer init chapter15/`, then `packer build chapter15/etherpad.pkr.hcl`. Packer launches and provisions an instance, creates an AMI, and terminates the build instance. Test an instance launched from the resulting AMI. section 15.3 section 15.3.1
  • Pass the new AMI ID to a CloudFormation stack. Its EC2 launch template uses that image; an Auto Scaling group rolls out replacement instances. Keep environment-specific settings in user data rather than baking them into the AMI. section 15.3 section 15.3.1

Etherpad deployment

An Etherpad AMI contains the web application's installed dependencies. At launch, user data supplies settings such as its database connection. CloudFormation and an Auto Scaling group replace instances using the new image while an Application Load Balancer routes requests. section 15.3 section 15.0

Listing 15.7 The Packer template to build an Etherpad AMI, part 2
source "amazon-ebs" "etherpad" {
  ami_name = "awsinaction-etherpad-{{timestamp}}"   #1
  tags = {
    Name = "awsinaction-etherpad"
  }
  instance_type = "t2.micro"                        #2
  region        = "us-east-1"                       #3
  source_ami_filter {                               #4
    filters = {
      name = "amzn2-ami-hvm-2.0.*-x86_64-gp2"       #5
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true                              #6
    owners      = ["137112412989"]                  #7
  }
  ssh_username         = "ec2-user"
  ssh_interface        = "session_manager"          #8
  communicator         = "ssh"
  iam_instance_profile = "ec2-ssm-core"             #9
  ami_groups = ["all"]
  ami_regions = ["us-east-1"]                       #10
}

The Etherpad source configuration shows how Packer selects a base AMI, sizes and locates its temporary EC2 instance, and connects through Systems Manager. section 15.3

Prebuilt images make instances start quickly and reduce installation failures during boot. The trade-offs are lower deployment agility and an AMI lifecycle to manage: delete unused images to avoid storage costs. For comparison, CodeDeploy changes running machines in place, while CloudFormation with user data runs deployment steps when new machines boot. section 15.4

How Amazon Web Services in Action, Third Edition: An in-depth guide to AWS covers it

How deep it goes
Walks through template components, build commands, AMI testing, and rolling rollout; does not detail Packer's implementation internals.
Examples
Builds an Etherpad AMI and shows a separate configuration for prebuilt Imagery server and worker AMIs.
What it assumes
Assumes basic Auto Scaling, load balancing, and CloudFormation knowledge; provides the Packer template and build sequence.
What you can do after
Build it from scratch with Packer, test the AMI, and roll it out through CloudFormation and an Auto Scaling group.
How it connects
Connects AMIs and immutable servers to EC2, Systems Manager, CloudFormation, user data, and Auto Scaling groups.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Amazon Web Services in Action, Third Edition: An in-depth guide to AWS. The section links point to the parts of the book it draws on.