Listen to this Post
Using serverless components and tools like Amazon EventBridge, you can automate almost anything in AWS without having to set up or manage any infrastructure. In most cases, it will all be covered under the free tier. Examples of using these tools are below.
Using the AWS SDK, you can programmatically perform almost any action in your account or make calls to external services. You can use serverless tools like AWS Lambda to execute SDK code and reach out to these.
Using automation to deal with cleaning up unused resources is an excellent use case for these types of approaches. Using tools like EventBridge allows you to set up regular scheduling for spinning down services to save costs or react to almost any kind of event in your account.
The article below from Manmeet Singh shows the pieces needed to run some of this automation, including what is needed to stop EC2 instances on a schedule from an AWS Lambda function.
You Should Know:
1. AWS Lambda Function to Stop EC2 Instances:
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
instances = ec2.describe_instances(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
instance_ids = [instance['InstanceId'] for reservation in instances['Reservations'] for instance in reservation['Instances']]
if instance_ids:
ec2.stop_instances(InstanceIds=instance_ids)
print(f"Stopped instances: {instance_ids}")
else:
print("No running instances to stop.")
2. Setting Up Amazon EventBridge Rule:
- Go to the Amazon EventBridge console.
- Create a new rule with a schedule expression (e.g., `cron(0 20 * * ? *)` to stop instances every day at 8 PM UTC).
- Set the target to the Lambda function created above.
3. AWS CLI Command to List Running Instances:
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query "Reservations[].Instances[].InstanceId"
4. AWS CLI Command to Stop Instances:
aws ec2 stop-instances --instance-ids <instance-id>
5. Automating Cleanup of Unused EBS Volumes:
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
volumes = ec2.describe_volumes(Filters=[{'Name': 'status', 'Values': ['available']}])
volume_ids = [volume['VolumeId'] for volume in volumes['Volumes']]
if volume_ids:
ec2.delete_volume(VolumeIds=volume_ids)
print(f"Deleted volumes: {volume_ids}")
else:
print("No unused volumes to delete.")
- AWS CLI Command to Delete Unused EBS Volumes:
aws ec2 delete-volume --volume-id <volume-id>
What Undercode Say:
Automation in AWS is a powerful way to optimize costs and manage resources efficiently. By leveraging tools like AWS Lambda and EventBridge, you can automate routine tasks such as stopping EC2 instances, cleaning up unused EBS volumes, and more. This not only saves time but also reduces unnecessary expenses. The provided Python scripts and AWS CLI commands are practical examples of how you can implement these automations in your own AWS environment. For more detailed steps and examples, refer to the original article: Cost Optimization through automation in AWS using Python.
References:
Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅



