In this mail we will testify a style to pass an AWS EC2 AMI to vmdk format to be able to use it in VirtualBox and Vagrant, in this tutorial nosotros used an Amazon Machine Prototype created by the community with Debian Jessie as an example. Yet, when information technology comes to exporting AMI's at that place are limitations y'all tin can detect them in the following Link. likewise, it is not guaranteed that this method works with other Os tools or versions different from those mentioned here, withal, in the aforementioned fashion, maybe if it works as information technology is or implementing some adaptations.

Requirements

  • Vagrant
  • VirtualBox
  • VirtualBox guest additions
  • Recommended same Debian version installation disk (net install) every bit the exported OS epitome

Process

In first step we need to create an S3 bucket or we can use an existing one, you tin can create an Amazon S3 bucket for storing the exported instances and grant VM Import/Export permission to access the bucket. If you already accept a bucket and want to apply it, you can use it instead of creating a bucket.

(Optional) create an S3 bucket

  1. Open the Amazon S3 console at https://panel.aws.amazon.com/s3/.

Creation bucket

  1. Choose Create Bucket.
  2. In the Create a Bucket dialog box, do the following:
  • For Saucepan Name, type a name for your bucket. This proper name must exist unique across all existing bucket names in Amazon S3. Don't employ capital, spaces, dashes or underscores.
  • For Region, select a region.
  • Cull Create. When the bucket is created, the details pane opens, then in the following options practise:
    • proper name and region --> put a name and region
    • configure options --> leave untouched
    • fix permissions --> leave selected all options
  • On the buckets list choose the bucket but created
  • In the popuped window select click on permissions
    • in the submenu options select Access Control List
    • and then in Admission for other AWS accounts select +Add together account
    • enter user vm-import-consign@amazon.com whit minimum options marked, List objects, write objects and read bucket permissions
    • click on relieve

Prerequisites to export a VM (instance)

To export a VM from Amazon EC2, install the AWS CLI on the instance, to achieve this follow the instructions in this Link. afterwards install it you demand to configure AWS with your credentials for example:

          $ aws configure AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY Default region name [None]: us-west-2 Default output format [None]: json                  

Consign the instance

To export your instance, use the create-instance-export-job control. You must provide the information required to properly export the case to the selected virtualization format. The exported file is saved in the S3 saucepan that you specify. As y'all can see in the next example:

$ aws ec2 create-example-export-task --instance-id id --target-surroundings target_enviroment --export-to-s3-task DiskImageFormat=disk_image_format,ContainerFormat=ova,S3Bucket=bucket,S3Prefix=prefix

where:

  • id = The ID of the instance.
  • target_environment = The target environs (vmware | citrix | microsoft)
  • disk_image_format = The disk epitome format (VMDK | VHD).
  • bucket = The name of the S3 bucket.
  • prefix = The image is written to an object in the S3 bucket using the post-obit S3 central: prefix/consign-i-xxxxxxxx.format (for case, my-exports/export-i-12345678.ova).

Monitor Instance Export

To monitor the exporting process of your example, use the following describe-export-tasks control:

          aws ec2 describe-export-tasks --export-task-ids export-i-12345678                  

When the export has been finished yous can open up your s3 bucket and if yous don't meet nothing don't forget to refresh the folio and you will run into a folder with containing the ova file from the contempo exporting EC2 AMI prototype, then y'all can select and download it to your local enviroment.

Prepare virtual disk image

Side by side we need to extract from ova file the virtual disk file (.vmdk) and mount the image in Virtualbox as a secondary HD with an OS guest with the aforementioned OS AMI version recommended, we tin can use the net install version since nosotros just need the repair selection to install the Grub (if necessary) and make lilliputian changes needed to kicking the system correctly with Vagrant.

First we need to create a new virtual machine in Virtualbox with follow characteristics:

Virtual machine creation menu

  • name: whatever you want
  • blazon: Linux
  • version: Debian

In retentivity size 2048 is recommended for this purpose.

Memory size

In the hard disk option, we need to choose "use an existing virtual hard disk file" option and open and navigate to path where we previously extracted virtual disk (.vmdk) from the ova file and use them, so push the create push button.

Existing virtual hard disk

Now we can see the new virtual machine in in the left pane, the next step is configure it, to do that select the VM and press the settings button, we need to modify the next options equally is described.

  • In the system tab we demand to deselect in the kick order the Floppy choice and exist sure the kicking order be like the adjacent image.

Boot order

  • In storage tab we need to add the installation deejay of the Debian OS system, click on the empty controller IDE and so click in the disc icon to open, search and select the iso prototype.

Attach SO

  • In the audio tab options, we demand to disable it.

Disable audio

  • In network tab we demand to confirm that the beginning adapter network is enabled and fastened to NAT.

NAT Network

That it'south all for the VM config now is time to run information technology so we start the machine and in the Options card from the Debian installation we cull the Advanced options > Graphical rescue mode.

Boot menu 1 Boot menu 2

Side by side we cull the basic config options like Language, location, Keyboard, hostname, domain and time zone. This is but important to this process since this is simply required to get to the adjacent footstep.

Adjacent we can select the rescue mode type and we chose the device to use equally root file then select /dev/sda1

Device root system

In the side by side step choose the rescue operations what would be Execute a shell in /dev/sda1

shell

In the shell we need to modify or add the password for the users root and admin that come by default in EC2 AMI with the passwd command for each user, for the user root, Vagrant recommends set the password "vagrant", for user admin you tin can put whatever you desire.

Change default loggin values

Next press go back button and in the File options for the VM select close option and power off the machine, afterwards that we need to disable the optical boot from the config options for the machine or unmount the attached iso installation image for the optical drive.

Then we need to kickoff the VM over again just now will boot from the virtual hard bulldoze, in these steps the first boot will take a long time (3 or 4 minutes) to kicking, that's because nosotros need to disable some options that the EC2 AMI of amazon has past default.

At present the VM is booted we demand to admission it with the user root and the laissez passer "vagrant" to exercise next modifications and changes to adjust information technology and can be functional with Vagrant.

TODO

  • (optional) change hostname
  1. Type the following command to edit /etc/hostname using nano or 6 text editor:

    $ sudo nano /etc/hostname

  2. Delete the old name and setup new proper name.

  3. Next Edit the /etc/hosts file:

    $ sudo nano /etc/hosts

  4. Supersede any occurrence of the existing computer proper noun with your new one, example: 127.0.ane.1 ip-154-32-ii… to 127.0.one.ane newhostname

  5. The changes have effect after reboot the organisation.

  • Uninstall cloud-init or disable it past changing the name of /usr/bin/cloud-init and cloud-init-cfg with different name, with this, we resolve the long boot time considering these programs trying to connect intranet Amazon addresses, to verify the higher up start run the command $ systemd-analyze blame and you will see something like next image.

systemd-analize blame results

After that you lot can continue with the method you prefer to disable cloud-init, if you choose uninstall it then execute the follow commands:

To just uninstall cloud-init sudo apt-get remove deject-init.

To Uninstall it and remove dependencies sudo apt-become remove --auto-remove cloud-init.

To purge your config/data sudo apt-get purge --auto-remove cloud-init.

Add together user and home path for Vagrant

Past default, Vagrant expects a "vagrant" user to SSH into the automobile every bit. This user should be setup with the insecure keypair that Vagrant uses as a default to attempt to SSH. Also, even though Vagrant uses cardinal-based authentication by default, it is a general convention to set up the countersign for the "vagrant" user to "vagrant". This lets people login as that user manually if they need to.

With the next command: $ sudo useradd -yard -s $(which fustigate) -G sudo vagrant will create a new user with a home dir, bash login, shell and the power to sudo

Then to create the password for the vagrant user practice passwd vagrant and enter vagrant every bit a new password.

(optional) Root Countersign: "vagrant"

Vagrant does not really utilize or expect any root password. Still, having a generally well-known root countersign makes it easier for the general public to modify the car if needed.

Publicly bachelor base boxes usually use a root password of "vagrant" to keep things easy.

To do that type $ passwd root and enter vagrant as a new password.

Password-less Sudo

This is important! Many aspects of Vagrant wait the default SSH user to have passwordless sudo configured. This lets Vagrant configure networks, mount synced folders, install software, and more.

To practice that you tin can configure it (usually using visudo) to allow passwordless sudo for the "vagrant" user. This can be done with the following line at the cease of the configuration file:
vagrant ALL=(ALL) NOPASSWD:ALL
Or add together file in path /etc/sudoers.d/vagrant with the aforementioned line before added.

Configure SSH

Vagrant's documentation suggests that you lot change the SSH configuration to avoid contrary DNS lookups. To do this, open /etc/ssh/sshd_config, notice the line which reads #UseDNS yes and change information technology to UseDNS no, if there are no line create at the end of file.

Then restart the SSH service with sudo service ssh restart later on that be sure the service is running with the command sudo service ssh status.

Enable access via non secure primal

To configure SSH access with the insecure keypair, place the public key into the ~/.ssh/authorized_keys file for the "vagrant" user.

To practise that open a login shell for the vagrant user (su - vagrant), download its public key and set the right permissions with next suggested steps:

          $ mkdir -p /domicile/vagrant/.ssh $ chmod 0700 /domicile/vagrant/.ssh $ wget https://raw.githubusercontent.com/mitchellh/vagrant/principal/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys $ chmod 0600 /abode/vagrant/.ssh/authorized_keys $ chown -R vagrant:vagrant /dwelling/vagrant/.ssh                  

Leave the vagrant'southward user shell

(optional) Let rootlogin only with central hallmark

Allows root login just with public key authentication. This is often used with shell scripts and automated tasks.

  1. Open /etc/ssh/sshd_config, uncomment and change the line #PermitRootLogin to PermitRootLogin prohibit-countersign

  2. Next restart ssh service: service ssh restart

(optional) Update and upgrade

It is recommended to update and upgrading before packaging the box

          $ sudo apt update --yes $ sudo apt upgrade --yes                  

Install VBoxLinuxAdditions

Yous'll want to reboot the machine now as probably installs a new kernel and nosotros want to compile VirtualBox' guest additions with the newest kernel available.

VirtualBox Guest Additions must be installed so that things such every bit shared folders can part. Installing invitee additions likewise usually improves functioning since the invitee Bone tin brand some optimizations by knowing it is running within VirtualBox.

Earlier installing the guest additions, you lot will need the linux kernel headers and the basic developer tools. On Ubuntu, you can easily install these like so:

          $ sudo apt-become install linux-headers-$(uname -r) build-essential dkms gcc brand perl                  

To install via the command line, you tin find the appropriate guest additions version to match your VirtualBox version past selecting the appropriate version here. The examples below apply 5.2.0, which was the latest stable VirtualBox version at the time of writing.

          $ wget http://download.virtualbox.org/virtualbox/5.2.0_RC1/VBoxGuestAdditions_5.two.0_RC1.iso $ sudo mkdir /media/VBoxGuestAdditions $ sudo mountain -o loop,ro VBoxGuestAdditions_5.2.0_RC1.iso /media/VBoxGuestAdditions $ sudo sh /media/VBoxGuestAdditions/VBoxLinuxAdditions.run $ rm VBoxGuestAdditions_5.two.0_RC1.iso $ sudo umount /media/VBoxGuestAdditions $ sudo rmdir /media/VBoxGuestAdditions                  

Parcel the box

We have almost created the Vagrant box. Now we just have to create a parcel.box file. We can do it with the help of vagrant parcel command. In your host estimator create a binder where volition exist save the package.box file.

          $ mkdir ~/foldername $ cd ~/foldername $ vagrant package --base <proper noun-of-VM-in-GUI>                  

Later that y'all wil have a package.box file in your actual folder.

Use this box

Y'all can now import this base box into Vagrant with the command:

          $ vagrant box add proper name bundle.box                  

Bibliography

  • Downloading an Amazon EC2 AMI to local drive
  • Converting an EC2 AMI to a VirtualBox .vdi
  • Exporting an Example equally a VM Using VM Import/Export
  • systemd: Very slow boot fourth dimension (4 Minutes)
  • Creating a Base Box
  • Ubuntu Linux Change Hostname (calculator proper noun)
  • How to build a Vagrant base box from a VirtualBox VM
  • Edifice Custom Vagrant box
  • How should you develop your project.
  • AWS ECS - How to use containerized applications with EC2 and FARGATE.