<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Varun Chandak</title>
  
  <subtitle>Notes on cloud, Linux, security, and automation</subtitle>
  <link href="/atom.xml" rel="self"/>
  
  <link href="https://blog.vrnchndk.in/"/>
  <updated>2024-03-02T07:14:38.000Z</updated>
  <id>https://blog.vrnchndk.in/</id>
  
  <author>
    <name>Varun Chandak</name>
    
  </author>
  
  <generator uri="http://hexo.io/">Hexo</generator>
  
  <entry>
    <title>Automatically Organize Your Screenshots on Mac with launchd</title>
    <link href="https://blog.vrnchndk.in/2024/03/02/Automatically-Organize-Your-Screenshots-on-Mac-with-launchd/"/>
    <id>https://blog.vrnchndk.in/2024/03/02/Automatically-Organize-Your-Screenshots-on-Mac-with-launchd/</id>
    <published>2024-03-02T07:14:38.000Z</published>
    <updated>2024-03-02T07:14:38.000Z</updated>
    
    <content type="html"><![CDATA[<p>If you&#x2019;re anything like me, you take a lot of screenshots for various purposes&#x2014;be it work, personal projects, or just capturing something interesting on your screen. Over time, however, these screenshots can clutter your desktop or screenshots folder, making it hard to find what you need. I use Shottr, a fantastic screenshot tool for Mac (find them on Twitter), which makes taking screenshots a breeze. But, like many, I needed a solution to keep these screenshots organized without manually sorting them into folders.</p><p>Today, I&#x2019;ll show you how to automate organizing your screenshots into folders by their creation dates using a simple script and <code>launchd</code>, macOS&#x2019;s built-in scheduler. This method is not only efficient but also runs quietly in the background, keeping your folders tidy with zero effort on your part.</p><hr><h2 id="Step-1-The-Cleanup-Script"><a href="#Step-1-The-Cleanup-Script" class="headerlink" title="Step 1: The Cleanup Script"></a>Step 1: The Cleanup Script</h2><p>First, we need to create a script that does the heavy lifting&#x2014;moving screenshots into year-month formatted folders. Open your favorite text editor and save the following script as <code>screenshots-cleanup.sh</code> in a convenient location (e.g., your home directory):</p><figure class="highlight zsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/zsh</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Define the path to your screenshots folder</span></span><br><span class="line">SCREENSHOTS_DIR=<span class="string">&quot;<span class="variable">$HOME</span>/Desktop/Screenshots&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">cd</span> <span class="string">&quot;<span class="variable">$SCREENSHOTS_DIR</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> file <span class="keyword">in</span> *; <span class="keyword">do</span></span><br><span class="line">    <span class="comment"># Skip directories</span></span><br><span class="line">    <span class="keyword">if</span> [[ -d <span class="string">&quot;<span class="variable">$file</span>&quot;</span> ]]; <span class="keyword">then</span></span><br><span class="line">        <span class="built_in">continue</span></span><br><span class="line">    <span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># Extract year and month from the file&apos;s modification time</span></span><br><span class="line">    year_month=$(<span class="built_in">stat</span> -f <span class="string">&quot;%Sm&quot;</span> -t <span class="string">&quot;%Y-%m&quot;</span> <span class="string">&quot;<span class="variable">$file</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># Create a directory for the year and month if it doesn&apos;t exist</span></span><br><span class="line">    <span class="keyword">if</span> [[ ! -d <span class="string">&quot;<span class="variable">$year_month</span>&quot;</span> ]]; <span class="keyword">then</span></span><br><span class="line">        mkdir <span class="string">&quot;<span class="variable">$year_month</span>&quot;</span></span><br><span class="line">    <span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># Move the file into the corresponding directory</span></span><br><span class="line">    mv <span class="string">&quot;<span class="variable">$file</span>&quot;</span> <span class="string">&quot;<span class="variable">$year_month</span>/&quot;</span></span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure><p><em>Make sure to replace <code>&quot;$HOME/Desktop/Screenshots&quot;</code> with the path to your actual screenshots folder. If you&#x2019;re using Shottr, you can configure its preferences to save screenshots to a specific folder.</em></p><h3 id="Make-the-Script-Executable"><a href="#Make-the-Script-Executable" class="headerlink" title="Make the Script Executable"></a>Make the Script Executable</h3><p>Run the following command in your terminal to make the script executable:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">chmod +x /path/to/screenshots-cleanup.sh</span><br></pre></td></tr></table></figure><h2 id="Step-2-Setting-Up-launchd"><a href="#Step-2-Setting-Up-launchd" class="headerlink" title="Step 2: Setting Up launchd"></a>Step 2: Setting Up <code>launchd</code></h2><p>Next, we&#x2019;ll use <code>launchd</code> to schedule this script to run automatically. Create a <code>.plist</code> file named <code>com.user.organizescreenshots.plist</code> in <code>~/Library/LaunchAgents</code> with the following content:</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line"><span class="meta">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">plist</span> <span class="attr">version</span>=<span class="string">&quot;1.0&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dict</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">key</span>&gt;</span>Label<span class="tag">&lt;/<span class="name">key</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">string</span>&gt;</span>com.user.organizescreenshots<span class="tag">&lt;/<span class="name">string</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">key</span>&gt;</span>ProgramArguments<span class="tag">&lt;/<span class="name">key</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">array</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">string</span>&gt;</span>/path/to/screenshots-cleanup.sh<span class="tag">&lt;/<span class="name">string</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">array</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">key</span>&gt;</span>StartCalendarInterval<span class="tag">&lt;/<span class="name">key</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dict</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">key</span>&gt;</span>Weekday<span class="tag">&lt;/<span class="name">key</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">integer</span>&gt;</span>0<span class="tag">&lt;/<span class="name">integer</span>&gt;</span> <span class="comment">&lt;!-- 0 is Sunday --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">key</span>&gt;</span>Hour<span class="tag">&lt;/<span class="name">key</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">integer</span>&gt;</span>10<span class="tag">&lt;/<span class="name">integer</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">key</span>&gt;</span>Minute<span class="tag">&lt;/<span class="name">key</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">integer</span>&gt;</span>0<span class="tag">&lt;/<span class="name">integer</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dict</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dict</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">plist</span>&gt;</span></span><br></pre></td></tr></table></figure><p>This configuration will run the cleanup script every Sunday at 10 AM. You can adjust the <code>&lt;integer&gt;</code> values under <code>&lt;key&gt;StartCalendarInterval&lt;/key&gt;</code> to suit your preferred schedule.</p><h3 id="Load-the-Launch-Agent"><a href="#Load-the-Launch-Agent" class="headerlink" title="Load the Launch Agent"></a>Load the Launch Agent</h3><p>To start the scheduled task, run this command:<br><figure class="highlight zsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">launchctl load ~/Library/LaunchAgents/com.user.organizescreenshots.plist</span><br></pre></td></tr></table></figure></p><p>And that&#x2019;s it! Your Mac will now automatically organize your screenshots into neatly labeled folders each week, making it easier to find and manage them.</p><h2 id="Conclusion"><a href="#Conclusion" class="headerlink" title="Conclusion"></a>Conclusion</h2><p>With Shottr for capturing screenshots and this automated organizing system, maintaining a clutter-free screenshots folder has never been easier. This setup saves you time and keeps your digital workspace organized, allowing you to focus on more important tasks.</p><p>Feel free to share this guide with anyone who might find it useful, and happy organizing!<br><br><br><br></p>]]></content>
    
    <summary type="html">
    
      
      
        &lt;p&gt;If you&amp;#x2019;re anything like me, you take a lot of screenshots for various purposes&amp;#x2014;be it work, personal projects, or just captu
      
    
    </summary>
    
      <category term="Linux" scheme="https://blog.vrnchndk.in/categories/Linux/"/>
    
      <category term="Shell Scripting" scheme="https://blog.vrnchndk.in/categories/Linux/Shell-Scripting/"/>
    
      <category term="MacOS" scheme="https://blog.vrnchndk.in/categories/Linux/Shell-Scripting/MacOS/"/>
    
      <category term="Automation" scheme="https://blog.vrnchndk.in/categories/Linux/Shell-Scripting/MacOS/Automation/"/>
    
    
      <category term="MacOS" scheme="https://blog.vrnchndk.in/tags/MacOS/"/>
    
      <category term="Shottr" scheme="https://blog.vrnchndk.in/tags/Shottr/"/>
    
      <category term="Screenshots" scheme="https://blog.vrnchndk.in/tags/Screenshots/"/>
    
      <category term="Launchd" scheme="https://blog.vrnchndk.in/tags/Launchd/"/>
    
      <category term="Productivity" scheme="https://blog.vrnchndk.in/tags/Productivity/"/>
    
      <category term="Automation" scheme="https://blog.vrnchndk.in/tags/Automation/"/>
    
      <category term="Scripting" scheme="https://blog.vrnchndk.in/tags/Scripting/"/>
    
      <category term="Organization" scheme="https://blog.vrnchndk.in/tags/Organization/"/>
    
      <category term="Tech" scheme="https://blog.vrnchndk.in/tags/Tech/"/>
    
      <category term="Tips" scheme="https://blog.vrnchndk.in/tags/Tips/"/>
    
  </entry>
  
  <entry>
    <title>SAML2.0 and OAuth2.0 Simplified</title>
    <link href="https://blog.vrnchndk.in/2023/08/09/SAML2-0-and-OAuth2-0-Simplified/"/>
    <id>https://blog.vrnchndk.in/2023/08/09/SAML2-0-and-OAuth2-0-Simplified/</id>
    <published>2023-08-09T12:04:28.000Z</published>
    <updated>2023-08-09T12:04:28.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="What-is-SAML2-0"><a href="#What-is-SAML2-0" class="headerlink" title="What is SAML2.0?"></a>What is SAML2.0?</h1><p>Okay! Let&#x2019;s imagine you&#x2019;re going to a big amusement park. At the entrance, there&#x2019;s a ticket booth where you show your ID and get a special wristband. This wristband lets you go on all the rides without having to show your ID again and again. </p><p>In the digital world, we have something similar called SAML 2.0. Instead of a wristband, it gives you a special &#x201C;digital ticket&#x201D; after you prove who you are (like logging in). With this &#x201C;digital ticket&#x201D;, you can access different websites and services without having to log in over and over.</p><p>So, SAML 2.0 is like the system that gives you the wristband, making sure you have fun without the hassle of showing your ID every time you want to go on a ride! &#x1F3A2;&#x1F39F;&#xFE0F;</p><h1 id="Now-what-is-OAuth2-0"><a href="#Now-what-is-OAuth2-0" class="headerlink" title="Now, what is OAuth2.0?"></a>Now, what is OAuth2.0?</h1><p>Imagine you&#x2019;re at the same amusement park, but this time, instead of just rides, there are also special game booths. Now, instead of just a wristband, there&#x2019;s a new system. You go to a special booth (let&#x2019;s call it the &#x201C;Permission Booth&#x201D;) and show your ID. This booth doesn&#x2019;t just give you a wristband, but also a special &#x201C;key card&#x201D;.</p><p>Now, when you go to a game booth, instead of playing the game directly, you show them this key card. The game booth will quickly check with the Permission Booth to see if you&#x2019;re allowed to play. The Permission Booth says &#x201C;Yep, they&#x2019;re good!&#x201D; and you get to play the game.</p><p>With OAuth2 in the digital world, websites and apps can ask another trusted service (like the Permission Booth) if you have permission to use them. You only log in once at the trusted service, get your &#x201C;key card&#x201D;, and then you can use multiple apps and websites without logging in again, as long as they trust the Permission Booth.</p><p>So, OAuth2 is like the system that gives you a key card to easily play games at different booths without having to prove yourself again and again. &#x1F3AE;&#x1F511;</p><h1 id="What&#x2019;s-the-difference-between-SAML2-0-and-OAuth2-0-They-seem-similar"><a href="#What&#x2019;s-the-difference-between-SAML2-0-and-OAuth2-0-They-seem-similar" class="headerlink" title="What&#x2019;s the difference between SAML2.0 and OAuth2.0? They seem similar."></a>What&#x2019;s the difference between SAML2.0 and OAuth2.0? They seem similar.</h1><p>You&#x2019;re right; on the surface, SAML 2.0 and OAuth2.0 do seem similar because they both deal with authentication (proving who you are) and authorization (what you&#x2019;re allowed to do). However, there are some key differences:</p><ol class="ui list"><li><p><strong>Purpose</strong>:</p><ul><li><strong>SAML 2.0</strong>: It&#x2019;s mainly about authentication. It ensures that you are who you say you are. Once you&#x2019;re authenticated, you get a &#x201C;digital ticket&#x201D; (SAML assertion) that you can use to access other services.</li><li><strong>OAuth2.0</strong>: It&#x2019;s more about authorization. It allows one application to access certain parts of another application on your behalf. For example, allowing a photo printing service to access your photos on a cloud storage, without giving them your cloud storage password.</li></ul></li><li><p><strong>Format</strong>:</p><ul><li><strong>SAML 2.0</strong>: Uses XML format for its messages.</li><li><strong>OAuth2.0</strong>: Uses JSON format and is generally considered to be more lightweight.</li></ul></li><li><p><strong>Use Cases</strong>:</p><ul><li><strong>SAML 2.0</strong>: Often used for Single Sign-On (SSO) between businesses. It means you log in once and can access various services.</li><li><strong>OAuth2.0</strong>: Often used to give apps limited access to user data without exposing user credentials. Like when a mobile app asks for permission to access your camera or contacts.</li></ul></li><li><p><strong>Flow</strong>:</p><ul><li><strong>SAML 2.0</strong>: You authenticate with a central service (Identity Provider) and get a SAML assertion (like a digital ticket). You present this assertion to access other services.</li><li><strong>OAuth2.0</strong>: You authenticate with a central service (Authorization Server) and get an access token. You present this token to access resources or services. Sometimes, there&#x2019;s also a &#x201C;refresh token&#x201D; to get new access tokens without logging in again.</li></ul></li></ol><p>Going back to the amusement park analogy:</p><ul class="ui list"><li><strong>SAML 2.0</strong> is like getting a wristband after showing your ID. You can then access all the rides because they trust the wristband system.</li><li><strong>OAuth2.0</strong> is like getting a key card that can be used at game booths. Each booth might allow you to play different games based on what permissions your card has.</li></ul><p>In summary, while they both deal with identity and access, SAML is more about proving who you are across multiple services, while OAuth is about granting permissions to apps to do specific things on your behalf.</p>]]></content>
    
    <summary type="html">
    
      
      
        &lt;h1 id=&quot;What-is-SAML2-0&quot;&gt;&lt;a href=&quot;#What-is-SAML2-0&quot; class=&quot;headerlink&quot; title=&quot;What is SAML2.0?&quot;&gt;&lt;/a&gt;What is SAML2.0?&lt;/h1&gt;&lt;p&gt;Okay! Let&amp;#x2019
      
    
    </summary>
    
      <category term="Security" scheme="https://blog.vrnchndk.in/categories/Security/"/>
    
      <category term="Technology" scheme="https://blog.vrnchndk.in/categories/Security/Technology/"/>
    
    
      <category term="saml" scheme="https://blog.vrnchndk.in/tags/saml/"/>
    
      <category term="oauth" scheme="https://blog.vrnchndk.in/tags/oauth/"/>
    
      <category term="security" scheme="https://blog.vrnchndk.in/tags/security/"/>
    
      <category term="authentication" scheme="https://blog.vrnchndk.in/tags/authentication/"/>
    
      <category term="authorization" scheme="https://blog.vrnchndk.in/tags/authorization/"/>
    
      <category term="saml 2.0" scheme="https://blog.vrnchndk.in/tags/saml-2-0/"/>
    
      <category term="oauth2.0" scheme="https://blog.vrnchndk.in/tags/oauth2-0/"/>
    
      <category term="single sign-on (sso)" scheme="https://blog.vrnchndk.in/tags/single-sign-on-sso/"/>
    
      <category term="access token" scheme="https://blog.vrnchndk.in/tags/access-token/"/>
    
      <category term="identity provider (idp)" scheme="https://blog.vrnchndk.in/tags/identity-provider-idp/"/>
    
      <category term="token-based authentication" scheme="https://blog.vrnchndk.in/tags/token-based-authentication/"/>
    
  </entry>
  
  <entry>
    <title>Bypassing paywalled webpages</title>
    <link href="https://blog.vrnchndk.in/2023/04/08/Bypassing-paywalled-webpages/"/>
    <id>https://blog.vrnchndk.in/2023/04/08/Bypassing-paywalled-webpages/</id>
    <published>2023-04-08T06:18:02.000Z</published>
    <updated>2023-04-08T06:18:02.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="What-is-a-paywall"><a href="#What-is-a-paywall" class="headerlink" title="What is a paywall?"></a>What is a paywall?</h1><p>A paywall is a digital barrier that limits access to certain online content, such as news articles, videos, or other media, by requiring users to pay a fee or subscription in order to access the content.</p><h1 id="Why-do-I-hate-paywalls"><a href="#Why-do-I-hate-paywalls" class="headerlink" title="Why do I hate paywalls?"></a>Why do I hate paywalls?</h1><p>I dislike paywalls for various reasons, such as the cost, inconvenience, or limited accessibility of the content. Few might feel that the content behind a paywall is not worth the cost or inconvenience, which can negatively impact their perception of the publisher.</p><h1 id="How-to-bypass-paywalls"><a href="#How-to-bypass-paywalls" class="headerlink" title="How to bypass paywalls?"></a>How to bypass paywalls?</h1><p>There are various 3rd-party tools and software available that can do this job, I prefer a straightforward, hassle-free solution that works almost all the time. The service I use exclusively is: <a href="https://12ft.io/" target="_blank" rel="noopener">https://12ft.io/</a>.</p><p><a class="magnific-img" href="/images/paywall-12ft-io.png"><img src="/images/paywall-12ft-io.png" alt="img.png" class="ui centered image"></a></p><p>You basically enter the webpage url, and you&#x2019;ll view the content without any paywall. This might not work for select few websites but does the job efficiently.</p><h1 id="Shortcut-tip"><a href="#Shortcut-tip" class="headerlink" title="Shortcut tip"></a>Shortcut tip</h1><blockquote><p>This is tested on Google Chrome and is working properly.</p></blockquote><p>Instead typing 12ft.io manually before the URL everytime, or copy pasting the link in the website, simply create a bookmark and it will automatically append 12ft.io in 1 click.</p><h2 id="Creating-a-bookmark"><a href="#Creating-a-bookmark" class="headerlink" title="Creating a bookmark"></a>Creating a bookmark</h2><ol class="ui list"><li>Open Google Chrome on your computer.</li><li>Click on the three dots icon (&#x22EE;) located at the top-right corner of the Chrome window.</li><li>From the drop-down menu, select &#x201C;Bookmarks&#x201D; and then select &#x201C;Bookmark manager&#x201D; from the sub-menu. Alternatively, you can use the keyboard shortcut &#x201C;Ctrl + Shift + O&#x201D; (Windows) or &#x201C;Command + Option + B&#x201D; (Mac) to open the bookmark manager.</li><li>In the bookmark manager, click on the &#x201C;Add a new bookmark&#x201D; button located in the three dots icon (&#x22EE;) located at the top-right corner. This will bring up a new window.</li><li>In the new window, enter a name for the bookmark in the &#x201C;Name&#x201D; field.</li><li>In the &#x201C;URL&#x201D; field, enter the code block provided below.</li><li>Choose where you want to save the bookmark (in the bookmarks bar or in the bookmarks menu) by selecting the appropriate folder from the &#x201C;Folder&#x201D; drop-down menu.</li><li>Click the &#x201C;Save&#x201D; button to save the bookmark.</li></ol><p>It should look something like this:<br><a class="magnific-img" href="/images/12ft-bookmark.png"><img src="/images/12ft-bookmark.png" alt="img.png" class="ui centered image"></a></p><h2 id="Code-Block"><a href="#Code-Block" class="headerlink" title="Code Block"></a>Code Block</h2><p>Use this in step 6 above<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javascript:<span class="built_in">window</span>.location = <span class="string">&quot;https://12ft.io/&quot;</span> + <span class="built_in">window</span>.location.href;</span><br></pre></td></tr></table></figure></p><hr><h5 id="Disclaimer"><a href="#Disclaimer" class="headerlink" title="Disclaimer"></a>Disclaimer</h5><blockquote><p>It is important to note that bypassing paywalls may be illegal or unethical, and could lead to consequences such as loss of access to the website or legal action. It is recommended to respect the terms of service of websites and publishers, and consider paying for access to content to support the creators and ensure continued access to quality content.</p></blockquote>]]></content>
    
    <summary type="html">
    
      
      
        &lt;h1 id=&quot;What-is-a-paywall&quot;&gt;&lt;a href=&quot;#What-is-a-paywall&quot; class=&quot;headerlink&quot; title=&quot;What is a paywall?&quot;&gt;&lt;/a&gt;What is a paywall?&lt;/h1&gt;&lt;p&gt;A paywal
      
    
    </summary>
    
    
      <category term="linux" scheme="https://blog.vrnchndk.in/tags/linux/"/>
    
      <category term="paywall" scheme="https://blog.vrnchndk.in/tags/paywall/"/>
    
      <category term="internet" scheme="https://blog.vrnchndk.in/tags/internet/"/>
    
      <category term="windows" scheme="https://blog.vrnchndk.in/tags/windows/"/>
    
      <category term="mac" scheme="https://blog.vrnchndk.in/tags/mac/"/>
    
  </entry>
  
  <entry>
    <title>WSL2 with Windows Terminal and syncing with Mac OSX</title>
    <link href="https://blog.vrnchndk.in/2020/08/29/WSL2-with-Windows-Terminal-and-syncing-with-Mac-OSX/"/>
    <id>https://blog.vrnchndk.in/2020/08/29/WSL2-with-Windows-Terminal-and-syncing-with-Mac-OSX/</id>
    <published>2020-08-29T18:17:48.000Z</published>
    <updated>2020-08-29T18:17:48.000Z</updated>
    
    <content type="html"><![CDATA[<p>It has been a long time since I&#x2019;ve posted anything here and it&#x2019;s time to bring something new to this blog that&#x2019;s not cloud specific.</p><p>My work from home setup consists of a powerful Windows 10 machine with 2 monitors (in horizontal + vertical orientation). I also have a company provided Macbook Air 2019 model, which is not performing upto the mark, especially when multitasking with multiple IDE&#x2019;s, especially resource-hungry <strong>Google Chrome</strong> and <strong>Google Meet</strong>.</p><p>This prompted me to configure my desktop terminal with the personalization and settings of iTerm2 on my OSX. So let&#x2019;s get started&#x2026;</p><h2 id="Install-WSL2-on-Windows-10"><a href="#Install-WSL2-on-Windows-10" class="headerlink" title="Install WSL2 on Windows 10"></a>Install WSL2 on Windows 10</h2><p>For this, you need:</p><ul class="ui list"><li>updated Windows 10.</li><li>A computer with <strong>Hyper-V Virtualization</strong> support</li></ul><p>You can follow the <a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10" target="_blank" rel="noopener">Microsoft provided documentation</a> to get started or run the steps manually mentioned below:</p><h3 id="Enable-WSL"><a href="#Enable-WSL" class="headerlink" title="Enable WSL"></a>Enable WSL</h3><p>Open the PowerShell tool as an <strong>Administrator</strong> and run the command below:</p><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart</span><br></pre></td></tr></table></figure><h3 id="Enable-&#x201C;Virtual-Machine-Platform&#x201D;"><a href="#Enable-&#x201C;Virtual-Machine-Platform&#x201D;" class="headerlink" title="Enable &#x201C;Virtual Machine Platform&#x201D;"></a>Enable &#x201C;Virtual Machine Platform&#x201D;</h3><p>In the same Administrator Terminal, run the below command to enable <strong>Virtual Machine Platform</strong></p><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart</span><br></pre></td></tr></table></figure><h3 id="Set-WSL-2-as-default"><a href="#Set-WSL-2-as-default" class="headerlink" title="Set WSL 2 as default"></a>Set WSL 2 as default</h3><p>In the same Administrator Terminal, run the below command to set WSL 2 as default</p><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wsl --set-default-version <span class="number">2</span></span><br></pre></td></tr></table></figure><h3 id="Install-a-Linux-distribution"><a href="#Install-a-Linux-distribution" class="headerlink" title="Install a Linux distribution"></a>Install a Linux distribution</h3><p>For this I went ahead with <strong>Ubuntu 18.04 LTS</strong> as it&#x2019;s my preferred choice.</p><ol class="ui list"><li><p>Open <strong>Microsoft Store</strong> and search for Ubuntu. Select Ubuntu 18.04 to install and Launch it.<br><a class="magnific-img" href="/images/Screenshot_1.png"><img src="/images/Screenshot_1.png" alt="Screenshot_1" class="ui centered image"></a></p><p><em>Launching the distro for the 1st time will take some time to set things up in the backend.</em></p></li><li><p><strong>Congratulations !</strong> You have successfully installed the WSL 2 with Ubuntu. Update your machine with the following commands:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo apt update</span><br><span class="line">sudo apt upgrade -y</span><br><span class="line"> sudo reboot</span><br></pre></td></tr></table></figure><p><u><em>Note the space before the 3rd command</em>.</u></p></li></ol><h2 id="Terminal-Configuration"><a href="#Terminal-Configuration" class="headerlink" title="Terminal Configuration"></a>Terminal Configuration</h2><p><strong>Time for more fun.</strong></p><p>In this section, we&#x2019;ll configure the terminal with our configuration (in this case, mine). We all know OSX ships with ZSH by default (<em>no Bash, because licenses and shit</em>), so we&#x2019;ll configure that here as well.</p><p>By default Ubuntu comes with Bash Shell, and no pre-installed ZSH.</p><h3 id="Install-ZSH"><a href="#Install-ZSH" class="headerlink" title="Install ZSH"></a>Install ZSH</h3><ol class="ui list"><li><p>I prefer the package manager version for installing ZSH:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt install zsh -y</span><br></pre></td></tr></table></figure></li><li><p>Verify installation by running the below command for output <code>zsh 5.4.2</code> or higher.</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">zsh --version</span><br></pre></td></tr></table></figure></li><li><p>Now make your default shell to ZSH</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">chsh -s $(<span class="built_in">which</span> zsh)</span><br></pre></td></tr></table></figure></li><li><p>Log out and log back in and run <code>echo $SHELL</code> to see zsh configured.</p></li></ol><h3 id="Install-Oh-My-ZSH"><a href="#Install-Oh-My-ZSH" class="headerlink" title="Install Oh My ZSH"></a>Install Oh My ZSH</h3><p>Oh My Zsh is an open source, community-driven framework for managing your zsh configuration. It really brings out a whole new perspective in your terminal environment and will make you more productive.</p><p>I have OMZ installed on my MacBook, so I&#x2019;ll be doing that here as well. Official instructions for installation are available on their <a href="https://github.com/ohmyzsh/ohmyzsh" target="_blank" rel="noopener">Github repo</a>, but I&#x2019;ll post here for convenience:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sh -c <span class="string">&quot;<span class="variable">$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)</span>&quot;</span></span><br></pre></td></tr></table></figure><p>Once the installation is completed, you can install themes, add your favourite plugins (I&#x2019;ll share my heavily modified <code>~/.zshrc</code> later). I&#x2019;ll be not doing it as I will bring in my zshrc configuration file from my MacBook.</p><h4 id="Theme-I-use"><a href="#Theme-I-use" class="headerlink" title="Theme I use"></a>Theme I use</h4><ul class="ui list"><li>Powerlevel10k (<em>Link and installation</em>: <a href="https://github.com/romkatv/powerlevel10k#oh-my-zsh" target="_blank" rel="noopener">https://github.com/romkatv/powerlevel10k#oh-my-zsh</a>)</li></ul><h2 id="Syncing-between-OSX-and-Windows"><a href="#Syncing-between-OSX-and-Windows" class="headerlink" title="Syncing between OSX and Windows"></a>Syncing between OSX and Windows</h2><p>Here I&#x2019;ll be sharing a beautiful method of sharing configuration files using <strong>git</strong>. Thanks to <strong>Ferry Boender</strong> and the link to the blog here: <a href="https://www.electricmonk.nl/log/2015/06/22/keep-your-home-dir-in-git-with-a-detached-working-directory/" target="_blank" rel="noopener">https://www.electricmonk.nl/log/2015/06/22/keep-your-home-dir-in-git-with-a-detached-working-directory/</a>. I&#x2019;d highly recommended you to go through the entire article. <u>Very informative.</u> We could have also used file sharing service such as <strong>Dropbox</strong> or <strong>Google Drive</strong>, but that has some issues. Also, I am not comfortable using those for this specific use case.</p><p>I&#x2019;ll summarize the steps here from the blog here.</p><h3 id="On-your-Github"><a href="#On-your-Github" class="headerlink" title="On your Github"></a>On your Github</h3><ol class="ui list"><li>Create a private repo with a name of your choice (<em>preferably with a dot prefix to keep it hidden, if supported</em>)</li></ol><h3 id="On-your-MacBook"><a href="#On-your-MacBook" class="headerlink" title="On your MacBook"></a>On your MacBook</h3><h4 id="dgit-Setup"><a href="#dgit-Setup" class="headerlink" title="dgit Setup"></a>dgit Setup</h4><ol class="ui list"><li><p>Create a directory to hold your git database (the .git dir):</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mkdir ~/.dotfiles/</span><br><span class="line">cd ~/.dotfiles/</span><br><span class="line">git init .</span><br></pre></td></tr></table></figure></li><li><p>Create a <code>.gitignore</code> file with everything ignored:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;*&quot; &gt; .gitignore</span><br><span class="line">git add -f .gitignore</span><br><span class="line">git commit -m &quot;gitignore&quot;</span><br></pre></td></tr></table></figure></li><li><p>Create a <code>dgit</code> alias (<em>magic happens here</em>) and add to your shell profile, and source it:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> <span class="string">&quot;alias dgit=&apos;git --git-dir ~/.dotfiles/.git --work-tree=\$HOME&apos;&quot;</span> &gt;&gt; ~/.zshrc</span><br><span class="line"><span class="built_in">source</span> ~/.zshrc</span><br></pre></td></tr></table></figure></li><li><p>Go to your home directory, and do <code>dgit reset --hard</code>.</p></li></ol><h4 id="dgit-Usage"><a href="#dgit-Usage" class="headerlink" title="dgit Usage"></a>dgit Usage</h4><ol class="ui list"><li><p>Check Usage:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dgit status</span><br></pre></td></tr></table></figure></li><li><p>Add files by overriding the ignore with <code>-f</code> parameter:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">dgit add -f .zshrc</span><br><span class="line">dgit commit -m <span class="string">&quot;Added .zshrc&quot;</span></span><br></pre></td></tr></table></figure></li><li><p>Push to a remote repo (created earlier; I use <strong>AWS CodeCommit</strong>):</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">dgit remote add origin ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/.dotfiles</span><br><span class="line">dgit push origin master</span><br></pre></td></tr></table></figure></li></ol><h3 id="On-your-WSL-2-Machine"><a href="#On-your-WSL-2-Machine" class="headerlink" title="On your WSL 2 Machine"></a>On your WSL 2 Machine</h3><h4 id="dgit-Usage-1"><a href="#dgit-Usage-1" class="headerlink" title="dgit Usage"></a>dgit Usage</h4><ol class="ui list"><li><p>Go to your home directory and run the below commands. <em>This will overwrite files in your home directory with same names</em>.</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> <span class="variable">$HOME</span></span><br><span class="line">git <span class="built_in">clone</span> ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/.dotfiles ./.dotfiles</span><br><span class="line"><span class="built_in">alias</span> dgit=<span class="string">&apos;git --git-dir ~/.dotfiles/.git --work-tree=$HOME&apos;</span></span><br><span class="line">dgit reset --hard</span><br></pre></td></tr></table></figure></li><li><p><strong>Done</strong>.</p></li></ol><p><strong>Note:</strong> You can add more files/folders by the command <code>dgit add -f &lt;file/folder&gt;</code> on either machine, and then <code>dgit pull origin master</code> on the other machine.</p><hr><h2 id="My-Personalized-Configs"><a href="#My-Personalized-Configs" class="headerlink" title="My Personalized Configs"></a>My Personalized Configs</h2><h3 id="zshrc"><a href="#zshrc" class="headerlink" title=".zshrc"></a>.zshrc</h3><p>Since some commands behave differently on OSX (BSD based) and Linux (GNU based), I have added an <code>if-else-fi</code> clause in my <code>.zshrc</code> file to keep the commands working properly.</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.</span></span><br><span class="line"><span class="comment"># Initialization code that may require console input (password prompts, [y/n]</span></span><br><span class="line"><span class="comment"># confirmations, etc.) must go above this block; everything else may go below.</span></span><br><span class="line"><span class="keyword">if</span> [[ -r <span class="string">&quot;<span class="variable">${XDG_CACHE_HOME:-$HOME/.cache}</span>/p10k-instant-prompt-<span class="variable">${(%):-%n}</span>.zsh&quot;</span> ]]; <span class="keyword">then</span></span><br><span class="line">  <span class="built_in">source</span> <span class="string">&quot;<span class="variable">${XDG_CACHE_HOME:-$HOME/.cache}</span>/p10k-instant-prompt-<span class="variable">${(%):-%n}</span>.zsh&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># If you come from bash you might have to change your $PATH.</span></span><br><span class="line"><span class="comment"># export PATH=$HOME/bin:/usr/local/bin:$PATH</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Path to your oh-my-zsh installation.</span></span><br><span class="line"><span class="built_in">export</span> ZSH=<span class="string">&quot;<span class="variable">$HOME</span>/.oh-my-zsh&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Set name of the theme to load --- if set to &quot;random&quot;, it will</span></span><br><span class="line"><span class="comment"># load a random theme each time oh-my-zsh is loaded, in which case,</span></span><br><span class="line"><span class="comment"># to know which specific one was loaded, run: echo $RANDOM_THEME</span></span><br><span class="line"><span class="comment"># See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes</span></span><br><span class="line">ZSH_THEME=<span class="string">&quot;powerlevel10k/powerlevel10k&quot;</span></span><br><span class="line"><span class="comment">#ZSH_THEME=&quot;robbyrussell&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Set list of themes to pick from when loading at random</span></span><br><span class="line"><span class="comment"># Setting this variable when ZSH_THEME=random will cause zsh to load</span></span><br><span class="line"><span class="comment"># a theme from this variable instead of looking in ~/.oh-my-zsh/themes/</span></span><br><span class="line"><span class="comment"># If set to an empty array, this variable will have no effect.</span></span><br><span class="line"><span class="comment"># ZSH_THEME_RANDOM_CANDIDATES=( &quot;robbyrussell&quot; &quot;agnoster&quot; )</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line to use case-sensitive completion.</span></span><br><span class="line"><span class="comment"># CASE_SENSITIVE=&quot;true&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line to use hyphen-insensitive completion.</span></span><br><span class="line"><span class="comment"># Case-sensitive completion must be off. _ and - will be interchangeable.</span></span><br><span class="line"><span class="comment"># HYPHEN_INSENSITIVE=&quot;true&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line to disable bi-weekly auto-update checks.</span></span><br><span class="line"><span class="comment"># DISABLE_AUTO_UPDATE=&quot;true&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line to automatically update without prompting.</span></span><br><span class="line"><span class="comment"># DISABLE_UPDATE_PROMPT=&quot;true&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line to change how often to auto-update (in days).</span></span><br><span class="line"><span class="comment"># export UPDATE_ZSH_DAYS=13</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line if pasting URLs and other text is messed up.</span></span><br><span class="line"><span class="comment"># DISABLE_MAGIC_FUNCTIONS=true</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line to disable colors in ls.</span></span><br><span class="line"><span class="comment"># DISABLE_LS_COLORS=&quot;true&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line to disable auto-setting terminal title.</span></span><br><span class="line"><span class="comment"># DISABLE_AUTO_TITLE=&quot;true&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line to enable command auto-correction.</span></span><br><span class="line"><span class="comment"># ENABLE_CORRECTION=&quot;true&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line to display red dots whilst waiting for completion.</span></span><br><span class="line"><span class="comment"># COMPLETION_WAITING_DOTS=&quot;true&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line if you want to disable marking untracked files</span></span><br><span class="line"><span class="comment"># under VCS as dirty. This makes repository status check for large repositories</span></span><br><span class="line"><span class="comment"># much, much faster.</span></span><br><span class="line"><span class="comment"># DISABLE_UNTRACKED_FILES_DIRTY=&quot;true&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Uncomment the following line if you want to change the command execution time</span></span><br><span class="line"><span class="comment"># stamp shown in the history command output.</span></span><br><span class="line"><span class="comment"># You can set one of the optional three formats:</span></span><br><span class="line"><span class="comment"># &quot;mm/dd/yyyy&quot;|&quot;dd.mm.yyyy&quot;|&quot;yyyy-mm-dd&quot;</span></span><br><span class="line"><span class="comment"># or set a custom format using the strftime function format specifications,</span></span><br><span class="line"><span class="comment"># see &apos;man strftime&apos; for details.</span></span><br><span class="line"><span class="comment"># HIST_STAMPS=&quot;mm/dd/yyyy&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Would you like to use another custom folder than $ZSH/custom?</span></span><br><span class="line"><span class="comment"># ZSH_CUSTOM=/path/to/new-custom-folder</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># User configuration</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># export MANPATH=&quot;/usr/local/man:$MANPATH&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># You may need to manually set your language environment</span></span><br><span class="line"><span class="comment"># export LANG=en_US.UTF-8</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Preferred editor for local and remote sessions</span></span><br><span class="line"><span class="comment"># if [[ -n $SSH_CONNECTION ]]; then</span></span><br><span class="line"><span class="comment">#   export EDITOR=&apos;vim&apos;</span></span><br><span class="line"><span class="comment"># else</span></span><br><span class="line"><span class="comment">#   export EDITOR=&apos;mvim&apos;</span></span><br><span class="line"><span class="comment"># fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Compilation flags</span></span><br><span class="line"><span class="comment"># export ARCHFLAGS=&quot;-arch x86_64&quot;</span></span><br><span class="line"><span class="comment"># Set personal aliases, overriding those provided by oh-my-zsh libs,</span></span><br><span class="line"><span class="comment"># plugins, and themes. Aliases can be placed here, though oh-my-zsh</span></span><br><span class="line"><span class="comment"># users are encouraged to define aliases within the ZSH_CUSTOM folder.</span></span><br><span class="line"><span class="comment"># For a full list of active aliases, run `alias`.</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># Example aliases</span></span><br><span class="line"><span class="comment"># alias zshconfig=&quot;mate ~/.zshrc&quot;</span></span><br><span class="line"><span class="comment"># alias ohmyzsh=&quot;mate ~/.oh-my-zsh&quot;</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># Which plugins would you like to load?</span></span><br><span class="line"><span class="comment"># Standard plugins can be found in ~/.oh-my-zsh/plugins/*</span></span><br><span class="line"><span class="comment"># Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/</span></span><br><span class="line"><span class="comment"># Example format: plugins=(rails git textmate ruby lighthouse)</span></span><br><span class="line"><span class="comment"># Add wisely, as too many plugins slow down shell startup.</span></span><br><span class="line">plugins=(</span><br><span class="line">        git</span><br><span class="line">        colored-man-pages</span><br><span class="line">        colorize</span><br><span class="line">        pip</span><br><span class="line">        python</span><br><span class="line">        osx</span><br><span class="line">        gcloud</span><br><span class="line">        ansible</span><br><span class="line">        aws</span><br><span class="line">        zsh-syntax-highlighting</span><br><span class="line">)</span><br><span class="line"><span class="built_in">source</span> <span class="variable">$ZSH</span>/oh-my-zsh.sh</span><br><span class="line"></span><br><span class="line"><span class="built_in">export</span> LC_ALL=en_US.UTF-8</span><br><span class="line"><span class="built_in">export</span> LANG=en_US.UTF-8</span><br><span class="line"></span><br><span class="line"><span class="comment">###############################################################################################</span></span><br><span class="line"><span class="comment"># PATH GOES HERE</span></span><br><span class="line"><span class="keyword">if</span> [[ <span class="string">&quot;<span class="variable">$OSTYPE</span>&quot;</span> == <span class="string">&quot;darwin&quot;</span>* ]]; <span class="keyword">then</span></span><br><span class="line">    <span class="comment"># golang Environment variables</span></span><br><span class="line">    <span class="built_in">export</span> GOPATH=<span class="variable">$HOME</span>/.go <span class="comment"># don&apos;t forget to change your path correctly!</span></span><br><span class="line">    <span class="built_in">export</span> GOROOT=/usr/<span class="built_in">local</span>/opt/go/libexec</span><br><span class="line">    <span class="built_in">export</span> PATH=<span class="string">&quot;<span class="variable">$GOPATH</span>/bin:<span class="variable">$GOROOT</span>/bin:<span class="variable">$PATH</span>&quot;</span></span><br><span class="line">    <span class="comment"># php stuff</span></span><br><span class="line">    <span class="built_in">export</span> PATH=<span class="string">&quot;/usr/local/opt/php@7.3/bin:/usr/local/opt/php@7.3/sbin:<span class="variable">$HOME</span>/.composer/vendor/bin:<span class="variable">$PATH</span>&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># ruby stuff</span></span><br><span class="line">    <span class="comment"># Install Ruby Gems to ~/gems</span></span><br><span class="line">    <span class="built_in">export</span> GEM_HOME=<span class="variable">$HOME</span>/gems</span><br><span class="line">    <span class="built_in">export</span> PATH=<span class="string">&quot;<span class="variable">$HOME</span>/.gem/ruby/2.7.0/bin:<span class="variable">$PATH</span>&quot;</span></span><br><span class="line">    <span class="built_in">export</span> PATH=<span class="variable">$HOME</span>/gems/bin:<span class="variable">$PATH</span></span><br><span class="line">    <span class="built_in">export</span> PATH=<span class="string">&quot;/usr/local/opt/ruby/bin:<span class="variable">$PATH</span>&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$HOME/Library/Python/2.7/bin:/usr/local/opt/gnu-sed/libexec/gnubin:$PATH</span></span><br><span class="line">    <span class="built_in">export</span> PATH=/usr/<span class="built_in">local</span>/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/<span class="built_in">local</span>/sbin:/usr/<span class="built_in">local</span>/opt/gnu-sed/libexec/gnubin:<span class="variable">$PATH</span></span><br><span class="line">    <span class="built_in">export</span> PATH=<span class="string">&quot;/usr/local/opt/grep/libexec/gnubin:<span class="variable">$PATH</span>&quot;</span></span><br><span class="line">    <span class="built_in">export</span> PATH=<span class="string">&quot;/usr/local/opt/mysql-client/bin:<span class="variable">$PATH</span>&quot;</span></span><br><span class="line">    <span class="built_in">export</span> PATH=<span class="string">&quot;<span class="variable">$HOME</span>/Library/Python/3.8/bin:<span class="variable">$PATH</span>&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">alias</span> date=gdate</span><br><span class="line">    <span class="built_in">alias</span> tar=gtar</span><br><span class="line">    <span class="built_in">alias</span> brew=<span class="string">&apos;HOMEBREW_NO_AUTO_UPDATE=1 /usr/local/bin/brew&apos;</span></span><br><span class="line">    <span class="built_in">alias</span> brew-upgrade=<span class="string">&apos;/usr/local/bin/brew upgrade&apos;</span></span><br><span class="line">    <span class="built_in">alias</span> sed=gsed</span><br><span class="line">    <span class="built_in">alias</span> awk=gawk</span><br><span class="line">    <span class="built_in">alias</span> grep=ggrep</span><br><span class="line">    <span class="comment"># alias python=/usr/local/bin/python3</span></span><br><span class="line">    <span class="built_in">alias</span> pip=/usr/<span class="built_in">local</span>/bin/pip3</span><br><span class="line">    <span class="built_in">alias</span> python=/usr/<span class="built_in">local</span>/bin/python3</span><br><span class="line">    <span class="comment"># aws autocomplete</span></span><br><span class="line">    <span class="built_in">autoload</span> bashcompinit &amp;&amp; bashcompinit</span><br><span class="line">    complete -C <span class="string">&apos;/usr/local/bin/aws_completer&apos;</span> aws</span><br><span class="line"></span><br><span class="line">    <span class="comment"># The next line updates PATH for the Google Cloud SDK.</span></span><br><span class="line">    <span class="keyword">if</span> [ -f <span class="string">&apos;$HOME/google-cloud-sdk/path.zsh.inc&apos;</span> ]; <span class="keyword">then</span> . <span class="string">&apos;$HOME/google-cloud-sdk/path.zsh.inc&apos;</span>; <span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># The next line enables shell command completion for gcloud.</span></span><br><span class="line">    <span class="keyword">if</span> [ -f <span class="string">&apos;$HOME/google-cloud-sdk/completion.zsh.inc&apos;</span> ]; <span class="keyword">then</span> . <span class="string">&apos;$HOME/google-cloud-sdk/completion.zsh.inc&apos;</span>; <span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># Source chtf</span></span><br><span class="line">    <span class="keyword">if</span> [[ -f /usr/<span class="built_in">local</span>/share/chtf/chtf.sh ]]; <span class="keyword">then</span> <span class="built_in">source</span> <span class="string">&quot;/usr/local/share/chtf/chtf.sh&quot;</span>; <span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">export</span> NVM_DIR=<span class="string">&quot;<span class="variable">$HOME</span>/.nvm&quot;</span></span><br><span class="line">    [ -s <span class="string">&quot;<span class="variable">$NVM_DIR</span>/nvm.sh&quot;</span> ] &amp;&amp; . <span class="string">&quot;<span class="variable">$NVM_DIR</span>/nvm.sh&quot;</span>  <span class="comment"># This loads nvm</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">elif</span> [[ <span class="string">&quot;<span class="variable">$OSTYPE</span>&quot;</span> == <span class="string">&quot;linux-gnu&quot;</span> ]]; <span class="keyword">then</span></span><br><span class="line">        <span class="built_in">cd</span> <span class="variable">$HOME</span></span><br><span class="line">        <span class="built_in">export</span> PATH=/home/vrnchndk/.<span class="built_in">local</span>/bin:<span class="variable">$PATH</span></span><br><span class="line">        <span class="built_in">export</span> PATH=<span class="string">&quot;<span class="variable">$(tr &apos;:&apos; &apos;\n&apos; &lt;&lt;&lt;&quot;$PATH&quot; | grep -Ev &apos;^(/mnt)</span>/c/&apos; | paste -sd &apos;:&apos;)&quot;</span></span><br><span class="line">        <span class="built_in">export</span> NVM_DIR=<span class="string">&quot;<span class="variable">$HOME</span>/.nvm&quot;</span></span><br><span class="line">        [ -s <span class="string">&quot;<span class="variable">$NVM_DIR</span>/nvm.sh&quot;</span> ] &amp;&amp; \. <span class="string">&quot;<span class="variable">$NVM_DIR</span>/nvm.sh&quot;</span>  <span class="comment"># This loads nvm</span></span><br><span class="line">        [ -s <span class="string">&quot;<span class="variable">$NVM_DIR</span>/bash_completion&quot;</span> ] &amp;&amp; \. <span class="string">&quot;<span class="variable">$NVM_DIR</span>/bash_completion&quot;</span>  <span class="comment"># This loads nvm bash_completion</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment">###############################################################################################</span></span><br><span class="line"><span class="comment"># aliases go here</span></span><br><span class="line"><span class="built_in">alias</span> dgit=<span class="string">&apos;git --git-dir ~/.dotfiles/.git --work-tree=$HOME&apos;</span></span><br><span class="line"><span class="built_in">alias</span> ip=<span class="string">&apos;curl icanhazip.com&apos;</span></span><br><span class="line"><span class="built_in">alias</span> ll=<span class="string">&apos;ls -GFlashrt&apos;</span></span><br><span class="line"><span class="built_in">alias</span> axel=<span class="string">&apos;axel -a -n 8&apos;</span></span><br><span class="line"><span class="built_in">alias</span> ssh=<span class="string">&apos;ssh -o StrictHostKeyChecking=false&apos;</span></span><br><span class="line"><span class="built_in">alias</span> scp=<span class="string">&apos;scp -o StrictHostKeyChecking=false&apos;</span></span><br><span class="line"><span class="built_in">alias</span> sub=<span class="string">&apos;subliminal download -l en&apos;</span></span><br><span class="line"><span class="built_in">alias</span> harpic=<span class="string">&quot;clear &amp;&amp; printf &apos;\e[3J&apos;&quot;</span></span><br><span class="line"><span class="built_in">alias</span> youtube-dl=<span class="string">&apos;youtube-dl --external-downloader axel --external-downloader-args &quot;-a -n 8&quot;&apos;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">###############################################################################################</span></span><br><span class="line"><span class="comment">######## Bash History stuff goes down here ######</span></span><br><span class="line"><span class="built_in">export</span> SHELL_SESSION_HISTORY=0</span><br><span class="line"><span class="built_in">export</span> HISTFILESIZE=100000</span><br><span class="line"><span class="built_in">export</span> HISTSIZE=100000</span><br><span class="line"><span class="built_in">export</span> HISTTIMEFORMAT=<span class="string">&quot;%F %T &quot;</span></span><br><span class="line"><span class="built_in">export</span> HISTCONTROL=ignoreboth</span><br><span class="line"><span class="built_in">export</span> HISTIGNORE=<span class="string">&apos;rm&apos;</span></span><br><span class="line"><span class="comment">#setopt histappend</span></span><br><span class="line"><span class="built_in">setopt</span> BANG_HIST</span><br><span class="line"><span class="built_in">setopt</span> EXTENDED_HISTORY</span><br><span class="line"><span class="built_in">setopt</span> INC_APPEND_HISTORY</span><br><span class="line"><span class="built_in">setopt</span> SHARE_HISTORY</span><br><span class="line"><span class="built_in">setopt</span> HIST_EXPIRE_DUPS_FIRST</span><br><span class="line"><span class="built_in">setopt</span> HIST_IGNORE_DUPS</span><br><span class="line"><span class="built_in">setopt</span> HIST_FIND_NO_DUPS</span><br><span class="line"><span class="built_in">setopt</span> HIST_IGNORE_SPACE</span><br><span class="line"><span class="built_in">setopt</span> HIST_SAVE_NO_DUPS</span><br><span class="line"><span class="built_in">setopt</span> HIST_REDUCE_BLANKS</span><br><span class="line"><span class="comment"># After each command, append to the history file and reread it</span></span><br><span class="line"><span class="comment">#export PROMPT_COMMAND=&quot;${PROMPT_COMMAND:+$PROMPT_COMMAND$&apos;\n&apos;}history -a; history -c; history -r&quot;</span></span><br><span class="line"><span class="comment">#export PROMPT_COMMAND=&apos;history -a&apos;</span></span><br><span class="line"><span class="comment">##################################################</span></span><br><span class="line"><span class="comment"># Few Tricks</span></span><br><span class="line"><span class="comment">## File History Completion</span></span><br><span class="line"><span class="comment">#autoload -Uz up-line-or-beginning-search down-line-or-beginning-search</span></span><br><span class="line"><span class="comment">#zle -N up-line-or-beginning-search</span></span><br><span class="line"><span class="comment">#zle -N down-line-or-beginning-search</span></span><br><span class="line"><span class="comment">#bindkey &apos;^[[A&apos;  up-line-or-beginning-search    # Arrow up</span></span><br><span class="line"><span class="comment">#bindkey &apos;^[OA&apos;  up-line-or-beginning-search</span></span><br><span class="line"><span class="comment">#bindkey &apos;^[[B&apos;  down-line-or-beginning-search  # Arrow down</span></span><br><span class="line"><span class="comment">#bindkey &apos;^[OB&apos;  down-line-or-beginning-search</span></span><br><span class="line"><span class="comment">##</span></span><br><span class="line"></span><br><span class="line"><span class="comment">##################################################</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Create a folder and move into it in one command</span></span><br><span class="line"><span class="keyword">function</span> <span class="function"><span class="title">mkcd</span></span>() { mkdir -p <span class="string">&quot;<span class="variable">$@</span>&quot;</span> &amp;&amp; <span class="built_in">cd</span> <span class="string">&quot;<span class="variable">$_</span>&quot;</span>; }</span><br><span class="line"></span><br><span class="line"><span class="comment"># Specify default editor. Possible values: vim, nano, ed etc.</span></span><br><span class="line"><span class="built_in">export</span> EDITOR=vim</span><br><span class="line"><span class="built_in">export</span> VISUAL=vim</span><br><span class="line"></span><br><span class="line"><span class="comment"># ANSIBLE_HOST_KEY_CHECKING=False</span></span><br><span class="line"><span class="built_in">export</span> ANSIBLE_HOST_KEY_CHECKING=False</span><br><span class="line"></span><br><span class="line"><span class="comment">##################################################</span></span><br><span class="line"><span class="comment">#PS1=&apos;${SSH_CONNECTION+&quot;%{$fg_bold[green]%}%n@%m:&quot;}%{$fg_bold[green]%}: %~%{$reset_color%} # &apos;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Terraform SDK Load to use with roles</span></span><br><span class="line"><span class="built_in">export</span> AWS_SDK_LOAD_CONFIG=1</span><br><span class="line"></span><br><span class="line"><span class="comment"># To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.</span></span><br><span class="line">[[ ! -f ~/.p10k.zsh ]] || <span class="built_in">source</span> ~/.p10k.zsh</span><br></pre></td></tr></table></figure><p>If you check under <code>linux-gnu</code> block, I have added a small line for <code>PATH</code></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">export</span> PATH=<span class="string">&quot;<span class="variable">$(tr &apos;:&apos; &apos;\n&apos; &lt;&lt;&lt;&quot;$PATH&quot; | grep -Ev &apos;^(/mnt)</span>/c/&apos; | paste -sd &apos;:&apos;)&quot;</span></span><br></pre></td></tr></table></figure><p>What this does is, it removes the <code>/mnt/c/</code> PATH definitions from environment variables and just retains the paths specific to the Linux box. Keeps the path simple and easy to read.</p><h3 id="Theme-Settings"><a href="#Theme-Settings" class="headerlink" title="Theme Settings"></a>Theme Settings</h3><p>The theme I use is <strong>Powerlevel10k</strong>. Link to installation: <a href="https://github.com/romkatv/powerlevel10k#oh-my-zsh" target="_blank" rel="noopener">https://github.com/romkatv/powerlevel10k#oh-my-zsh</a>.</p><p>After installing, run <code>p10k configure</code> to set the terminal to your taste.</p><h3 id="Preview"><a href="#Preview" class="headerlink" title="Preview"></a>Preview</h3><p><a class="magnific-img" href="/images/Screenshot_2.png"><img src="/images/Screenshot_2.png" alt="Screenshot_2" class="ui centered image"></a></p><hr><p>In case you want to add something to this guide, or I may have missed anything, do let me know. Do comment and share if this guide have helped you.</p>]]></content>
    
    <summary type="html">
    
      
      
        &lt;p&gt;It has been a long time since I&amp;#x2019;ve posted anything here and it&amp;#x2019;s time to bring something new to this blog that&amp;#x2019;s not
      
    
    </summary>
    
    
      <category term="terminal" scheme="https://blog.vrnchndk.in/tags/terminal/"/>
    
      <category term="windows" scheme="https://blog.vrnchndk.in/tags/windows/"/>
    
      <category term="mac" scheme="https://blog.vrnchndk.in/tags/mac/"/>
    
      <category term="git" scheme="https://blog.vrnchndk.in/tags/git/"/>
    
      <category term="osx" scheme="https://blog.vrnchndk.in/tags/osx/"/>
    
      <category term="wsl" scheme="https://blog.vrnchndk.in/tags/wsl/"/>
    
      <category term="wsl2" scheme="https://blog.vrnchndk.in/tags/wsl2/"/>
    
      <category term="macbook" scheme="https://blog.vrnchndk.in/tags/macbook/"/>
    
  </entry>
  
  <entry>
    <title>RDS Authentication via IAM User/Role</title>
    <link href="https://blog.vrnchndk.in/2019/05/01/RDS-Authentication-via-IAM-User-Role/"/>
    <id>https://blog.vrnchndk.in/2019/05/01/RDS-Authentication-via-IAM-User-Role/</id>
    <published>2019-05-01T04:56:40.000Z</published>
    <updated>2019-05-01T04:56:40.000Z</updated>
    
    <content type="html"><![CDATA[<p>I&#x2019;ll keep this post a short one as it just gives an overview of enabling IAM based login to AWS RDS. Before we continue, please note:</p><h2 id="Prerequisites"><a href="#Prerequisites" class="headerlink" title="Prerequisites"></a>Prerequisites</h2><p>IAM database authentication is available for the following database engines and DB instance classes:</p><ol class="ui list"><li>MySQL 8.0, minor version 8.0.16 or higher</li><li>MySQL 5.7, minor version 5.7.16 or higher</li><li>MySQL 5.6, minor version 5.6.34 or higher</li><li>PostgreSQL 13, all minor versions</li><li>PostgreSQL 12, all minor versions</li><li>PostgreSQL 11, all minor versions</li><li>PostgreSQL 10, minor version 10.6 or higher</li><li>PostgreSQL 9.6, minor version 9.6.11 or higher</li><li>PostgreSQL 9.5, minor version 9.5.15 or higher</li><li>All DB instance classes are supported, except for <code>db.t2.small</code> and <code>db.m1.small</code>.</li><li>IAM database authentication is not supported for MySQL 5.5.</li></ol><hr><h2 id="Enabling-IAM-Auth-for-RDS"><a href="#Enabling-IAM-Auth-for-RDS" class="headerlink" title="Enabling IAM Auth for RDS"></a>Enabling IAM Auth for RDS</h2><p>Alright, so if this is sorted, we can continue with the below steps:</p><ol class="ui list"><li><p>Enable IAM Authentication in existing RDS using the link here: <strong><a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Enabling.html" target="_blank" rel="noopener">Enabling and Disabling IAM Database Authentication</a></strong></p></li><li><p>Login to RDS with master username password.</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql -h &lt;RDS_ENDPOINT&gt; --user &lt;MASTER_USERNAME&gt; --password</span><br></pre></td></tr></table></figure></li><li><p>Create Users with specific database access using the below commands:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; CREATE USER &lt;USERNAME&gt; IDENTIFIED WITH AWSAuthenticationPlugin AS &apos;RDS&apos;;</span><br><span class="line">mysql&gt; grant all privileges on &lt;DATABASE_NAME&gt;.* to &lt;USERNAME&gt; IDENTIFIED WITH AWSAuthenticationPlugin AS &apos;RDS&apos;;</span><br></pre></td></tr></table></figure></li><li><p>Get the DB Identifier for IAM policy generation:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DB_ID=<span class="string">&quot;<span class="variable">$(aws rds describe-db-instances --query &apos;DBInstances[?DBInstanceIdentifier==`&lt;RDS_NAME&gt;`].DbiResourceId&apos; --output text)</span>&quot;</span></span><br></pre></td></tr></table></figure></li><li><p>Generate and Attach the policy for the IAM user:</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line">    <span class="attr">&quot;Version&quot;</span>: <span class="string">&quot;2012-10-17&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;Statement&quot;</span>: [</span><br><span class="line">        {</span><br><span class="line">            <span class="attr">&quot;Effect&quot;</span>: <span class="string">&quot;Allow&quot;</span>,</span><br><span class="line">            <span class="attr">&quot;Action&quot;</span>: [</span><br><span class="line">                <span class="string">&quot;rds-db:connect&quot;</span></span><br><span class="line">            ],</span><br><span class="line">            <span class="attr">&quot;Resource&quot;</span>: [</span><br><span class="line">                <span class="string">&quot;arn:aws:rds-db:&lt;REGION&gt;:&lt;ACCOUNT_ID&gt;:dbuser:&lt;DB_ID&gt;/&lt;USERNAME&gt;&quot;</span></span><br><span class="line">            ]</span><br><span class="line">        }</span><br><span class="line">    ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><p>Use the below script to connect to the RDS:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Get RDS endpoint</span></span><br><span class="line">RDS_ENDPOINT=<span class="string">&quot;<span class="variable">$(aws rds describe-db-instances --query &apos;DBInstances[?DBInstanceIdentifier==`&lt;RDS_NAME&gt;`].Endpoint.Address&apos; --output text)</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Get Temporary Token for Connection</span></span><br><span class="line">TOKEN=<span class="string">&quot;<span class="variable">$(aws rds generate-db-auth-token --hostname $RDS_ENDPOINT --port 3306 --region &lt;REGION&gt; --username &lt;USERNAME&gt; --output text)</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Connect to RDS using the TOKEN</span></span><br><span class="line">mysql --host=<span class="string">&quot;<span class="variable">$RDS_ENDPOINT</span>&quot;</span> --port=3306 --<span class="built_in">enable</span>-cleartext-plugin --user=<span class="string">&quot;&lt;USERNAME&gt;&quot;</span> --password=<span class="string">&quot;<span class="variable">$TOKEN</span>&quot;</span></span><br></pre></td></tr></table></figure></li></ol><h2 id="Notes"><a href="#Notes" class="headerlink" title="Notes"></a>Notes</h2><ul class="ui list"><li><p>You can add access for more RDS instances (running in different regions) in a single IAM policy</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line">    <span class="attr">&quot;Version&quot;</span>: <span class="string">&quot;2012-10-17&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;Statement&quot;</span>: [</span><br><span class="line">        {</span><br><span class="line">            <span class="attr">&quot;Effect&quot;</span>: <span class="string">&quot;Allow&quot;</span>,</span><br><span class="line">            <span class="attr">&quot;Action&quot;</span>: [</span><br><span class="line">                <span class="string">&quot;rds-db:connect&quot;</span></span><br><span class="line">            ],</span><br><span class="line">            <span class="attr">&quot;Resource&quot;</span>: [</span><br><span class="line">                <span class="string">&quot;arn:aws:rds-db:&lt;REGION&gt;:&lt;ACCOUNT_ID&gt;:dbuser:&lt;DB_ID1&gt;/&lt;USERNAME1&gt;&quot;</span>,</span><br><span class="line">                <span class="string">&quot;arn:aws:rds-db:&lt;REGION&gt;:&lt;ACCOUNT_ID&gt;:dbuser:&lt;DB_ID2&gt;/&lt;USERNAME2&gt;&quot;</span>,</span><br><span class="line">                <span class="string">&quot;arn:aws:rds-db:&lt;REGION&gt;:&lt;ACCOUNT_ID&gt;:dbuser:&lt;DB_ID3&gt;/&lt;USERNAME3&gt;&quot;</span></span><br><span class="line">            ]</span><br><span class="line">        }</span><br><span class="line">    ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul><li><code>USERNAME1</code> can have read only access to <code>DB_ID1</code></li><li><code>USERNAME2</code> can have full admin access to entire <code>DB_ID2</code></li><li><code>USERNAME3</code> can have admin access to <code>DB_ID3</code> for single database only.</li></ul></li><li><p>The <code>&lt;USERNAME&gt;</code> has to be created via admin privileges.</p></li><li>Add the proper endpoints/names in the placeholders (<code>&lt;XXX&gt;</code>) given above.</li><li>This has been tested on a demo environment.</li></ul><hr><p>Link to Github Gist: <a href="https://gist.github.com/varunchandak/8e42a927de26d81b340dfe906d1fefe9" target="_blank" rel="noopener">HERE</a></p>]]></content>
    
    <summary type="html">
    
      
      
        &lt;p&gt;I&amp;#x2019;ll keep this post a short one as it just gives an overview of enabling IAM based login to AWS RDS. Before we continue, please no
      
    
    </summary>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="awscli" scheme="https://blog.vrnchndk.in/tags/awscli/"/>
    
      <category term="cloud" scheme="https://blog.vrnchndk.in/tags/cloud/"/>
    
      <category term="iam" scheme="https://blog.vrnchndk.in/tags/iam/"/>
    
      <category term="rds" scheme="https://blog.vrnchndk.in/tags/rds/"/>
    
      <category term="mysql" scheme="https://blog.vrnchndk.in/tags/mysql/"/>
    
  </entry>
  
  <entry>
    <title>Auto update ASG with latest image and launch config</title>
    <link href="https://blog.vrnchndk.in/2019/04/06/Auto-update-ASG-with-latest-image-and-launch-config/"/>
    <id>https://blog.vrnchndk.in/2019/04/06/Auto-update-ASG-with-latest-image-and-launch-config/</id>
    <published>2019-04-06T17:53:26.000Z</published>
    <updated>2019-04-06T17:53:26.000Z</updated>
    
    <content type="html"><![CDATA[<p>Normally, we create AMIs manually and update the launch configurations of different auto scaling groups using those AMIs. This kind of repetitive work becomes tedious on a daily basis and there&#x2019;s no such feature in AWS to do this automatically.</p><p>After getting frustrated, I thought of getting this fixed on a permanent basis. Using my knowledge of shell scripting and basic AWS CLI commands, I have created a shell script to do this automatically.</p><hr><h2 id="What-does-this-script-do"><a href="#What-does-this-script-do" class="headerlink" title="What does this script do ?"></a>What does this script do ?</h2><p>Well, the script is straight-forward. Pass the autoscaling group name parameter to it and it will randomly pick any 1 instance from it and then create it&#x2019;s AMI. Once the AMI is created, the script will then fetch the existing launch configuration, create a new one with existing parameters, except the AMI of course, and attach it to the autoscaling group.</p><h2 id="How-to-choose-a-specific-instance-from-ASG"><a href="#How-to-choose-a-specific-instance-from-ASG" class="headerlink" title="How to choose a specific instance from ASG ?"></a>How to choose a specific instance from ASG ?</h2><p>In that case, you can specify the instance ID as well along with the autoscaling group name. The script will then create it&#x2019;s AMI and update the launch configuration. Easy Peasy !!</p><h2 id="Where-is-the-script"><a href="#Where-is-the-script" class="headerlink" title="Where is the script ?"></a>Where is the script ?</h2><p>The script can be found in the link here: <a href="https://github.com/varunchandak/automate-launch-configuration-update-with-latest-image" target="_blank" rel="noopener">https://github.com/varunchandak/automate-launch-configuration-update-with-latest-image</a>. Make sure to go through the <code>README</code> carefully and test it in your development environment before executing it in the production environment.</p><hr><p>Any bugs or enhancements are welcome. Enjoy !</p>]]></content>
    
    <summary type="html">
    
      
      
        &lt;p&gt;Normally, we create AMIs manually and update the launch configurations of different auto scaling groups using those AMIs. This kind of re
      
    
    </summary>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="awscli" scheme="https://blog.vrnchndk.in/tags/awscli/"/>
    
      <category term="cloud" scheme="https://blog.vrnchndk.in/tags/cloud/"/>
    
      <category term="ec2" scheme="https://blog.vrnchndk.in/tags/ec2/"/>
    
      <category term="asg" scheme="https://blog.vrnchndk.in/tags/asg/"/>
    
  </entry>
  
  <entry>
    <title>NFS Failover with DRBD and Heartbeat on AWS EC2</title>
    <link href="https://blog.vrnchndk.in/2018/11/28/NFS-Failover-with-DRBD-and-Heartbeat-on-AWS-EC2/"/>
    <id>https://blog.vrnchndk.in/2018/11/28/NFS-Failover-with-DRBD-and-Heartbeat-on-AWS-EC2/</id>
    <published>2018-11-28T17:18:15.000Z</published>
    <updated>2018-11-28T17:18:15.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Setup-NFS-Failover-with-DRBD-and-Heartbeat-on-AWS-EC2"><a href="#Setup-NFS-Failover-with-DRBD-and-Heartbeat-on-AWS-EC2" class="headerlink" title="Setup NFS Failover with DRBD and Heartbeat on AWS EC2"></a>Setup NFS Failover with DRBD and Heartbeat on AWS EC2</h1><p>This tutorial will help to configure NFS Failover using DRBD and Heartbeat on AWS EC2. There are multiple tutorials on implementing the above, but not on <strong>AWS EC2</strong>.</p><p><strong><em>NOTE: Run the commands/scripts on BOTH SERVERS (Primary and Secondary), otherwise mentioned explicitly.</em></strong></p><h2 id="Requirements"><a href="#Requirements" class="headerlink" title="Requirements"></a>Requirements</h2><ul class="ui list"><li>VPC with a public subnet and Internet gateway.</li><li>IAM Role (DescribeInstances and AssignPrivateAddress) for EC2</li><li>2 x Ubuntu Instances (for NFS Primary and Secondary)</li><li>1 x Ubuntu Instance (for NFS Client; testing)</li><li>3 Elastic IPs (for Primary, Secondary and Virtual IP)</li></ul><h3 id="Create-a-VPC"><a href="#Create-a-VPC" class="headerlink" title="Create a VPC"></a>Create a VPC</h3><p>Follow this guide: <strong><a href="http://docs.aws.amazon.com/AmazonVPC/latest/GettingStartedGuide/getting-started-create-vpc.html" target="_blank" rel="noopener">HERE</a></strong></p><h3 id="Create-a-Security-Group"><a href="#Create-a-Security-Group" class="headerlink" title="Create a Security Group"></a>Create a Security Group</h3><p>Follow this guide: <strong><a href="http://docs.aws.amazon.com/AmazonVPC/latest/GettingStartedGuide/getting-started-create-security-group.html" target="_blank" rel="noopener">HERE</a></strong></p><h3 id="Create-an-Amazon-EC2-IAM-Role-with-the-following-policy"><a href="#Create-an-Amazon-EC2-IAM-Role-with-the-following-policy" class="headerlink" title="Create an Amazon EC2 IAM Role with the following policy"></a>Create an Amazon EC2 IAM Role with the following policy</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line">&quot;Statement&quot;: [{</span><br><span class="line">&quot;Action&quot;: [</span><br><span class="line">&quot;ec2:AssignPrivateIpAddresses&quot;,</span><br><span class="line">&quot;ec2:DescribeInstances&quot;</span><br><span class="line">],</span><br><span class="line">&quot;Effect&quot;: &quot;Allow&quot;,</span><br><span class="line">&quot;Resource&quot;: &quot;*&quot;</span><br><span class="line">}]</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="Launch-Two-Ubuntu-EC2-instances-into-Your-VPC&#x2019;s-Public-Subnet"><a href="#Launch-Two-Ubuntu-EC2-instances-into-Your-VPC&#x2019;s-Public-Subnet" class="headerlink" title="Launch Two Ubuntu EC2 instances into Your VPC&#x2019;s Public Subnet"></a>Launch Two Ubuntu EC2 instances into Your VPC&#x2019;s Public Subnet</h3><ol class="ui list"><li>Assign EC2 IAM Role created above to the instance <strong>(do not skip this or forget)</strong></li><li>Assign Private IPs (Primary: 192.168.0.10, Secondary: 192.168.0.11, Virtual IP: 192.168.0.12)</li><li>Assign the Security Group created above.</li><li>Configure Elastic IP Addresses for Your Instances.</li></ol><hr><h2 id="Create-the-vipup-script"><a href="#Create-the-vipup-script" class="headerlink" title="Create the vipup script"></a>Create the <strong>vipup</strong> script</h2><p><strong>Make changes according to the server (don&#x2019;t just copy paste like a kid)</strong></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># This script will monitor another HA node and take over a Virtual IP (VIP)</span></span><br><span class="line"><span class="comment"># if communication with the other node fails</span></span><br><span class="line">HA_Node_IP=192.168.0.11</span><br><span class="line">VIP=192.168.0.12</span><br><span class="line">REGION=us-east-1</span><br><span class="line">Instance_ID=$(curl --silent http://169.254.169.254/latest/meta-data/instance-id)</span><br><span class="line">ENI_ID=$(ec2-describe-instances <span class="string">&quot;<span class="variable">$Instance_ID</span>&quot;</span> --region <span class="string">&quot;<span class="variable">$REGION</span>&quot;</span> | grep eni -m 1 | awk <span class="string">&apos;{print $2;}&apos;</span>)</span><br><span class="line"></span><br><span class="line">pingresult=$(ping -c3 -W 1 <span class="variable">$HA_Node_IP</span> | grep time= | wc -l)</span><br><span class="line"><span class="keyword">if</span> [ <span class="string">&quot;<span class="variable">$pingresult</span>&quot;</span> == <span class="string">&quot;0&quot;</span> ]; <span class="keyword">then</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$(date)</span> -- HA heartbeat failed, taking over VIP&quot;</span></span><br><span class="line">  ec2-assign-private-ip-addresses -n <span class="string">&quot;<span class="variable">$ENI_ID</span>&quot;</span> \</span><br><span class="line">  --secondary-private-ip-address <span class="string">&quot;<span class="variable">$VIP</span>&quot;</span> \</span><br><span class="line">  --allow-reassignment --region <span class="string">&quot;<span class="variable">$REGION</span>&quot;</span></span><br><span class="line">  ifconfig eth0:0 <span class="string">&quot;<span class="variable">$VIP</span>&quot;</span> netmask 255.255.255.0 &gt; /dev/null 2&gt;&amp;1</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure><p>Mark it executable:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ chmod +x vipup</span><br></pre></td></tr></table></figure></p><hr><h2 id="Additional-Steps-to-configure-on-servers"><a href="#Additional-Steps-to-configure-on-servers" class="headerlink" title="Additional Steps to configure on servers:"></a>Additional Steps to configure on servers:</h2><ol class="ui list"><li><p>Add Route Table for virtual IP:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ echo &quot;2 eth1_rt&quot; &gt;&gt; /etc/iproute2/rt_tables ;</span><br></pre></td></tr></table></figure></li><li><p>Add <code>eth0:0</code> interface and copy the below configuration so that it is available on AWS internal network:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ vim /etc/network/interfaces.d/eth0:0.cfg</span><br><span class="line"></span><br><span class="line">auto eth0:0</span><br><span class="line">iface eth0:0 inet dhcp</span><br><span class="line"> up ip route add default via 192.168.0.1 dev eth0:0 table eth1_rt</span><br><span class="line"> up ip rule add from 192.168.0.12 lookup eth1_rt prio 1000</span><br></pre></td></tr></table></figure></li><li><p>Edit hosts file to enter details about Primary and Secondary Servers</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ vim /etc/hosts</span><br><span class="line"></span><br><span class="line">192.168.0.10primary.nfs.server</span><br><span class="line">192.168.0.11secondary.nfs.server</span><br></pre></td></tr></table></figure></li><li><p>Edit the hostname to reflect on the server:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ vim /etc/hostname</span><br><span class="line"></span><br><span class="line">primary.nfs.server (on Primary Server)</span><br><span class="line">secondary.nfs.server (on Secondary Server)</span><br></pre></td></tr></table></figure></li><li><p>Set the hostname (and logout, login)</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hostname -F /etc/hostname</span><br></pre></td></tr></table></figure></li><li><p>Update the repositories and install aws tools:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ apt-add-repository ppa:awstools-dev/awstools</span><br><span class="line">$ apt-get update</span><br><span class="line">$ apt-get install ntpdate tzdata ec2-api-tools ec2-ami-tools iamcli rdscli moncli ascli elasticache awscli</span><br></pre></td></tr></table></figure></li><li><p>Test:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ ec2-describe-instances</span><br></pre></td></tr></table></figure><p> or</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ aws ec2 describe-instances</span><br></pre></td></tr></table></figure></li><li><p>Update the time for proper synchronization (<strong>very important</strong>):</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ ntpdate -u in.pool.ntp.org</span><br></pre></td></tr></table></figure></li></ol><hr><h2 id="Install-DRBD"><a href="#Install-DRBD" class="headerlink" title="Install DRBD:"></a>Install DRBD:</h2><h3 id="Update-the-repositories-and-Install-the-DRBD-software-reboot-required"><a href="#Update-the-repositories-and-Install-the-DRBD-software-reboot-required" class="headerlink" title="Update the repositories and Install the DRBD software (reboot required)"></a>Update the repositories and Install the DRBD software <em>(reboot required)</em></h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ apt-get update</span><br><span class="line">$ apt-get install drbd8-utils</span><br><span class="line">$ apt-get install linux-image-extra-virtual</span><br><span class="line">$ depmod -a</span><br><span class="line">$ sleep 5; reboot</span><br></pre></td></tr></table></figure><h3 id="Setup-DRBD-for-a-particular-device-Example-dev-xvdb"><a href="#Setup-DRBD-for-a-particular-device-Example-dev-xvdb" class="headerlink" title="Setup DRBD for a particular device (Example: /dev/xvdb):"></a>Setup DRBD for a particular device (Example: /dev/xvdb):</h3><ol class="ui list"><li>To configure drbd, edit <code>/etc/drbd.conf</code> and change <strong>global { usage-count yes; }</strong> to <strong>no</strong> (Ignore if already changed)</li><li><p>Create a resource file <code>r0.res</code> in <code>/etc/drbd.d/</code></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">$ vim /etc/drbd.d/r0.res (edit as required)</span><br><span class="line"></span><br><span class="line">resource r0 {</span><br><span class="line">net { </span><br><span class="line"><span class="comment">#on-congestion pull-ahead;</span></span><br><span class="line"><span class="comment">#congestion-fill 1G;</span></span><br><span class="line"><span class="comment">#congestion-extents 3000;</span></span><br><span class="line"><span class="comment">#sndbuf-size 1024k; </span></span><br><span class="line">sndbuf-size 0;</span><br><span class="line">max-buffers 8000;</span><br><span class="line">max-epoch-size 8000; </span><br><span class="line">}</span><br><span class="line">disk {</span><br><span class="line"><span class="comment">#no-disk-barrier;</span></span><br><span class="line"><span class="comment">#no-disk-flushes;</span></span><br><span class="line">no-md-flushes;</span><br><span class="line">}</span><br><span class="line">syncer {</span><br><span class="line">c-plan-ahead 20;</span><br><span class="line">c-fill-target 50k;</span><br><span class="line">c-min-rate 10M;</span><br><span class="line">al-extents 3833;</span><br><span class="line">rate 100M;</span><br><span class="line">use-rle;</span><br><span class="line">}</span><br><span class="line">startup { become-primary-on master ; }</span><br><span class="line">protocol C;</span><br><span class="line"></span><br><span class="line">on master {</span><br><span class="line">device /dev/drbd0;</span><br><span class="line">disk /dev/xvdb;</span><br><span class="line">meta-disk internal;</span><br><span class="line">address 192.168.0.10:7801;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">on slave {</span><br><span class="line">device /dev/drbd0;</span><br><span class="line">disk /dev/xvdb;</span><br><span class="line">meta-disk internal;</span><br><span class="line">address 192.168.0.11:7801;</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><p>The file <strong>r0.res</strong> should be same on both the servers.</p></li><li><p>Now using the drbdadm utility initialize the meta data storage. On each server execute:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ drbdadm create-md r0</span><br></pre></td></tr></table></figure></li><li><p>Next, on both hosts, start the drbd daemon:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ /etc/init.d/drbd start</span><br></pre></td></tr></table></figure></li><li><p>On Primary Server, run:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ drbdadm -- --overwrite-data-of-peer primary all</span><br></pre></td></tr></table></figure></li><li><p>After executing the above command, the data will start syncing with the Secondary server. To watch the progress, on Secondary Server enter the following:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ watch -n1 cat /proc/drbd</span><br></pre></td></tr></table></figure></li><li><p>Finally, add a filesystem to <strong>/dev/drbd0</strong> and mount it:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ mkfs.ext4 /dev/drbd0</span><br><span class="line">$ mkdir /drbd</span><br><span class="line">$ mount /dev/drbd0 /drbd</span><br></pre></td></tr></table></figure></li></ol><hr><h2 id="Install-Heartbeat-for-node-failure-detection"><a href="#Install-Heartbeat-for-node-failure-detection" class="headerlink" title="Install Heartbeat (for node failure detection)"></a>Install Heartbeat (for node failure detection)</h2><ol class="ui list"><li><p>Install heartbeat:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ apt-get install heartbeat</span><br></pre></td></tr></table></figure></li><li><p>Edit the ha.cf file under /etc/ha.d/:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">$ vim ha.cf</span><br><span class="line"></span><br><span class="line"><span class="comment"># Give cluster 30 seconds to start</span></span><br><span class="line">initdead 60</span><br><span class="line"><span class="comment"># Keep alive packets every 1 second</span></span><br><span class="line">keepalive 1</span><br><span class="line"><span class="comment"># Misc settings</span></span><br><span class="line">traditional_compression off</span><br><span class="line">deadtime 60</span><br><span class="line">deadping 60</span><br><span class="line">warntime 5</span><br><span class="line"><span class="comment"># Nodes in cluster</span></span><br><span class="line">node primary.nfs.server secondary.nfs.server</span><br><span class="line"><span class="comment"># Use logd, configure /etc/logd.cf</span></span><br><span class="line">use_logd on</span><br><span class="line"><span class="comment"># Don&apos;t move service back to preferred host when it comes up</span></span><br><span class="line">auto_failback off</span><br><span class="line"><span class="comment"># Takover if pings (above) fail</span></span><br><span class="line">respawn hacluster /usr/lib/heartbeat/ipfail</span><br><span class="line"><span class="comment">##### Use unicast instead of default multicast so firewall rules are easier</span></span><br><span class="line"><span class="comment"># primary</span></span><br><span class="line">ucast eth0 192.168.0.10</span><br><span class="line"><span class="comment"># secondary</span></span><br><span class="line">ucast eth0 192.168.0.11</span><br><span class="line">bcast eth0</span><br></pre></td></tr></table></figure></li><li><p>Edit <strong>haresources</strong> file for heartbeat to use:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ vim haresources</span><br><span class="line"></span><br><span class="line">primary.nfs.server drbddisk::r0 Filesystem::/dev/drbd0::/drbd::ext4 vipup nfs-kernel-server</span><br></pre></td></tr></table></figure></li><li><p>Edit the authkeys file (for authentication between nodes):</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ vim authkeys</span><br><span class="line"></span><br><span class="line"><span class="comment"># Automatically generated authkeys file</span></span><br><span class="line">auth 1</span><br><span class="line">1 sha1 1a8c3f11ca9e56497a1387c40ea95ce1</span><br></pre></td></tr></table></figure><p> or, generate the file from below command:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">cat &lt;&lt;EOF &gt; /etc/ha.d/authkeys</span><br><span class="line">       <span class="comment"># Automatically generated authkeys file</span></span><br><span class="line">       auth 1</span><br><span class="line">       1 sha1 `dd <span class="keyword">if</span>=/dev/urandom count=4 2&gt;/dev/null | md5sum | cut -c1-32`</span><br><span class="line">EOF</span><br></pre></td></tr></table></figure></li><li><p>Enable logging for Heartbeat:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ vim /etc/logd.cf</span><br><span class="line"></span><br><span class="line">debugfile /var/<span class="built_in">log</span>/ha-debug</span><br><span class="line">logfile/var/<span class="built_in">log</span>/ha-log</span><br><span class="line">syslogprefix linux-ha</span><br></pre></td></tr></table></figure></li><li><p>Create softlink of nfs-kernel-server in /etc/ha.d/resource.d/ folder:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ ln -s /etc/init.d/nfs-kernel-server /etc/ha.d/resource.d/</span><br></pre></td></tr></table></figure></li><li><p>Add fstab entries for auto mounting device using heartbeat:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># DRBD, mounted by heartbeat</span></span><br><span class="line">/dev/drbd0/drbdext4noatime,noauto,nobarrier0 0</span><br></pre></td></tr></table></figure></li></ol><hr><h2 id="Configure-NFS-Exports"><a href="#Configure-NFS-Exports" class="headerlink" title="Configure NFS Exports"></a>Configure NFS Exports</h2><ol class="ui list"><li><p>Edit the exports file:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ vim /etc/exports</span><br><span class="line"></span><br><span class="line">/drbd   192.168.0.0/24(rw,async,no_subtree_check,fsid=0)</span><br></pre></td></tr></table></figure></li><li><p>Export the configuration to the network:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ exportfs -a</span><br></pre></td></tr></table></figure></li></ol><h2 id="NFS-Client"><a href="#NFS-Client" class="headerlink" title="NFS Client:"></a>NFS Client:</h2><ol class="ui list"><li><p>Install NFS packages on client so that exports can be mounted:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ apt-get install nfs-common</span><br></pre></td></tr></table></figure></li><li><p>Check for mounts available:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ showmount -e 192.168.0.12</span><br></pre></td></tr></table></figure></li><li><p>Mount the shared folder on the server:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ mkdir /drbd</span><br><span class="line">$ mount -vvv 192.168.0.12:/drbd/ -o nfsvers=3,rsize=32768,wsize=32768,hard,timeo=50,<span class="built_in">bg</span>,actimeo=3,noatime,nodiratime,intr /drbd/</span><br><span class="line">$ df -h</span><br></pre></td></tr></table></figure></li><li><p>Start copying data (for testing):</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ rsync -av --progress --append --bwlimit=10240 /drbd/A_BIG_FILE /tmp/</span><br></pre></td></tr></table></figure></li></ol><hr><h2 id="Testing-NFS-Fail-over"><a href="#Testing-NFS-Fail-over" class="headerlink" title="Testing NFS Fail-over:"></a>Testing NFS Fail-over:</h2><ol class="ui list"><li>From another system, mount the NFS share from the cluster</li><li>Use rsync &#x2013;progress -av to start copying a large file (1-2 GB) to the share.</li><li>When the progress is 20%-30%, stop heartbeat service on primary (or turn off/reboot instance)</li><li>Rsync will lock up (as intended) due to NFS blocking.</li><li>After 5-10 seconds, the file should continue transferring until finished with no errors.</li><li>Do an md5 checksum comparison of the original file and the file on the NFS share.</li><li>Both files should be identical, if not, there was corruption of some kind.</li><li>Try the test again by reading from NFS, rather than writing to it</li></ol><hr><h3 id="References"><a href="#References" class="headerlink" title="References:"></a>References:</h3><ul class="ui list"><li><a href="https://www.drbd.org/en/doc/users-guide-83/s-heartbeat-r1" target="_blank" rel="noopener">https://www.drbd.org/en/doc/users-guide-83/s-heartbeat-r1</a></li><li><a href="https://www.howtoforge.com/high_availability_heartbeat_centos#configuration" target="_blank" rel="noopener">https://www.howtoforge.com/high_availability_heartbeat_centos#configuration</a></li><li><a href="https://www.howtoforge.com/high-availability-nfs-with-drbd-plus-heartbeat#heartbeat" target="_blank" rel="noopener">https://www.howtoforge.com/high-availability-nfs-with-drbd-plus-heartbeat#heartbeat</a></li><li><a href="https://aws.amazon.com/articles/2127188135977316" target="_blank" rel="noopener">https://aws.amazon.com/articles/2127188135977316</a></li><li><a href="https://www.vivaolinux.com.br/artigo/Instalando-DRBD-+-Heartbeat-no-Debian-6?pagina=3" target="_blank" rel="noopener">https://www.vivaolinux.com.br/artigo/Instalando-DRBD-+-Heartbeat-no-Debian-6?pagina=3</a></li><li><a href="http://linuxmanage.com/fast-failover-configuration-with-drbd-and-heartbeat-on-debian-squeeze.html" target="_blank" rel="noopener">http://linuxmanage.com/fast-failover-configuration-with-drbd-and-heartbeat-on-debian-squeeze.html</a></li></ul>]]></content>
    
    <summary type="html">
    
      
      
        &lt;h1 id=&quot;Setup-NFS-Failover-with-DRBD-and-Heartbeat-on-AWS-EC2&quot;&gt;&lt;a href=&quot;#Setup-NFS-Failover-with-DRBD-and-Heartbeat-on-AWS-EC2&quot; class=&quot;heade
      
    
    </summary>
    
      <category term="Amazon Web Services" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/"/>
    
      <category term="Linux" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Linux/"/>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="ec2" scheme="https://blog.vrnchndk.in/tags/ec2/"/>
    
      <category term="linux" scheme="https://blog.vrnchndk.in/tags/linux/"/>
    
      <category term="ubuntu" scheme="https://blog.vrnchndk.in/tags/ubuntu/"/>
    
      <category term="amazon" scheme="https://blog.vrnchndk.in/tags/amazon/"/>
    
      <category term="drbd" scheme="https://blog.vrnchndk.in/tags/drbd/"/>
    
  </entry>
  
  <entry>
    <title>AMI Backups and Retention using AWS Lambda</title>
    <link href="https://blog.vrnchndk.in/2018/08/04/AMI-Backups-and-Retention-using-AWS-Lambda/"/>
    <id>https://blog.vrnchndk.in/2018/08/04/AMI-Backups-and-Retention-using-AWS-Lambda/</id>
    <published>2018-08-04T16:31:39.000Z</published>
    <updated>2018-08-04T16:31:39.000Z</updated>
    
    <content type="html"><![CDATA[<p>Hello folks. It has been a long time since I have posted anything. I was involved heavily under some projects and what not. Maybe it could be due to lack of motivation too :-P. (will do something about this too).</p><p>Anyhow, this post will help you with one of the top features required in any AWS account.</p><h3 id="AMI-Backups-and-Retentions"><a href="#AMI-Backups-and-Retentions" class="headerlink" title="AMI Backups and Retentions"></a>AMI Backups and Retentions</h3><p>Normally, we would launch a separate server and put our scripts there on cron. However, this will be serverless and we&#x2019;ll be using AWS Lambda to achieve this.</p><hr><p><strong>NOTE</strong>: AMI and Instance Names and Name Tags must be between <code>3</code> and <code>128</code> characters long, may contain <code>letters</code>, <code>numbers</code>, <code>(</code>, <code>)</code>, <code>.</code>, <code>-</code>, <code>/</code> and <code>_</code> only. <em>Not following this nomenclature will lead to failure of the lambda function.</em></p><p>Here, we are using 2 AWS Lambda functions viz., <strong>createAMI</strong> and <strong>deleteAMI</strong>, which will create AMIs and delete AMIs, respectively. Both these functions are explained in detail below. <strong><u>Please note that both the lambda functions cover all the regions.</u></strong></p><hr><h2 id="createAMI"><a href="#createAMI" class="headerlink" title="createAMI"></a>createAMI</h2><p><strong>Table of details:</strong></p><table><thead><tr><th>Index</th><th>Value</th></tr></thead><tbody><tr><td>Name of the Lambda Function</td><td><code>createAMI</code></td></tr><tr><td>Timeout</td><td><code>5 min</code></td></tr><tr><td>Role Permissions</td><td><code>ec2:*</code></td></tr><tr><td>Runtime</td><td><code>python2.7</code></td></tr><tr><td>File Name</td><td><code>createAMI.py</code></td></tr><tr><td>Schedule</td><td><code>rate(5 minutes)</code></td></tr></tbody></table><p><strong>Documentation:</strong></p><p>The lambda function make use of tags on EC2 intances which provides all the information required to create an AMI. The table below explains the tags required.</p><table><thead><tr><th>Tag Name</th><th>Format</th><th>Default Value</th><th>Mandatory Tag</th></tr></thead><tbody><tr><td>AMIBackup</td><td>Yes/No</td><td>No</td><td>Yes</td></tr><tr><td>AMITime</td><td>HH:MM</td><td>15:00</td><td>Yes</td></tr><tr><td>Reboot</td><td>Yes/No</td><td>No</td><td>No</td></tr><tr><td>Retention</td><td>Whole Number</td><td>7</td><td>No</td></tr></tbody></table><p>In the above table:</p><ul class="ui list"><li><code>AMIBackup</code>: It is used to specify which Instance has to be backed up.</li><li><code>AMITime</code>: It is used to specify the time <strong>in UTC</strong> when the AMI has to be created. </li><li><code>Reboot</code>: It is used to specify if the instance has to be rebooted when creating the AMI.</li><li><code>Retention</code>: It is used to specify the AMI retention period in <strong>days</strong>.</li></ul><hr><h2 id="deleteAMI"><a href="#deleteAMI" class="headerlink" title="deleteAMI"></a>deleteAMI</h2><p><strong>Table of details:</strong></p><table><thead><tr><th>Name</th><th>Value</th></tr></thead><tbody><tr><td>Name of the Lambda Function</td><td>deleteAMI</td></tr><tr><td>Timeout</td><td>5 min</td></tr><tr><td>Role Permissions</td><td><code>ec2:*</code></td></tr><tr><td>Runtime</td><td><code>python2.7</code></td></tr><tr><td>File Name</td><td><code>deleteAMI.py</code></td></tr><tr><td>Schedule</td><td><code>cron(00 15 * * ? *)</code></td></tr></tbody></table><p><strong>Documentation:</strong></p><p>The lambda function make use of tags on EC2 intances which provides all the information required to delete an AMI. The tags are copied from the instance to AMI via <strong>createAMI</strong> function. The table below explains the tags required.</p><table><thead><tr><th>Tag Name</th><th>Format</th><th>Default Value</th></tr></thead><tbody><tr><td>AMIBackup</td><td>Yes/No</td><td>Copied from Instance</td></tr><tr><td>AMITime</td><td>HH:MM</td><td>Copied from Instance</td></tr><tr><td>Reboot</td><td>Yes/No</td><td>Copied from Instance</td></tr><tr><td>Retention</td><td>Whole Number</td><td>Copied from Instance</td></tr></tbody></table><p>In the above table(s):</p><ul class="ui list"><li><code>AMIBackup</code>: It is used to specify which Instance has to be backed up. <strong>deleteAMI</strong> will only get triggered if this tag is present with <code>Yes</code> value.</li><li><code>AMITime</code>: It is used to specify the time when the AMI has to be created.</li><li><code>Reboot</code>: It is used to specify if the instance has to be rebooted when creating the AMI.</li><li><code>Retention</code>: It is used to specify the AMI retention period in <strong>days</strong>.</li></ul><hr><p>Both the scripts are available in Github (<a href="https://github.com/varunchandak/automate-lambda-for-ami-creation-deletion" target="_blank" rel="noopener">HERE</a>).</p><p>Thanks for your time. Comments are welcome.</p>]]></content>
    
    <summary type="html">
    
      
      
        &lt;p&gt;Hello folks. It has been a long time since I have posted anything. I was involved heavily under some projects and what not. Maybe it coul
      
    
    </summary>
    
      <category term="Amazon Web Services" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/"/>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="python" scheme="https://blog.vrnchndk.in/tags/python/"/>
    
      <category term="lambda" scheme="https://blog.vrnchndk.in/tags/lambda/"/>
    
      <category term="automation" scheme="https://blog.vrnchndk.in/tags/automation/"/>
    
  </entry>
  
  <entry>
    <title>The actual Memory Usage on Linux</title>
    <link href="https://blog.vrnchndk.in/2018/01/15/The-actual-Memory-Usage-on-Linux/"/>
    <id>https://blog.vrnchndk.in/2018/01/15/The-actual-Memory-Usage-on-Linux/</id>
    <published>2018-01-15T08:52:59.000Z</published>
    <updated>2018-01-15T08:52:59.000Z</updated>
    
    <content type="html"><![CDATA[<p>Hi,</p><p>It has been quite long since I have posted here. The main reason was the lots of work in the office and preparing for my next AWS Certification, which was <code>AWS Solutions Architect Professional</code>. I will write a short note on the exam details and what I did to clear it, later on.</p><h2 id="What-was-happening"><a href="#What-was-happening" class="headerlink" title="What was happening ?"></a>What was happening ?</h2><p>Coming back to this topic, I had setup Cloudwatch alerts for Memory Usage and Disk Usage and then alert the stakeholders when the usage breaches the threshold set. For me it was set to <strong>90%</strong> for both the metrics. But upon observing closely, the alerts for memory usage were quite frequent, say 2 times per 5 minutes. This meant that something was definitely wrong and I needed to recheck the script deployed, fast.</p><p>Upon inspection, my previous script was calculating the memory from <code>free -m</code> command, using the first column:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">             total       used       free     shared    buffers     cached</span><br><span class="line">Mem:          7984       5423       2561          0        385       3130</span><br><span class="line">-/+ buffers/cache:       1907       6077</span><br><span class="line">Swap:            0          0          0</span><br></pre></td></tr></table></figure><p>The calculation was as follows:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Memory Usage Percentage = (Used/Total) * 100</span><br></pre></td></tr></table></figure><p>This comes out to be <strong><u>67.92%</u></strong>.</p><p>Now I had used a custom script <code>ps_mem.py</code> <a href="https://raw.githubusercontent.com/pixelb/ps_mem/master/ps_mem.py" target="_blank" rel="noopener">LINK</a> to calculate the approximate memory usage:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">[ec2-user@ip-172-31-X-Y tmp]$ sudo ./ps_mem.py</span><br><span class="line"> Private  +   Shared  =  RAM usedProgram</span><br><span class="line"></span><br><span class="line">144.0 KiB +  58.0 KiB = 202.0 KiBagetty</span><br><span class="line">168.0 KiB +  44.5 KiB = 212.5 KiBrngd</span><br><span class="line">196.0 KiB +  68.0 KiB = 264.0 KiBatd</span><br><span class="line">272.0 KiB +  76.5 KiB = 348.5 KiBirqbalance</span><br><span class="line">444.0 KiB + 164.5 KiB = 608.5 KiBrpcbind</span><br><span class="line">580.0 KiB + 140.5 KiB = 720.5 KiBauditd</span><br><span class="line">628.0 KiB +  98.5 KiB = 726.5 KiBdbus-daemon</span><br><span class="line">680.0 KiB + 115.0 KiB = 795.0 KiBcrond</span><br><span class="line">532.0 KiB + 305.0 KiB = 837.0 KiBmingetty (6)</span><br><span class="line">780.0 KiB + 125.5 KiB = 905.5 KiBinit</span><br><span class="line">356.0 KiB + 569.5 KiB = 925.5 KiBmysqld_safe</span><br><span class="line">856.0 KiB + 254.5 KiB =   1.1 MiBrpc.statd</span><br><span class="line">636.0 KiB + 570.5 KiB =   1.2 MiBbash</span><br><span class="line">492.0 KiB + 885.5 KiB =   1.3 MiBudevd (3)</span><br><span class="line">  1.1 MiB + 421.0 KiB =   1.5 MiBntpd</span><br><span class="line">  1.0 MiB + 560.0 KiB =   1.5 MiBsudo</span><br><span class="line">  1.0 MiB + 547.5 KiB =   1.6 MiBdhclient (2)</span><br><span class="line">  2.4 MiB + 580.5 KiB =   3.0 MiBcupsd</span><br><span class="line">  1.4 MiB +   2.9 MiB =   4.3 MiBsshd (3)</span><br><span class="line">  3.4 MiB +   1.2 MiB =   4.6 MiBsendmail.sendmail (2)</span><br><span class="line">  4.7 MiB + 129.0 KiB =   4.8 MiBrsyslogd</span><br><span class="line">  9.9 MiB +  14.5 KiB =   9.9 MiBdocker-containerd</span><br><span class="line"> 34.7 MiB + 108.0 KiB =  34.8 MiBdockerd</span><br><span class="line"> 43.5 MiB + 732.0 KiB =  44.2 MiBmysqld</span><br><span class="line">  1.4 GiB +   2.0 MiB =   1.4 GiBjava (2)</span><br><span class="line">---------------------------------</span><br><span class="line">                          1.5 GiB</span><br><span class="line">=================================</span><br><span class="line">Warning: Swap is not reported by this system.</span><br></pre></td></tr></table></figure><p>As per the output above, the actual memory usage is about 1.5 GB or 1536 MB, which is <strong>19.23%</strong>.</p><hr><h2 id="Calculating-the-memory-usage-properly"><a href="#Calculating-the-memory-usage-properly" class="headerlink" title="Calculating the memory usage, properly"></a>Calculating the memory usage, properly</h2><p>I tried various flavours of linux, such as Ubuntu, RHEL, CentOS and Amazon Linux, and found variations of the commands such as:</p><ul class="ui list"><li><code>free -m</code></li><li><code>cat /proc/meminfo</code></li></ul><p>However, 1 specific command <code>vmstat</code> was giving the output <em>almost</em> properly among all the linux flavours. So I decided to go with it.</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"># vmstat -s</span><br><span class="line">      8176604  total memory</span><br><span class="line">      5555796  used memory</span><br><span class="line">      4379188  active memory</span><br><span class="line">       745036  inactive memory</span><br><span class="line">      2620808  free memory</span><br><span class="line">       394516  buffer memory</span><br><span class="line">      3206024  swap cache</span><br><span class="line">            0  total swap</span><br><span class="line">            0  used swap</span><br><span class="line">            0  free swap</span><br><span class="line">       664123 non-nice user cpu ticks</span><br><span class="line">           45 nice user cpu ticks</span><br><span class="line">       138166 system cpu ticks</span><br><span class="line">     75768535 idle cpu ticks</span><br><span class="line">        39822 IO-wait cpu ticks</span><br><span class="line">            0 IRQ cpu ticks</span><br><span class="line">         1913 softirq cpu ticks</span><br><span class="line">        12845 stolen cpu ticks</span><br><span class="line">      5794205 pages paged in</span><br><span class="line">     10155760 pages paged out</span><br><span class="line">            0 pages swapped in</span><br><span class="line">            0 pages swapped out</span><br><span class="line">     57218586 interrupts</span><br><span class="line">    101894616 CPU context switches</span><br><span class="line">   1515624326 boot time</span><br><span class="line">       589690 forks</span><br></pre></td></tr></table></figure><p>In the above output, we are looking for:</p><ul class="ui list"><li><code>total memory</code></li><li><code>free memory</code></li><li><code>buffer memory</code></li><li><code>swap cache</code>, or simply <code>cache</code>.</li></ul><h3 id="Command-1"><a href="#Command-1" class="headerlink" title="Command 1:"></a>Command 1:</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Gathering info</span></span><br><span class="line">vmstat -s | sed -e <span class="string">&apos;s/^[ \t]*//&apos;</span> | cut -d<span class="string">&apos; &apos;</span> -f1,3- | tr -s <span class="string">&apos; &apos;</span> <span class="string">&apos;,&apos;</span> | grep -e <span class="string">&apos;total,memory&apos;</span> -e <span class="string">&apos;free,memory&apos;</span> -e <span class="string">&apos;buffer,memory&apos;</span> -e <span class="string">&apos;swap,cache&apos;</span> | awk -F, <span class="string">&apos;{print $1/1024&quot;,&quot;$2&quot; &quot;$3}&apos;</span></span><br></pre></td></tr></table></figure><h4 id="Output"><a href="#Output" class="headerlink" title="Output:"></a>Output:</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">7984.96,total memory</span><br><span class="line">2553.59,free memory</span><br><span class="line">386.012,buffer memory</span><br><span class="line">3131.41,swap cache</span><br></pre></td></tr></table></figure><p>With some mathematical formulas, I am able to calculate the near accurate (actual) memory usage on the system.</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Total Memory</span></span><br><span class="line">MemTotal=<span class="string">&quot;<span class="variable">$(cat /tmp/mem-usage.csv | grep -e &apos;total memory&apos; | awk -F, &apos;{print $1}&apos;)</span>&quot;</span></span><br><span class="line"><span class="comment"># Free Memory</span></span><br><span class="line">MemFree=<span class="string">&quot;<span class="variable">$(cat /tmp/mem-usage.csv | grep -e &apos;free memory&apos; | awk -F, &apos;{print $1}&apos;)</span>&quot;</span></span><br><span class="line"><span class="comment"># Buffers</span></span><br><span class="line">Buffers=<span class="string">&quot;<span class="variable">$(cat /tmp/mem-usage.csv | grep -e &apos;buffer memory&apos; | awk -F, &apos;{print $1}&apos;)</span>&quot;</span></span><br><span class="line"><span class="comment"># Cached</span></span><br><span class="line">Cached=<span class="string">&quot;<span class="variable">$(cat /tmp/mem-usage.csv | grep -e &apos;swap cache&apos; | awk -F, &apos;{print $1}&apos;)</span>&quot;</span></span><br><span class="line"><span class="comment"># Calculating total free memory (Free + Buffers + cached)</span></span><br><span class="line">TotalMemFree=<span class="string">&quot;<span class="variable">$(bc -l &lt;&lt;&lt; $MemFree+$Buffers+$Cached)</span>&quot;</span></span><br><span class="line"><span class="comment"># Calculating total memory used (Total Memory - Total Free Memory)</span></span><br><span class="line">TotalMemUsed=<span class="string">&quot;<span class="variable">$(bc -l &lt;&lt;&lt; $MemTotal-$TotalMemFree)</span>&quot;</span></span><br><span class="line"><span class="comment"># Percentage Used</span></span><br><span class="line">mem=<span class="string">&quot;<span class="variable">$(bc &lt;&lt;&lt; &quot;scale=2; $TotalMemUsed*100/$MemTotal&quot;)</span>&quot;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$mem</span>&quot;</span></span><br></pre></td></tr></table></figure><hr><h3 id="Conclusion"><a href="#Conclusion" class="headerlink" title="Conclusion"></a>Conclusion</h3><p>Till now, this is the best method I have come up with, to calculate the memory usage of the linux boxes. If you have any other method to calculate the memory usage, do let me know in the comments boxes.</p><p>The Memory and Disk Usage script is available on my <a href="https://github.com/varunchandak/aws-scripts/blob/master/automate-custom-metrics-installation/linux/custom-metrics-disk-memory-linux.sh" target="_blank" rel="noopener">Github</a> account.</p>]]></content>
    
    <summary type="html">
    
      
      
        &lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;It has been quite long since I have posted here. The main reason was the lots of work in the office and preparing for my next 
      
    
    </summary>
    
      <category term="Amazon Web Services" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/"/>
    
      <category term="Linux" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Linux/"/>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="ec2" scheme="https://blog.vrnchndk.in/tags/ec2/"/>
    
      <category term="cloudwatch" scheme="https://blog.vrnchndk.in/tags/cloudwatch/"/>
    
      <category term="ubuntu" scheme="https://blog.vrnchndk.in/tags/ubuntu/"/>
    
      <category term="centos" scheme="https://blog.vrnchndk.in/tags/centos/"/>
    
      <category term="rhel" scheme="https://blog.vrnchndk.in/tags/rhel/"/>
    
      <category term="amazon linux" scheme="https://blog.vrnchndk.in/tags/amazon-linux/"/>
    
      <category term="RAM" scheme="https://blog.vrnchndk.in/tags/RAM/"/>
    
      <category term="Memory" scheme="https://blog.vrnchndk.in/tags/Memory/"/>
    
  </entry>
  
  <entry>
    <title>Using email IDs as IAM Username</title>
    <link href="https://blog.vrnchndk.in/2017/10/05/Using-email-IDs-as-IAM-Username/"/>
    <id>https://blog.vrnchndk.in/2017/10/05/Using-email-IDs-as-IAM-Username/</id>
    <published>2017-10-05T09:35:16.000Z</published>
    <updated>2017-10-05T09:35:16.000Z</updated>
    
    <content type="html"><![CDATA[<p>Let&#x2019;s face it. Finding out which IAM User belongs to whom, is a mammoth task, if you have a long list of them.</p><h3 id="What-if-we-use-email-ID&#x2019;s-as-IAM-username"><a href="#What-if-we-use-email-ID&#x2019;s-as-IAM-username" class="headerlink" title="What if we use email ID&#x2019;s as IAM username ?"></a>What if we use email ID&#x2019;s as IAM username ?</h3><p>I think it&#x2019;s a good idea as it can be helpful in many ways:</p><ul class="ui list"><li>Send an email directly to the user with their credentials, instead of scavenging documents or emails to check the user.</li><li>Email ID can represent where the user belongs. Let&#x2019;s assume 2 users, <a href="mailto:`foo@bar.com" target="_blank" rel="noopener">`foo@bar.com</a><code>and</code><a href="mailto:bar@foo.com" target="_blank" rel="noopener">bar@foo.com</a>`. Here we can see 2 users with different domains, which clearly distinguishes the IAM users.</li><li>Set&#x2019;s a unique name to each IAM user.</li><li>The most important (what I think) is, using automation with IAM users. Let&#x2019;s say we have password rotation script in place, which resets the password of all IAM users. Using automation, we can send the password alerts to the email IDs using command line or the password rotation script.</li></ul><h3 id="What-about-service-based-IAM-users"><a href="#What-about-service-based-IAM-users" class="headerlink" title="What about service-based IAM users?"></a>What about service-based IAM users?</h3><p>As far as service-based IAM users are concerned, they can be named as: <code>&lt;SERVICE_NAME&gt;</code>-<code>&lt;REQUESTER_EMAILID&gt;</code></p><p>This way, we can keep track of who has requested IAM credentials. I think, apart from service-based IAM users, rest of the users can be changed to email addresses. I am going to implement this as a prerequisite across all the AWS accounts I am handling to streamline methods and communications.</p><p>Comments are welcome.</p>]]></content>
    
    <summary type="html">
    
      
      
        &lt;p&gt;Let&amp;#x2019;s face it. Finding out which IAM User belongs to whom, is a mammoth task, if you have a long list of them.&lt;/p&gt;
&lt;h3 id=&quot;What-if
      
    
    </summary>
    
      <category term="Amazon Web Services" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/"/>
    
      <category term="Shell Scripting" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Shell-Scripting/"/>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="awscli" scheme="https://blog.vrnchndk.in/tags/awscli/"/>
    
  </entry>
  
  <entry>
    <title>Automatic Route53 DNS Entries using Shell Script</title>
    <link href="https://blog.vrnchndk.in/2017/06/13/Automatic-Route53-DNS-Entries-using-Shell-Script/"/>
    <id>https://blog.vrnchndk.in/2017/06/13/Automatic-Route53-DNS-Entries-using-Shell-Script/</id>
    <published>2017-06-13T09:35:45.000Z</published>
    <updated>2017-06-13T09:35:45.000Z</updated>
    
    <content type="html"><![CDATA[<p>Alright ! How to deal with tons of Route53 entries to be created in AWS ?</p><p><strong>Two Ways</strong>:</p><ol class="ui list"><li>Using AWS Console, i.e., manually one by one</li><li>Using automation, i.e., using shell scripts or other automation tool (terraform/ansible/blah/blah).</li></ol><p>For this, I am using shell scripting to <code>only add new</code> entries to Route53. I&#x2019;ll explain the code in sections below.</p><h3 id="Basic-stuff"><a href="#Basic-stuff" class="headerlink" title="Basic stuff"></a>Basic stuff</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">export</span> PATH=/usr/<span class="built_in">local</span>/bin:/usr/bin:/bin:/usr/sbin:/sbin</span><br><span class="line"><span class="built_in">alias</span> aws=<span class="string">&apos;&apos;</span><span class="string">&quot;<span class="variable">$(which aws)</span>&quot;</span><span class="string">&apos; --profile &lt;PROFILE_NAME&gt; --output text&apos;</span></span><br><span class="line"><span class="built_in">shopt</span> -s expand_aliases</span><br></pre></td></tr></table></figure><p>The above code will make use of proper <strong>PATHs</strong> and will not break any commands in the script (read <code>command not found</code>).</p><h3 id="Main-Logic"><a href="#Main-Logic" class="headerlink" title="Main Logic"></a>Main Logic</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">DNSFILE=<span class="string">&quot;<span class="variable">$2</span>&quot;</span></span><br><span class="line">DOMAINNAME=<span class="string">&quot;<span class="variable">$1</span>&quot;</span></span><br><span class="line">TIMESTAMP=$(date +%s)</span><br><span class="line"><span class="built_in">export</span> MXCOUNT=$(cut -d, -f2 <span class="string">&quot;<span class="variable">$DNSFILE</span>&quot;</span> | grep -i -c MX)</span><br><span class="line"></span><br><span class="line"><span class="comment"># Create Hosted Zone</span></span><br><span class="line">HOSTEDZONE_ID=<span class="string">&quot;<span class="variable">$(aws route53 create-hosted-zone --name &quot;$DOMAINNAME&quot; --caller-reference &quot;$TIMESTAMP&quot; --query &apos;HostedZone.Id&apos; | cut -d\/ -f3)</span>&quot;</span></span><br><span class="line">sleep 5</span><br><span class="line">IFS=<span class="string">&apos;,&apos;</span></span><br><span class="line"><span class="keyword">while</span> <span class="built_in">read</span> DNSNAME DNSTYPE DNSVALUE</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line"><span class="keyword">case</span> <span class="string">&quot;<span class="variable">$DNSTYPE</span>&quot;</span> <span class="keyword">in</span></span><br><span class="line">a|A)    addArecord <span class="string">&quot;<span class="variable">$DNSNAME</span>&quot;</span> <span class="string">&quot;<span class="variable">$DNSVALUE</span>&quot;</span>; sleep 5   ;;</span><br><span class="line">cname|CNAME)  addCNAMErecord <span class="string">&quot;<span class="variable">$DNSNAME</span>&quot;</span> <span class="string">&quot;<span class="variable">$DNSVALUE</span>&quot;</span>; sleep 5 ;;</span><br><span class="line">mx|MX)    mxrecfunc ;;</span><br><span class="line">*)<span class="built_in">echo</span> <span class="string">&quot;Work in progress; Do manually&quot;</span>;;</span><br><span class="line"><span class="keyword">esac</span></span><br><span class="line"><span class="keyword">done</span> &lt; <span class="string">&quot;<span class="variable">$DNSFILE</span>&quot;</span></span><br></pre></td></tr></table></figure><p>The script takes 2 arguments as input to the script.</p><ul class="ui list"><li><code>DNSFILE</code>: CSV file containing the DNS records in csv format. More details on this later</li><li><code>DOMAINNAME</code>: Domain name for which Hosted zone and DNS records are to be created.</li></ul><h4 id="Other-variables"><a href="#Other-variables" class="headerlink" title="Other variables:"></a>Other variables:</h4><ul class="ui list"><li><code>TIMESTAMP</code>: Unique timestamp to create a hosted zone. The one I have used is in <code>epoch</code> format.</li><li><code>MXCOUNT</code>: To calculate the number of MX records present in CSV file. Used in functions later.</li></ul><p>The above code will do the following in series of steps:</p><ol class="ui list"><li>Create Hosted Zone and store the ID in <code>HOSTEDZONE_ID</code> variable.</li><li>For each line in the <code>DNSFILE</code> file, call the corresponding function to create the record. Currently, only <code>A</code>, <code>CNAME</code> and <code>MX</code> records can be updated by the script. For other&#x2019;s it will be updated later on Github itself.</li></ol><h3 id="A-Record-function"><a href="#A-Record-function" class="headerlink" title="A Record function"></a>A Record function</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">addArecord</span></span> () {</span><br><span class="line"><span class="comment"># Add A records</span></span><br><span class="line">ARECJSON=<span class="string">&quot;<span class="variable">$(echo &apos;{</span></span></span><br><span class="line"><span class="string"><span class="variable">&quot;Comment&quot;: &quot;A Record for &apos;&quot;$1&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">&quot;Changes&quot;: [</span></span></span><br><span class="line"><span class="string"><span class="variable">{</span></span></span><br><span class="line"><span class="string"><span class="variable">&quot;Action&quot;: &quot;CREATE&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">&quot;ResourceRecordSet&quot;: {</span></span></span><br><span class="line"><span class="string"><span class="variable">&quot;Name&quot;: &quot;&apos;&quot;$1&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">&quot;Type&quot;: &quot;A&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">&quot;TTL&quot;: 300,</span></span></span><br><span class="line"><span class="string"><span class="variable">&quot;ResourceRecords&quot;: [</span></span></span><br><span class="line"><span class="string"><span class="variable">{</span></span></span><br><span class="line"><span class="string"><span class="variable">&quot;Value&quot;: &quot;&apos;&quot;$2&quot;&apos;&quot;</span></span></span><br><span class="line"><span class="string"><span class="variable">}</span></span></span><br><span class="line"><span class="string"><span class="variable">]</span></span></span><br><span class="line"><span class="string"><span class="variable">}</span></span></span><br><span class="line"><span class="string"><span class="variable">}</span></span></span><br><span class="line"><span class="string"><span class="variable">]</span></span></span><br><span class="line"><span class="string"><span class="variable">}&apos;)</span>&quot;</span></span><br><span class="line"></span><br><span class="line">aws route53 change-resource-record-sets --hosted-zone-id <span class="string">&quot;<span class="variable">$HOSTEDZONE_ID</span>&quot;</span> --change-batch <span class="string">&quot;<span class="variable">$ARECJSON</span>&quot;</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="Usage"><a href="#Usage" class="headerlink" title="Usage"></a>Usage</h4><p>addArecord <code>$DNSNAME</code> <code>$DNSVALUE</code></p><p>Since AWS <code>change-resource-record-sets</code> command accepts JSON, so I had to create a JSON in a variable to use it. The function takes 2 arguments as input. </p><p><strong>Example:</strong></p><p>addArecord <code>testexample.com</code> <code>1.2.3.4</code></p><hr><h3 id="CNAME-Record"><a href="#CNAME-Record" class="headerlink" title="CNAME Record"></a>CNAME Record</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">addCNAMErecord</span></span> () {</span><br><span class="line"><span class="comment"># Add CNAME records</span></span><br><span class="line">CNAMERECJSON=<span class="string">&quot;<span class="variable">$(echo &apos;{</span></span></span><br><span class="line"><span class="string"><span class="variable">  &quot;Comment&quot;: &quot;CNAME Record for &apos;&quot;$2&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">  &quot;Changes&quot;: [</span></span></span><br><span class="line"><span class="string"><span class="variable">    {</span></span></span><br><span class="line"><span class="string"><span class="variable">      &quot;Action&quot;: &quot;CREATE&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">      &quot;ResourceRecordSet&quot;: {</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;Name&quot;: &quot;&apos;&quot;$1&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;Type&quot;: &quot;CNAME&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;TTL&quot;: 300,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;ResourceRecords&quot;: [</span></span></span><br><span class="line"><span class="string"><span class="variable">          {</span></span></span><br><span class="line"><span class="string"><span class="variable">            &quot;Value&quot;: &quot;&apos;&quot;$2&quot;&apos;&quot;</span></span></span><br><span class="line"><span class="string"><span class="variable">          }</span></span></span><br><span class="line"><span class="string"><span class="variable">        ]</span></span></span><br><span class="line"><span class="string"><span class="variable">      }</span></span></span><br><span class="line"><span class="string"><span class="variable">    }</span></span></span><br><span class="line"><span class="string"><span class="variable">  ]</span></span></span><br><span class="line"><span class="string"><span class="variable">}&apos;)</span>&quot;</span></span><br><span class="line">aws route53 change-resource-record-sets --hosted-zone-id <span class="string">&quot;<span class="variable">$HOSTEDZONE_ID</span>&quot;</span> --change-batch <span class="string">&quot;<span class="variable">$CNAMERECJSON</span>&quot;</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>Similar is the case of CNAME record. <u>Only thing to take care of is this:</u></p><p>addCNAMErecord <code>www.testexample.com</code> <code>test example.com</code> (<strong>CORRECT</strong>)</p><p>and not</p><p>addCNAMErecord <code>testexample.com</code> <code>www.testexample.com</code> (<strong>WRONG</strong>)</p><hr><h3 id="MX-Records"><a href="#MX-Records" class="headerlink" title="MX Records"></a>MX Records</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">addMXrecord</span></span> () {</span><br><span class="line"></span><br><span class="line"><span class="comment"># Add MX records</span></span><br><span class="line">MXRECJSON=<span class="string">&quot;<span class="variable">$(echo -e &apos;{</span></span></span><br><span class="line"><span class="string"><span class="variable">  &quot;Comment&quot;: &quot;MX Record for &apos;&quot;$DOMAINNAME&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">  &quot;Changes&quot;: [</span></span></span><br><span class="line"><span class="string"><span class="variable">    {</span></span></span><br><span class="line"><span class="string"><span class="variable">      &quot;Action&quot;: &quot;CREATE&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">      &quot;ResourceRecordSet&quot;: {</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;Name&quot;: &quot;&apos;&quot;$DOMAINNAME&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;Type&quot;: &quot;MX&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;TTL&quot;: 300,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;ResourceRecords&quot;: [</span></span></span><br><span class="line"><span class="string"><span class="variable">          {</span></span></span><br><span class="line"><span class="string"><span class="variable">            &quot;Value&quot;: &quot;&apos;&quot;$2&quot;&apos;&quot;</span></span></span><br><span class="line"><span class="string"><span class="variable">          }</span></span></span><br><span class="line"><span class="string"><span class="variable">        ]</span></span></span><br><span class="line"><span class="string"><span class="variable">      }</span></span></span><br><span class="line"><span class="string"><span class="variable">    }</span></span></span><br><span class="line"><span class="string"><span class="variable">  ]</span></span></span><br><span class="line"><span class="string"><span class="variable">}&apos;)</span>&quot;</span></span><br><span class="line">aws route53 change-resource-record-sets --hosted-zone-id <span class="string">&quot;<span class="variable">$HOSTEDZONE_ID</span>&quot;</span> --change-batch <span class="string">&quot;<span class="variable">$MXRECJSON</span>&quot;</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">addMultipleMXrecord</span></span> () {</span><br><span class="line"><span class="built_in">unset</span> MX_RECORDS</span><br><span class="line"><span class="keyword">while</span> <span class="built_in">read</span> -r line; <span class="keyword">do</span> </span><br><span class="line">  MX_RECORDS+=( <span class="string">&quot;&quot;</span>$(python -c <span class="string">&apos;import json, sys; print(json.dumps([{&quot;Value&quot;: v} for v in sys.argv[1:]]))&apos;</span> <span class="string">&quot;<span class="variable">$line</span>&quot;</span>)<span class="string">&quot;&quot;</span> )</span><br><span class="line"><span class="keyword">done</span> &lt; &lt;(grep -i MX <span class="string">&quot;<span class="variable">$DNSFILE</span>&quot;</span> | cut -d, -f3)</span><br><span class="line"></span><br><span class="line">DNSVALUES=<span class="string">&quot;<span class="variable">$(echo &quot;${MX_RECORDS[@]}&quot; | jq -c -s add)</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Add MX records</span></span><br><span class="line">MXRECJSON=<span class="string">&quot;<span class="variable">$(echo -e &apos;{</span></span></span><br><span class="line"><span class="string"><span class="variable">  &quot;Comment&quot;: &quot;MX Record for &apos;&quot;$DOMAINNAME&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">  &quot;Changes&quot;: [</span></span></span><br><span class="line"><span class="string"><span class="variable">    {</span></span></span><br><span class="line"><span class="string"><span class="variable">      &quot;Action&quot;: &quot;CREATE&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">      &quot;ResourceRecordSet&quot;: {</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;Name&quot;: &quot;&apos;&quot;$DOMAINNAME&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;Type&quot;: &quot;MX&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;TTL&quot;: 300,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;ResourceRecords&quot;: &apos;&quot;$DNSVALUES&quot;&apos;</span></span></span><br><span class="line"><span class="string"><span class="variable">      }</span></span></span><br><span class="line"><span class="string"><span class="variable">    }</span></span></span><br><span class="line"><span class="string"><span class="variable">  ]</span></span></span><br><span class="line"><span class="string"><span class="variable">}&apos;)</span>&quot;</span></span><br><span class="line">aws route53 change-resource-record-sets --hosted-zone-id <span class="string">&quot;<span class="variable">$HOSTEDZONE_ID</span>&quot;</span> --change-batch <span class="string">&quot;<span class="variable">$MXRECJSON</span>&quot;</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">mxrecfunc</span></span>() {</span><br><span class="line">  <span class="keyword">if</span> [[ <span class="string">&quot;<span class="variable">$MXCOUNT</span>&quot;</span> == 1 ]]; <span class="keyword">then</span></span><br><span class="line">    addMXrecord <span class="string">&quot;<span class="variable">$DNSNAME</span>&quot;</span> <span class="string">&quot;<span class="variable">$DNSVALUE</span>&quot;</span></span><br><span class="line">    sleep 5</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">    addMultipleMXrecord</span><br><span class="line">    sleep 5</span><br><span class="line">  <span class="keyword">fi</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>Updating MX records is tricky. The values can be 1 or more than 1. For DNS with single MX record is easy. However, updating with 2 MX records cause replacing of 1st MX with the 2nd one.</p><p>Here, we are using 3 functions <code>addMXrecord</code>, <code>addMultipleMXrecord</code> and <code>mxrecfunc</code>.</p><h4 id="addMXrecord"><a href="#addMXrecord" class="headerlink" title="addMXrecord"></a>addMXrecord</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">addMXrecord</span></span> () {</span><br><span class="line"></span><br><span class="line"><span class="comment"># Add MX records</span></span><br><span class="line">MXRECJSON=<span class="string">&quot;<span class="variable">$(echo -e &apos;{</span></span></span><br><span class="line"><span class="string"><span class="variable">  &quot;Comment&quot;: &quot;MX Record for &apos;&quot;$DOMAINNAME&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">  &quot;Changes&quot;: [</span></span></span><br><span class="line"><span class="string"><span class="variable">    {</span></span></span><br><span class="line"><span class="string"><span class="variable">      &quot;Action&quot;: &quot;CREATE&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">      &quot;ResourceRecordSet&quot;: {</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;Name&quot;: &quot;&apos;&quot;$DOMAINNAME&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;Type&quot;: &quot;MX&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;TTL&quot;: 300,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;ResourceRecords&quot;: [</span></span></span><br><span class="line"><span class="string"><span class="variable">          {</span></span></span><br><span class="line"><span class="string"><span class="variable">            &quot;Value&quot;: &quot;&apos;&quot;$2&quot;&apos;&quot;</span></span></span><br><span class="line"><span class="string"><span class="variable">          }</span></span></span><br><span class="line"><span class="string"><span class="variable">        ]</span></span></span><br><span class="line"><span class="string"><span class="variable">      }</span></span></span><br><span class="line"><span class="string"><span class="variable">    }</span></span></span><br><span class="line"><span class="string"><span class="variable">  ]</span></span></span><br><span class="line"><span class="string"><span class="variable">}&apos;)</span>&quot;</span></span><br><span class="line">aws route53 change-resource-record-sets --hosted-zone-id <span class="string">&quot;<span class="variable">$HOSTEDZONE_ID</span>&quot;</span> --change-batch <span class="string">&quot;<span class="variable">$MXRECJSON</span>&quot;</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>The above code is similar to A records and CNAME records, and is applicable for 1 MX record only.</p><h4 id="addMultipleMXrecord"><a href="#addMultipleMXrecord" class="headerlink" title="addMultipleMXrecord"></a>addMultipleMXrecord</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">addMultipleMXrecord</span></span> () {</span><br><span class="line"><span class="built_in">unset</span> MX_RECORDS</span><br><span class="line"><span class="keyword">while</span> <span class="built_in">read</span> -r line; <span class="keyword">do</span> </span><br><span class="line">  MX_RECORDS+=( <span class="string">&quot;&quot;</span>$(python -c <span class="string">&apos;import json, sys; print(json.dumps([{&quot;Value&quot;: v} for v in sys.argv[1:]]))&apos;</span> <span class="string">&quot;<span class="variable">$line</span>&quot;</span>)<span class="string">&quot;&quot;</span> )</span><br><span class="line"><span class="keyword">done</span> &lt; &lt;(grep -i MX <span class="string">&quot;<span class="variable">$DNSFILE</span>&quot;</span> | cut -d, -f3)</span><br><span class="line">DNSVALUES=<span class="string">&quot;<span class="variable">$(echo &quot;${MX_RECORDS[@]}&quot; | jq -c -s add)</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Add MX records</span></span><br><span class="line">MXRECJSON=<span class="string">&quot;<span class="variable">$(echo -e &apos;{</span></span></span><br><span class="line"><span class="string"><span class="variable">  &quot;Comment&quot;: &quot;MX Record for &apos;&quot;$DOMAINNAME&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">  &quot;Changes&quot;: [</span></span></span><br><span class="line"><span class="string"><span class="variable">    {</span></span></span><br><span class="line"><span class="string"><span class="variable">      &quot;Action&quot;: &quot;CREATE&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">      &quot;ResourceRecordSet&quot;: {</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;Name&quot;: &quot;&apos;&quot;$DOMAINNAME&quot;&apos;&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;Type&quot;: &quot;MX&quot;,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;TTL&quot;: 300,</span></span></span><br><span class="line"><span class="string"><span class="variable">        &quot;ResourceRecords&quot;: &apos;&quot;$DNSVALUES&quot;&apos;</span></span></span><br><span class="line"><span class="string"><span class="variable">      }</span></span></span><br><span class="line"><span class="string"><span class="variable">    }</span></span></span><br><span class="line"><span class="string"><span class="variable">  ]</span></span></span><br><span class="line"><span class="string"><span class="variable">}&apos;)</span>&quot;</span></span><br><span class="line">aws route53 change-resource-record-sets --hosted-zone-id <span class="string">&quot;<span class="variable">$HOSTEDZONE_ID</span>&quot;</span> --change-batch <span class="string">&quot;<span class="variable">$MXRECJSON</span>&quot;</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>Here, the code gets a little nasty. For multiple MX values, <code>ResourceRecords</code> require Key Value in JSON format. So to get the code in JSON, I had to use a small <code>python</code> snippet to get values in array.</p><p>End result:</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[</span><br><span class="line">  {</span><br><span class="line">    <span class="attr">&quot;Value&quot;</span>: <span class="string">&quot;1 A&quot;</span></span><br><span class="line">  },</span><br><span class="line">  {</span><br><span class="line">    <span class="attr">&quot;Value&quot;</span>: <span class="string">&quot;2 B&quot;</span></span><br><span class="line">  }</span><br><span class="line">]</span><br></pre></td></tr></table></figure><p>or </p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[{<span class="attr">&quot;Value&quot;</span>:<span class="string">&quot;1 A&quot;</span>},{<span class="attr">&quot;Value&quot;</span>:<span class="string">&quot;2 B&quot;</span>}]</span><br></pre></td></tr></table></figure><p>Since the above have 2 values, it can be used to update MX records easily.</p><h4 id="mxrecfunc"><a href="#mxrecfunc" class="headerlink" title="mxrecfunc"></a>mxrecfunc</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">mxrecfunc</span></span>() {</span><br><span class="line">  <span class="keyword">if</span> [[ <span class="string">&quot;<span class="variable">$MXCOUNT</span>&quot;</span> == 1 ]]; <span class="keyword">then</span></span><br><span class="line">    addMXrecord <span class="string">&quot;<span class="variable">$DNSNAME</span>&quot;</span> <span class="string">&quot;<span class="variable">$DNSVALUE</span>&quot;</span></span><br><span class="line">    sleep 5</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">    addMultipleMXrecord</span><br><span class="line">    sleep 5</span><br><span class="line">  <span class="keyword">fi</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>The above function simply checks for the count of MX records in DNS file. If the count is 1, then <code>addMXrecord</code> is called, else <code>addMultipleMXrecord</code>.</p><hr><h3 id="DNS-File-Contents-Format"><a href="#DNS-File-Contents-Format" class="headerlink" title="DNS File Contents/Format"></a>DNS File Contents/Format</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">testexample.com,A,1.2.3.4</span><br><span class="line">qa.testexample.com,A,4.3.2.1</span><br><span class="line">ftp.testexample.com,CNAME,testexample.com</span><br><span class="line">www.testexample.com,CNAME,testexample.com</span><br><span class="line">testexample.com,MX,5 testexample1.com</span><br><span class="line">testexample.com,MX,10 testexample2.com</span><br></pre></td></tr></table></figure><hr><h3 id="Screenshots"><a href="#Screenshots" class="headerlink" title="Screenshots"></a>Screenshots</h3><h4 id="Terminal-Output"><a href="#Terminal-Output" class="headerlink" title="Terminal Output"></a>Terminal Output</h4><p><a class="magnific-img" href="/images/r53-terminal-output.png"><img src="/images/r53-terminal-output.png" alt="" class="ui centered image"></a></p><h4 id="AWS-Console-Output"><a href="#AWS-Console-Output" class="headerlink" title="AWS Console Output"></a>AWS Console Output</h4><p><a class="magnific-img" href="/images/r53-gui-output.png"><img src="/images/r53-gui-output.png" alt="" class="ui centered image"></a></p><p>Code is available on <a href="http://www.github.com" target="_blank" rel="noopener">Github</a></p><hr><h2 id="NOTES"><a href="#NOTES" class="headerlink" title="NOTES:"></a>NOTES:</h2><ol class="ui list"><li>I am not responsible for any command not working, it&#x2019;s working for me as intended.</li><li>Suggestions/Edits are welcome.</li><li>Use of <code>Python</code> and <code>jq</code> are  incorporated, make sure they are installed and configured properly.</li></ol>]]></content>
    
    <summary type="html">
    
      
      
        &lt;p&gt;Alright ! How to deal with tons of Route53 entries to be created in AWS ?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Two Ways&lt;/strong&gt;:&lt;/p&gt;
&lt;ol class=&quot;ui list&quot;&gt;
&lt;li&gt;
      
    
    </summary>
    
      <category term="Amazon Web Services" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/"/>
    
      <category term="Linux" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Linux/"/>
    
      <category term="Shell Scripting" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Linux/Shell-Scripting/"/>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="route53" scheme="https://blog.vrnchndk.in/tags/route53/"/>
    
      <category term="json" scheme="https://blog.vrnchndk.in/tags/json/"/>
    
      <category term="dns" scheme="https://blog.vrnchndk.in/tags/dns/"/>
    
      <category term="aws cli" scheme="https://blog.vrnchndk.in/tags/aws-cli/"/>
    
  </entry>
  
  <entry>
    <title>Automatic OpenVPN User Creation with Email Alert</title>
    <link href="https://blog.vrnchndk.in/2017/04/12/Automatic-OpenVPN-User-Creation-with-Email-Alert/"/>
    <id>https://blog.vrnchndk.in/2017/04/12/Automatic-OpenVPN-User-Creation-with-Email-Alert/</id>
    <published>2017-04-12T07:06:54.000Z</published>
    <updated>2017-04-12T07:06:54.000Z</updated>
    
    <content type="html"><![CDATA[<p>Normally, what we do is:</p><ul class="ui list"><li>use <code>easy-rsa</code> commandline tool to generate the <code>.crt</code>, <code>.key</code> files and the <code>.ovpn</code> file,</li><li>copy the required files to the desktop using <strong>WinSCP</strong> or <strong>FileZilla</strong> or any other software.</li></ul><p>The above seems very tedious task to perform manually everytime a user ID has to be created. In this blog post, I&#x2019;ll share a <u>Shell Script</u> to automate this process, as well as send an email with a <code>.zip</code> file attached, containing all the relevant files.</p><a id="more"></a><p>I am assuming the following is already in place:</p><ul class="ui list"><li><strong>OpenVPN Server</strong> on Linux (Version <code>OpenVPN 2.3.6 x86_64-redhat-linux-gnu</code>)</li><li><strong>Easy RSA 3</strong></li><li>OS: <code>Amazon Linux AMI 2016.03</code></li></ul><p>The script uses the following command line  tools to work:</p><ul class="ui list"><li><strong>zip</strong> (to zip all the necessary files)</li><li><strong>mutt</strong> (to send email with attachment)</li></ul><hr><h1 id="Usage-and-Working"><a href="#Usage-and-Working" class="headerlink" title="Usage and Working"></a>Usage and Working</h1><h3 id="Usage"><a href="#Usage" class="headerlink" title="Usage:"></a>Usage:</h3><p><code>./createvpnuser.sh &quot;OpenVPN User&quot; [To Email ID (without spaces)]</code></p><hr><h3 id="Working"><a href="#Working" class="headerlink" title="Working:"></a>Working:</h3><p>The script works in the following way:</p><ol class="ui list"><li>The script takes 2 arguments for userID and email address, respectively.</li><li>Password of the user is generated using <code>/dev/urandom</code>, which is a <em>pseudo random number generator</em>.</li><li><code>./easyrsa build-client-full &lt;USERID&gt; nopass</code> command is used to generate the required files.</li><li>The files are zipped to a predefined location.</li><li>An email is sent to address given in <strong>Step 1</strong>.</li></ol><h1 id="Full-Script"><a href="#Full-Script" class="headerlink" title="Full Script"></a>Full Script</h1><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line">SCRIPT_NAME=$(basename <span class="variable">$0</span>)</span><br><span class="line"><span class="function"><span class="title">usage</span></span> () {</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&apos;</span></span><br><span class="line"><span class="string">Usage: &apos;</span><span class="string">&quot;<span class="variable">$SCRIPT_NAME</span>&quot;</span><span class="string">&apos; &quot;OpenVPN User&quot; [To Email ID (without spaces)]</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">E.g.</span></span><br><span class="line"><span class="string">&apos;</span><span class="string">&quot;<span class="variable">$SCRIPT_NAME</span>&quot;</span><span class="string">&apos; anonymous someone@somewhere.com&apos;</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="string">&quot;<span class="variable">$#</span>&quot;</span> -ne 2 ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">        usage</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"></span><br><span class="line">USER_NAME=<span class="string">&quot;<span class="variable">$1</span>&quot;</span></span><br><span class="line">USERPASSWD=<span class="string">&quot;<span class="variable">$(cat /dev/urandom| tr -dc &apos;a-zA-Z0-9-_@#&apos;|fold -w 10 | head -n 1)</span>&quot;</span></span><br><span class="line">EMAIL_ID=<span class="string">&quot;<span class="variable">$2</span>&quot;</span></span><br><span class="line"><span class="comment">#Adding username</span></span><br><span class="line">useradd <span class="string">&quot;<span class="variable">$USER_NAME</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#Changing password</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$USER_NAME</span>:<span class="variable">$USERPASSWD</span>&quot;</span> | chpasswd</span><br><span class="line"></span><br><span class="line"><span class="comment">#Generating Key and Crt for user</span></span><br><span class="line"><span class="built_in">cd</span> /etc/openvpn/easy-rsa/</span><br><span class="line">./easyrsa build-client-full <span class="string">&quot;<span class="variable">$USER_NAME</span>&quot;</span> nopass</span><br><span class="line"></span><br><span class="line"><span class="comment">#Gathering credentials</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;</span></span><br><span class="line"><span class="string">Username: <span class="variable">$USER_NAME</span></span></span><br><span class="line"><span class="string">Password: <span class="variable">$USERPASSWD</span></span></span><br><span class="line"><span class="string">&quot;</span> &gt; credentials.txt</span><br><span class="line"></span><br><span class="line"><span class="comment">#Generating OVPN file</span></span><br><span class="line">sed -e <span class="string">&apos;/^cert/c\cert &apos;</span><span class="string">&quot;<span class="variable">$USER_NAME</span>&quot;</span><span class="string">&apos;.crt&apos;</span> -e <span class="string">&apos;/^key/c\key &apos;</span><span class="string">&quot;<span class="variable">$USER_NAME</span>&quot;</span><span class="string">&apos;.key&apos;</span> /root/openvpn/openvpn.ovpn &gt; openvpn_<span class="string">&quot;<span class="variable">$USER_NAME</span>&quot;</span>.ovpn</span><br><span class="line"></span><br><span class="line"><span class="comment">#Zipping the files:</span></span><br><span class="line">zip -j -r /root/generated_openvpn_users/<span class="string">&quot;<span class="variable">$USER_NAME</span>&quot;</span>_openvpn_files.zip /etc/openvpn/easy-rsa/pki/private/<span class="string">&quot;<span class="variable">$USER_NAME</span>&quot;</span>.key /etc/openvpn/easy-rsa/pki/issued/<span class="string">&quot;<span class="variable">$USER_NAME</span>&quot;</span>.crt credentials.txt /etc/openvpn/ca.crt openvpn_<span class="string">&quot;<span class="variable">$USER_NAME</span>&quot;</span>.ovpn</span><br><span class="line"></span><br><span class="line"><span class="comment">#Going into temp</span></span><br><span class="line"><span class="built_in">cd</span> /root/generated_openvpn_users/</span><br><span class="line"></span><br><span class="line"><span class="comment">#Sending email</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$USER_NAME</span> OpenVPN Config Files&quot;</span> | mutt -e <span class="string">&apos;my_hdr From:sender@somewhere.com&apos;</span> -a /root/generated_openvpn_users/<span class="string">&quot;<span class="variable">$USER_NAME</span>&quot;</span>_openvpn_files.zip -s <span class="string">&quot;<span class="variable">$USER_NAME</span> OpenVPN Config Files&quot;</span> -- <span class="string">&quot;<span class="variable">$EMAIL_ID</span>&quot;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;Waiting 15 seconds&quot;</span></span><br><span class="line">sleep 15</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure><h1 id="List-of-Files-included-in-zip"><a href="#List-of-Files-included-in-zip" class="headerlink" title="List of Files included in zip"></a>List of Files included in zip</h1><ol class="ui list"><li><code>&lt;USERID&gt;.key</code></li><li><code>&lt;USERID&gt;.crt</code></li><li><code>credentials.txt</code></li><li><code>ca.crt</code></li><li><code>openvpn_&lt;USERID&gt;.ovpn</code></li></ol><h1 id="Notes"><a href="#Notes" class="headerlink" title="Notes"></a>Notes</h1><ol class="ui list"><li>The above works for me. Cannot guarantee for you.</li><li>Go through the script <strong>carefully</strong> before executing.</li><li>You can download the <code>easyrsa</code> file from Github (link below).</li><li>Happy to help in any issues.</li></ol>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Normally, what we do is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;use &lt;code&gt;easy-rsa&lt;/code&gt; commandline tool to generate the &lt;code&gt;.crt&lt;/code&gt;, &lt;code&gt;.key&lt;/code&gt; files and the &lt;code&gt;.ovpn&lt;/code&gt; file,&lt;/li&gt;
&lt;li&gt;copy the required files to the desktop using &lt;strong&gt;WinSCP&lt;/strong&gt; or &lt;strong&gt;FileZilla&lt;/strong&gt; or any other software.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The above seems very tedious task to perform manually everytime a user ID has to be created. In this blog post, I’ll share a &lt;u&gt;Shell Script&lt;/u&gt; to automate this process, as well as send an email with a &lt;code&gt;.zip&lt;/code&gt; file attached, containing all the relevant files.&lt;/p&gt;
    
    </summary>
    
      <category term="Linux" scheme="https://blog.vrnchndk.in/categories/Linux/"/>
    
    
      <category term="linux" scheme="https://blog.vrnchndk.in/tags/linux/"/>
    
      <category term="ubuntu" scheme="https://blog.vrnchndk.in/tags/ubuntu/"/>
    
      <category term="centos" scheme="https://blog.vrnchndk.in/tags/centos/"/>
    
      <category term="network" scheme="https://blog.vrnchndk.in/tags/network/"/>
    
      <category term="remote" scheme="https://blog.vrnchndk.in/tags/remote/"/>
    
      <category term="ssh" scheme="https://blog.vrnchndk.in/tags/ssh/"/>
    
      <category term="terminal" scheme="https://blog.vrnchndk.in/tags/terminal/"/>
    
      <category term="openvpn" scheme="https://blog.vrnchndk.in/tags/openvpn/"/>
    
      <category term="openvpn-client" scheme="https://blog.vrnchndk.in/tags/openvpn-client/"/>
    
  </entry>
  
  <entry>
    <title>Using AWS CLI with Cross Account Access</title>
    <link href="https://blog.vrnchndk.in/2017/04/02/Using-AWS-CLI-with-Cross-Account-Access/"/>
    <id>https://blog.vrnchndk.in/2017/04/02/Using-AWS-CLI-with-Cross-Account-Access/</id>
    <published>2017-04-02T09:30:07.000Z</published>
    <updated>2017-04-02T09:30:07.000Z</updated>
    
    <content type="html"><![CDATA[<p>In my earlier blog post <a href="http://vrnchndk.in/2017/02/14/cross-account-access-aws-accounts-gui/" target="_blank" rel="noopener">HERE</a>, I had described on how to use AWS Console using Cross Account Access.</p><p>However, this was for <u>AWS Console</u> access only, but not for <strong>AWS CLI</strong>. In this blog post, I will be describing on how to access AWS resources using <code>AWS CLI</code> using <code>Cross Account Access</code>.</p><a id="more"></a><h1 id="Testing"><a href="#Testing" class="headerlink" title="Testing"></a>Testing</h1><p>For testing purposes, I have already created a role named <code>cc-access</code> in the parent AWS account. To make things, simpler, the below image is the contents of my <code>.aws/config</code> and <code>.aws/credentials</code> files. <em>I hope you get the gist.</em></p><p><a class="magnific-img" href="/images/cross-account-access-aws-cli.png"><img src="/images/cross-account-access-aws-cli.png" alt="" class="ui centered image"></a></p><p>In the above image,</p><ul class="ui list"><li>Two profiles, viz., <strong>default</strong> and <strong>testaccount</strong> are created</li><li><strong>default</strong> is the parent account</li><li><strong>testaccount</strong> is the account which has cross account access to <strong>default</strong> account.</li><li><code>role_arn</code> line has to be edited as follows:<ul><li><code>role_arn = arn:aws:iam::123456789012:role/cc-access</code> ( where <strong>123456789012</strong> is the account ID of <strong>testaccount</strong>.)</li></ul></li></ul><hr><h1 id="Checking-output"><a href="#Checking-output" class="headerlink" title="Checking output"></a>Checking output</h1><ol class="ui list"><li><h4 id="Output-1-Parent-Account"><a href="#Output-1-Parent-Account" class="headerlink" title="Output 1 (Parent Account)"></a>Output 1 (Parent Account)</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">aws s3 ls</span><br></pre></td></tr></table></figure><p> <a class="magnific-img" href="/images/caa-output1.png"><img src="/images/caa-output1.png" alt="" class="ui centered image"></a></p></li><li><h4 id="Output-2-testaccount-Account"><a href="#Output-2-testaccount-Account" class="headerlink" title="Output 2 (testaccount Account)"></a>Output 2 (testaccount Account)</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">aws s3 ls --profile testaccount</span><br></pre></td></tr></table></figure><p> <a class="magnific-img" href="/images/caa-output2.png"><img src="/images/caa-output2.png" alt="" class="ui centered image"></a></p></li></ol><hr><p>Please do comments in case I have missed out anything. The above setup works for me properly, might differ in your case (or maybe not).</p><h3 id="Enjoy"><a href="#Enjoy" class="headerlink" title="Enjoy !!"></a>Enjoy !!</h3>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;In my earlier blog post &lt;a href=&quot;http://vrnchndk.in/2017/02/14/cross-account-access-aws-accounts-gui/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;HERE&lt;/a&gt;, I had described on how to use AWS Console using Cross Account Access.&lt;/p&gt;
&lt;p&gt;However, this was for &lt;u&gt;AWS Console&lt;/u&gt; access only, but not for &lt;strong&gt;AWS CLI&lt;/strong&gt;. In this blog post, I will be describing on how to access AWS resources using &lt;code&gt;AWS CLI&lt;/code&gt; using &lt;code&gt;Cross Account Access&lt;/code&gt;.&lt;/p&gt;
    
    </summary>
    
      <category term="Amazon Web Services" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/"/>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="ec2" scheme="https://blog.vrnchndk.in/tags/ec2/"/>
    
      <category term="aws cli" scheme="https://blog.vrnchndk.in/tags/aws-cli/"/>
    
      <category term="iam" scheme="https://blog.vrnchndk.in/tags/iam/"/>
    
      <category term="cross account" scheme="https://blog.vrnchndk.in/tags/cross-account/"/>
    
      <category term="iam role" scheme="https://blog.vrnchndk.in/tags/iam-role/"/>
    
  </entry>
  
  <entry>
    <title>Automate AWS AMI backups with Retention Period</title>
    <link href="https://blog.vrnchndk.in/2017/03/21/Automate-AWS-AMI-backups-with-Retention-Period/"/>
    <id>https://blog.vrnchndk.in/2017/03/21/Automate-AWS-AMI-backups-with-Retention-Period/</id>
    <published>2017-03-21T05:55:16.000Z</published>
    <updated>2017-03-21T05:55:16.000Z</updated>
    
    <content type="html"><![CDATA[<h3 id="Automate-AWS-AMI-backups-with-Retention-Period-of-n-Days"><a href="#Automate-AWS-AMI-backups-with-Retention-Period-of-n-Days" class="headerlink" title="Automate AWS AMI backups with Retention Period of n Days"></a>Automate AWS AMI backups with Retention Period of <code>n</code> Days</h3><hr><p>Normally, we get requests to enable AMI backups and keep them for last 7 days or 30 days, etc. As it is not feasible to do this manually, it&#x2019;s best to automate this process.</p><p>When we say automation, first thing that comes to mind is <strong>DevOps</strong>, where we can use different tools such as <code>Ansible</code> or <code>Chef</code> or <code>Puppet</code>. Since, I like to do things in Shell Script, I&#x2019;ll be sharing the script here (also on Github).</p><p>This script make use of an additional CSV file, which has the content as below:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#InstanceID,RetentionDays</span><br><span class="line">i-b060363e,2</span><br><span class="line">i-f10f5e7f,2</span><br><span class="line">i-07d41dc8,2</span><br><span class="line">i-715b81be,2</span><br></pre></td></tr></table></figure><p>The above CSV has 2 columns, viz., <strong>InstanceID</strong> and <strong>RetentionDays</strong>. This file is used by the script to automate the AMI backup process.</p><h1 id="Script"><a href="#Script" class="headerlink" title="Script"></a>Script</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Script to create AMI of server on daily basis and deleting AMI older than n no of days</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">export</span> PATH=/usr/<span class="built_in">local</span>/sbin:/usr/<span class="built_in">local</span>/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/snap/bin</span><br><span class="line"></span><br><span class="line"><span class="built_in">alias</span> aws=<span class="string">&apos;&apos;</span>$(<span class="built_in">which</span> aws)<span class="string">&apos; --output text --region ap-southeast-1&apos;</span></span><br><span class="line"><span class="built_in">shopt</span> -s expand_aliases</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">createAMI</span></span>() {</span><br><span class="line">        <span class="comment">#To create a unique AMI name for this script</span></span><br><span class="line">        INST_NAME=<span class="string">&quot;<span class="variable">$(aws ec2 describe-instances --filters Name=instance-id,Values=$1 --query &apos;Reservations[*].Instances[*].Tags[?Key==`Name`].Value&apos;)</span>&quot;</span></span><br><span class="line">        INST_TAG=<span class="string">&quot;<span class="variable">$INST_NAME</span>&quot;</span>_<span class="string">&quot;<span class="variable">$(date +%d%b%y)</span>&quot;</span></span><br><span class="line">        <span class="built_in">echo</span> -e <span class="string">&quot;Starting the Daily AMI creation: <span class="variable">$INST_TAG</span>\n&quot;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">#To create AMI of defined instance</span></span><br><span class="line">        AMI_ID=$(aws ec2 create-image --instance-id <span class="string">&quot;<span class="variable">$1</span>&quot;</span> --name <span class="string">&quot;<span class="variable">$INST_TAG</span>&quot;</span> --description <span class="string">&quot;<span class="variable">$1</span>&quot;</span>_<span class="string">&quot;<span class="variable">$(date +%d%b%y)</span>&quot;</span> --no-reboot)</span><br><span class="line">        <span class="built_in">echo</span> <span class="string">&quot;New AMI Id is: <span class="variable">$AMI_ID</span>&quot;</span></span><br><span class="line">        <span class="built_in">echo</span> <span class="string">&quot;Waiting for 0.5 minutes&quot;</span></span><br><span class="line">        sleep 30</span><br><span class="line"></span><br><span class="line">        <span class="comment">#Renaming AMI and its Snapshots</span></span><br><span class="line">        aws ec2 create-tags --resources <span class="string">&quot;<span class="variable">$AMI_ID</span>&quot;</span> --tags Key=Name,Value=<span class="string">&quot;<span class="variable">$INST_TAG</span>&quot;</span></span><br><span class="line">        aws ec2 describe-images --image-id <span class="string">&quot;<span class="variable">$AMI_ID</span>&quot;</span> --query <span class="string">&apos;Images[*].BlockDeviceMappings[*].Ebs.SnapshotId&apos;</span> | tr -s <span class="string">&apos;\t&apos;</span> <span class="string">&apos;\n&apos;</span> &gt; /tmp/newsnaplist.txt</span><br><span class="line">        <span class="keyword">while</span> <span class="built_in">read</span> SNAP_ID; <span class="keyword">do</span></span><br><span class="line">                aws ec2 create-tags --resources <span class="string">&quot;<span class="variable">$SNAP_ID</span>&quot;</span> --tags Key=Name,Value=<span class="string">&quot;<span class="variable">$INST_TAG</span>&quot;</span></span><br><span class="line">        <span class="keyword">done</span> &lt; /tmp/newsnaplist.txt</span><br><span class="line"></span><br><span class="line">        <span class="comment">#Finding AMI older than n which needed to be removed</span></span><br><span class="line">        <span class="keyword">if</span> [[ $(aws ec2 describe-images --filters Name=description,Values=<span class="string">&quot;<span class="variable">$1</span>&quot;</span>_<span class="string">&quot;<span class="variable">$(date +%d%b%y --date &apos;&apos;$2&apos; days ago&apos;)</span>&quot;</span> --query <span class="string">&apos;Images[*].BlockDeviceMappings[*].Ebs.SnapshotId&apos;</span> | tr -s <span class="string">&apos;\t&apos;</span> <span class="string">&apos;\n&apos;</span>) ]]</span><br><span class="line">        <span class="keyword">then</span></span><br><span class="line">                AMIDELTAG=<span class="string">&quot;<span class="variable">$1</span>&quot;</span>_<span class="string">&quot;<span class="variable">$(date +%d%b%y --date &apos;&apos;$2&apos; days ago&apos;)</span>&quot;</span></span><br><span class="line"></span><br><span class="line">                <span class="comment">#Finding Image ID of instance which needed to be Deregistered</span></span><br><span class="line">                AMIDELETE=$(aws ec2 describe-images --filters Name=description,Values=<span class="string">&quot;<span class="variable">$AMIDELTAG</span>&quot;</span> --query <span class="string">&apos;Images[*].ImageId&apos;</span> | tr -s <span class="string">&apos;\t&apos;</span> <span class="string">&apos;\n&apos;</span>)</span><br><span class="line"></span><br><span class="line">                <span class="comment">#Find the snapshots attached to the Image need to be Deregister</span></span><br><span class="line">                aws ec2 describe-images --filters Name=image-id,Values=<span class="string">&quot;<span class="variable">$AMIDELETE</span>&quot;</span> --query <span class="string">&apos;Images[*].BlockDeviceMappings[*].Ebs.SnapshotId&apos;</span> | tr -s <span class="string">&apos;\t&apos;</span> <span class="string">&apos;\n&apos;</span> &gt; /tmp/snap.txt</span><br><span class="line"></span><br><span class="line">                <span class="comment">#Deregistering the AMI</span></span><br><span class="line">                aws ec2 deregister-image --image-id <span class="string">&quot;<span class="variable">$AMIDELETE</span>&quot;</span></span><br><span class="line"></span><br><span class="line">                <span class="comment">#Deleting snapshots attached to AMI</span></span><br><span class="line">                <span class="keyword">while</span> <span class="built_in">read</span> SNAP_DEL; <span class="keyword">do</span></span><br><span class="line">                        aws ec2 delete-snapshot --snapshot-id <span class="string">&quot;<span class="variable">$SNAP_DEL</span>&quot;</span></span><br><span class="line">                <span class="keyword">done</span> &lt; /tmp/snap.txt</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">                <span class="built_in">echo</span> <span class="string">&quot;No AMI present&quot;</span></span><br><span class="line">        <span class="keyword">fi</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">###################################################################</span></span><br><span class="line"><span class="comment">########## Call the instance function below as shown ##############</span></span><br><span class="line"><span class="comment">###################################################################</span></span><br><span class="line"></span><br><span class="line">IFS=<span class="string">&apos;,&apos;</span></span><br><span class="line">grep -v <span class="string">&apos;^#&apos;</span> /root/cloudcover/ami_automation/instanceList.csv | <span class="keyword">while</span> <span class="built_in">read</span> -r INST_ID RETENTION; <span class="keyword">do</span></span><br><span class="line">        createAMI <span class="variable">$INST_ID</span> <span class="variable">$RETENTION</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">######### Removing temporary files</span></span><br><span class="line">rm -f /tmp/snap.txt /tmp/newsnaplist.txt</span><br></pre></td></tr></table></figure><p>I&#x2019;ll explain the above code below:</p><ol class="ui list"><li>Export <code>PATH</code> and set <code>alias</code> for aws command.</li><li>Declare a function <code>createAMI</code> which encompasses <code>create-image</code>, <code>decribe-images</code>, <code>deregister-image</code>, etc API calls.</li><li>With the <strong>instanceList.csv</strong> file, we call the <code>createAMI</code> function for each line (or Instance ID) with retention period in days.</li></ol><p>Notes:</p><ul class="ui list"><li>The script assumes default AWS CLI profile being used (or role assigned to an instance).</li><li>You can change your input file (<strong>instanceList.csv</strong>) in the script.</li><li>The script has been tested by me and is working on production level.</li><li>It is <strong>highly recommended</strong> to test before using this script.</li><li>I am not responsible for any damage caused by this. (Given that there is no <code>rm -rf *</code> command in it.)</li></ul><hr><h3 id="Any-optimization-in-the-script-is-welcome-This-script-is-uploaded-on-Github"><a href="#Any-optimization-in-the-script-is-welcome-This-script-is-uploaded-on-Github" class="headerlink" title="Any optimization in the script is welcome. This script is uploaded on Github."></a>Any optimization in the script is welcome. This script is uploaded on Github.</h3>]]></content>
    
    <summary type="html">
    
      
      
        &lt;h3 id=&quot;Automate-AWS-AMI-backups-with-Retention-Period-of-n-Days&quot;&gt;&lt;a href=&quot;#Automate-AWS-AMI-backups-with-Retention-Period-of-n-Days&quot; class=
      
    
    </summary>
    
      <category term="Amazon Web Services" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/"/>
    
      <category term="Shell Scripting" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Shell-Scripting/"/>
    
      <category term="Linux" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Shell-Scripting/Linux/"/>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="awscli" scheme="https://blog.vrnchndk.in/tags/awscli/"/>
    
      <category term="linux" scheme="https://blog.vrnchndk.in/tags/linux/"/>
    
      <category term="acm" scheme="https://blog.vrnchndk.in/tags/acm/"/>
    
      <category term="ami" scheme="https://blog.vrnchndk.in/tags/ami/"/>
    
  </entry>
  
  <entry>
    <title>Automate SSL Certificate with AWS ACM and CloudFront</title>
    <link href="https://blog.vrnchndk.in/2017/03/08/Automate-SSL-Certificate-with-AWS-ACM-and-CloudFront/"/>
    <id>https://blog.vrnchndk.in/2017/03/08/Automate-SSL-Certificate-with-AWS-ACM-and-CloudFront/</id>
    <published>2017-03-08T07:35:16.000Z</published>
    <updated>2017-03-08T07:35:16.000Z</updated>
    
    <content type="html"><![CDATA[<p>As we all know that AWS Certificate Manager now supports importing of custom SSL/TLS certificates. Also, <strong>Let&#x2019;s Encrypt</strong> is another service to generate SSL certificates for small time bloggers like me. Even I have set this blog with <strong>Let&#x2019;s Encrypt</strong> SSL certificate, which is pretty neat.</p><p>In this blog, I&#x2019;ll be writing on how to automate:</p><ul class="ui list"><li>generating <strong>Let&#x2019;s Encrypt</strong> SSL certificate</li><li>uploading the certificate to AWS Certificate Manager</li><li>Using the uploaded certificate to use in AWS Cloudfront<a id="more"></a></li></ul><hr><h2 id="Generating-Let&#x2019;s-Encrypt-SSL-certificate"><a href="#Generating-Let&#x2019;s-Encrypt-SSL-certificate" class="headerlink" title="Generating Let&#x2019;s Encrypt SSL certificate:"></a>Generating <strong>Let&#x2019;s Encrypt</strong> SSL certificate:</h2><p>Let&#x2019;s start by creating the certificate files using letsencrypt command line tool.</p><ol class="ui list"><li><p>Clone letsencrypt git repo:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt</span><br></pre></td></tr></table></figure></li><li><p>Generate certificate for your domain (domain.com)</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd /opt/letsencrypt</span><br><span class="line">sudo -H ./letsencrypt-auto certonly --standalone -d domain.com -d www.domain.com</span><br></pre></td></tr></table></figure></li></ol><hr><p>Note:</p><ul class="ui list"><li>Enter the values while prompted (email ID, etc)</li><li>The domain must be valid and pointed to a resource.</li></ul><p>This will create the key, cert and chain files in <code>/etc/letsencrypt/live/&lt;domain_name&gt;/</code> folder.</p><p><strong>Screenshot:</strong></p><p><a class="magnific-img" href="/images/cert_ls.png"><img src="/images/cert_ls.png" alt="" class="ui centered image"></a></p><hr><h2 id="Uploading-SSL-certs-and-Keys-to-AWS-Certificate-Manager"><a href="#Uploading-SSL-certs-and-Keys-to-AWS-Certificate-Manager" class="headerlink" title="Uploading SSL certs and Keys to AWS Certificate Manager"></a>Uploading SSL certs and Keys to AWS Certificate Manager</h2><p>As we have generated the certificate and keys, it&#x2019;s time to import them in <strong>AWS Certificate Manager</strong>. Certificates imported (uploaded) in us-east-1 <strong>only</strong> can be used in AWS CloudFront.</p><h3 id="Using-the-GUI"><a href="#Using-the-GUI" class="headerlink" title="Using the GUI"></a>Using the GUI</h3><ol class="ui list"><li>Login to AWS Console and go to Certificate Manager.</li><li>If we are importing a new certificate, then click on <strong>Import Certificate</strong><br><img src="/images/aws_acm.png" alt=""></li><li>Copy the contents of PEM files as follows:<br><strong>cert.pem</strong> file contents go in <strong>Certificate Body</strong><br><strong>privkey.pem</strong> file contents go in <strong>Certificate Private Key</strong><br><strong>fullchain.pem</strong> file contents go in <strong>Certificate Chain</strong></li><li>Clicking <strong>Import</strong> will bring you to next screen, which means the certificate is imported successfully.</li></ol><h3 id="Using-AWS-CLI"><a href="#Using-AWS-CLI" class="headerlink" title="Using AWS CLI"></a>Using AWS CLI</h3><ol class="ui list"><li><p>The following command will upload your certificate to <strong>us-east-1</strong> region:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">aws acm import-certificate \</span><br><span class="line">    --region us-east-1 \</span><br><span class="line">    --certificate file:///etc/letsencrypt/live/&lt;domain_name&gt;/cert.pem \</span><br><span class="line">    --private-key file:///etc/letsencrypt/live/&lt;domain_name&gt;/privkey.pem \</span><br><span class="line">    --certificate-chain file:///etc/letsencrypt/live/&lt;domain_name&gt;/fullchain.pem</span><br></pre></td></tr></table></figure></li><li><p>If the certificate is already imported, you can specify <code>--certificate-arn</code> in the above command.</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">aws acm import-certificate \</span><br><span class="line">    --region us-east-1 \</span><br><span class="line">    --certificate-arn arn:aws:acm:us-east-1:12345678901:certificate/ev45v24v-2fd6-49f1-9aba-62dfc48w5g6ergt34 \</span><br><span class="line">    --certificate file:///etc/letsencrypt/live/&lt;domain_name&gt;/cert.pem \</span><br><span class="line">    --private-key file:///etc/letsencrypt/live/&lt;domain_name&gt;/privkey.pem \</span><br><span class="line">    --certificate-chain file:///etc/letsencrypt/live/&lt;domain_name&gt;/fullchain.pem</span><br></pre></td></tr></table></figure></li><li><p>The certificate and Key will be uploaded to ACM. Make note of the output <strong>Certificate ARN</strong> generated so that you can use it for renewal.</p></li></ol><p>You can now use this certificate in Load Balancer and/or CloudFront.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;As we all know that AWS Certificate Manager now supports importing of custom SSL/TLS certificates. Also, &lt;strong&gt;Let’s Encrypt&lt;/strong&gt; is another service to generate SSL certificates for small time bloggers like me. Even I have set this blog with &lt;strong&gt;Let’s Encrypt&lt;/strong&gt; SSL certificate, which is pretty neat.&lt;/p&gt;
&lt;p&gt;In this blog, I’ll be writing on how to automate:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;generating &lt;strong&gt;Let’s Encrypt&lt;/strong&gt; SSL certificate&lt;/li&gt;
&lt;li&gt;uploading the certificate to AWS Certificate Manager&lt;/li&gt;
&lt;li&gt;Using the uploaded certificate to use in AWS Cloudfront
    
    </summary>
    
      <category term="Amazon Web Services" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/"/>
    
      <category term="Shell Scripting" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Shell-Scripting/"/>
    
      <category term="Linux" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Shell-Scripting/Linux/"/>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="awscli" scheme="https://blog.vrnchndk.in/tags/awscli/"/>
    
      <category term="linux" scheme="https://blog.vrnchndk.in/tags/linux/"/>
    
      <category term="acm" scheme="https://blog.vrnchndk.in/tags/acm/"/>
    
      <category term="cloudfront" scheme="https://blog.vrnchndk.in/tags/cloudfront/"/>
    
      <category term="letsencrypt" scheme="https://blog.vrnchndk.in/tags/letsencrypt/"/>
    
  </entry>
  
  <entry>
    <title>Resize Amazon EBS disks easily</title>
    <link href="https://blog.vrnchndk.in/2017/03/01/resize-amazon-ebs-disks-easily/"/>
    <id>https://blog.vrnchndk.in/2017/03/01/resize-amazon-ebs-disks-easily/</id>
    <published>2017-03-01T08:33:12.000Z</published>
    <updated>2017-03-01T08:33:12.000Z</updated>
    
    <content type="html"><![CDATA[<p>Now, AWS has introduced <a href="https://aws.amazon.com/ebs/details/#elasticvolumes" target="_blank" rel="noopener">Elastic Volumes for EBS</a>, We still have to resize the volumes once they are modified. This tutorial will show 2 ways to increase/extend the volume after modification.<a id="more"></a></p><h1 id="Part-1-Using-fdisk"><a href="#Part-1-Using-fdisk" class="headerlink" title="Part 1 - Using fdisk:"></a>Part 1 - Using fdisk:</h1><ol class="ui list"><li><p>Check disk size:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">df -h</span><br></pre></td></tr></table></figure><p> Output:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Filesystem      Size  Used Avail Use% Mounted on</span><br><span class="line">/dev/xvda1      6.0G  2.0G  3.7G  35% / </span><br><span class="line">tmpfs            15G     0   15G   0% /dev/shm</span><br></pre></td></tr></table></figure></li><li><p>Check disk cylinders/sectors:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fdisk -l</span><br></pre></td></tr></table></figure><p> Output:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Disk /dev/xvda: 21.5 GB, 21474836480 bytes</span><br><span class="line">97 heads, 17 sectors/track, 25435 cylinders</span><br><span class="line">Units = cylinders of 1649 * 512 = 844288 bytes</span><br><span class="line">Sector size (logical/physical): 512 bytes / 512 bytes</span><br><span class="line">I/O size (minimum/optimal): 512 bytes / 512 bytes</span><br><span class="line">Disk identifier: 0x0003b587</span><br><span class="line"></span><br><span class="line">    Device Boot      Start         End      Blocks   Id  System</span><br><span class="line">/dev/xvda1   *           2        7632     6291456   83  Linux</span><br></pre></td></tr></table></figure></li><li><p>Start fdisk command and follow the inside steps <em>carefully</em>:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fdisk /dev/xvda</span><br></pre></td></tr></table></figure><p>Output:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line">WARNING: DOS-compatible mode is deprecated. It&apos;s strongly recommended to</span><br><span class="line">         switch off the mode (command &apos;c&apos;) and change display units to</span><br><span class="line">         sectors (command &apos;u&apos;).</span><br><span class="line"></span><br><span class="line">Command (m for help): u</span><br><span class="line">Changing display/entry units to sectors</span><br><span class="line"></span><br><span class="line">Command (m for help): p</span><br><span class="line"></span><br><span class="line">Disk /dev/xvda: 21.5 GB, 21474836480 bytes</span><br><span class="line">97 heads, 17 sectors/track, 25435 cylinders, total 41943040 sectors</span><br><span class="line">Units = sectors of 1 * 512 = 512 bytes</span><br><span class="line">Sector size (logical/physical): 512 bytes / 512 bytes</span><br><span class="line">I/O size (minimum/optimal): 512 bytes / 512 bytes</span><br><span class="line">Disk identifier: 0x0003b587</span><br><span class="line"></span><br><span class="line">Device     Boot      Start         End      Blocks   Id  System</span><br><span class="line">/dev/xvda1   *        2048    12584959     6291456   83  Linux</span><br><span class="line"></span><br><span class="line">Command (m for help): d</span><br><span class="line">Selected partition 1</span><br><span class="line"></span><br><span class="line">Command (m for help): n</span><br><span class="line">Command action</span><br><span class="line">   e   extended</span><br><span class="line">   p   primary partition (1-4)</span><br><span class="line">p</span><br><span class="line">Partition number (1-4): 1</span><br><span class="line">First sector (17-41943039, default 17): 2048</span><br><span class="line">Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): &lt;&lt;11&gt;&gt;</span><br><span class="line">Using default value 41943039</span><br><span class="line"></span><br><span class="line">Command (m for help): p</span><br><span class="line"></span><br><span class="line">Disk /dev/xvda: 21.5 GB, 21474836480 bytes</span><br><span class="line">97 heads, 17 sectors/track, 25435 cylinders, total 41943040 sectors</span><br><span class="line">Units = sectors of 1 * 512 = 512 bytes</span><br><span class="line">Sector size (logical/physical): 512 bytes / 512 bytes</span><br><span class="line">I/O size (minimum/optimal): 512 bytes / 512 bytes</span><br><span class="line">Disk identifier: 0x0003b587</span><br><span class="line"></span><br><span class="line">Device     Boot      Start         End      Blocks   Id  System</span><br><span class="line">/dev/xvda1            2048    41943039    20970496   83  Linux</span><br><span class="line"></span><br><span class="line">Command (m for help): a</span><br><span class="line">Partition number (1-4): 1</span><br><span class="line"></span><br><span class="line">Command (m for help): w</span><br><span class="line">The partition table has been altered!</span><br><span class="line"></span><br><span class="line">Calling ioctl() to re-read partition table.</span><br><span class="line"></span><br><span class="line">WARNING: Re-reading the partition table failed with error 16: Device or resource busy.</span><br><span class="line">The kernel still uses the old table. The new table will be used at</span><br><span class="line">the next reboot or after you run partprobe(8) or kpartx(8)</span><br><span class="line">Syncing disks.</span><br></pre></td></tr></table></figure></li><li><p>Reboot the machine for the changes to take place:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sleep 10; reboot</span><br></pre></td></tr></table></figure></li><li><p>Check for disk space after reboot:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">df -h</span><br></pre></td></tr></table></figure><p>Output:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Filesystem      Size  Used Avail Use% Mounted on</span><br><span class="line">/dev/xvda1       20G  2.0G   17G  11% / </span><br><span class="line">tmpfs            15G     0   15G   0% /dev/shm</span><br></pre></td></tr></table></figure></li><li><p>Cross check partition with <strong>resize2fs</strong>:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">resize2fs /dev/xvda1</span><br></pre></td></tr></table></figure><p>Output:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">resize2fs 1.41.12 (17-May-2010)</span><br><span class="line">The filesystem is already 5242624 blocks long.  Nothing to do!</span><br></pre></td></tr></table></figure></li></ol><hr><h1 id="Part-2-Using-growpart"><a href="#Part-2-Using-growpart" class="headerlink" title="Part 2 - Using growpart:"></a>Part 2 - Using growpart:</h1><ol class="ui list"><li><p>Install EPEL:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install epel-release -y</span><br></pre></td></tr></table></figure><p> Check partitions:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lsblk</span><br></pre></td></tr></table></figure><p> Output:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT</span><br><span class="line">xvda    202:0    0  30G  0 disk </span><br><span class="line">&#x2514;&#x2500;xvda1 202:1    0   8G  0 part /</span><br></pre></td></tr></table></figure></li><li><p>Install <strong>growpart</strong>:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install cloud-utils-growpart -y</span><br></pre></td></tr></table></figure></li><li><p>Run the final command:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">growpart /dev/xvda 1</span><br></pre></td></tr></table></figure></li><li><p>Reboot the instance for changes to take effect:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sleep 10; reboot</span><br></pre></td></tr></table></figure></li><li><p>After the reboot, Check for disk size:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lsblk</span><br></pre></td></tr></table></figure><p> Output:</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT</span><br><span class="line">xvda    202:0    0  30G  0 disk </span><br><span class="line">&#x2514;&#x2500;xvda1 202:1    0  30G  0 part /</span><br></pre></td></tr></table></figure></li></ol><h3 id="NOTES"><a href="#NOTES" class="headerlink" title="NOTES:"></a>NOTES:</h3><ul class="ui list"><li>All the commands must be run as root.</li><li>Tested on CentOS box.</li><li>OP is not responsible for any damage, obviously !</li><li>In some cases, reboot is not required. Check before proceeding.</li></ul>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Now, AWS has introduced &lt;a href=&quot;https://aws.amazon.com/ebs/details/#elasticvolumes&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Elastic Volumes for EBS&lt;/a&gt;, We still have to resize the volumes once they are modified. This tutorial will show 2 ways to increase/extend the volume after modification.
    
    </summary>
    
      <category term="Amazon Web Services" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/"/>
    
      <category term="Linux" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Linux/"/>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="ec2" scheme="https://blog.vrnchndk.in/tags/ec2/"/>
    
      <category term="instance" scheme="https://blog.vrnchndk.in/tags/instance/"/>
    
      <category term="centos" scheme="https://blog.vrnchndk.in/tags/centos/"/>
    
      <category term="ebs" scheme="https://blog.vrnchndk.in/tags/ebs/"/>
    
      <category term="ebs volumes" scheme="https://blog.vrnchndk.in/tags/ebs-volumes/"/>
    
      <category term="root" scheme="https://blog.vrnchndk.in/tags/root/"/>
    
  </entry>
  
  <entry>
    <title>Using Cross Account Access for multiple AWS Accounts</title>
    <link href="https://blog.vrnchndk.in/2017/02/14/cross-account-access-aws-accounts-gui/"/>
    <id>https://blog.vrnchndk.in/2017/02/14/cross-account-access-aws-accounts-gui/</id>
    <published>2017-02-14T10:15:36.000Z</published>
    <updated>2017-02-14T10:15:36.000Z</updated>
    
    <content type="html"><![CDATA[<p>This tutorial will brief you on how to access multiple AWS Console using a single IAM User with Cross Account Access facility of AWS.<a id="more"></a></p><p>Normally, we would sign-in to the different AWS Console with the customized IAM Sign In Link. For 1 or 2 accounts it&#x2019;s acceptable. But for a much larger number, it becomes difficult to switch browsers to private browsing, or using multiple browsers, which kind of becomes complex in handling all the accounts and their credentials as it tends to increase the user management because of each AWS account having its own IAM users, groups, and roles. This tutorial is focussed on setting up Cross Account Access via AWS Console (or AWS GUI, as some might prefer that).</p><p>I&#x2019;ll be assuming the below variables in this blog post:</p><p>Main AWS Account ID: <strong>1111111111</strong><br>3rd Party AWS Account ID: <strong>2222222222</strong></p><p>In simpler terms, We will be using the IAM users of <strong>1111111111</strong> to access the resources of <strong>2222222222</strong>.</p><p>Sounds Easy? Let&#x2019;s get started.</p><h3 id="Step-1"><a href="#Step-1" class="headerlink" title="Step 1:"></a>Step 1:</h3><p>Login to 2222222222 account Console and create an IAM role for 1111111111 IAM users to access resources of 2222222222 account.</p><p><a class="magnific-img" href="/images/screenshot_14Feb2017-15-02-19-PM.png"><img src="/images/screenshot_14Feb2017-15-02-19-PM.png" alt="IAM Create Role" class="ui centered image"></a></p><h3 id="Step-2"><a href="#Step-2" class="headerlink" title="Step 2:"></a>Step 2:</h3><p>Choose <strong>Role for Cross-Account Access</strong> and select &#x201C;<strong>Provide access between AWS accounts you own</strong>&#x201C;. Use <strong>ReadOnly</strong> access for testing purposes.</p><p><a class="magnific-img" href="/images/screenshot_14Feb2017-15-02-39-PM-1024x331.png"><img src="/images/screenshot_14Feb2017-15-02-39-PM-1024x331.png" alt="Create Cross Account Access" class="ui centered image"></a></p><h3 id="Step-3"><a href="#Step-3" class="headerlink" title="Step 3:"></a>Step 3:</h3><p>Enter the Account ID of the main account, which is 1111111111 and proceed.</p><p><a class="magnific-img" href="/images/screenshot_14Feb2017-15-03-33-PM.png"><img src="/images/screenshot_14Feb2017-15-03-33-PM.png" alt="IAM Read Only Access" class="ui centered image"></a></p><h3 id="Step-4"><a href="#Step-4" class="headerlink" title="Step 4:"></a>Step 4:</h3><p>Finish the role creation wizard.</p><h3 id="Step-5"><a href="#Step-5" class="headerlink" title="Step 5:"></a>Step 5:</h3><p>Now login to 1111111111 account, and click on the username with account ID on top right corner of the webpage. Click &#x201C;Switch Role&#x201D;.</p><p><a class="magnific-img" href="/images/screenshot_14Feb2017-15-08-14-PM.png"><img src="/images/screenshot_14Feb2017-15-08-14-PM.png" alt="IAM Switch Role" class="ui centered image"></a></p><h3 id="Step-6"><a href="#Step-6" class="headerlink" title="Step 6:"></a>Step 6:</h3><p>Click Switch Role and enter the values accordingly and proceed.</p><p><a class="magnific-img" href="/images/screenshot_14Feb2017-15-10-05-PM-1024x397.png"><img src="/images/screenshot_14Feb2017-15-10-05-PM-1024x397.png" alt="Switch Role Details" class="ui centered image"></a></p><h3 id="Step-7"><a href="#Step-7" class="headerlink" title="Step 7:"></a>Step 7:</h3><p>The page will be refreshed with the AWS Console Dashboard and a change (with color) in top right corner of the webpage.</p><p><a class="magnific-img" href="/images/screenshot_14Feb2017-15-10-41-PM.png"><img src="/images/screenshot_14Feb2017-15-10-41-PM.png" alt="Cross Account Access Role" class="ui centered image"></a></p><h3 id="Step-8"><a href="#Step-8" class="headerlink" title="Step 8:"></a>Step 8:</h3><p>Verify the access by opening a service currently in use.</p><p>Using the Cross Account access for multiple accounts removes the hassles of maintaining multiple accounts and their credentials.</p><p>References: <a href="http://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_cross-account-with-roles.html" target="_blank" rel="noopener">http://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_cross-account-with-roles.html</a></p><p>Comments are welcome. Cheers.</p><h6 id="Next-Up-Using-AWS-CLI-for-Cross-Account-Access"><a href="#Next-Up-Using-AWS-CLI-for-Cross-Account-Access" class="headerlink" title="Next Up: Using AWS CLI for Cross Account Access"></a>Next Up: Using AWS CLI for Cross Account Access</h6>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;This tutorial will brief you on how to access multiple AWS Console using a single IAM User with Cross Account Access facility of AWS.
    
    </summary>
    
      <category term="Amazon Web Services" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/"/>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="ec2" scheme="https://blog.vrnchndk.in/tags/ec2/"/>
    
      <category term="iam" scheme="https://blog.vrnchndk.in/tags/iam/"/>
    
      <category term="cross account" scheme="https://blog.vrnchndk.in/tags/cross-account/"/>
    
      <category term="iam role" scheme="https://blog.vrnchndk.in/tags/iam-role/"/>
    
  </entry>
  
  <entry>
    <title>SSH tunneling using Linux Machine</title>
    <link href="https://blog.vrnchndk.in/2017/02/07/ssh-tunneling-using-linux-machine/"/>
    <id>https://blog.vrnchndk.in/2017/02/07/ssh-tunneling-using-linux-machine/</id>
    <published>2017-02-07T04:15:47.000Z</published>
    <updated>2017-02-07T04:15:47.000Z</updated>
    
    <content type="html"><![CDATA[<p>Hi friends,</p><p>Earlier I had written about SSH tunnel using a Windows machine. Now I&#x2019;ll be writing about how I&#x2019;ll be tunneling through SSH using a Linux machine.<a id="more"></a></p><p>Execute the following command:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ssh -vD 9999 -C -N IP_OR_HOSTNAME_OF_REMOTE_MACHINE</span><br></pre></td></tr></table></figure><p>For example,</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ssh -vD 9999 -C -N 11.22.33.44</span><br></pre></td></tr></table></figure><p>In the above command,</p><ul class="ui list"><li><strong>-v</strong> : verbose mode, output will be detailed.</li><li><strong>-D</strong> <strong>[port]</strong> : a dynamic unused port number on the remote machine and local machine.</li><li><strong>-C</strong> : enabling compression for faster data transfer</li><li><strong>-N</strong> : Used for forwarding ports only, no command execution.<br>This will open a connection between remote machine and local machine, and all the traffic will be then forwarded via the remote machine.</li></ul><p>After the command is executed, open the web browser, go to the tools -&gt; settings -&gt; network and make the settings as shown in the screenshot below:</p><p><a class="magnific-img" href="/images/aaaa1.png"><img src="/images/aaaa1.png" alt="" class="ui centered image"></a></p><p>Make Sure to open the port <strong>9999</strong> in the firewall/security groups.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Hi friends,&lt;/p&gt;
&lt;p&gt;Earlier I had written about SSH tunnel using a Windows machine. Now I’ll be writing about how I’ll be tunneling through SSH using a Linux machine.
    
    </summary>
    
      <category term="Linux" scheme="https://blog.vrnchndk.in/categories/Linux/"/>
    
    
      <category term="ec2" scheme="https://blog.vrnchndk.in/tags/ec2/"/>
    
      <category term="command" scheme="https://blog.vrnchndk.in/tags/command/"/>
    
      <category term="linux" scheme="https://blog.vrnchndk.in/tags/linux/"/>
    
      <category term="ubuntu" scheme="https://blog.vrnchndk.in/tags/ubuntu/"/>
    
      <category term="centos" scheme="https://blog.vrnchndk.in/tags/centos/"/>
    
      <category term="network" scheme="https://blog.vrnchndk.in/tags/network/"/>
    
      <category term="remote" scheme="https://blog.vrnchndk.in/tags/remote/"/>
    
      <category term="ssh" scheme="https://blog.vrnchndk.in/tags/ssh/"/>
    
      <category term="terminal" scheme="https://blog.vrnchndk.in/tags/terminal/"/>
    
      <category term="proxy" scheme="https://blog.vrnchndk.in/tags/proxy/"/>
    
      <category term="hack" scheme="https://blog.vrnchndk.in/tags/hack/"/>
    
  </entry>
  
  <entry>
    <title>Access Blocked Websites using SSH Tunnel</title>
    <link href="https://blog.vrnchndk.in/2017/01/27/access-blocked-websites-using-ssh-tunnel/"/>
    <id>https://blog.vrnchndk.in/2017/01/27/access-blocked-websites-using-ssh-tunnel/</id>
    <published>2017-01-27T10:28:58.000Z</published>
    <updated>2017-01-27T10:28:58.000Z</updated>
    
    <content type="html"><![CDATA[<p>For this to work, You&#x2019;ll be needing the following:</p><ul class="ui list"><li>A VPS or Linux Based Server on Cloud<a id="more"></a></li></ul><h3 id="Step-1"><a href="#Step-1" class="headerlink" title="Step 1"></a>Step 1</h3><p>Download and Install <strong>PuTTY</strong> from <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html" target="_blank" rel="noopener">here</a></p><h3 id="Step-2"><a href="#Step-2" class="headerlink" title="Step 2"></a>Step 2</h3><p>Install/Run PuTTY to get the following screen.</p><p><a class="magnific-img" href="/images/Screenshot.png"><img src="/images/Screenshot.png" alt="" class="ui centered image"></a></p><h3 id="Step-3"><a href="#Step-3" class="headerlink" title="Step 3"></a>Step 3</h3><p>Enter the IP Address of your VPS or private Server in the Host Name field.</p><h3 id="Step-4"><a href="#Step-4" class="headerlink" title="Step 4"></a>Step 4</h3><p>On the left side, under <em>Connection - SSH - Tunnels</em>, Enter a 4 digit Source Port (e.g., 9870), Select Dynamic and Click Add, as shown below.</p><p><a class="magnific-img" href="/images/Screenshot-1.png"><img src="/images/Screenshot-1.png" alt="" class="ui centered image"></a></p><h3 id="Step-5"><a href="#Step-5" class="headerlink" title="Step 5"></a>Step 5</h3><p>Go back to the tree on the left, Click Session and write a text in the Saved Sessions text box and click on Save.</p><h3 id="Step-6"><a href="#Step-6" class="headerlink" title="Step 6"></a>Step 6</h3><p>Double Click on the saved name, a login box will open. Login using your user credentials. Make sure you login else the following steps will not work.</p><p><a class="magnific-img" href="/images/Screenshot-3.png"><img src="/images/Screenshot-3.png" alt="" class="ui centered image"></a></p><h3 id="Step-7"><a href="#Step-7" class="headerlink" title="Step 7"></a>Step 7</h3><p>Open up Mozilla Firefox (download yourself), Go to <em>Tools - Options -Advanced - Network - Settings</em>. Select Manual Proxy Configuration, un-check the checkbox and clear all the text boxes under this option.</p><h3 id="Step-8"><a href="#Step-8" class="headerlink" title="Step 8"></a>Step 8</h3><p>In the SOCKS Host text box, type: <strong>127.0.0.1</strong> as IP address and <strong>9870</strong> (or the port specified in Step 4) and select <strong>SOCKS v5</strong> option underneath it.</p><p><a class="magnific-img" href="/images/Screenshot-4.png"><img src="/images/Screenshot-4.png" alt="" class="ui centered image"></a></p><h3 id="Step-9"><a href="#Step-9" class="headerlink" title="Step 9"></a>Step 9</h3><p>If all done correctly, try logging into any 1 of the blocked websites and it will load.</p><hr><h3 id="Before-and-After-Screenshots"><a href="#Before-and-After-Screenshots" class="headerlink" title="Before and After Screenshots"></a><strong>Before and After Screenshots</strong></h3><h6 id="Before"><a href="#Before" class="headerlink" title="Before:"></a><strong>Before:</strong></h6><p><a href="/images/Screenshot-5.png"><a class="magnific-img" href="/images/Screenshot-5.png"><img src="/images/Screenshot-5.png" alt="" class="ui centered image"></a></a></p><h6 id="After"><a href="#After" class="headerlink" title="After:"></a><strong>After:</strong></h6><p><a href="/images/Screenshot-6.png"><a class="magnific-img" href="/images/Screenshot-6.png"><img src="/images/Screenshot-6.png" alt="" class="ui centered image"></a></a></p><p>Cheers !!</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;For this to work, You’ll be needing the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A VPS or Linux Based Server on Cloud
    
    </summary>
    
      <category term="Linux" scheme="https://blog.vrnchndk.in/categories/Linux/"/>
    
      <category term="Random Technology" scheme="https://blog.vrnchndk.in/categories/Linux/Random-Technology/"/>
    
    
      <category term="linux" scheme="https://blog.vrnchndk.in/tags/linux/"/>
    
      <category term="ubuntu" scheme="https://blog.vrnchndk.in/tags/ubuntu/"/>
    
      <category term="centos" scheme="https://blog.vrnchndk.in/tags/centos/"/>
    
      <category term="remote" scheme="https://blog.vrnchndk.in/tags/remote/"/>
    
      <category term="ssh" scheme="https://blog.vrnchndk.in/tags/ssh/"/>
    
      <category term="internet" scheme="https://blog.vrnchndk.in/tags/internet/"/>
    
      <category term="windows" scheme="https://blog.vrnchndk.in/tags/windows/"/>
    
      <category term="block" scheme="https://blog.vrnchndk.in/tags/block/"/>
    
      <category term="bypass" scheme="https://blog.vrnchndk.in/tags/bypass/"/>
    
      <category term="computer" scheme="https://blog.vrnchndk.in/tags/computer/"/>
    
      <category term="proxy" scheme="https://blog.vrnchndk.in/tags/proxy/"/>
    
      <category term="putty" scheme="https://blog.vrnchndk.in/tags/putty/"/>
    
      <category term="tunnel" scheme="https://blog.vrnchndk.in/tags/tunnel/"/>
    
      <category term="vps" scheme="https://blog.vrnchndk.in/tags/vps/"/>
    
      <category term="websites" scheme="https://blog.vrnchndk.in/tags/websites/"/>
    
  </entry>
  
  <entry>
    <title>Shell Script to change Instance Type using AWS CLI</title>
    <link href="https://blog.vrnchndk.in/2017/01/25/shell-script-to-change-instance-type-using-aws-cli/"/>
    <id>https://blog.vrnchndk.in/2017/01/25/shell-script-to-change-instance-type-using-aws-cli/</id>
    <published>2017-01-25T09:37:23.000Z</published>
    <updated>2017-01-25T09:37:23.000Z</updated>
    
    <content type="html"><![CDATA[<p>Normally, we use AWS Console to change instance type, say from <strong>t2.micro</strong> to <strong>m4.large</strong>, of particular instance which involves:</p><ol class="ui list"><li>Stopping the instance</li><li>Right click and change the instance type</li><li>Start the instance.<br>What if this has to be done at a time when you might be asleep or in a meeting, or in a movie theatre. There is no such thing known as <strong>&#x201C;Work From Movie Theatre&#x201D;</strong>.</li></ol><p>So I have made a simple shell script which you can schedule it in crontab.<a id="more"></a></p><h3 id="Working-of-the-shell-script"><a href="#Working-of-the-shell-script" class="headerlink" title="Working of the shell script"></a>Working of the shell script</h3><p>This script will:</p><ul class="ui list"><li>Take 2 arguments, viz., <em>instance ID</em> and <em>instance type</em></li><li>Stop the instance</li><li>Wait for instance to stop, and then change the instance type.</li><li>Start the instance<br><em>Note: You have to configure AWS CLI with proper permissions in order to run the script.</em></li></ul><p>The script can be copied from below. Don&#x2019;t forget to modify as per your requirement.</p><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"> </span><br><span class="line">SCRIPT_NAME=$(basename <span class="variable">$0</span>)</span><br><span class="line"><span class="function"><span class="title">usage</span></span> () {</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&apos;</span></span><br><span class="line"><span class="string">Usage: &apos;</span><span class="string">&quot;<span class="variable">$SCRIPT_NAME</span>&quot;</span><span class="string">&apos; &quot;Instance ID&quot; &quot;Instance Type&quot;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">E.g.</span></span><br><span class="line"><span class="string">&apos;</span><span class="string">&quot;<span class="variable">$SCRIPT_NAME</span>&quot;</span><span class="string">&apos; i-abcd1234 t2.medium</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">Note: </span></span><br><span class="line"><span class="string">- Check console if the particular instance type is available to the instance before setting the schedule.</span></span><br><span class="line"><span class="string">&apos;</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="string">&quot;<span class="variable">$#</span>&quot;</span> -ne 2 ]; <span class="keyword">then</span></span><br><span class="line">usage</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"><span class="built_in">alias</span> aws=<span class="string">&apos;&apos;</span>`<span class="built_in">which</span> aws`<span class="string">&apos; --output text&apos;</span></span><br><span class="line"><span class="built_in">shopt</span> -s expand_aliases</span><br><span class="line"></span><br><span class="line"><span class="comment"># Assigning variables from arguments</span></span><br><span class="line">INST_ID=<span class="string">&quot;<span class="variable">$1</span>&quot;</span></span><br><span class="line">INST_TYPE=<span class="string">&quot;<span class="variable">$2</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Stopping Instance</span></span><br><span class="line">aws ec2 stop-instances --instance-ids <span class="string">&quot;<span class="variable">$INST_ID</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Check if the instance is stopped, then make changes</span></span><br><span class="line"><span class="keyword">while</span> <span class="literal">true</span>; <span class="keyword">do</span></span><br><span class="line">INSTSTATE=<span class="string">&quot;<span class="variable">$(aws ec2 describe-instances --instance-id &quot;$INST_ID&quot; --query &apos;Reservations[*].Instances[*].State.Name&apos;)</span>&quot;</span></span><br><span class="line"><span class="keyword">if</span> [ <span class="string">&quot;<span class="variable">$INSTSTATE</span>&quot;</span> == <span class="string">&quot;stopped&quot;</span> ]; <span class="keyword">then</span></span><br><span class="line">aws ec2 modify-instance-attribute --instance-id <span class="string">&quot;<span class="variable">$INST_ID</span>&quot;</span> --instance-type <span class="string">&quot;<span class="variable">$INST_TYPE</span>&quot;</span></span><br><span class="line"><span class="built_in">break</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line">sleep 10</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Start Instance</span></span><br><span class="line">aws ec2 start-instances --instance-ids <span class="string">&quot;<span class="variable">$INST_ID</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure><h3 id="Setting-cron"><a href="#Setting-cron" class="headerlink" title="Setting cron"></a>Setting cron</h3><p>Name the script file name as: <strong>changeInstanceType.sh</strong></p><p>To set the cron, open crontab console:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">crontab -l</span><br></pre></td></tr></table></figure><p>Add the following line to schedule it for <strong>26th January, 2017 at 10:30 PM (or 2230 hours)</strong>:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">30 22 26 01 * &#xA0; /bin/bash -x /path/to/script/changeInstanceType.sh i-abcd123 t2.medium &gt;&gt; /var/log/changeInstanceType.sh.log</span><br></pre></td></tr></table></figure><p>Don&#x2019;t forget to remove the cron tab, else it will run again, <strong>NEXT YEAR !</strong></p><hr><p>PS: May be there is a term &#x201C;<strong>Work from Theatre</strong>&#x201C;. (My Colleague :P )</p><p><a class="magnific-img" href="/images/work_from_theatre.jpg"><img src="/images/work_from_theatre.jpg" alt="" class="ui centered image"></a></p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Normally, we use AWS Console to change instance type, say from &lt;strong&gt;t2.micro&lt;/strong&gt; to &lt;strong&gt;m4.large&lt;/strong&gt;, of particular instance which involves:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Stopping the instance&lt;/li&gt;
&lt;li&gt;Right click and change the instance type&lt;/li&gt;
&lt;li&gt;Start the instance.&lt;br&gt;What if this has to be done at a time when you might be asleep or in a meeting, or in a movie theatre. There is no such thing known as &lt;strong&gt;“Work From Movie Theatre”&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So I have made a simple shell script which you can schedule it in crontab.
    
    </summary>
    
      <category term="Amazon Web Services" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/"/>
    
      <category term="Linux" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Linux/"/>
    
      <category term="Shell Scripting" scheme="https://blog.vrnchndk.in/categories/Amazon-Web-Services/Linux/Shell-Scripting/"/>
    
    
      <category term="amazon web services" scheme="https://blog.vrnchndk.in/tags/amazon-web-services/"/>
    
      <category term="aws" scheme="https://blog.vrnchndk.in/tags/aws/"/>
    
      <category term="awscli" scheme="https://blog.vrnchndk.in/tags/awscli/"/>
    
      <category term="cloud" scheme="https://blog.vrnchndk.in/tags/cloud/"/>
    
      <category term="ec2" scheme="https://blog.vrnchndk.in/tags/ec2/"/>
    
      <category term="command" scheme="https://blog.vrnchndk.in/tags/command/"/>
    
      <category term="linux" scheme="https://blog.vrnchndk.in/tags/linux/"/>
    
      <category term="ubuntu" scheme="https://blog.vrnchndk.in/tags/ubuntu/"/>
    
      <category term="cron" scheme="https://blog.vrnchndk.in/tags/cron/"/>
    
      <category term="crontab" scheme="https://blog.vrnchndk.in/tags/crontab/"/>
    
  </entry>
  
</feed>
