EC2 AMI Baking with Packer: A Hands-On Guide
Published: 2023-10-02 09:53:11

## Preliminary Conceptions

Integrating application development & operations has become an inescapable cog in the software industry's machine. Among the constellation of tools that facilitate this integration, often referred to as **DevOps**, *Packer* has marked its territory as a preeminent tool for automating the creation of consistent and reusable machine images, known as AMIs (Amazon Machine Images), particularly for Amazon's EC2 (Elastic Compute Cloud) instances.

Before diving into the in-depth mechanics, let's clarify some semantics—AMIs are simply a standard template conjured up to fire up new instances with pre-configured settings, much in-line with the wise adage from a founder of modern computing, Alan Kay: "Simple things should be simple, complex things should be possible." 

## Gearing Up

For this hands-on expedition, we need a few tools in our armory:

1. An AWS Account i.e., the canvas for our masterpiece
2. Packer i.e., our paintbrush
3. AWS CLI i.e., the commands that make our paintbrush dance

## Manifest Destiny with Packer 

Packer scripts, known as **templates**, are written in JSON, fostering ease and clarity following the *UNIX philosophy* - "Do one thing and do it well." 

A duck soup Packer template to create an AMI with a pre-installed Apache Web Server might look like:

```json
{
  "builders": [{
    "type": "amazon-ebs",
    "access_key": "YOUR ACCESS KEY",
    "secret_key": "YOUR SECRET KEY",
    "region": "us-east-1",
    "source_ami_filter": {
      "filters": {
        "virtualization-type": "hvm",
        "name": "amzn-ami-hvm-*-x86_64-gp2",
        "root-device-type": "ebs"
      },
      "owners": ["amazon"],
      "most_recent": true
    },
    "instance_type": "t2.micro",
    "ssh_username": "ec2-user",
    "ami_name": "packer-example {{timestamp}}"
  }],
  "provisioners": [{
    "type": "shell",
    "inline": [
      "sudo yum update -y",
      "sudo yum install httpd -y",
      "sudo service httpd start"
    ]
  }]
}
```

**Note**: Do replace `"access_key"` & `"secret_key"` with your own AWS credentials.

## Understanding the Template

Below is a brief exposé of the different components:

- **Builders** spawn and form the image.
- **amazon-ebs** signals EC2 backed by EBS.
- **access_key** & **secret_key** are your AWS credentials.
- **source_ami_filter** ensures the use of the most recent AMI.
- **instance_type** specifies the type of instance to launch.
- **ssh_username** is the username to SSH into the instance.
- **ami_name** denotes AMI's name.
- **provisioners** install & configure needed software.

## Baking the AMI

To get the oven running, we need to validate our template first. This can be done using the command `packer validate example.json`, where `example.json` is your JSON file. 

In the spirit of Steve Jobs, reconciling technology with liberal arts, once validated, the AMI can be baked using the command `packer build example.json`. On successful execution, packer returns the freshly-baked AMI ID. 

## Wrapping It Up

It's safe to say 'AMI baking with Packer' offers programmers fertile ground for unlocking the full potential of EC2 instances, echoing the words of the father of Linux, Linus Torvalds—“Talk is cheap. Show me the code.” So let's embrace this mélange of ingenuity & coding prowess, and revolutionize the way we perceive DevOps.

Like the iconic Dodge Viper, cutting through the Great Plains, we have swiftly traversed the intricacies of EC2 AMI Baking using Packer, embodying the echoes of the profound yet elegant complexity of American muscle. Let's accelerate further into this grand highway of infinite possibilities, honoring & indulging the spirit of the culture and tech revolution that is quintessentially—DevOps.