Amazon Linux 2 is available as virtual machine images for development on-premises. If you are using Amazon Linux 2 EC2 instances it is a compelling idea to have a local version of the same for testing and development.
But how? The follow details a path to running Amazon Linux 2 locally in a VirtualBox VM.
Before continuing it's assumed that you've worked with VirtualBox before. To continue realize that Virtual Box creates a virtual machine inside a named directory and uses an associated virtual disk image
. This disk image as you can imagine is the virtual machine's hard disk.
The virtual machines (VMs) are stored in a directory defaultly named VirtualBoxVMs
under your home directory. When you create a VM from scratch the associated virtual disk image
file (VDI) will be in the same directory. For the plan detailed here we will want to keep our VDI file in a separate directory. The recommendation to keep the VDI separate allows for the configuration files and script to be located with the VDI. It also allows you to easily delete your VM and rebuild using a new copy of the VDI file.
Amazon Linux 2 is a newish version of Linux available on AWS when setting up EC2 instances. To make a version useful inside of VirtualBox, Amazon has release a VDI file. This VDI file will be the virtual disk image
for your local virtual machine after you do a bit of configuring.
Because you are likely to create multiple VMs or may want to re-create your local VM to replicate tests and experiments I suggest you store your original Amazon VDI in a known directory and use copies for each VM you create.
The Amazon VDI file is available at https://cdn.amazonlinux.com/os-images/2.0.20181024/virtualbox/. Download the vdi file and save it.
$ cd BACKUP
$ wget https://cdn.amazonlinux.com/os-images/2.0.20181024/virtualbox/amzn2-virtualbox-2.0.20181024-x86_64.xfs.gpt.vdi
First, create a project directory. For explanation purposes let's call it amzn2-virtualbox
. Inside, create a BACKUP
directory and put the newly download VDI inside.
Next, create a work directory. Here, I'll call it amzn2-instance
. To keep things clear I suggest you use the same name when you create your VM.
Initial configuration information is need to boot a new VM with the VDI file. To create the iso file you'll need two files called meta-data
and user-data
. These are compiled into the iso file.
Inside the amzn2-instance
directory create a directory called config
and start two files meta-data
and user-data
.
Here is a sample meta-data
file.
local-hostname: amzn2.instance.onprem
# ----------------------------------------------------------
# eth0 is the default network interface enabled in the image
# ----------------------------------------------------------
# The following section is for a dynamic IP using DHCP
#
# ----------------------------------------------------------
network-interfaces: |
auto eth0
iface eth0 inet dhcp
# ----------------------------------------------------------
# The following section is an exampl if you want to assign
# a static IP address. Comment the above example and
# uncomment this section to enable.
# NOTE: address in this example to 192.168.1.8
# ----------------------------------------------------------
# network-interfaces: |
# auto eth0
# iface eth0 inet static
# address 192.168.1.8
# network 192.168.1.0
# netmask 255.255.255.0
# broadcast 192.168.1.255
# gateway 192.168.1.254
Here is a sample `user-data file.
#cloud-config
# vim:syntax=yaml
users:
default
chpasswd:
list: |
ec2-user:password
# ssh_authorized_keys:
# - <enter your public key here
Points to note. The meta-data
file shown defaults to dynamic IP assignment via DHCP. The commented out section shows how to assign a static IP.
The shown user-data
file assumes you want a password for your ec2-user of password
. You can change this. Also, if you have a public key you can use the ssh_authorized_keys
section to set that thereby enabling password-less ssh access.
The boot image iso file is created by a special program. On the Mac install hdiutil
. For Linux, install genisoimage
.
Assuming you have your data files in a config directory, you can use the following command to create the init.iso
file.
hdiutil makehybrid -o init.iso -hfs -joliet -iso -default-volume-name cidata config
Each VDI has a unique identifier. If you make a copy of a VDI to create a second VM you'll get an error like the following:
Cannot register the hard disk '/Users/blucas/work/amzn2-virtualbox/amzn2-instance/amzn2-virtualbox-2.0.20181024-x86_64.xfs.gpt.vdi' {10794df2-3ada-4732-a4dd-48d9e4bd81ea}
because a hard disk '/Users/blucas/work/amzn2-virtualbox/amzn2-instance/amzn2-virtualbox-2.0.20181024-x86_64.xfs.gpt.vdi'
with UUID {10794df2-3ada-4732-a4dd-48d9e4bd81ea} already exists.
To work around this you'll need to stamp the copy with another UUID.
vboxmanage internalcommands sethduuid <filepath>
For example:
Brads-MBP.home:~/work/amzn2-virtualbox/amzn2-instance$ vboxmanage internalcommands sethduuid amzn2-virtualbox-2.0.20181024-x86_64.xfs.gpt.vdi
UUID changed to: 50700f9b-d9fa-443d-bfed-46517c445321
A sample script to build or rebuild your VDI is:
#!/bin/bash
rm -f init.iso
# For a non-MAC use the application `genisoimage`
hdiutil makehybrid -o init.iso -hfs -joliet -iso -default-volume-name cidata config
cp ../BACKUP/amzn2-virtualbox-2.0.20181024-x86_64.xfs.gpt.vdi .
vboxmanage internalcommands sethduuid amzn2-virtualbox-2.0.20181024-x86_64.xfs.gpt.vdi
Select the Virtual Machine in the left pan and then click on Settings
In this section we are going to attach the ISO file created above so it will be read when the VM starts
Set the Network to use the Bridged Adapter
.