Creating AMIs For AWS
Creating AMIs For AWS
Milind Nirgun
[email protected]
22 June 2018
Creating Virtualbox VM
● Hyper-V should be turned off on Windows 10 for installing 64 bit OSes. To do this, go to Windows 10
Settings -> Turn Windows Features On or Off and uncheck the Hyper-V checkbox and restart the
computer.
● Create a new virtual machine with 4GB RAM and 32GB virtual disk with default parameters. The 32GB
Installing the OS
Centos7
● Start the machine and in the installation screen TAB to the manual boot parameters to add
● Click on “Network & Host Name”; flip the switch for eth0 to ON;
○ Leave the hostname to localhost.localdomain - this gets changed by the cloud provider
○ On the General tab, check the “Automatically connect to this network when it is available” box.
○ Keep the default settings on the Ethernet, 802.1X, and DCB tabs.
○ On the IPV4 tab, leave the “Method” on “Automatic (DHCP)” and check the “Require IPv4
addressing for this connection to complete” box. In the field “Additional DNS Servers” add
“8.8.8.8 8.8.4.4”.
○ On the IPV6 tab, change “Method” to “Ignore” and click Save and then press Done.
1
● Click on the “Date & Time” and select the right Timezone. Make sure “Network Time” is ON. If there is a
yellow warning bar at the bottom stating “you have no network time server setup”, click the gears icon
next to the Network Time button to make sure NTP servers are configured.
○ Choose “I will configure partitioning”. Click Done to proceed with next steps.
○ Click on the “Click here to create them automatically” link to create the 3 default partitions
for /dev/sda1 -> /boot, /dev/sda3 -> / and /dev/sda2 -> swap.
● Click on Begin Installation and set root password. If password is weak, ignore warning by clicking on
Done twice.
● After installation is complete, reboot into the machine. For Oracle Linux, boot into the standard kernel
● Go to Machine in VBox console and under Network->NAT->Port Forwarding add a rule for Protocol:
TCP, Host Port: 6036 (any available port on host), Guest IP: 10.0.2.15 (or whatever is the assigned IP
variable.
● Any changes to grub will require a rebuild of grub with the command
Grub2-mkconfig -o /boot/grub2/grub.cfg
● Change /etc/dracut.conf to add XEN drivers to the initramfs image which are not installed in
Virtualbox. This step has to done or else the AMI will not boot in AWS. Uncomment the following line
2
# additional kernel modules to the default
add_drivers+="xen-blkfront xen-netfront"
○ Remove UEK kernels from boot options and uninstall them as AWS VMImport has problems
DEFAULTKERNEL=kernel-uek to DEFAULTKERNEL=kernel
● Rebuild the initramfs to add the XEN drivers for all installed kernels with the command:
rpm -qa kernel| sed 's/^kernel-//'|xargs -I {} dracut -f /boot/initramfs-{}.img {}
○ Disable ol7_UEKR3 and ol7_UEKR4 to prevent the UEK kernel being reinstalled with a yum
update
vi /etc/yum.repos.d/public-yum-ol7.repo
Note: after this step, booting into this VM is not possible. Therefore complete the rest of the steps to export
this VM as a OVA.
3
● Change SElinux to permissive
vi /etc/selinux/config
Change ssh_deletekeys=1
And under system_info:, change name=cloud_user to name=ec2_user
With these changes, with the deployment of a new instance, new ssh keys for the server are
generated, a new ec2_user with passwordless sudo to root is created and ssh to root is disabled as well
● Cleaning up the VM for AMI - delete log files, command history etc.
rm -rf /var/lib/cloud/
yum -y clean packages
rm -rf /var/cache/yum
rm -rf /var/lib/yum
cd /var/log
rm -rf cloud-init.log cloud-init-output.log messages boot.log dmesg dmesg.old yum.log
wtmp lastlog
history -c
shutdown -h now
I've been struggling with this as well and finally got it to work. Here's something that may help
understanding what's happening, and what may go wrong:
Apparently the import/export task runs by some sort of AWS-owned agent ("service"). This agent is
called vmie.amazonaws.com. As soon as that agent starts running, it assumes a role that you have to
define yourself. If you don't supply a role name (aws ec2 import-image --role-name something) the
role "vmimport" will be used by default.
This role, "vmimport" or otherwise, needs two important properties. First, there needs to be a trust
relationship so that the Amazon agent vmie.amazonaws.com can assume this role. Second, there
needs to be a policy that gives access to both the S3 bucket and EC2 to perform the import.
4
The first, the trust relationship, is achieved as follows: Within IAM, go to "Roles" and select the role
you want to use for this (or create a role "vmimport"). Go to Trust Relationships; Edit Trust
Relationships. The policy document should look like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
},
{
"Effect": "Allow",
"Principal": {
"Service": "vmie.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Second, the policy. Easiest is to attach the following policies to this role:
● AmazonEC2FullAccess
●
● AmazonS3FullAccess
●
Obviously you can define custom policies as well, that only allow access to the S3 bucket where your
to-be-imported images are stored, and limit access to EC2 as well.
Next Steps
● Install the AWS command line client and configure it using the following steps:
Copy the exported OVA file to the bucket - Use console upload from the S3 bucket
Configure the role vmimport and assign trust policy. Files: trust-policy.json, role-policy.json
and a container.json
5
Create a new role, add policies AmazonEC2FullAccess and AmazonS3FullAccess, edit
Use vmimport
Enable firewall
References:
https://technology.amis.nl/2017/07/14/aws-build-oracle-linux-7-ami-cloud/
https://docs.aws.amazon.com/cli/latest/userguide/installing.html
https://forums.aws.amazon.com/thread.jspa?threadID=179090
https://docs.aws.amazon.com/vm-import/latest/userguide/vmimport-image-
import.html#import-vm-image