How to Automate AWS Resource Cleanup with AWS Systems Manager in the vast expanse of cloud computing
Tags: cleanup, systemsmanager, aws, automation
Published: 2024-04-08 06:57:49

<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading">Introduction</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>There is no need to explain the importance of cloud computing in the digital world. But with all these things comes other things as well. The importance of resource cleanup cannot be ignored when following the cloud computing approach. Neglecting this task will only lead to cluttered environment and spiraling costs. It is bizarre to think that you cannot save money when using cloud computing. Guess what, you can save a lot if you follow the optimization steps properly.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>AWS doesn’t stay behind and offers its customers a service which will help them in automating operational tasks across its resources along with a secure, organized and cost-effective cloud environment. The service is none other than <a href="https://aws.amazon.com/systems-manager/">AWS Manager</a>. There are many advantages of using this service like the cleanup process of unused resources is automated, cost leakage is also prevented and the environment stays efficient.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Understanding AWS Systems Manager</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you want to minimize the cost of your resources and optimize their performance then there is nothing better than using AWS Systems Manager. This service stands out to be a robust service of AWS as it provides developers and system administrators the necessary tools that they will require to manage and monitor the AWS resources.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">AWS Systems Manager Capabilities</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>AWS System Manager offers features that are designed to facilitate the centralized management of your AWS resources. However, these categories can be organized into different areas where each area addresses a specific aspect of the resource management.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Systems manager makes sure that it provides you a detailed visibility into EC2 instances created on AWS along with on-premises servers. Following this you can secure your compute resources by administrating them remotely. Execution of scripts, collection of inventory and management of patches are all included in it.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>AWS System Manager also simplifies the deployment and maintenance process of your applications across <a href="https://aws.amazon.com/pm/ec2-amd/?gclid=Cj0KCQjw2a6wBhCVARIsABPeH1uNw_jPlz3v7R0XNJdUapFmVRY-fmAf4e6VC4Z-IaVKL-KbS--VYhcaAlkdEALw_wcB&amp;trk=b12018fa-12e4-4dcd-9d3f-98fc71b815ec&amp;sc_channel=ps&amp;ef_id=Cj0KCQjw2a6wBhCVARIsABPeH1uNw_jPlz3v7R0XNJdUapFmVRY-fmAf4e6VC4Z-IaVKL-KbS--VYhcaAlkdEALw_wcB:G:s&amp;s_kwcid=AL!4422!3!525736709982!p!!g!!aws%20dev%20environment!13385003189!128480858612">AWS environments</a>. Moreover, it helps to keep your software updated and make it run as expected.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Using this service, you can even track the configuration compliance of your resources with the desired configurations. This is important for maintaining the compliance and security standards.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Moreover, you can also monitor and perform a diagnosis of your AWS resources. It offers and supports automated actions in response to specific conditions or events.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Last but not the least, <a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html">System Manager Parameter Store</a> also provides a centralized store that lets you manage the configuration data (plain as well as encrypted).</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Utilizing AWS Systems Manager for Resource Management and Automation</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You can use AWS Systems Manager’s powers for effective resource management and automation in many ways.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Automatic patches are applied for on-premises servers and AWS resources to make sure that the systems present are up to date along with latest security updates.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>To maintain consistent configurations across AWS environment, you will have to use <a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-state.html">State Manager</a> which is a feature of Systems Manager. It will automatically apply changes or fixes as required.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Make use of Systems Manager in order to automate the cleanup process of unused resources such as EBS volumes, EC2 instances and RDS instances.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>All in all, AWS Systems Manager is indeed a helpful tool used for the management of AWS resources. Its capabilities allow organizations to ensure compliance, streamline the operations and automate the routing tasks.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Preparing Your AWS Environment for Automation</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you want to use the full potential of AWS Systems Manager in order for resource cleanup and other operational tasks then you need to prepare the AWS environment carefully. This process involves setting up and allowing necessary permissions, configuring the <a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/ssm-agent.html">SSM (Systems Manager Agent)</a> and organizing your resources effectively.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Setting Up Necessary Permissions: IAM Roles and Policies for Systems Manager</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>If you want Systems Manager to perform various operations on your behalf then you need to create <a href="https://aws.amazon.com/iam/?gclid=Cj0KCQjw2a6wBhCVARIsABPeH1u3dznbqWEVYzOnL1r2KAHK1eLRXZsP-rZJiY-Ke5J_wPi-12MrF2IaArfdEALw_wcB&amp;trk=ed6b9778-818b-45b5-bb8f-a4cd592d7f09&amp;sc_channel=ps&amp;ef_id=Cj0KCQjw2a6wBhCVARIsABPeH1u3dznbqWEVYzOnL1r2KAHK1eLRXZsP-rZJiY-Ke5J_wPi-12MrF2IaArfdEALw_wcB:G:s&amp;s_kwcid=AL!4422!3!651510255315!p!!g!!amazon%20iam!19835791352!147106034796">IAM roles</a> with some necessary permissions. Pre-defined policy templates provided by AWS grants the Systems Manager all the necessary permissions required to manage the instances.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Make sure your EC2 instances have IAM role with necessary and appropriate policies attached. This will help in secure communication between instances and Systems Manager.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>In case you want your IAM policies to provide more control over the Systems Manager dos and don’ts, you can create custom IAM policies.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Configuring Systems Manager Agent (SSM Agent) on Your Instances</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>SSM Agent is a software that should be installed on your EC2 instances and on-premises servers to allow Systems Manager to handle the management. Majority of <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html">Amazon Machine Images (AMIs)</a> come with pre-installed SSM agent.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Make sure that SSM Agent is properly installed on your instances. If SSM Agent is not installed then you can consult AWS documentation where detailed instructions are present regarding its installation and updating.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>You need to be sure of this thing that SSM Agent is running so that proper communication can be made with Systems Manager. If the case is otherwise then change the status of SSM Agent to running using CLI or Systems Manager itself.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Best Practices for Organizing Resources for Effective Management</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>For every AWS resource there is a tag associated with it which acts like a key-value pair. These tags are beneficial as they provide flexibility in organizing your resources in such a way that they get align with your billing and operational practices. You can even use tags to indicate ownership.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In case there is a collection of resources sharing one or more tags, you can use <a href="https://docs.aws.amazon.com/ARG/latest/APIReference/Welcome.html">AWS Resource Groups</a> for managing and automating tasks. Managing tasks become easier when alike resources are grouped.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Structure your resources hierarchically by using a combination of accounts, <a href="https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scps.html">SCPs (Service Control Policies)</a> and <a href="https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_ous.html">OUs (Organizational Units)</a> within AWS Organizations. This structure helps in the application of operational policies and governance.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Prepare your AWS environment as stated if you want to automate operations using AWS Systems Manager. Enable all the necessary permissions, configure the instances with SSM Agent correctly and organize your resources efficiently if you want to benefit the most from Systems Manager.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Automating Resource Cleanup with Maintenance Windows</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Bringing automation in resource cleanup process is important for efficient management and cost-effective AWS environment. <a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-maintenance.html">AWS Systems Manager Maintenance Windows</a> offer a smart way using which you can schedule and automate the cleanup tasks. Moreover, it is ensured that they perform regularly and without involving any type of manual interference.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Introduction to Maintenance Windows in Systems Manager</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Maintenance Windows as the name suggests define specific time windows for carrying out different maintenance tasks related to your AWS resources. These tasks include software updates, software patches and resource cleanup. The execution timing of these tasks is set in such a way that your environment will remain optimized and no critical services will be disrupted.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Step-by-Step Guide to Creating a Maintenance Window for Resource Cleanup</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Create a Maintenance Window</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Go to AWS Systems Manager Console and navigate to Maintenance Windows. Select the option saying “Create Maintenance Window” which will be present under Actions and Change.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Give name, specify schedule as well as duration for your maintenance window.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Register Targets</strong><strong></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>After you have created the Maintenance Window, register targets for cleanup by clicking on the option Register Targets.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Define the targets using tags. You can also manually select the resources, the choice is yours.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Assign Tasks to the Maintenance Window</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Select “Assign Tasks” option and then choose Run Command. You can also choose “AWS-RunAutomationDocument” depending on the cleanup task.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>You can either select the custom SSM documents or <a href="https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/systems-manager-automation-docs.html">AWS-provided automation documents</a> for cleanup.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Setting Up Automation Tasks Within the Maintenance Window</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Using AWS-Provided Automation Documents</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>As told earlier, AWS offers pre-built Automation documents that are commonly used for cleanup tasks. This includes AWS-RemoveUnusedEBSVolumes or AWS-DeleteSnapshot.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>When you are about to assign a task, search and select your preferred AWS-provided documents and also configure its parameters.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Creating Custom SSM Documents for Specific Cleanup Tasks</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>If the AWS-provided documents don’t meet your cleanup needs then you can create custom SSM Automation Documents as well.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Now head towards “Documents” which is present under Systems Manager shared resources. After this select the option saying “Create Document”. For the document type choose “Automation”.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Defining your automation steps will require you to use SSM document syntax. For instance, you can also write a script which will identify and delete the unattached EBS volumes which are present more than a certain number of days.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Use the custom documents when assigning tasks in Maintenance Window.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Following these steps will allow you to effectively automate the cleanup process of AWS resources. To assist here, Maintenance Windows in Systems Manager play a very vital role. Along with saving tons of money and resources you will be able to experience an efficient cloud environment.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Implementing Custom Cleanup Scripts</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Understanding Systems Manager is not enough, you need to have the ability to write custom scripts as well. These scripts are mostly written in Python which utilize <a href="https://aws.amazon.com/sdk-for-python/">Boto3 AWS SDK</a>. Their working involves targeting unused resources like ELB, EC2 instances, snapshots and EBS volumes for deletion. Now it is time to learn and explore about custom scripts, packaging them as SSM documents and executing them through System Manager using both AWS Management Console and AWS CLI.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Writing Custom Cleanup Scripts</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Python and Boto3 for Resource Cleanup</strong><strong></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Key components of a Python script using Boto3 for the purpose of finding and deleting unattached EBS volumes are:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Initialize the Boto3 client for EC2.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Utilize filters to identify resources not currently in use (e.g., EBS volumes with the status available).</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Implement logic to delete the identified resources safely.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Script Considerations</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Include confirmation prompts to prevent accidental deletion of any resources.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Implement logging to track the script's actions and outcomes.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Packaging Scripts as SSM Documents</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Creating the SSM Document</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Use AWS SSM Document for packaging your cleanup script.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>SSM Documents assist in defining the actions that Systems Manager performs on your managed instances.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Document Format</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>SSM Documents can be written in YAML or JSON. Choose the format you prefer.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>AWS CLI Command to Create an SSM Document</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code><em>aws ssm create-document --content file://YourDocumentContent.json --name "CustomCleanupScript" --document-type "Command" --document-format JSON</em></code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Replace YourDocumentContent.json with the path to your own YAML or JSON file containing the SSM Document content.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Executing Custom Scripts through Systems Manager</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Using the AWS Management Console</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Head towards AWS Systems Manager Dashboard.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Navigate to Documents section and then select Create Document option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Go for Command Document and paste the script that has been packaged as an SSM Document.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Specify a proper name and create the document.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>After you have successfully created the document, select “Run Command” option.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>For the newly created document specify the target instances and then execute the cleanup operation.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Using the AWS CLI</strong><strong></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>To execute your custom script on target instances, use the following CLI command:</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>send-command

aws ssm send-command --document-name "CustomCleanupScript" --targets "Key=instanceids,Values=instanceID1,instanceID2" --parameters commands=YourCommand</code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Replace "CustomCleanupScript" with the name of your SSM Document.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Replace "instanceID1,instanceID2" with the IDs of the instances you want to target.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>If you follow these steps properly then you can use the automation features of AWS Systems Manager to create as well as execute custom resource cleanup tasks. After this you will experience an organized and cost-efficient AWS environment.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Monitoring and Logging</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Another important aspect included in the automation of AWS resources is monitoring the operations and logging their outcomes. For this case, <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/working_with_metrics.html">AWS CloudWatch</a> has to be the perfect fit. It offers various tools which are needed to track down the performance of automation tasks including setting up notifications based on their outcomes as well as custom cleanup scripts.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Configuring CloudWatch Logs for Systems Manager Operations</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Enable CloudWatch Logging for Systems Manager</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Execution logs from Systems Manager are sent to CloudWatch. In order to enable the logging, you will have to attach an IAM role having CloudWatch permissions to EC2 instances or Systems Manager role.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Setting Up CloudWatch Log Groups</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>To create a dedicated CloudWatch Log Group for Systems Manager operations, there are two ways; AWS Management Console and AWS CLI.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code><em>aws logs create-log-group --log-group-name /aws/ssm/SystemsManagerLogGroup</em></code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Now what this log group will do is collect the logs from Systems Manager Operations along with the execution of custom cleanup scripts.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Monitoring the Execution of Cleanup Tasks</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>View Execution Logs in CloudWatch</strong><strong></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>After the logging has been enabled, you can use CloudWatch console to monitor the execution of cleanup tasks. You can also find the relevant Log Group and review the log streams for task execution details.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Utilize Systems Manager Run Command History</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>With so many other things, Systems Manager also provide a history of commands which are sent through the service. It also includes execution of cleanup tasks. The history informs regarding the success or failure of each task.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Setting Up Alarms and Notifications for Automation Task Outcomes</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Create CloudWatch Alarms Based on Log Metrics</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Another feature that you can use is <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html">CloudWatch Alarms</a>. You can create and set these alarms based on log patterns or specific metrics from Systems Manager operations. For example, you can set up alarm for failed cleanup tasks by identifying the related error patterns in the logs.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>You can enable the notification via Amazon SNS.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Configuring SNS Notifications</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>It is time to create an SNS topic dedicated to receiving notifications about Systems Manager task outcomes.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Subscribe your SMS number or email address to the SNS topic for receiving notifications.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>To automate the notification process, link the SNS topic to your CloudWatch Alarm.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code><em>aws sns create-topic --name SystemsManagerNotifications</em></code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>For real time notifications on task executions, attach the topic to CloudWatch alarm.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Following this approach will make you familiar with proactive management and allowing you to identify the issues more quickly in order to maintain the optimal performance of your AWS operations.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Security and Compliance Considerations</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Security and compliance cannot be ignored when automating resource cleanup in AWS. To take care of these factors you need to come up with a strategic approach. This section highlights best practices for maintaining security and compliance throughout the automation process.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Make sure to implement the principle of least privilege by giving away only the necessary permissions for the execution of each task.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Store SSM Documents and custom scripts securely with the help of <a href="https://aws.amazon.com/secrets-manager/">AWS Secrets Manager</a> or Systems Manager Parameter Store. You will need access keys to access this sensitive information.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>First understand and then define <a href="https://www.druva.com/glossary/what-is-a-data-retention-policy-definition-and-related-faqs#:~:text=Data%20retention%20policies%20concern%20what,storage%2C%20depending%20on%20the%20requirements.">data retention policies</a> that align perfectly with the compliance requirements. Make sure to implement safeguard scripts to prevent accidental deletion of resources.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Conduct regular audits and reviews of your automated cleanup processes.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Maintain a well-formatted document of automated processes including the permission required, rationale for specific cleanup tasks and impact on compliance.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Advanced Strategies for Cleanup Automation</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you want to automate the cleanup in AWS without the manual triggers or scheduled tasks then it is indeed possible! You just need to incorporate the advanced strategies that uses broader AWS eco-system. This section deals with the usage of <a href="https://aws.amazon.com/pm/lambda/?gclid=Cj0KCQjw2a6wBhCVARIsABPeH1txaZR59syL-7JnUQatInoHNipq9NJt1LPZa3s8S7iDc5ETR95-HiEaAslcEALw_wcB&amp;trk=cc9d3bb4-0a21-43d0-8236-0f2deaffe082&amp;sc_channel=ps&amp;ef_id=Cj0KCQjw2a6wBhCVARIsABPeH1txaZR59syL-7JnUQatInoHNipq9NJt1LPZa3s8S7iDc5ETR95-HiEaAslcEALw_wcB:G:s&amp;s_kwcid=AL!4422!3!651510255294!e!!g!!aws%20lambda!19828212645!149982299751">AWS Lambda</a> for event-driven cleanup along with the integration of Systems Manager with other services offered by AWS.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Leveraging AWS Lambda for Event-Driven Cleanup</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>AWS Lambda allows you to run the code whenever any change occurs such as change in state or time. This capability can be used to start the cleanup tasks automatically when certain conditions are met.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Identify the events that indicate the resources which are ready to experience a cleanup like TerminateInstances or DeleteDBInstance.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Now write Lambda functions in whatever language you prefer so that cleanup task can be performed.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Configure the Lambda functions in such a way that they get triggered by AWS services events.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>AWS Lambda will perform the cleanup process at its earliest so that no extra charges are to be paid. It also provides the advantage of auto-scaling.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Integrating Systems Manager with Other AWS Services</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html">AWS CloudFormation</a> allows you to describe the infrastructure resources in your cloud environment. By integrating <a href="https://aws.amazon.com/config/">AWS Config</a> and Systems Manager, you can automate the actions like cleanup tasks. Another service, <a href="https://docs.aws.amazon.com/servicecatalog/latest/adminguide/introduction.html">AWS Service Catalog</a> allows the organizations to manage and create catalogs of IT services. By using these event-driven mechanisms along with AWS Lambda and performing the integration of Systems Manager with AWS Config and CloudFormation, you can achieve a much more efficient resource cleanup process.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In this article we have explored the automation of AWS resource cleanup using AWS Systems Manager. All the critical steps and strategies which are required for a cost-effective cloud environment have been explained above in detail. Organizations and companies of all sizes are encouraged to explore and implement the automation strategies for AWS resource cleanup process.</p>
<!-- /wp:paragraph -->