<?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 Jordi Casanella on Medium]]></title>
        <description><![CDATA[Stories by Jordi Casanella on Medium]]></description>
        <link>https://medium.com/@jcasanella79?source=rss-eb7e149965d8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*QI4Duc2Jxk6hKgWL</url>
            <title>Stories by Jordi Casanella on Medium</title>
            <link>https://medium.com/@jcasanella79?source=rss-eb7e149965d8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 08:01:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jcasanella79/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[How to restart AWS Aurora using lambdas and CloudWatch Alarms]]></title>
            <link>https://medium.com/@jcasanella79/how-to-restart-aws-aurora-using-lambdas-and-cloudwatch-alarms-792c3d67026f?source=rss-eb7e149965d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/792c3d67026f</guid>
            <category><![CDATA[cloudwatch-alarm]]></category>
            <category><![CDATA[lambda-function]]></category>
            <category><![CDATA[aws-aurora]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Jordi Casanella]]></dc:creator>
            <pubDate>Fri, 03 Feb 2023 13:32:46 GMT</pubDate>
            <atom:updated>2023-02-03T13:32:46.117Z</atom:updated>
            <content:encoded><![CDATA[<h3>Overview</h3><p>Monitoring helps us to ensure the correctness of our applications. When something does not work as expected, it’s important to be notified and take some actions to reduce the impact.</p><p>To illustrate the process, will create a CloudWatch alarm with a metric <strong>CPUUtilization</strong> that will be triggered when the <strong>CPUUtilization &gt; 75%</strong> for more than 5 minutes, the action will restart the Amazon Aurora instance, associated with the metric. You must adapt these line for your purpose but hopefully will drive you to the first steps to take an action when an alarm occurs.</p><h3>What is a metric and an Alarm</h3><p>Before to jump into the process, let’s define some concepts that will help us to understand this reading.</p><p>AWS defines a metric as:</p><blockquote>a time ordered set of data points that are published to CloudWatch. Think of a metric as a variable to monitor, and the data points as representing the values of that variable over time. For example, the CPU usage of a particular EC2 instance is one metric provided by Amazon EC2.</blockquote><p>An alarm as:</p><blockquote><strong>A metric alarm watches a single CloudWatch metric or the result of a math expression based on CloudWatch metrics</strong>. The alarm performs one or more actions based on the value of the metric or expression relative to a threshold over a number of time periods.</blockquote><h3>Designing the solution</h3><p>To restart an AWS Aurora instance when the alarm (CPUUsage &gt; 75%) is triggered, we must set up different components:</p><ul><li>Role and IAM policy to be used by the lambda function</li><li>Lambda function that allows us to restart the Aurora instance</li><li>Define the metric in CloudWatch.</li><li>Enable the alarm with the boundary and window time</li><li>Create a SNS topic where to send the notification</li><li>Subscribe the lambda function to the SNS topic</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JwdVn4TQR4ipflLDkaD9oA.png" /></figure><ol><li>Create a custom <strong>IAM policy</strong> such as:</li></ol><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Sid&quot;: &quot;VisualEditor0&quot;,<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: [<br>                &quot;rds:StopDBInstance&quot;,<br>                &quot;rds:StartDBInstance&quot;,<br>                &quot;rds:RebootDBInstance&quot;<br>            ],<br>            &quot;Resource&quot;: &quot;*&quot;<br>        }<br>    ]<br>}</pre><p>2. Create an <strong>IAM Role</strong>, with the properties:<br>- <em>AWS Service</em> — Use case lambda<br>- <em>Permissions</em> — IAM policy created in the Step 1.</p><figure><img alt="Role — Policies associated" src="https://cdn-images-1.medium.com/max/1024/1*lADIEKThacd4dUIMc1WcHg.png" /></figure><figure><img alt="Role — Trust Relationship" src="https://cdn-images-1.medium.com/max/1024/1*rAE2luHBcbn0b4eyqh-2Ww.png" /></figure><p>3. Create <strong>Lambda</strong> <strong>function</strong>:<br>- <em>Runtime</em>: Python 3.9 (this is the most recent version at the moment to write this document)<br>- <em>Use an existing role</em>: Select the role created in the Step 2. <br>- <em>Create an environment variable</em> with the name of the AWS Aurora instance to be rebooted. We named as <strong>DB_INSTANCE</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m4h12227wsIINxO9sFZBPQ.png" /></figure><ul><li><em>Lambda Code:</em></li></ul><pre>import sys<br>import botocore<br>import boto3<br>import json<br>import logging<br>import os<br>from botocore.exceptions import ClientError<br><br>rds = boto3.client(&quot;rds&quot;)<br>logger = logging.getLogger()<br>logger.setLevel(logging.INFO)<br><br><br>def lambda_handler(event, context):<br>    dbInstance = os.environ[&quot;DB_INSTANCE&quot;]<br>    logger.info(f&quot;dbInstance to reboot: ${dbInstance}&quot;)<br><br>    reboot_rds_instances(dbInstance)<br><br>    return {<br>        &quot;statusCode&quot;: 200,<br>        &quot;body&quot;: json.dumps(<br>            &quot;Script execution completed. See Cloudwatch logs for complete output&quot;<br>        ),<br>    }<br><br><br>### reboot rds instances<br>def reboot_rds_instances(dbInstance):<br>    try:<br>        rds.reboot_db_instance(DBInstanceIdentifier=dbInstance)<br>        logger.info(&quot;Success :: reboot_db_instance &quot; + dbInstance)<br>    except ClientError as e:<br>        logger.error(e)<br>    return &quot;reboot:OK&quot;</pre><p><strong>Note</strong>: To run another action, check the documentation. See below the link to the python API for RDS (<a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rds.html#RDS.Client.reboot_db_instance">https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rds.html#RDS.Client.reboot_db_instance</a>)</p><ul><li><em>Test</em>: First must be deployed, click <strong>Deploy button</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oIiGehMObvTtvNsjwKMuWw.png" /></figure><p>With the Lambda deployed, click <strong>Test button a</strong>nd in the execution console you will see status and output of the lambda output.</p><figure><img alt="Lambda — Test execution" src="https://cdn-images-1.medium.com/max/1024/1*nnt0GAx6X5EDAPziipKHJw.png" /></figure><p><strong>4. </strong>Create a<strong> CloudWatch Alarm</strong>: In the CloudWatch page, on the left panel there’s the <em>Alarm</em> section. Click <strong>All alarms</strong> and <strong>Create Alarm </strong>button. The wizard will ask us for the metric to create the alarm — We’re using <strong>CPUUtilization</strong> and select the <strong>RDS&gt;By Database Class </strong>and the instance to collect the metrics but feel free to use any other metric.</p><figure><img alt="CloudWatch — Select metric CPUUtilization" src="https://cdn-images-1.medium.com/max/1024/1*6bmKrZ49SxdvzUkEqYWa_A.png" /></figure><p>Now it’s time to add the Alarm conditions to trigger the alarm: <br>- <em>Period</em>: 5 minutes <br>- <em>Condition</em>: Greater than <br>- <em>Value</em>: 75</p><p>The alarm will be activated if CPUUtilization for the AWS Aurora instance is &gt; 75% for a consecutive period of 5 minutes. Remember this Alarm is created for illustrative purposes, create the alarm that matches your requirements.</p><p><strong>Note</strong>: To test really quick, you can change the period to 1 min and as condition Less than 75 (<strong>don’t do in production, this is just to testing</strong>)</p><figure><img alt="CloudWatch Alarm — Configure how to be notified" src="https://cdn-images-1.medium.com/max/1024/1*dlZbIlMdxS7Vao_q-0Wbyw.png" /></figure><p>Once configured the Alarm, specify how to be notified, in our example we’re using SNS and we create a new topic (you can use an existing one)</p><p>5. <strong>Subscribe</strong> the lambda function to SNS topic. To receive the SNS notification and execute the Lambda, must be subscribed to the SNS topic.</p><p>First copy the <em>ARN for the Lambda function</em> created in step 3. Look for <em>Simple Notification Service</em> in the AWS Console and choose the topic created/referenced in step 4. We must configure the following properties:</p><ul><li><em>Protocol</em>: AWS Lambda</li><li><em>Endpoint</em>: ARN Lambda function</li></ul><figure><img alt="SNS — Subscription" src="https://cdn-images-1.medium.com/max/1024/1*mpQXD6U1CjaWR13ilDFZFw.png" /></figure><p>All set up is done. Once is triggered the alarm the instace will be rebooted. Hope this reading helps you to understand how works CloudWatch Alarms and how can automate the process to take an action, when an alarm is triggered.</p><h3>References</h3><ul><li>Amazon CloudWatch vs Events (or EventBridge): <a href="https://blog.shikisoft.com/amazon-cloudwatch-alarms-vs-events-or-eventbridge/">https://blog.shikisoft.com/amazon-cloudwatch-alarms-vs-events-or-eventbridge/</a></li><li>Rebooting a DB instance: <a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_RebootInstance.html">https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_RebootInstance.html</a></li><li>Schedule Amazon RDS stop and start using AWS Lambda: <a href="https://aws.amazon.com/blogs/database/schedule-amazon-rds-stop-and-start-using-aws-lambda/">https://aws.amazon.com/blogs/database/schedule-amazon-rds-stop-and-start-using-aws-lambda/</a></li><li>Setting up a CPU usage alarm using the AWS Management Console: <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/US_AlarmAtThresholdEC2.html">https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/US_AlarmAtThresholdEC2.html</a></li><li>Creating CloudWatch alarms to monitor Amazon Aurora: <a href="https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/creating_alarms.html">https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/creating_alarms.html</a></li><li>python RDS documentation: <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rds.html#RDS.Client.reboot_db_instance">https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rds.html#RDS.Client.reboot_db_instance</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=792c3d67026f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>