<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abhishek Pawar on Medium]]></title>
        <description><![CDATA[Stories by Abhishek Pawar on Medium]]></description>
        <link>https://medium.com/@pawarabhishek.26?source=rss-14ba5acaee87------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nycMp8o4QHXq4VZinYBwZg.png</url>
            <title>Stories by Abhishek Pawar on Medium</title>
            <link>https://medium.com/@pawarabhishek.26?source=rss-14ba5acaee87------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 19:51:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pawarabhishek.26/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Create a simple Ubuntu AMI with Custom User Credentials using Packer]]></title>
            <link>https://medium.com/@pawarabhishek.26/create-a-simple-ubuntu-ami-with-custom-user-credentials-using-packer-e99e21b9690e?source=rss-14ba5acaee87------2</link>
            <guid isPermaLink="false">https://medium.com/p/e99e21b9690e</guid>
            <category><![CDATA[hashicorp-packer]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[custom-user-credentials]]></category>
            <category><![CDATA[aws-ami]]></category>
            <dc:creator><![CDATA[Abhishek Pawar]]></dc:creator>
            <pubDate>Sun, 19 Sep 2021 08:42:27 GMT</pubDate>
            <atom:updated>2021-09-19T11:19:25.262Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*uB59Jru9JNwlzeqf.png" /></figure><h3>Introduction</h3><p>Creation of custom AMIs is generally considered a best practice. <strong>A</strong>mazon<strong> M</strong>achine <strong>I</strong>mage(AMI) is a template that is required to launch AWS EC2 instances. The benefits of AMI are that by using one single AMI many instances can be launched and that too with the mentioned custom configuration and utilities required by the developers to support the application to be deployed on instances, thus saving time to perform repetitive tasks in an automated manner.</p><p>Here we will see how to use Hashicorp’s PACKER tool to create an AMI easily and configure a custom user with a password instead of the default ubuntu user which is password-less. For configuring a custom user with the password we will use the properties from the cloud-init definition file which will be used by EC2 at each boot time.</p><h3>Installing PACKER</h3><pre>curl -fsSL <a href="https://apt.releases.hashicorp.com/gpg">https://apt.releases.hashicorp.com/gpg</a> | sudo apt-key add -</pre><pre>sudo apt-add-repository &quot;deb [arch=amd64] <a href="https://apt.releases.hashicorp.com/">https://apt.releases.hashicorp.com</a> $(lsb_release -cs) main&quot;</pre><pre>sudo apt-get update &amp;&amp; sudo apt-get install packer</pre><pre># Verify the installation<br>packer --version</pre><h3>Packer Template</h3><p>Let us create a template file for packer which will act as a configuration file for providing instructions on what image we want to build. Create a file named image.json and paste the following content into it</p><pre>{<br>   &quot;variables&quot;:{<br>      &quot;aws_access_key&quot;:&quot;&quot;,<br>      &quot;aws_secret_key&quot;:&quot;&quot;,<br>      &quot;aws_region&quot;:&quot;ap-south-1&quot;<br>   },<br>   &quot;builders&quot;:[<br>      {<br>         &quot;type&quot;:&quot;amazon-ebs&quot;,<br>         &quot;access_key&quot;:&quot;{{user `aws_access_key`}}&quot;,<br>         &quot;secret_key&quot;:&quot;{{user `aws_secret_key`}}&quot;,<br>         &quot;region&quot;:&quot;{{user `aws_region`}}&quot;,<br>         &quot;source_ami_filter&quot;:{<br>            &quot;filters&quot;:{<br>               &quot;virtualization-type&quot;:&quot;hvm&quot;,<br>               &quot;name&quot;:&quot;ubuntu/images/*ubuntu-bionic-18.04-amd64-server-*&quot;,<br>               &quot;root-device-type&quot;:&quot;ebs&quot;<br>            },<br>            &quot;owners&quot;:[<br>               &quot;099720109477&quot;<br>            ],<br>            &quot;most_recent&quot;:true<br>         },<br>         &quot;instance_type&quot;:&quot;t2.micro&quot;,<br>         &quot;ssh_username&quot;:&quot;myuser&quot;,<br>         &quot;ami_name&quot;:&quot;custom-base-ami-{{timestamp}}&quot;,<br>         &quot;user_data_file&quot;:&quot;./defaults.cfg&quot;<br>      }<br>   ],<br>   &quot;provisioners&quot;:[<br>      {<br>         &quot;type&quot;:&quot;file&quot;,<br>         &quot;source&quot;:&quot;./defaults.cfg&quot;,<br>         &quot;destination&quot;:&quot;/tmp/defaults.cfg&quot;<br>      },<br>      {<br>         &quot;type&quot;:&quot;shell&quot;,<br>         &quot;inline&quot;:[<br>            &quot;sudo mv /tmp/defaults.cfg /etc/cloud/cloud.cfg.d/defaults.cfg&quot;<br>         ]<br>      },<br>      {<br>         &quot;type&quot;:&quot;shell&quot;,<br>         &quot;script&quot;:&quot;./setup.sh&quot;<br>      }<br>   ]<br>}</pre><p>Variables Section</p><ul><li>Access Key &amp; Secret Key : Pass the access key and secret access key to the Packer tool which will be used to communicate with the AWS account and to create resources in the account. Ensure that the keys have EC2FullAccess permission</li><li>Region : Region in which AMI is to be created</li></ul><p>Builders Section</p><ul><li>Launches a t2.micro instance with Ubuntu 18.04 latest image as the AMI</li><li>Assigns SSH username as <strong>myuser</strong></li><li>AMI will be created with name in the format custom-base-ami-{{timestamp}} , for e.g custom-base-ami-1610896342</li><li>We will pass a defaults.cfg file which will be used as User data file at the time of instance launch</li></ul><p>Provisioners Section</p><ul><li>Copy the defaults.cfg file from local to tmp location and move it to the cloud init location /etc/cloud/cloud.cfg.d/ directory</li><li>Run setup.sh script to install required additional utilities/packages</li></ul><h3>Cloud Init File</h3><p>Create a deafults.cfg file with the following content</p><pre>#cloud-config<br>system_info:<br>  default_user:<br>    name: myuser<br>    plain_text_passwd: &#39;myuser2021&#39;<br>    lock_passwd: false</pre><ul><li>The #cloud-config line at the top is required in order to identify the commands as cloud-init directives</li><li>Assign value to plain_text_passwd which is set as the user’s password</li><li>Set lock_passwd to false to enable password login</li><li>Many other properties in <a href="https://cloudinit.readthedocs.io/en/latest/topics/examples.html">cloud-init</a> file can be set as per requirement</li></ul><h3>Setup Script</h3><p>Now create a setup.sh and paste the following contents into it. These commands install additional utilities which you can change as per your requirement and also add <strong>myuser</strong> to the list of sudo users</p><pre>#!/bin/bash<br>set -x</pre><pre># Install necessary dependencies<br>sudo apt-get -y -qq install curl wget git vim apt-transport-https ca-certificates</pre><pre># Create an user group<br>sudo groupadd -r mygroup</pre><pre># Add myuser to mygroup<br>sudo usermod -a -G mygroup myuser<br> <br># Grant privileges to myuser to run any command on the system <br>echo &quot;myuser  ALL=(ALL:ALL) ALL&quot; | sudo tee /etc/sudoers.d/myuser</pre><h3>Final Step : Create the AMI</h3><ul><li>packer validate image.json : Parses the template and checks if the configuration is valid</li><li>packer build -var &#39;access_key=XXXXXX&#39; -var &#39;secret_key=XXXXXX&#39; image.json : Builds an image using the configuration in the template &amp; the environment variables passed in the command</li></ul><p>You can now go and check in the AWS EC2 console in AMI’s section that a new AMI is created with name <strong>custom-base-ami-{{timestamp}}</strong>. This AMI can now be used for creating a new instance now onwards which will have your custom user as the root user.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e99e21b9690e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>