Building a Hardened OpenVPN Server AMI with Packer and Terraform

Exposing SSH or internal tools to the internet is how small cloud environments get breached. A better pattern is a single, hardened entrypoint: an OpenVPN server that sits at the edge of the VPC while everything else lives in private subnets. This post walks through how such a server is built as a golden AMI with Packer, then deployed with Terraform — the same pattern used for bastions, Jenkins, and other management hosts.

The architecture

Internet -> Elastic IP -> OpenVPN server (ASG, size 1) -> private subnets
                              |
                              +- PKI certs backed up to encrypted S3
                              +- logs + metrics -> CloudWatch + alarms

Two separable concerns:

  1. Image build (Packer) — everything that doesn’t change per-deploy gets baked in.
  2. Runtime deploy (Terraform + user-data) — environment-specific config injected at boot.

The Packer template

The template (openvpn-server-ubuntu.pkr.hcl) starts from Canonical’s Ubuntu 20.04 AMI, resolved through a data.amazon-ami source so builds always start from the latest published image:

source "amazon-ebs" "openvpn-server" {
  ami_name           = "${var.ami_name}-${var.version_tag}-${formatdate("YYYYMMDD-hhmm", timestamp())}"
  ami_regions        = var.copy_to_regions
  region_kms_key_ids = var.region_kms_key_ids
  encrypt_boot       = var.encrypt_boot
  kms_key_id         = var.encrypt_kms_key_id
}

Three details worth stealing for any AMI pipeline:

  • Timestamped AMI names — every build produces a uniquely named, taggable artifact (service, version tags), so rollback is terraform apply with an older AMI ID.
  • Multi-region encrypted copies — ami_regions + region_kms_key_ids copies the AMI to DR regions, each encrypted with that region’s own KMS key. Encrypted AMIs can’t be copied with a single key; the per-region key map is the correct pattern.
  • Cross-account sharing — ami_users shares the AMI with the account IDs that run the server, so the image is built once in a “builder” account and consumed elsewhere.

The provisioner hardens at bake time:

provisioner "shell" {
  inline = [
    "sudo DEBIAN_FRONTEND=noninteractive apt-get update && sudo apt-get -y upgrade",
    "gruntwork-install --module-name mgmt/openvpn-server --tag ${var.service_catalog_ref}"
  ]
}

install.sh then layers in an EC2 baseline: fail2ban for brute-force protection, auto-update for unattended security patches, ip-lockdown, NTP, CloudWatch log/metric agents, and IAM-group-based SSH via ssh-grunt. The AMI is already the hardened target — no drift-prone configuration at boot.

The Terraform side

The deployed resources are deliberately small: an ASG of size 1 (if the instance dies, a replacement re-attaches the same Elastic IP), an IAM instance profile scoped to the S3 PKI backup bucket and KMS key, a security group exposing only UDP 1194, and a Route 53 record.

Boot-time configuration happens in user-data.sh, which calls init-openvpn to generate the PKI (CA country/org/expiry are parameters) and backs certificates up to an encrypted S3 bucket. If the ASG replaces the instance, the new node restores the same PKI — clients never notice.

Why this pattern matters

  • Immutable infrastructure: patching is a rebuild, not an SSH session. Rebuild the AMI, roll the ASG, done.
  • One exposed surface: everything else sits in private subnets with no public IPs; the only public host is maximally hardened and fully observable.
  • Auditable: the AMI is built from versioned code (service_catalog_ref, version_tag), so the image in production traces back to a commit.

The demo video shows a full terraform apply of this module end-to-end — about four minutes from zero to a working VPN endpoint.


<
Previous Post
Palo Alto App Id Policy Denial
>
Next Post
Aws Account Baseline Root