<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rahul Sahotay on Medium]]></title>
        <description><![CDATA[Stories by Rahul Sahotay on Medium]]></description>
        <link>https://medium.com/@sahotay.rahul?source=rss-4c87ab09af6c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*wKAH7ZHZVDdSa7-EEIHqZA.jpeg</url>
            <title>Stories by Rahul Sahotay on Medium</title>
            <link>https://medium.com/@sahotay.rahul?source=rss-4c87ab09af6c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 19:26:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sahotay.rahul/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Serverless — Removing AMI using AWS Lambda]]></title>
            <link>https://medium.com/@sahotay.rahul/serverless-ami-housekeeping-using-aws-lambda-f36983f1d93e?source=rss-4c87ab09af6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/f36983f1d93e</guid>
            <category><![CDATA[backup]]></category>
            <category><![CDATA[deletion]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[housekeeping]]></category>
            <category><![CDATA[ami]]></category>
            <dc:creator><![CDATA[Rahul Sahotay]]></dc:creator>
            <pubDate>Mon, 25 May 2020 05:28:35 GMT</pubDate>
            <atom:updated>2020-05-25T05:36:10.108Z</atom:updated>
            <content:encoded><![CDATA[<h3>Serverless — Removing AMI using AWS Lambda</h3><p>As your AWS usage grows, so will your usage of EBS snapshots and AMI images. Many of these are used for backups, while others are used to launch EC2 instances.</p><p>As your AMI images get accumulated, you may have to start removing the old AMI images. However, in your zeal to delete old AMI images, if such an image is deleted while it’s being used by an Auto Scaling group, that Auto Scaling group won’t be able to launch a new EC2 instance should it need to scale up.</p><p>Hence you have to develop a solution to verify Image usage in the Autoscaling group or in EC2 before deregistering the Image. Looking at <em>DeregisterImage() </em>it appears that it does not have any Condition Keys to “Preserve reference Image” directly. So how to identify if the image is in use? Well the easiest way I found is just to check if AMI is being used by EC2 instance and if it is not associated with EC2 that means it is not with launch template or Autoscaling groups as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/139355202ff078a0c868f09121e4ddac/href">https://medium.com/media/139355202ff078a0c868f09121e4ddac/href</a></iframe><p>You may also have to preserve your AMIs based on <em>Retention period, BackUp images, </em>for that its best to use retention time and tags</p><p>In the given example I am looking for AMI older than 45 days and those are not having tags “<em>do-not-remove”, “AWS Backup Solution”.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66a2ce520308306201c8be20c5c0ed77/href">https://medium.com/media/66a2ce520308306201c8be20c5c0ed77/href</a></iframe><p>To Conclude all together and build a running solution here is ready to go solution which you can trigger using <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/Create-CloudWatch-Events-Rule.html">CloudWatch event Rules</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ef107b9a4d2500a27360050b94edc5a/href">https://medium.com/media/5ef107b9a4d2500a27360050b94edc5a/href</a></iframe><p><strong>IAM Role</strong></p><p>AWS Lambda needs an IAM role with the necessary permission to execute this function. here is the policy you can attach to your IAM role</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/003ba3f2501f3aadd396567caf6c3a96/href">https://medium.com/media/003ba3f2501f3aadd396567caf6c3a96/href</a></iframe><p><strong><em>Note: </em></strong>The trust policy allows Lambda to use the role’s permissions by giving the service principal <em>lambda.amazonaws.com</em> permission to call the AWS Security Token Service AssumeRole action.</p><p><strong>AWS CloudWatch Event</strong></p><p><a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html">Amazon document</a> has detailed steps to create CloudWatch event Rule but make sure to add your Target as Lambda.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*slNLfSMEeWSQnsNQMO9jcg.png" /></figure><p><strong>Conclusion</strong></p><p>We built a lambda function for our AMI housekeeping, IAM Role with relevant permissions, Cloudwatch Event Rule.</p><p>I hope you will find this setup useful. Please follow me on <a href="https://www.linkedin.com/in/sahotayrahul/">LinkedIn</a> in case of any questions 🙏</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f36983f1d93e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lambda Function to Set Custom DNS using Route53]]></title>
            <link>https://medium.com/@sahotay.rahul/lambda-function-to-set-custom-dns-using-route53-ab452d797c9d?source=rss-4c87ab09af6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab452d797c9d</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[dynamodb]]></category>
            <category><![CDATA[dynamic-dns]]></category>
            <category><![CDATA[route-53]]></category>
            <dc:creator><![CDATA[Rahul Sahotay]]></dc:creator>
            <pubDate>Sun, 03 May 2020 19:41:39 GMT</pubDate>
            <atom:updated>2020-05-03T19:41:39.733Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, let me walk you through an example to set Dynamic DNS for EC2 instances using followings services:</p><ul><li><a href="https://aws.amazon.com/dynamodb/"><strong>AWS DynamoDB</strong></a></li><li><a href="https://aws.amazon.com/lambda/"><strong>AWS Lambda</strong></a></li><li><a href="https://aws.amazon.com/route53/"><strong>AWS Route53</strong></a></li><li><a href="https://aws.amazon.com/autoscaling/"><strong>AutoScaling</strong></a></li></ul><h3>An overview of the solution</h3><p>Lets’ say you need predefined hostname for EC2 instances running in the Autoscaling group and you would like to resolve hostnames through <a href="https://en.wikipedia.org/wiki/Domain_Name_System">DNS</a>. This solution will help you to set hostname not only to EC2 instances locally but to resolve those hostnames through Route53/DNS. I am using DynamoDB to keep track of hostnames and their IPs so that Lambda won&#39;t reassign unless the previous instances are terminated. Lambda function is getting triggered via <a href="https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html"><em>Autoscaling LifeCycle groups</em></a> (both at instance launch and instance terminate).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CBFqv17sNzl0JGQ3UAD7cg.png" /><figcaption>Flow of services</figcaption></figure><p><strong>UseCase:</strong></p><p>This solution helped my organization to run IBM MQ Agents dynamically in AWS and register to the MQ cluster on-prem.</p><p>Here is the explanation of all the components used in this framework:</p><p><strong>DynamoDB Table</strong></p><p>DynamoDB is keeping track of the hostname entries and IP associated with those hostnames. Lambda checks the DynamoDB table before assigning the hostname to new instances in the Autoscaling group.</p><p><em>Hint: Make sure to use primary key wisely</em></p><p><strong>Lambda Function</strong></p><p>Lambda is the main key in this framework. IAM role is required to execute the Lambda we are going to create. Make sure IAM role policy has access to describe<em> Autoscaling tags</em>, Update <em>DynamoDb table records, </em>and also to<em> update Route53 records </em>(Click <a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html">here</a> to get more details about creating an IAM role).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc25a7cc2ebd4ffde847d3615f2062fe/href">https://medium.com/media/fc25a7cc2ebd4ffde847d3615f2062fe/href</a></iframe><p>Before building the Lambda lets’ identify what are the environment variables needed:</p><ol><li><strong>TableName</strong>: <em>DynamoDB Table name to manage hostname entries</em></li><li><strong>domainName: </strong><em>A domain name for your hostname like .</em><strong>rahulsahotay.com</strong></li><li><strong>ROUTE53HostedZoneID: </strong><em>Hosted Zone id where you manage records for domain</em> <strong>.rahulsahotay.com</strong></li></ol><p>Aside to this, you have to pass at least following Tags in your autoscaling group:</p><ol><li><strong>hostStub: </strong><em>hostStub is EC2 family name like </em><strong>webapps</strong><em>, </em><strong>database</strong></li><li><strong>hostIDset: </strong><em>hostIDset is the range of max instance in Autoscaling you are looking to have</em></li><li><strong>host_env: </strong><em>Host environment like </em><strong>sbox</strong><em>, </em><strong>QA</strong><em>, </em><strong>prod</strong>,<em> etc.</em></li></ol><blockquote>Now as we covered most of our variables, <a href="https://en.wikipedia.org/wiki/Fully_qualified_domain_name">Fully Qualified Domain Name (FQDN) </a>relies on the following:<br><strong>hostStub</strong> + range(<strong>hostIDset) + host_env + domainName , For Example: </strong>webapp<strong> </strong>+ 01 + sbox + rahulsahotay.com</blockquote><p>Here is the source code for the lambda function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a56374e5761bdf374e69be208b574dcf/href">https://medium.com/media/a56374e5761bdf374e69be208b574dcf/href</a></iframe><p><strong>Conclusion</strong></p><p>The Dynamic DNS system described in this post is quite flexible to use along with the environment and even to scale the Route53 hosted zone to a shared services account. In order to update hostname to EC2 instance don’t forget to have user-data to set hostname locally.</p><p>I hope you will find this setup useful to run your dynamic DNS on AWS. Please follow me on <a href="https://www.linkedin.com/in/sahotayrahul/">LinkedIn</a> in case of any questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab452d797c9d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>