Gifted my wife an AWS EC2 Automation Script
Tags: aws, cloud, beginners, tutorial
Published: 2023-01-03 15:12:13

<!-- wp:paragraph -->
<p>For this new year, I gifted my wife an AWS EC2 automation script. Read on to know more about this in this blog about our problem statement and its simple solution.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Problem Statement</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We have one of the national bank accounts in India to do financial transactions every month. Recently, we couldn't log in to the account as it is blocking foreign IPs. I tried it from a couple of VPNs to access it, but no luck. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Solution</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Then, I spun an EC2 instance Windows instance in the ap-south-1 region. It worked. For every month, I manually log in into my AWS console to spin up an instance, then I provide her with the credentials. This has continued for the last couple of months. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Better Solution</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Got bored with the manual steps. Enter automation. I could have done this with Terraform or Ansible, but it would be too techie for my wife. Hence, the bash. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Prerequisites</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The following are the prerequisites to implement this simple solution. Initially, I had to set this up on her laptop. </p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>AWS Account</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>AWS CLI</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>IAM Role to create, describe, and terminate EC2</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>PEM file to decrypt password</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>As I mentioned above, you would need to have an AWS account with the right IAM role and the key pair. Also, you need to have AWS CLI configured with the right API Access Key and Secret.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>How to?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Once the setup is done, follow the steps below to create an EC2 instance.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Clone <a href="https://github.com/QAInsights/aws-ec2-windows" target="_blank" rel="noreferrer noopener">this repo</a>&nbsp;<code>git clone https://github.com/QAInsights/aws-ec2-windows.git</code></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>cd aws-ec2-windows</code></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Run the below commands</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">chmod +x spin-ec2-windows.sh terminate-ec2.sh</pre>
<!-- /wp:preformatted -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>To spin up an EC2 instance</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">./spin-ec2-windows.sh</pre>
<!-- /wp:preformatted -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>After a while, it will display the credentials as shown below</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"align":"center","id":9848,"sizeSlug":"full","linkDestination":"media"} -->
<figure class="wp-block-image aligncenter size-full"><a href="https://qainsights.com/wp-content/uploads/2023/01/image.png"><img src="https://qainsights.com/wp-content/uploads/2023/01/image.png" alt="Gifted my wife an AWS EC2 Automation Script" class="wp-image-9848"/></a><figcaption class="wp-element-caption">Gifted my wife an AWS EC2 Automation Script</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>To terminate this spanned instance, run the below command.<br><code>./terminate-ec2.sh</code></p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":9849,"sizeSlug":"full","linkDestination":"media"} -->
<figure class="wp-block-image aligncenter size-full"><a href="https://qainsights.com/wp-content/uploads/2023/01/image-1.png"><img src="https://qainsights.com/wp-content/uploads/2023/01/image-1.png" alt="Terminate EC2" class="wp-image-9849"/></a><figcaption class="wp-element-caption">Terminate EC2</figcaption></figure>
<!-- /wp:image -->

<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"backgroundColor":"accent","textColor":"contrast","align":"center"} -->
<div class="wp-block-button aligncenter"><a class="wp-block-button__link has-contrast-color has-accent-background-color has-text-color has-background wp-element-button" href="https://github.com/QAInsights/aws-ec2-windows" target="_blank" rel="noreferrer noopener">GitHub Repo</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->

<!-- wp:heading -->
<h2>Configuration</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Once the repo is cloned, you can configure the below parameters in <code>spin-ec2-windows.sh</code> </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Importantly, change the <code>name</code>, <code>pem_location</code>, <code>key_name</code>, <code>security_group</code>, <code>image_id</code>, and <code>region</code> parameters. Running with the default parameters will fail to create an instance. </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>region="ap-south-1" #mumbai
pem_location="/mnt/c/aws/"
image_id="ami-0de07eb85c12b8dbb"
instance_type="t2.medium"
key_name="win11-mum"
count=1
security_group="sg-43ec5f28"
name="Your_Name"
windows_username="Administrator"</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>While creating the instance, the script will display random love quotes from https://api.quotable.io/random?tags=love</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you want to disable this, comment out the function <code>get_random_quote</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Final Thoughts</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>My wife loves this and makes it easy for her so that she is not dependent on me. The above script can be improved further by installing dependencies on its own. But I do not want to overcomplicate things. Hope this helps someone. If you have any comments, please let me know. </p>
<!-- /wp:paragraph -->