Vagrant Boxes That Mirror Amazon Linux AMI for Seamless Local Development


2 views

When developing for Amazon EC2 with Amazon Linux AMI (like the 2013.03 version), you'll quickly notice significant differences from standard Ubuntu/Debian Vagrant boxes. The package naming conventions (php5-fpm vs php54-fpm) and available repositories create deployment headaches. This can lead to "it works on my machine" scenarios that break in production.

Amazon provides several ways to get local parity:


# Using the AWS CLI to export an EC2 instance as Vagrant box
aws ec2 create-image --instance-id i-1234567890abcdef0 --name "My AMI for Vagrant" --description "AMI for local Vagrant testing"
aws ec2 export-image --image-id ami-123456789 --disk-image-format VMDK --s3-export-location S3Bucket=my-bucket,S3Prefix=exports/

However, this process is complex for local development. Instead, consider these alternatives:

Several community-maintained boxes closely mimic Amazon Linux:

  1. Official Amazon Linux 2 Box (Recommended):
  2. vagrant init amazon/awslinux-2
    vagrant up
  3. Custom CentOS-based boxes (Since Amazon Linux is RHEL-based):
  4. vagrant init centos/7
    vagrant ssh -c "sudo yum install -y epel-release"

For exact package matching, modify your Vagrantfile:


Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  
  config.vm.provision "shell", inline: <<-SHELL
    # Add Amazon Linux repos
    sudo tee /etc/yum.repos.d/amzn.repo <<EOL
    [amzn-main]
    name=Amazon Linux main
    mirrorlist=http://repo.us-east-1.amazonaws.com/$releasever/main/mirror.list
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-amazon-ga
    
    [amzn-updates]
    name=Amazon Linux updates
    mirrorlist=http://repo.us-east-1.amazonaws.com/$releasever/updates/mirror.list
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-amazon-ga
    EOL
    
    # Install AWS-specific packages
    sudo yum install -y php54-fpm
  SHELL
end

For quick validation without full VM overhead:


docker run -it amazonlinux:2013.03
yum list available | grep php

When you must support multiple platforms:


# Puppet example for cross-platform PHP
case $::operatingsystem {
  'Amazon': {
    package { 'php54-fpm': ensure => installed }
  }
  'Ubuntu': {
    package { 'php5-fpm': ensure => installed }
  }
}

When developing applications destined for Amazon EC2, many engineers face the frustrating package naming discrepancies between standard Vagrant boxes (like Precise32) and Amazon Linux AMIs. The core issue manifests in package naming conventions:

# On Ubuntu-based Vagrant box:
apt-get install php5-fpm

# On Amazon Linux AMI:
yum install php54-fpm

The most straightforward solution is using Amazon's official Vagrant boxes designed to mirror their EC2 environments:

# Add the Amazon Linux 2 box
vagrant box add aws/amazonlinux-2

# Or for older versions (2018.03)
vagrant box add aws/amazonlinux-2018.03

If you need to modify an existing Vagrant box to match Amazon's package ecosystem:

# Example Puppet manifest to handle cross-platform packages
case $operatingsystem {
  'Amazon': {
    package { 'php':
      name   => 'php54-fpm',
      ensure => installed
    }
  }
  'Ubuntu': {
    package { 'php':
      name   => 'php5-fpm',
      ensure => installed
    }
  }
}

For complete control, you can create a custom box from an actual EC2 instance:

# On an EC2 instance:
sudo yum install -y ruby-devel gcc make
sudo gem install fpm

# Package the environment
sudo fpm -s dir -t rpm -n custom-amazon-box -v 1.0 /
  --after-install /path/to/postinstall-script /
  --exclude /etc/ssh/ssh_host_*

Implement conditional testing in your provisioning scripts:

# In your Vagrantfile
config.vm.provision "shell", inline: <<-SHELL
  if [ -f /etc/system-release ]; then
    # Amazon Linux specific commands
    sudo yum -y install php54-fpm
  else
    # Ubuntu fallback
    sudo apt-get -y install php5-fpm
  fi
SHELL

Remember that Amazon Linux is based on RHEL/CentOS, so CentOS boxes can serve as reasonable alternatives when exact matching isn't critical.