Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 36 ยท Projects and Practice

Project: A Golden Web Server Image

Plan and write a complete Packer project: a hardened, tested Nginx web server image with variables, provisioners, a cleanup step, a manifest and a pull request pipeline.

In this project you assemble what the previous lessons taught into one realistic template: a golden web server image based on Ubuntu with Nginx installed, a small health page, hardened SSH, a cleanup step, tests and a manifest. You will lay out the repository, write the template, and use JavaScript stand-ins to check the plan.

Packer and AWS cannot run on this site, so the template is read-only and untested here. Run it in your own account after replacing the placeholder values, and read the plugin documentation for the argument names of your versions. The JavaScript examples are stand-ins that validate your plan.

Requirements

  1. Base: the latest official Ubuntu 24.04 image from a trusted owner.
  2. Software: Nginx, a monitoring agent placeholder, the curl tool.
  3. Config: a static health endpoint at /healthz, a version file /etc/image-release.
  4. Security: SSH hardened, no build user left, encrypted volumes, IMDSv2 required.
  5. Quality: format and validate on every PR, boot test before promotion.
  6. Traceability: tags and a manifest with commit SHA and version.

Repository layout

TEXT
images/web/
  web.pkr.hcl          # packer block, source, build
  variables.pkr.hcl    # variable declarations
  ci.pkrvars.hcl       # harmless values for validation
  files/
    nginx-site.conf
    healthz.html
  scripts/
    install.sh
    harden.sh
    cleanup.sh
  tests/
    goss.yaml

Keeping configuration in files/ and logic in scripts/ keeps the template short and lets you lint the scripts with ShellCheck.

Variables

HCL
variable "region"     { type = string  default = "eu-central-1" }
variable "version"    { type = string }
variable "git_sha"    { type = string  default = "unknown" }
variable "subnet_id"  { type = string  default = "" }

The template

HCL
packer {
  required_plugins {
    amazon = {
      source  = "github.com/hashicorp/amazon"
      version = "~> 1.3"
    }
  }
}

source "amazon-ebs" "web" {
  region        = var.region
  instance_type = "t3.small"
  ssh_username  = "ubuntu"
  subnet_id     = var.subnet_id != "" ? var.subnet_id : null
  ami_name      = "web-base-${var.version}-${var.git_sha}"
  encrypt_boot  = true
  imds_support  = "v2.0"

  source_ami_filter {
    filters = {
      name                = "ubuntu/images/hvm-ssd-gp3/ubuntu-*-24.04-amd64-server-*"
      virtualization-type = "hvm"
      root-device-type    = "ebs"
    }
    owners      = ["099720109477"]
    most_recent = true
  }

  tags = {
    Name    = "web-base"
    Version = var.version
    GitSha  = var.git_sha
    BuiltBy = "packer"
  }
}

build {
  name    = "web"
  sources = ["source.amazon-ebs.web"]

  provisioner "file" {
    source      = "files/"
    destination = "/tmp/files"
  }

  provisioner "shell" {
    scripts          = ["scripts/install.sh", "scripts/harden.sh"]
    environment_vars = ["VERSION=${var.version}", "DEBIAN_FRONTEND=noninteractive"]
    execute_command  = "sudo -E bash '{{ .Path }}'"
  }

  provisioner "file" {
    source      = "tests/goss.yaml"
    destination = "/tmp/goss.yaml"
  }

  provisioner "shell" {
    inline = ["sudo goss -g /tmp/goss.yaml validate --format documentation"]
  }

  provisioner "shell" {
    script          = "scripts/cleanup.sh"
    execute_command = "sudo -E bash '{{ .Path }}'"
  }

  post-processor "manifest" {
    output      = "packer-manifest.json"
    custom_data = { version = var.version, git_sha = var.git_sha }
  }
}

The Goss step assumes the goss binary was installed by install.sh. The cleanup.sh script runs last: clear apt caches, histories, SSH host keys, /tmp, and cloud-init clean. See hardening and testing.

Pipeline

  1. PR: packer fmt -check, packer init, packer validate -var-file=ci.pkrvars.hcl -var version=0.0.0, ShellCheck on scripts.
  2. Main: build with OIDC credentials (GitHub Actions), upload the manifest.
  3. Boot test: launch from the new AMI, wait for /healthz, run Goss remotely, terminate.
  4. Promote: move the staging channel, then prod after approval (promotion).

Try it: check the repository plan

JS Repository checklist (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Try it: compose the AMI name and tags

JS Name and tag builder (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Extension ideas

  • Add a second source for another region or architecture (arm64) and a matrix job.
  • Replace the shell scripts with an Ansible role and keep the same tests.
  • Publish the version to a parameter store so Terraform can read "current web image".

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the cleanup provisioner run last?

    Any step after cleanup could reintroduce leftovers, so it runs just before the snapshot.
  2. 2. What makes the template traceable?

    Version and SHA in tags and the manifest let you trace any instance to its source.
  3. 3. What is the purpose of ci.pkrvars.hcl?

    It lets validation work for everyone, including forks.
  4. 4. Where does the Goss test run?

    The provisioner runs it on the builder; a non-zero exit fails the build.

Summary

  • A good image project separates template, files, scripts and tests.
  • Build-time tests, a cleanup step and a manifest turn a template into a pipeline-ready product.
  • Tags and manifest data tie every instance back to a commit and version.
  • Validate on PRs, build on main, boot-test, then promote.

Next: Project: a multi-cloud image matrix