Mindfulness

Declutter AWS Environment

Updated · By the Relaxmid editors

Reading time
9 min
Sections
7
Topic
Mindfulness

A decluttered AWS environment saves money, reduces security risk, and makes it easier to see what is actually running in your cloud. The biggest mistake people make is waiting until a service is broken to delete it, or deleting resources by hand without checking for dependencies. To clean up safely, you must first identify unused resources, then remove them in the correct order, and finally lock down the remaining configuration so nothing can be recreated by accident.

Identify Unused AWS Resources

Before you delete anything, list every resource in your account and mark the ones you do not recognize or are not paying for. AWS provides cost reports and service dashboards, but you need a full inventory to find orphaned items. Start with the most obvious candidates: old EC2 instances, unused Elastic IPs, and stopped or terminated but still billed resources.

Run a cost allocation report to see which services are consuming the most money. Look for services that appear in the report but have no active workloads. For example, an EC2 instance that has been stopped for more than 30 days is a candidate for deletion. Likewise, an Elastic IP that has not been attached to a running instance for a long period is likely unused. You can also use AWS Trusted Advisor or third‑party tools to scan for idle resources automatically.

Next, check for unused storage resources. S3 buckets that contain no objects or are not referenced by any application are candidates. EBS volumes that are attached to stopped instances or are not needed for backups should also be reviewed. DynamoDB tables that have zero read or write requests for an extended period are often abandoned. Make a clear list of resources you intend to keep and a separate list for deletion. Keep this list visible while you work so you do not accidentally remove something you still need.

Check for Dependencies Before Deleting

Deleting a resource without checking its dependencies can break your applications or create security gaps. AWS enforces some dependencies, such as an Elastic IP must be disassociated from an instance before deletion, but other relationships are not enforced automatically. You must verify these manually to avoid surprise downtime.

For EC2 instances, ensure that the instance is stopped or terminated and that it has no Elastic IPs attached. Check if the instance is part of an Auto Scaling group or a load balancer target group. If it is, you must remove it from those groups before terminating the instance. Also verify that any security groups attached to the instance are not used by other running resources. If the security group is only used by this instance, you can safely delete it after the instance is gone.

For S3 buckets, confirm that no application or service references the bucket in its configuration. Look for references in CloudFormation templates, AWS Lambda functions, or EC2 user data scripts. Even if the bucket is empty, it may still be referenced. If you find references, update the configuration to use a different bucket or a new bucket before deleting the old one. AWS does not allow deletion of a non‑empty bucket, so you must first delete the objects or empty the bucket.

For Elastic IP addresses, verify that they are not listed in any Elastic Network Interface (ENI) or in any CloudFormation stack that still exists. If an Elastic IP is disassociated but still allocated, it is still incurring charges. You can release it back to the pool once you are sure it is not needed. For load balancers, check that no target groups are associated with the load balancer and that no listeners are configured. Once a load balancer is deleted, you cannot recover its DNS name, so confirm that you no longer need it.

Remove EC2 Instances and Associated Resources

When you are ready to delete EC2 instances, follow a consistent sequence to avoid breaking dependencies. First, stop the instance if it is running. Stopping is different from terminating; stopping keeps the instance available for later restart, while terminating permanently removes it. If you only want to clean up unused capacity, stop the instance. If you are sure you no longer need it, terminate it to release the underlying instance type and any associated storage.

After stopping or terminating the instance, detach any Elastic IP addresses. Go to the EC2 console, select the instance, and choose the Elastic IP from the network settings. Click Disassociate. Once the Elastic IP is disassociated, you can release it back to the AWS pool. This stops the hourly charge for the IP address even if the instance is still running.

Next, remove the instance from any Auto Scaling groups. In the EC2 console, go to the Auto Scaling groups section and select the group. Click the Instances tab and find the instance you are deleting. Click the instance ID and choose Detach from group. If the group is no longer needed, you can also delete the entire Auto Scaling group after confirming that no other resources depend on it.

Remove the instance from any load balancer target groups. In the EC2 console, navigate to the load balancer and then to the Target groups section. Select the target group and click Targets. Find the instance ID and choose Deregister from target group. Once the instance is deregistered, it will no longer receive traffic from the load balancer.

Finally, delete the instance. In the EC2 console, select the stopped or terminated instance and choose Instance state > Terminate instance. Confirm the deletion when prompted. AWS will release the underlying compute resources and any attached EBS volumes. If the EBS volume is a root volume, it will be deleted along with the instance. If it is a data volume, you can choose to delete it or keep it for later use.

Clean Up Unused Storage and Databases

Storage resources can silently accumulate and consume a large portion of your AWS bill. Start by reviewing S3 buckets. Open the S3 console and list all buckets. For each bucket, check the last modified date and the number of objects. A bucket that has not been modified in six months is a good candidate for deletion if it is not referenced by any application.

To delete an S3 bucket, you must first delete all objects within it. You can do this manually by selecting each object and choosing Delete, or you can use the AWS CLI with a command like aws s3 rm s3://bucket-name –recursive. Once the bucket is empty, you can delete the bucket itself with aws s3 rb s3://bucket-name –force. Be careful with the –force flag, as it will permanently remove the bucket and all its objects.

For EBS volumes, identify those that are attached to stopped instances or are not part of a backup strategy. In the EC2 console, go to the Volumes section and filter by status. Volumes that are available or attached to stopped instances are candidates for deletion. Select the volume and choose Delete volume. AWS will permanently remove the volume and all its data, so confirm that you do not need the data before proceeding.

DynamoDB tables can also accumulate unused data. In the DynamoDB console, list all tables and check the Last Accessed timestamp. Tables that have not been accessed in a long period may be abandoned. You can delete a table by selecting it and choosing Delete table. This will permanently remove the table and all its data, so ensure that no application references the table before you delete it.

Release Elastic IPs and Load Balancers

Elastic IP addresses are charged even when they are not in use. Release any Elastic IP addresses that are not currently attached to a running instance. In the EC2 console, go to the Network & Security section and select Elastic IPs. Select the Elastic IP and choose Release elastic IP (address). Confirm the release when prompted. This stops the hourly charge for the IP address.

For load balancers, identify those that are no longer receiving traffic. In the EC2 console, go to the Load Balancers section and select the load balancer. Check the health checks and listener configurations. If the load balancer has no active listeners and no target groups, it is likely unused. You can delete the load balancer by selecting it and choosing Delete. AWS will remove the load balancer and all associated resources, such as security groups and target groups.

When deleting a load balancer, you may also need to delete its security groups. Go to the Security Groups section in the EC2 console and select the security group associated with the load balancer. Check that no running instances or other resources use this security group. If it is only used by the load balancer, you can delete it. Be cautious, as deleting a security group can affect any resources that rely on it.

Set Up Budgets and Alarms

After cleaning up your environment, set up automated alerts to prevent resources from growing again. AWS Budgets allows you to set monthly or quarterly spending limits and receive notifications when you approach or exceed them. Create a budget for your most expensive services, such as EC2, S3, and DynamoDB. Set the alert threshold to 80% of your planned spend and choose to receive email notifications or create an SNS topic for push notifications.

CloudWatch alarms can provide more granular control. Create alarms for specific metrics, such as the number of running EC2 instances or the amount of storage used in an S3 bucket. For example, set an alarm on the number of running EC2 instances to trigger when the count exceeds a certain number. This can help you identify sudden increases in usage that may indicate a forgotten resource.

Enable AWS Trusted Advisor to get ongoing recommendations for cost optimization and security. Trusted Advisor scans your account for unused resources, overly permissive security groups, and other inefficiencies. Review the recommendations regularly and apply the changes that are safe to implement. This proactive approach helps you maintain a clean environment over time.

How to Check If a Resource Is Still Needed

To verify if a resource is still needed, trace its usage across your infrastructure. For an EC2 instance, check if it is referenced in any CloudFormation templates, Terraform scripts, or application configuration files. Look for references in CI/CD pipelines, deployment scripts, or monitoring tools. If you cannot find any references and the instance is not critical, it is safe to delete.

For an S3 bucket, search your codebase for references to the bucket name. Use grep or a text search tool to find any strings that match the bucket name. Check if any Lambda functions, CloudFormation stacks, or EC2 user data scripts reference the bucket. If no references are found, the bucket is likely unused.

For a security group, check which instances are attached to it. In the EC2 console, select the security group and look at the Inbound and outbound rules. The Instances tab will show you which running instances use the security group. If no instances are attached, but the security group is still listed in the console, it may be orphaned. You can safely delete it if no other resources depend on it.

What Happens If You Delete a Resource by Mistake

Deletion in AWS is often permanent. If you accidentally delete an EC2 instance or an S3 bucket, you cannot recover the resources themselves. However, you may be able to recover data if you had backups or if the resource was in a multi‑region configuration. Always test your deletion process in a non‑production environment first.

To minimize the risk of accidental deletion, enable AWS Config rules that alert you when resources are deleted. AWS Config can track changes to your resources and send notifications to an SNS topic. You can then review the deletion and take action if it was not intentional. This provides an additional layer of protection for critical resources.

Related

General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more

Spotted an error? Tell us and we will correct it (corrections policy).