<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Drorit Zilberberg</title>
    <description>The latest articles on Forem by Drorit Zilberberg (@droritzz).</description>
    <link>https://forem.com/droritzz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F830993%2F4eaa2d20-b09d-4566-88de-b943e6ff3ea2.jpeg</url>
      <title>Forem: Drorit Zilberberg</title>
      <link>https://forem.com/droritzz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/droritzz"/>
    <language>en</language>
    <item>
      <title>From ISO to AMI - how to create your own custom AMI?</title>
      <dc:creator>Drorit Zilberberg</dc:creator>
      <pubDate>Sun, 10 Apr 2022 11:23:35 +0000</pubDate>
      <link>https://forem.com/otomato_io/from-iso-to-ami-how-to-create-your-own-custom-ami-5213</link>
      <guid>https://forem.com/otomato_io/from-iso-to-ami-how-to-create-your-own-custom-ami-5213</guid>
      <description>&lt;h3&gt;
  
  
  Why do we need custom AMI?
&lt;/h3&gt;

&lt;p&gt;While it might be very simple to get an instance up and running in AWS, this instance might be not exactly what you have been looking for. Some files are already customized, several packages that you do not either want or need are installed and in some cases you'll want a clean kernel and not the &lt;a href="https://packages.ubuntu.com/search?keywords=linux-aws" rel="noopener noreferrer"&gt;AWS-provided version&lt;/a&gt; that is compatible with  specific cloud tools only. So applying anything related to package updates on any other platform besides AWS will be like trying to start someone's car with your own key - it has the same functionality, but you cannot communicate with it.&lt;/p&gt;

&lt;p&gt;You can always buy an AMI that someone prepared for such purposes. For those of us who like to do it the hard way and break their machine in the process there is an alternative way - create your own AMI from scratch.&lt;/p&gt;

&lt;p&gt;After spending some time googling this particular task I sadly have found out that most of the tutorials are just copy-paste and do not lead to the desired goal. This is one that works. It might not be the best way or the shortest way, but it works. &lt;/p&gt;

&lt;h3&gt;
  
  
  What do we need?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ISO&lt;/strong&gt; of the distribution of your choice - I used Ubuntu 20.04 server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Virtual Box&lt;/strong&gt; - there are many options to create a VM, chose your weapon according to your needs. I found Oracle VirtualBox to be the most suited for this task, I used 6.1.30 version.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AWS account&lt;/strong&gt; - you will need some extended privileges to create buckets, roles and policies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configured &lt;code&gt;aws cli&lt;/code&gt;&lt;/strong&gt; - if you have never used this tool, you can look up how to download it according to your distribution from &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html" rel="noopener noreferrer"&gt;this link&lt;/a&gt;. In order to use it you will need to create a pair of the &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/getting-started-prereqs.html#getting-started-prereqs-keys" rel="noopener noreferrer"&gt;access key ID and secret access key&lt;/a&gt;. Once you have it, run &lt;code&gt;aws configure&lt;/code&gt; from your terminal and add your key ID and secret access key. The prompted region should match your working AWS region.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to do it?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 1 - create a machine&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Download your favorite flavor of the ISO.&lt;/li&gt;
&lt;li&gt;Create a new machine in the Virtual Box and attach the ISO to it. Match the settings to your needs, my goal was to merely download and pack latest OS security patches, so the basic CPU and RAM did the job.&lt;/li&gt;
&lt;li&gt;Run the machine, install the distribution and set username and password. You will need them later!&lt;/li&gt;
&lt;li&gt;Either during the installation or afterwards install the &lt;code&gt;openssh&lt;/code&gt; package and enable the service.&lt;/li&gt;
&lt;li&gt;If you want to connect to your future instance with a specific SSH key, import the public SSH key to the dedicated directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

cd
mkdir .ssh
touch ./ssh/authorized_keys


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Paste you public SSH key in the &lt;code&gt;authorized_keys&lt;/code&gt; file. Otherwise you will be able to connect to the instance only with the username and the password you have created for the VM.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stop the machine and convert it to OVA format.

&lt;ul&gt;
&lt;li&gt; click on file --&amp;gt; export appliance --&amp;gt; chose your machine to export 
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtxn2mfi6ku52p64ipsw.png" alt="chose your machine to export"&gt;
chose the destination to save OVA file 
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ltoseqwwy7mfztyp8ox.png" alt="chose the destination to save OVA file"&gt;
export
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vmc9ov46s600xkeu5bt.png" alt="export"&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Step 2 - prepare AWS resources&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Go to your S3 storage and create a bucket and upload your OVA.&lt;/li&gt;
&lt;li&gt;Create a local directory with following files.
&lt;code&gt;containers.json&lt;/code&gt;, &lt;code&gt;role-policy.json&lt;/code&gt;, &lt;code&gt;trust-policy.json&lt;/code&gt;.
These files will define a role, a policy and parameters for your AMI conversion.&lt;/li&gt;
&lt;li&gt;First setup a IAM role that will execute the conversion - vmimport. Edit &lt;code&gt;trust-policy.json&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
   "Version": "2012-10-17",
   "Statement": [
      {
         "Effect": "Allow",
         "Principal": { "Service": "vmie.amazonaws.com" },
         "Action": "sts:AssumeRole",
         "Condition": {
            "StringEquals":{
               "sts:Externalid": "vmimport"
            }
         }
      }
   ]
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Then setup the policy for the role to use in the conversion process. Edit &lt;code&gt;role-policy.json&lt;/code&gt;, insert the name of your bucket, where the OVA is stored.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect": "Allow",
         "Action": [
            "s3:GetBucketLocation",
            "s3:GetObject",
            "s3:ListBucket"
         ],
         "Resource": [
            "arn:aws:s3:::YOUR BUCKET",
            "arn:aws:s3:::YOUR BUCKET/*"
         ]
      },
      {
         "Effect": "Allow",
         "Action": [
            "s3:GetBucketLocation",
            "s3:GetObject",
            "s3:ListBucket",
            "s3:PutObject",
            "s3:GetBucketAcl"
         ],
         "Resource": [
            "arn:aws:s3:::YOUR BUCKET",
            "arn:aws:s3:::YOUR BUCKET/*"
         ]
      },
      {
         "Effect": "Allow",
         "Action": [
            "ec2:ModifySnapshotAttribute",
            "ec2:CopySnapshot",
            "ec2:RegisterImage",
            "ec2:Describe*"
         ],
         "Resource": "*"
      }
   ]
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Now create the role. From your terminal run: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;aws iam create-role --role-name vmimport --assume-role-policy-document "file://path/to/trust-policy.json"&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Afterwards attach the policy to it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;aws iam put-role-policy --role-name vmimport --policy-name vmimport --policy-document "file://path/to/role-policy.json"&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The last file you edit will reference the format of the image and the bucket.&lt;code&gt;containers.json&lt;/code&gt; references the command that will read from it and just contains all the information about your image - format, number of disks, bucket and the name (S3Key) of the image:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

[
    {
     "Description":"vm import",
     "Format":"ova",
     "UserBucket": {
        "S3Bucket": "YOUR BUCKET",
        "S3Key": "NAME OF YOUR IMAGE.ova"
}
}]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Start the process of conversion. Run this command from your terminal:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;aws ec2 import-image --description "YOUR DESCRIPTION" --disk-containers "file://path/to/containers.json".&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;An output of the command will provide you with ami number, use it to check the process:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws ec2 describe-import-image-tasks --import-task-ids import-ami-12345678&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the process is completed your ami will be uploaded directly to your AMI directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Troubleshooting and aftermath
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Although it is possible to use another format of the images (vmdk, vhd), however I had no success with them. The format is important. There is a &lt;a href="https://docs.aws.amazon.com/vm-import/latest/userguide/vm-import-ug.pdf" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; for it, it might work for you.&lt;/li&gt;
&lt;li&gt;Another issue still needs checking - does it work for the distributions that are not supported by AWS by default.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy clouding!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>iso</category>
      <category>ova</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
