We will start automating the resource cleanup by setting up necessary infrastructure such as creating an IAM Role and IAM User for automation access, defining an SSM Automation Document that performs the cleanup actions, and setting up an SNS Topic to notify the users about resources being deleted. These steps ensure the cleanup process has the right permissions, execution logic, and visibility before we actually run it.
Step 1: Setup SSM Automation Document
In this step, we will setup the workflow for cleanup automation
- Go to AWS Systems Manager console, select Documents, click Create Document, click Automation

- Document Name: CostManager-Cleanup
- Select Code tab and paste the following JSON code below:
{
"schemaVersion": "0.3",
"description": "Automates cleanup of unattached EBS volumes and unassociated Elastic IPs.",
"assumeRole": "{{ AutomationAssumeRole }}",
"parameters": {
"AutomationAssumeRole": {
"type": "String",
"description": "IAM role that allows Automation to clean up resources"
},
"Approver": {
"type": "String",
"description": "Email or ARN of the approver for cleanup actions"
}
},
"mainSteps": [
{
"name": "FindUnattachedVolumes",
"action": "aws:executeAwsApi",
"nextStep": "FindUnassociatedEIPs",
"isEnd": false,
"inputs": {
"Service": "ec2",
"Api": "DescribeVolumes",
"Filters": [
{
"Name": "status",
"Values": [
"available"
]
}
]
},
"outputs": [
{
"Name": "VolumeIds",
"Selector": "$.Volumes..VolumeId",
"Type": "StringList"
}
]
},
{
"name": "FindUnassociatedEIPs",
"action": "aws:executeAwsApi",
"nextStep": "ApproveDeletion",
"isEnd": false,
"inputs": {
"Service": "ec2",
"Api": "DescribeAddresses",
"Filters": [
{
"Name": "domain",
"Values": [
"vpc"
]
}
]
},
"outputs": [
{
"Name": "EipAllocationIds",
"Selector": "$.Addresses",
"Type": "MapList"
}
]
},
{
"name": "ApproveDeletion",
"action": "aws:approve",
"nextStep": "DeleteResources",
"isEnd": false,
"inputs": {
"Approvers": [
"{{ Approver }}"
],
"MinRequiredApprovals": 1,
"Message": "Approve cleanup of unattached Volumes and unassociated EIPs."
}
},
{
"name": "DeleteResources",
"action": "aws:executeScript",
"nextStep": "NotifySuccess",
"isEnd": false,
"inputs": {
"Runtime": "python3.12",
"Handler": "cleanup_handler",
"InputPayload": {
"volumes": "{{ FindUnattachedVolumes.VolumeIds }}",
"raw_eips": "{{ FindUnassociatedEIPs.EipAllocationIds }}"
},
"Script": "import boto3\n\ndef cleanup_handler(event, context):\n ec2 = boto3.client('ec2')\n results = []\n \n volumes = event.get('volumes', [])\n if isinstance(volumes, str):\n volumes = [volumes] if volumes else []\n \n for vol in volumes:\n try:\n ec2.delete_volume(VolumeId=vol)\n results.append(f'Deleted volume: {vol}')\n except Exception as e:\n results.append(f'Failed volume {vol}: {str(e)}')\n \n raw_eips = event.get('raw_eips', [])\n for eip in raw_eips:\n if 'AssociationId' not in eip:\n allocation_id = eip.get('AllocationId')\n if allocation_id:\n try:\n ec2.release_address(AllocationId=allocation_id)\n results.append(f'Released EIP: {allocation_id}')\n except Exception as e:\n results.append(f'Failed EIP {allocation_id}: {str(e)}')\n \n return {'status': 'completed', 'details': results}"
}
},
{
"name": "NotifySuccess",
"action": "aws:executeAwsApi",
"isEnd": true,
"inputs": {
"Service": "sns",
"Api": "Publish",
"TopicArn": "arn:aws:sns:us-east-1:505679505148:CostManagerAlerts",
"Message": "Cleanup completed for unattached Volumes and EIPs. Check details in Automation execution output."
}
}
]
}
Make sure to replace the REGION:ACCOUNT_ID with our region and account ID

The flow diagram on the right side shows us the document works with the following steps:
- Step 1-2: Discovers unattached EBS volumes and Elastic IPs
- Step 3: Pauses for human approval before deletion
- Step 4: Executes cleanup using Python script
- Step 5: Sends success notification via SNS
Then click Create Runbook, our CostManager-Cleanup runbook is ready to be used now

Step 2: Setup SNS Topic for Notifications
This will enable email notifications for approval requests and completion status
- Go to SNS Console, select Topics, click Create topic
- Type: Standard
- Name:
CostManagerAlerts - Click Create topic

- Add Email Subscription → Create subscription
- Protocol: Email
- Endpoint: Add Email Subscription
- Click Create subscription

- Next, check email inbox and confirm our subscription

This way, automation will send us an email whenever it needs approval or cleanup completed.
Step 3: Setup IAM User for Automation Approval
We need to create a dedicated user account that can approve cleanup actions during automation execution. Because AWS Systems Manager requires explicit user ARNs for approval workflows, not just email addresses.
- Go to to IAM Console, select IAM Users, click Create user
- Name the user: CostManager-AutomationUser
- Enable Programmatic and Console access (optional).
- Select “I want to create an IAM user”
- Console password: Autogenerated

- Click Next
- Select Attach Policies directly → Attach the following policies:
- AmazonEC2FullAccess (for deleting volumes and release IPs)
- AmazonSNSFullAccess (for sending notifications)
- AmazonSSMFullAccess (for approving automation executions)

- Click next for review

- Then click Create user

- Please note down the IAM ARN for this user and also the login credentials (can download it in form .csv file)
- When executing the automation:
- Use this IAM user’s ARN for the Approver field.
- Log in as this IAM user (not root) to manually approve the request in Systems Manager → Automation Executions.
Step 4: Setup IAM Role for Automation Execution
Here we will provide the automation document that we have created at the 1st step with necessary permissions to discover and delete AWS resources. SSM Automation needs temporary AWS credentials to execute the Python cleanup script.
- Go to IAM Console, select Roles, click Create Role
- Trusted entity: AWS Service
- Use case: Systems Manager
- click Next

- Attach policies:
- AmazonEC2FullAccess (describe/delete volumes, release EIPs)
- AmazonSNSFullAccess (send notifications)
- AmazonSSMFullAccess (run automation without permission issues)

- Role name:
CostManager-AutomationRole - Click Create role

- Important: Copy the Role ARN – needed for automation execution parameter
This role is only assumed by AWS Systems Manager during automation execution, not by users directly
Summary
In this part we have setup the automation infrastructure along with the IAM user and IAM role that can be used by AWS system manager run the workflow starting from resource detection, deletion approval, and notification sending through email.
In the next part, we will trigger this automation, approve cleanup via the console, and verify the deletion