<?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"><channel><title><![CDATA[Upasana Singh – Cloud & DevOps Insights]]></title><description><![CDATA[Upasana Singh's personal blog on Cloud, DevOps, AWS automation, and serverless solutions.]]></description><link>https://upasanasingh.com.np</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 03:28:16 GMT</lastBuildDate><atom:link href="https://upasanasingh.com.np/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[AWS ALB Host Header Rules:
Strengthening Traffic Control]]></title><description><![CDATA[In modern cloud architectures, controlling incoming traffic at the application layer is critical. Amazon Web Services Application Load Balancer (ALB) enables fine-grained routing using listener rules,]]></description><link>https://upasanasingh.com.np/aws-alb-host-header-rules</link><guid isPermaLink="true">https://upasanasingh.com.np/aws-alb-host-header-rules</guid><category><![CDATA[AWS]]></category><category><![CDATA[alb]]></category><category><![CDATA[applicationloadbalancer]]></category><category><![CDATA[host-header]]></category><category><![CDATA[ListenerRules]]></category><category><![CDATA[cloudsecurity]]></category><dc:creator><![CDATA[Upasana Singh]]></dc:creator><pubDate>Tue, 30 Dec 2025 04:45:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6685294b9ec528462cfa3646/bef6fcf6-b662-41fc-a613-49cff20bbb07.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In modern cloud architectures, controlling incoming traffic at the application layer is critical. Amazon Web Services <strong>Application Load Balancer (ALB)</strong> enables fine-grained routing using <strong>listener rules</strong>, including filtering based on host headers.</p>
<hr />
<h3>What is a Host Header Rule?</h3>
<p>A <strong>Host header rule</strong> ensures that only requests matching specific domain names (e.g., <a href="http://demo.cloud.com"><code>demo.cloud.com</code></a>) are allowed and routed to your backend.</p>
<p>This helps:</p>
<ul>
<li><p>Route traffic to the correct service</p>
</li>
<li><p>Restrict access to only valid domains</p>
</li>
</ul>
<hr />
<h3>How to Configure (as shown in screenshot)</h3>
<ol>
<li><p>Go to <strong>EC2 → Load Balancers → Listeners</strong></p>
</li>
<li><p>Select your <strong>HTTPS:443 listener</strong></p>
</li>
<li><p>Click <strong>Add rule</strong></p>
</li>
<li><p>Under <strong>Conditions → Add condition → Host header</strong></p>
</li>
<li><p>Enter your domain:</p>
<ul>
<li><p>Exact: <a href="http://app.example.com"><code>demo.example.com</code></a></p>
</li>
<li><p>Wildcard: <code>*.</code><a href="http://example.com"><code>example.com</code></a></p>
</li>
</ul>
</li>
<li><p>Set action → <strong>Forward to target group</strong></p>
</li>
<li><p>Assign <strong>priority</strong> (lower number = higher priority)</p>
</li>
</ol>
<img src="https://cdn.hashnode.com/uploads/covers/6685294b9ec528462cfa3646/8fc8681a-0cc4-460f-ad2e-aae8319898d6.png" alt="" style="display:block;margin:0 auto" />

<img src="https://cdn.hashnode.com/uploads/covers/6685294b9ec528462cfa3646/c112b4bf-2ac3-4a27-bedd-842d05f54086.png" alt="" style="display:block;margin:0 auto" />

<img src="https://cdn.hashnode.com/uploads/covers/6685294b9ec528462cfa3646/1b600250-8288-4996-83c5-a7ca23b33d92.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h3>Important Behavior</h3>
<ul>
<li><p>Rules are evaluated <strong>top-down (by priority)</strong></p>
</li>
<li><p>If no rule matches → <strong>Default action is triggered</strong> (commonly 403)</p>
</li>
</ul>
<hr />
<h3>Why It Matters</h3>
<ul>
<li><p>Prevents unwanted traffic hitting your backend</p>
</li>
<li><p>Reduces exposure via ALB DNS</p>
</li>
<li><p>Helps mitigate <strong>Host Header Injection risks</strong></p>
</li>
</ul>
<hr />
<h3>Key Takeaway</h3>
<p>Use <strong>Host Header rules</strong> to allow only trusted domains and enforce a <strong>default deny (403)</strong> for everything else.</p>
]]></content:encoded></item><item><title><![CDATA[Cutting CloudWatch Costs: A Simple ECS Log Optimization Strategy]]></title><description><![CDATA[Before diving into the steps, here’s a quick look at the real CloudWatch cost reduction I achieved after optimizing ECS logging.
Before Optimization

After Optimization

Step-by-Step: Disabling CloudWatch Log Collection in ECS
After identifying that ...]]></description><link>https://upasanasingh.com.np/cutting-cloudwatch-costs-a-simple-ecs-log-optimization-strategy</link><guid isPermaLink="true">https://upasanasingh.com.np/cutting-cloudwatch-costs-a-simple-ecs-log-optimization-strategy</guid><category><![CDATA[AWS]]></category><category><![CDATA[CloudWatch Logs]]></category><category><![CDATA[#CloudWatch]]></category><category><![CDATA[AWS ECS]]></category><category><![CDATA[AWS Cost Optimization]]></category><category><![CDATA[Devops]]></category><category><![CDATA[ec2]]></category><category><![CDATA[logging]]></category><dc:creator><![CDATA[Upasana Singh]]></dc:creator><pubDate>Thu, 30 Oct 2025 15:07:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1759828308447/0cfadb51-c5ac-49d3-b6a3-ad578d915216.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Before diving into the steps, here’s a quick look at the <strong>real CloudWatch cost reduction</strong> I achieved after optimizing ECS logging.</p>
<h2 id="heading-before-optimization">Before Optimization</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759835378366/5beda1c0-a47b-497d-a698-166e88af6360.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-after-optimization">After Optimization</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759835402071/622abc6d-b01f-40ef-b990-985a801e67fb.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-step-by-step-disabling-cloudwatch-log-collection-in-ecs">Step-by-Step: Disabling CloudWatch Log Collection in ECS</h2>
<p>After identifying that CloudWatch logs were driving up the cost, I looked into where these logs were being generated i.e. <strong>ECS Task Definition</strong>.</p>
<p>ECS task streams container logs to CloudWatch using the <strong>awslogs</strong> driver.This is helpful for debugging but quickly becomes expensive in testing, production environments where logs are continuous.</p>
<blockquote>
<p><strong>Note:</strong> Disabling CloudWatch log collection is recommended only for testing or development environments.</p>
</blockquote>
<p><strong>Instead of sending all logs to CloudWatch</strong>, you can use a <strong>local log driver with rotation</strong>.<br />This approach keeps your logs available for debugging directly on the ECS host while dramatically reducing ingestion and storage costs in CloudWatch.</p>
<p><strong>Step 1: View Current Log Configuration (When Enabled)</strong></p>
<p>In the ECS console, open your cluster’s service and navigate to the <strong>Task Definition</strong> associated with that service. Under each container definition, you’ll see the <strong>log collection (awslogs) option enabled</strong>. This configuration pushes all container logs to <strong>CloudWatch</strong>, which can increase both log ingestion and storage costs.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759899153113/fc692c6f-c40f-40b1-a054-4c03cd557789.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759899190994/3752d858-20b7-4e2c-8039-c0318eb6ba3e.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759899264727/ea83887e-bd5a-4d22-bca7-845445d7ae48.png" alt class="image--center mx-auto" /></p>
<p><strong>Step 2: Disable the Log Configuration</strong></p>
<p>To stop unnecessary log streaming, create a <strong>new Task Definition revision</strong>. In your container definition, open the <strong>Log Configuration</strong> section, <strong>disable the log collection option</strong>, then <strong>save and deploy</strong> the new revision in your ECS service.</p>
<p>After deployment, you’ll notice that the <strong>Logs tab in your ECS service now appears empty</strong>, this is expected, as log streaming to CloudWatch has been disabled.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759899668759/d1bd026e-3aec-425c-ac0e-4847dd168404.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759899696674/54071ef2-292a-4614-bc58-5462956d8e72.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-setting-up-local-log-rotation-on-ecs-ec2-hosts">Setting Up Local Log Rotation on ECS (EC2 Hosts)</h2>
<p>Disabling CloudWatch log collection helps reduce costs, but we still need access to container logs for <strong>debugging and monitoring</strong>. To handle this efficiently, we can enable <strong>local log rotation</strong> on the ECS host (the EC2 instance running your containers).</p>
<p>This ensures that container logs are stored locally on the instance and automatically rotated when they reach a certain size, preventing the disk from filling up.</p>
<h3 id="heading-the-image-below-shows-the-system-logs-before-local-log-rotation">The image below shows the system logs before local log rotation</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759901370137/6533e263-5b85-4e0e-b2d3-004335f011b9.png" alt class="image--center mx-auto" /></p>
<p>To verify the logging driver used by a specific container, run the following command:</p>
<pre><code class="lang-plaintext">docker inspect -f '{{.HostConfig.LogConfig.Type}}' &lt;container_id&gt;

Example: docker inspect -f '{{.HostConfig.LogConfig.Type}}' 1e9c943d26d4
</code></pre>
<p><strong>Step 1: SSH or directly Session Connect into your ECS EC2 instance</strong></p>
<ul>
<li><p><strong>Switch to root:</strong> <code>sudo su</code></p>
</li>
<li><p><strong>Go to Docker config directory:</strong></p>
<pre><code class="lang-plaintext">  cd /etc/docker
  pwd   # confirm path
</code></pre>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759907485669/ac1f7cf6-2d4d-4132-8900-41a8e7c50d17.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Edit daemon.json:</strong> <code>vi daemon.json</code></p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759992658549/3f186efd-dd33-41a5-98b2-fc7e9bc22e4d.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Add log rotation code: adjust the</strong> <code>max-size</code> and <code>max-file</code> values based on your disk capacity and log volume requirements:</p>
<pre><code class="lang-plaintext">  {
    "log-driver": "local",
    "log-opts": {
      "max-size": "20m",  // Maximum size of a single log file (e.g., 20 megabytes)
      "max-file": "7"    // Maximum number of log files to keep
    }
  }
</code></pre>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759992702326/ccadcc6e-1971-4815-9bc7-174ac2b1658c.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Save the file and restart Docker:</strong> <code>sudo systemctl restart docker</code></p>
</li>
</ul>
<p><strong>Step 2: Verify Log Rotation</strong></p>
<ul>
<li><p><strong>After implementing Step 1, verify that log rotation is active for each container:</strong> <code>docker inspect -f '{{.HostConfig.LogConfig.Type}}' &lt;container-id&gt;</code></p>
<pre><code class="lang-plaintext">  docker inspect -f '{{.HostConfig.LogConfig.Type}}' &lt;container-id&gt;
  Example:
  docker inspect -f '{{.HostConfig.LogConfig.Type}}' 1f9c943d25d9
</code></pre>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759995546795/53909383-40f7-4ea2-8dec-ab7625a93503.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<p>This confirms that log rotation has been successfully applied and logs are ready for monitoring or debugging.</p>
<p><strong>verify the log path:</strong></p>
<pre><code class="lang-plaintext">sudo ls -lh /var/lib/docker/containers/&lt;container-id&gt;/
# or
sudo ls -lh /var/lib/docker/containers/&lt;container-id&gt;/local-logs/

# OR go to log path

sudo cd /var/lib/docker/containers/&lt;container-id&gt;/local-logs/

sudo cd /var/lib/docker/containers/&lt;container-id&gt;/
</code></pre>
<p>If log rotation is configured correctly, you’ll see files like:</p>
<pre><code class="lang-plaintext">container-json.log
container-json.log.1
container-json.log.2
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759995819254/c36e6d1f-0582-4902-b9e5-3d1195cc0465.png" alt class="image--center mx-auto" /></p>
<p><strong><em>The screenshot below shows an example of checking container logs:</em></strong> <code>docker logs &lt;container-id&gt;</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759996452859/63b63810-0fc8-4601-abb2-7a346196c729.png" alt class="image--center mx-auto" /></p>
<p><strong><em>The screenshot below shows how to optimize CloudWatch log costs by configuring log group retention periods.</em></strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1759996865214/d690d98d-dd39-4f40-9e04-9a28b0e477d1.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>By shifting non-essential ECS logs from <strong>CloudWatch</strong> to <strong>local Docker rotation</strong>, you gain immediate control over your log costs and significantly reduce your AWS bill without losing local troubleshooting capability.</p>
]]></content:encoded></item><item><title><![CDATA[AWS Cost Optimization: Automating EC2, RDS, and Redis Start/Stop with Lambda]]></title><description><![CDATA[1. Introduction
In cloud environments, managing costs is crucial to maintaining efficiency and profitability. With AWS, services like EC2, RDS, and Redis can run 24/7, incurring charges even when idle. By leveraging AWS Lambda for automation, you can...]]></description><link>https://upasanasingh.com.np/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda</link><guid isPermaLink="true">https://upasanasingh.com.np/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda</guid><category><![CDATA[AWS Cost Optimization Automation]]></category><category><![CDATA[AWS Cost Optimization]]></category><category><![CDATA[AWS EC2 RDS Redis Cost Optimization]]></category><category><![CDATA[aws lambda]]></category><category><![CDATA[AWS EventBridge]]></category><category><![CDATA[AWS]]></category><category><![CDATA[rds]]></category><category><![CDATA[ec2]]></category><category><![CDATA[eventbridge]]></category><category><![CDATA[Redis]]></category><category><![CDATA[AWS cost optimization best practices]]></category><dc:creator><![CDATA[Upasana Singh]]></dc:creator><pubDate>Sun, 29 Dec 2024 18:15:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1733480235452/9d7d2881-8818-436f-92f0-2f45050ddcd7.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-1-introduction">1. <strong>Introduction</strong></h3>
<p>In cloud environments, managing costs is crucial to maintaining efficiency and profitability. With AWS, services like EC2, RDS, and Redis can run 24/7, incurring charges even when idle. By leveraging AWS Lambda for automation, you can optimize these resources to start and stop during off-hours, reducing unnecessary costs.</p>
<h3 id="heading-2-why-automate-resource-shutdowns">2. <strong>Why Automate Resource Shutdowns?</strong></h3>
<p>EC2, RDS, and Redis instances continue to consume resources even when not in use, leading to higher costs. Automating their shutdown during off-hours helps minimize idle resource consumption, potentially saving up to 50% on monthly cloud expenses. For example, stopping EC2 instances during non-peak times or pausing RDS databases overnight can significantly reduce your AWS bill.</p>
<h3 id="heading-3-step-by-step-implementation">3. <strong>Step-by-Step Implementation</strong></h3>
<p>Below is a detailed step-by-step guide, along with screenshots, to help you implement the automation process for EC2, RDS, and Redis start/stop during off-hours using AWS Lambda. Follow each stage carefully to ensure a seamless setup.</p>
<p><strong>Step1:</strong> In the AWS Management Console, navigate to Lambda Service and create lambda function and edit configuration section to increase <code>Timeout value</code> and create new policy, attach to created existing lambda function role as shown in below attached screenshots.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733481969225/ba7cb31e-bb8e-4ddd-b6b9-e96036ca09b1.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736255272/767e3945-615a-4351-9f59-e956f57835cc.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736284954/f4d50557-c3d1-410f-8a45-9f2d63592364.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736307121/b5af5ed5-64a6-4c8b-930f-92cd5acc3013.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733482402857/a4089f69-1767-4e8d-adca-0f76ece39025.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736447233/8c369785-c887-4fa0-aad5-f9d3ea4d1921.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736589470/92d40216-3968-4d02-a0b7-561d7acbfe91.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736629770/2fcf816f-55c2-4109-9e24-9c283a97831d.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736680277/ba3999b5-fbb7-48c6-963c-bde74fcaf10b.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736704284/c209f4a9-275d-431b-845a-f372fd1a0ac3.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736732203/963f368d-6c0c-4762-8e7a-b80464a18648.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"Version"</span>: <span class="hljs-string">"2012-10-17"</span>,
  <span class="hljs-attr">"Statement"</span>: [
    {
      <span class="hljs-attr">"Effect"</span>: <span class="hljs-string">"Allow"</span>,
      <span class="hljs-attr">"Action"</span>: [
        <span class="hljs-string">"logs:CreateLogGroup"</span>,
        <span class="hljs-string">"logs:CreateLogStream"</span>,
        <span class="hljs-string">"logs:PutLogEvents"</span>
      ],
      <span class="hljs-attr">"Resource"</span>: <span class="hljs-string">"arn:aws:logs:*:*:*"</span>
    },
    {
      <span class="hljs-attr">"Effect"</span>: <span class="hljs-string">"Allow"</span>,
      <span class="hljs-attr">"Action"</span>: [
        <span class="hljs-string">"ec2:StartInstances"</span>,
        <span class="hljs-string">"ec2:StopInstances"</span>,
        <span class="hljs-string">"ec2:DescribeInstances"</span>,
        <span class="hljs-string">"rds:DescribeDBInstances"</span>,
        <span class="hljs-string">"rds:StartDBInstance"</span>,
        <span class="hljs-string">"rds:StopDBInstance"</span>,
        <span class="hljs-string">"elasticache:CreateCacheCluster"</span>,
        <span class="hljs-string">"elasticache:DeleteCacheCluster"</span>,
        <span class="hljs-string">"elasticache:DescribeCacheClusters"</span>
      ],
      <span class="hljs-attr">"Resource"</span>: <span class="hljs-string">"*"</span>
    }
  ]
}
</code></pre>
<blockquote>
<p>Note: If you’re using tagged resources, you can narrow the <code>"Resource": "*"</code> field to specific ARNs for better security. For example, replace <code>"*"</code> with the ARNs of your EC2, RDS, or ElastiCache resources.</p>
</blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736775880/212830a8-f898-4835-b418-f4daa6375e69.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736807570/981a4ca8-a0dc-44bc-889b-6339b23f06aa.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736840674/5ac696a4-3864-454e-993d-bff3b0f4cdf0.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736885536/5d7f3538-0af9-463e-bdc7-0ad0cdbb1372.png" alt class="image--center mx-auto" /></p>
<p>In the search bar, look for the policy you just created. For example, in my case, it is named <strong>lambda-starter-stopper-policy</strong>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733736915289/bcfee2a4-d639-4f8c-8940-a10b5d3d0263.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733737036234/fc91c385-72be-4f96-981c-fca27940c06a.png" alt class="image--center mx-auto" /></p>
<p><strong>Step2:</strong> After completing Step 1, click on the newly created Lambda function and enter the script for the EC2, RDS, and Redis instances that you want to automate for starting and stopping. Use the exact/correct name, instance-id and other required details.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733740329889/030d2031-ed0f-4013-9e99-707a18280f2d.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733737260134/2d5202f4-63e2-4f02-abcf-0101ed49831d.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733737288139/66cfa537-8cb5-466d-9dd1-e340816ca318.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733737315759/549b0dd2-2785-4af7-8144-51b469e82df2.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733737380418/381f4ea7-5280-4822-87d8-d17e2f0c6735.png" alt class="image--center mx-auto" /></p>
<p>After above configuration is done, add script to lambda function starter you created earlier like shown below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733741875069/9c0cc90d-7124-46dd-bef3-f15fddc3003a.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> boto3
region = <span class="hljs-string">'ap-south-1'</span> <span class="hljs-comment"># replace with your actual region</span>
instances = [<span class="hljs-string">'i-001g234456ef'</span>] <span class="hljs-comment"># replace with your actual instance-id</span>
db_instances = [<span class="hljs-string">'frm-test'</span>] <span class="hljs-comment"># replace with your actual db identifier name</span>

ec2 = boto3.client(<span class="hljs-string">'ec2'</span>, region_name=region)
rds = boto3.client(<span class="hljs-string">'rds'</span>, region_name=region)
elasticache = boto3.client(<span class="hljs-string">'elasticache'</span>, region_name=region)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">lambda_handler</span>(<span class="hljs-params">event, context</span>):</span>
    ec2.start_instances(InstanceIds=instances)
    <span class="hljs-keyword">for</span> db_instance <span class="hljs-keyword">in</span> db_instances:
        rds.start_db_instance(
        DBInstanceIdentifier = db_instance
     )
    print(<span class="hljs-string">'start your instances: '</span> + str(instances))
    response = elasticache.create_replication_group(
        ReplicationGroupId=<span class="hljs-string">'frm-test'</span>, <span class="hljs-comment"># replace with your actual redis cache name </span>
        ReplicationGroupDescription=<span class="hljs-string">'frm-test'</span>, <span class="hljs-comment"># you can keep the name same as cache name</span>
        NumCacheClusters=<span class="hljs-number">1</span>,
        CacheNodeType=<span class="hljs-string">'cache.t3.micro'</span>, <span class="hljs-comment"># replace with your cache node type</span>
        Engine=<span class="hljs-string">'redis'</span>,
        EngineVersion=<span class="hljs-string">'7.1'</span>, <span class="hljs-comment"># replace with your engine-version</span>
        CacheSubnetGroupName=<span class="hljs-string">'subnet-group-redis-project'</span>, <span class="hljs-comment"># replace with your actual subnet group name</span>
        SecurityGroupIds=[
            <span class="hljs-string">'sg-00946312345'</span>, <span class="hljs-comment"># replace with your security-group id used by redis</span>
        ],
        PreferredMaintenanceWindow=<span class="hljs-string">'sun:01:00-sun:02:00'</span>, <span class="hljs-comment"># replace with your actual details</span>
        Port=<span class="hljs-number">6379</span>, <span class="hljs-comment"># replace with your actual redis port</span>
        AutoMinorVersionUpgrade=<span class="hljs-literal">True</span>
    )
</code></pre>
<p><strong>Step 3:</strong> After completing Step 2, follow the same approach to create a Lambda function for stopping resources. Refer to the screenshots and script for guidance.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733742166371/461b4e6d-0107-4941-8b8b-76e79460c959.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> boto3

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">lambda_handler</span>(<span class="hljs-params">event, context</span>):</span>
    ec2 = boto3.client(<span class="hljs-string">'ec2'</span>)
    rds = boto3.client(<span class="hljs-string">'rds'</span>)
    elasticache = boto3.client(<span class="hljs-string">'elasticache'</span>)

    <span class="hljs-comment"># Stop EC2 instances</span>
    ec2_instances = [<span class="hljs-string">'i-001g234456ef'</span>] <span class="hljs-comment"># replace with your lambda starter mentioned ec2 instance-id</span>
    <span class="hljs-keyword">for</span> instance_id <span class="hljs-keyword">in</span> ec2_instances:
        ec2.stop_instances(InstanceIds=[instance_id])

    <span class="hljs-comment"># Stop RDS instances</span>
    rds_instances = [<span class="hljs-string">'frm-test'</span>] <span class="hljs-comment"># replace with your lambda starter mentioned db identifier name </span>
    <span class="hljs-keyword">for</span> instance_id <span class="hljs-keyword">in</span> rds_instances:
        rds.stop_db_instance(DBInstanceIdentifier=instance_id)

    <span class="hljs-comment"># Stop ElastiCache/Redis clusters</span>
    elasticache_clusters = [<span class="hljs-string">'frm-test'</span>] <span class="hljs-comment"># replace with your lambda starter mentioned redis cache name</span>
    <span class="hljs-keyword">for</span> cluster_id <span class="hljs-keyword">in</span> elasticache_clusters:
        elasticache.delete_replication_group(ReplicationGroupId=cluster_id)

    <span class="hljs-keyword">return</span> {
        <span class="hljs-string">'statusCode'</span>: <span class="hljs-number">200</span>,
        <span class="hljs-string">'body'</span>: json.dumps(<span class="hljs-string">'Resources stopped successfully'</span>)
    }
</code></pre>
<p><strong>Step4:</strong> Create AWS EventBridge scheduler rule for lambda starter/stopper automation.</p>
<p><strong>STOPPER REFERENCE:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253118581/456231f9-f332-42f2-8a4c-203cf07b39e5.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253234183/e88d949e-d06a-4c47-94b7-18a70ab34972.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253276099/6e9ad461-6156-4c77-8a11-ca779d642632.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253326269/8420167f-8a50-477a-bfbb-d10ca86c5b95.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253355379/815a795f-486d-4f33-918d-21db7fdc9081.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253389377/dc438c4c-05e4-4c1d-b3be-e15a130f79db.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253491731/ea601b71-05ca-437d-8fec-d74b04fd4ae9.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253619189/540d950b-dc8f-4c06-85e6-56da4ff546c6.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253681546/ffbdaa4a-558e-4035-875e-1a75c0aab718.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253823084/3f746872-f155-4dca-9820-8bc2c2cfb7b2.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253875372/2e755d52-fef5-4828-bbe1-74bfa128960b.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253924357/ca17efe6-19a2-4678-b5ff-f33ce60b8702.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734253957531/5eebc955-2517-48f3-abda-e2c24d1f07d4.png" alt class="image--center mx-auto" /></p>
<p><strong>STARTER REFERENCE:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734857805950/b4a82f26-b585-470c-b8b6-fffa7efaf83c.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734857973409/59e760e8-9fb2-4cbe-b29f-482dd6b0dc92.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734858007348/da89715f-afff-417a-bf1f-0e7a33801a88.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734858068709/df09ae3b-9df9-415a-a8a0-9c7c6f17ccb4.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734858113739/c2c7ab82-50e5-4ac6-8b75-a1cbc34b3795.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734858146172/f40ee461-7c31-4864-a94d-2c58425ace28.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734858181303/46551c86-8a71-4506-ac14-7b81f653191c.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734858221939/3ff3ead6-9e86-46bb-be8b-589407fecd1b.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734858416742/52886de6-a9ef-4e16-9c96-92c05ba1e738.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734858460109/c7d5a558-a742-426d-9f12-be59adf9a73b.png" alt class="image--center mx-auto" /></p>
<blockquote>
<p><strong>NOTE:</strong> After creating the scheduler, attach it to the respective starter/stopper Lambda functions for automation. Ensure the scheduler is properly linked to the functions.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1734858617139/75d80214-d821-44fd-9c37-284f416de328.png" alt class="image--center mx-auto" /></p>
</blockquote>
<p><strong>Conclusion</strong><br />By following these steps and creating the AWS EventBridge scheduler rule, you’ve successfully automated the starting and stopping of your resources during off-hours. This approach not only streamlines operations but also significantly optimizes your AWS costs.</p>
<p>Ensure your scheduler rule is correctly set up by referring to the attached screenshots. If you encounter any issues, revisit the steps or consult the official AWS documentation for further assistance.</p>
<p>If this guide was helpful or you have suggestions for improvement, feel free to share your feedback.</p>
]]></content:encoded></item><item><title><![CDATA[Prevent Storage Issues: Automated EC2 Cleanup Using AWS Systems Manager]]></title><description><![CDATA[Deploying services on AWS EC2 instances using Docker over time can lead to high storage, memory, and CPU usage. Manually increasing volume size and running prune commands to clear overlay storage and reduce memory usage is not a sustainable solution....]]></description><link>https://upasanasingh.com.np/prevent-storage-issues-automated-ec2-cleanup-using-aws-systems-manager</link><guid isPermaLink="true">https://upasanasingh.com.np/prevent-storage-issues-automated-ec2-cleanup-using-aws-systems-manager</guid><category><![CDATA[AWS EC2 Cleanup Automation]]></category><dc:creator><![CDATA[Upasana Singh]]></dc:creator><pubDate>Mon, 29 Jul 2024 18:15:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1721632904469/44378afe-df4c-4107-8d74-0f77c4df45f1.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Deploying services on AWS EC2 instances using Docker over time can lead to high storage, memory, and CPU usage. Manually increasing volume size and running prune commands to clear overlay storage and reduce memory usage is not a sustainable solution. To address this, we can automate the cleanup of EC2 instances using AWS Systems Manager.</p>
<p>By setting up a maintenance window with a <em>cron expression</em>, you can automate the regular cleanup of instances without manual intervention. This setup can be customized to run daily, during off-hours, or at specific intervals such as every few days or weeks. While there are other scheduling options available, configuring a <em>cron expression</em> is typically the easiest. This automation helps maintain optimal storage utilization and performance. Below is a step-by-step guide with screenshots to assist you through each stage of the process.</p>
<p>Step1: In the AWS Management Console, navigate to EC2, locate the IAM role for your instance, and attach the <code>AmazonSSMManagedInstanceCore</code> policy. This grants Systems Manager the permissions needed to manage and clean up your EC2 instances.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721635784159/50d78b40-0375-45de-8eae-fdc30eb5c48a.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721635590207/9987676e-6aa1-4ea8-9939-6c385ad3954f.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721635622001/f2e9708b-85c8-429b-9ca7-196836cb429c.png" alt class="image--center mx-auto" /></p>
<p>Step2: After completing Step 1, connect to the EC2 instance using Session Manager. Verify the Docker and system agent paths by running the following commands: <code>which docker</code> and <code>which systemctl</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721636115785/713ee5f9-d738-4cf0-a57c-0b35a50cb984.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721636135047/d8981a7e-4d97-43e4-8bc6-e1649cc00502.png" alt class="image--center mx-auto" /></p>
<p>Step3: Go to AWS Systems Manager, click on "Documents" in the left sidebar, and create a cleanup document as shown in the screenshots.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721636775372/01f78a9a-16f4-440f-b8ff-720729abc110.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721636798621/6310a7ea-da41-4d43-b462-89c36e7ea39e.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721636830116/29846165-54cd-40e6-bb34-edf71095218a.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721638887079/503a300b-7d25-44fb-9d3c-96b43d16fb2c.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>For AWS Linux-Based EC2 Instances(Confirm the Docker and system agent paths after Step 2):</p>
<pre><code class="lang-yaml">  <span class="hljs-attr">schemaVersion:</span> <span class="hljs-string">'2.2'</span>
  <span class="hljs-attr">description:</span> <span class="hljs-string">"Run Docker cleanup commands on EC2 instances"</span>
  <span class="hljs-attr">mainSteps:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">action:</span> <span class="hljs-string">aws:runShellScript</span>
      <span class="hljs-attr">name:</span> <span class="hljs-string">runDockerCleanup</span>
      <span class="hljs-attr">inputs:</span>
        <span class="hljs-attr">runCommand:</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">/bin/docker</span> <span class="hljs-string">stop</span> <span class="hljs-string">$(/bin/docker</span> <span class="hljs-string">ps</span> <span class="hljs-string">-q)</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">sleep</span> <span class="hljs-number">2</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">/bin/docker</span> <span class="hljs-string">system</span> <span class="hljs-string">prune</span> <span class="hljs-string">-a</span> <span class="hljs-string">-f</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">sleep</span> <span class="hljs-number">3</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">/bin/docker</span> <span class="hljs-string">volume</span> <span class="hljs-string">ls</span> <span class="hljs-string">-qf</span> <span class="hljs-string">dangling=true</span> <span class="hljs-string">|</span> <span class="hljs-string">xargs</span> <span class="hljs-string">-r</span> <span class="hljs-string">/bin/docker</span> <span class="hljs-string">volume</span> <span class="hljs-string">rm</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">sleep</span> <span class="hljs-number">3</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">/bin/systemctl</span> <span class="hljs-string">restart</span> <span class="hljs-string">ecs</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">sleep</span> <span class="hljs-number">3</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">/bin/docker</span> <span class="hljs-string">system</span> <span class="hljs-string">prune</span> <span class="hljs-string">-a</span> <span class="hljs-string">-f</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">sudo</span> <span class="hljs-string">sync;</span> <span class="hljs-string">sudo</span> <span class="hljs-string">echo</span> <span class="hljs-number">3</span> <span class="hljs-string">&gt;</span> <span class="hljs-string">/proc/sys/vm/drop_caches</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">/bin/systemctl</span> <span class="hljs-string">status</span> <span class="hljs-string">ecs</span>
          <span class="hljs-bullet">-</span> <span class="hljs-string">/bin/docker</span> <span class="hljs-string">ps</span> <span class="hljs-string">-a</span>
</code></pre>
</li>
<li><p>For Ubuntu-Based EC2 Instances(Confirm the Docker and system agent paths after Step 2):</p>
</li>
</ul>
<pre><code class="lang-yaml"><span class="hljs-attr">schemaVersion:</span> <span class="hljs-string">'2.2'</span>
<span class="hljs-attr">description:</span> <span class="hljs-string">"Run Docker cleanup commands on EC2 instances"</span>
<span class="hljs-attr">mainSteps:</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">action:</span> <span class="hljs-string">aws:runShellScript</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">runDockerCleanup</span>
    <span class="hljs-attr">inputs:</span>
      <span class="hljs-attr">runCommand:</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">/usr/bin/docker</span> <span class="hljs-string">stop</span> <span class="hljs-string">$(/bin/docker</span> <span class="hljs-string">ps</span> <span class="hljs-string">-q)</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">sleep</span> <span class="hljs-number">2</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">/usr/bin/docker</span> <span class="hljs-string">system</span> <span class="hljs-string">prune</span> <span class="hljs-string">-a</span> <span class="hljs-string">-f</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">sleep</span> <span class="hljs-number">3</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">/usr/bin/docker</span> <span class="hljs-string">volume</span> <span class="hljs-string">ls</span> <span class="hljs-string">-qf</span> <span class="hljs-string">dangling=true</span> <span class="hljs-string">|</span> <span class="hljs-string">xargs</span> <span class="hljs-string">-r</span> <span class="hljs-string">/bin/docker</span> <span class="hljs-string">volume</span> <span class="hljs-string">rm</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">sleep</span> <span class="hljs-number">3</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">/usr/bin/systemctl</span> <span class="hljs-string">restart</span> <span class="hljs-string">ecs</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">sleep</span> <span class="hljs-number">3</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">/usr/bin/docker</span> <span class="hljs-string">system</span> <span class="hljs-string">prune</span> <span class="hljs-string">-a</span> <span class="hljs-string">-f</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">sudo</span> <span class="hljs-string">sync;</span> <span class="hljs-string">sudo</span> <span class="hljs-string">echo</span> <span class="hljs-number">3</span> <span class="hljs-string">&gt;</span> <span class="hljs-string">/proc/sys/vm/drop_caches</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">/usr/bin/systemctl</span> <span class="hljs-string">status</span> <span class="hljs-string">ecs</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">/usr/bin/docker</span> <span class="hljs-string">ps</span> <span class="hljs-string">-a</span>
</code></pre>
<p><strong><em>NOTE</em></strong>: <em>If your services include</em> cron jobs*, avoid stopping running containers and restarting the ECS service as part of cleanup scripts. This can disrupt scheduled tasks. To modify the provided script for such scenarios, comment out the script lines* <code>docker stop</code> <em>and</em> <code>systemctl restart ecs</code> <em>Use only the prune and dangling volume cleanup commands for such services.</em></p>
<ul>
<li>After the document is created, you can view and verify it as shown in the screenshots below.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721639290013/2faf5cc2-e6b1-4f5e-899a-18dc71141d2e.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721639312482/12e58525-6c2a-4758-9230-1ea4e69d37f9.png" alt class="image--center mx-auto" /></p>
<p>Step4: Verify that your EC2 instance appears in Fleet Manager(found in the Systems Manager left sidebar), as shown in the screenshot below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721639647540/cbb52a9d-b3da-453e-bb17-02f9200b3c13.png" alt class="image--center mx-auto" /></p>
<p>Step5: Set up a maintenance window to automate the cleanup using the created document, as shown in the screenshots below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721640224713/3a463c16-a642-4eb2-8963-34170982ba35.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721640259294/d2b848bd-99fe-4270-9ef8-c0c5dd4970c9.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721640302123/32746b15-c8ff-40e1-a2f9-e079a9221d3d.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721641265573/acefaeea-7c8b-4e0f-9d23-7acc9a0162c0.png" alt class="image--center mx-auto" /></p>
<p>Step6: Navigate to the Systems Manager console, select "Maintenance Windows" from the left sidebar, choose your maintenance window, and click "Register Targets" to schedule the cleanup command on the selected EC2 instances, as shown in the screenshots below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721643698202/6bb26557-8a24-4a95-9b82-be6118c1b8c2.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721645479170/dcbc64ac-34a1-413e-bf69-82fb21217fd7.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721643725956/78b9842a-d26a-43d2-bcc1-bedbb01d4900.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721643754064/f15e1d01-c882-4d6b-bac9-250e16b6f740.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721644126916/a3a3d8e8-0472-4451-b1a7-70ed67df9d07.png" alt class="image--center mx-auto" /></p>
<p>Step7: Register a "Tasks" to link the cleanup document with the maintenance window and the registered target instances, as shown in the screenshots below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721645590493/cdc178f2-b7f2-4995-89dd-a0e92d4ae65a.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721645630521/9317e0d6-9ab8-4d31-92bc-a6b63411326b.png" alt class="image--center mx-auto" /></p>
<ul>
<li>Search for the created command document name in the command document search bar and select it.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721645734878/649d731e-283e-4613-a87c-ac81d8e264e7.png" alt class="image--center mx-auto" /></p>
<ul>
<li>Select the target you registered earlier in the maintenance window. Set the concurrency value to 1 (tasks run on one instance at a time) and the error threshold value to 1 (task stops if one instance fails). This ensures sequential execution and halts on errors for accurate control.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721646080483/70c9b985-337f-4b95-af9e-34e67481e4a1.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721646676681/c2d85a7a-45cf-42e7-b1d3-10e3da04449f.png" alt class="image--center mx-auto" /></p>
<ul>
<li>After registering the task, you can view and check the details, including description, tasks, and targets, as shown in the screenshots below.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721647555556/19187873-6206-4b9b-a69e-a5a86dd638f2.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721647575040/4b9af059-34fc-495c-ad15-6f5400771b51.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721647597738/5a404670-90ab-428a-8679-3603a0654863.png" alt class="image--center mx-auto" /></p>
<ul>
<li>Check the history to verify the success or failure status of the cleanup commands executed on EC2 instances during the configured time period, as shown in the screenshot below.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721648262394/1c9c972d-1248-45de-9e32-51ff9d11ccc2.png" alt class="image--center mx-auto" /></p>
<hr />
<blockquote>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong><em>Note:</em></strong><em> Check Docker overlays and system storage using </em><code>df -h</code><em> and monitor memory usage with </em><code>free -h</code><em> on your EC2 instances </em>both before and after executing the cleanup commands.<em> Automating cleanup commands helps prevent hitting storage and memory limits, which could cause loss of EC2 server access and poor performance due to full storage, memory, and CPU usage.</em></div>
</div></blockquote>
]]></content:encoded></item></channel></rss>