Step 1 : Firstly we have to create aws ec2 instance
Step 2 : In this step we create a snapshot
Then click on create snapshot
Step 3 : Create a lambda function
Step 4 : Then you can use below code
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
# Get all EBS snapshots
response = ec2.describe_snapshots(OwnerIds=['self'])
# Get all active EC2 instance IDs
instances_response = ec2.describe_instances(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
active_instance_ids = set()
for reservation in instances_response['Reservations']:
for instance in reservation['Instances']:
active_instance_ids.add(instance['InstanceId'])
# Iterate through each snapshot and delete if it's not attached to any volume or the volume is not attached to a running instance
for snapshot in response['Snapshots']:
snapshot_id = snapshot['SnapshotId']
volume_id = snapshot.get('VolumeId')
if not volume_id:
# Delete the snapshot if it's not attached to any volume
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as it was not attached to any volume.")
else:
# Check if the volume still exists
try:
volume_response = ec2.describe_volumes(VolumeIds=[volume_id])
if not volume_response['Volumes'][0]['Attachments']:
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as it was taken from a volume not attached to any running instance.")
except ec2.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'InvalidVolume.NotFound':
# The volume associated with the snapshot is not found (it might have been deleted)
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as its associated volume was not found.")
Note: Then save the file and click on deploy and after that click on test then it will fail
Note : Then go to the configuration and then go to the edit section and increase default execution time for lambda to 10 sec
Then go to the permission
Then go to the role name
Then create a policy
- Then you have to attach the created policies
Note: Then again create policies and list access level add describeinstance and describevolume
Then attach this policies and test your code
Here are some best practices to help you effectively manage and reduce your AWS costs:
Monitor Usage and Costs:
- Utilize AWS Cost Explorer and AWS Budgets to monitor your usage and costs. Set up cost and usage reports to receive regular updates on spending.
Rightsize Resources:
Choose instance types and sizes that match your workload's requirements. AWS offers a variety of instance types optimized for different use cases.
Use Auto Scaling to dynamically adjust resources based on demand, ensuring you're not over-provisioned.
Reserved Instances (RIs) and Savings Plans:
- Purchase Reserved Instances or Savings Plans for predictable workloads. These provide significant cost savings compared to On-Demand instances.
Spot Instances:
- Use Spot Instances for non-critical and fault-tolerant workloads. Spot Instances can be significantly cheaper than On-Demand instances.
Serverless and Managed Services:
- Leverage serverless architectures (AWS Lambda) and managed services (Amazon RDS, Amazon DynamoDB) to pay only for actual usage.
Use of Amazon S3 Storage Classes:
- Move infrequently accessed data to cheaper storage classes like Amazon S3 Glacier or S3 Infrequent Access.
Data Transfer Costs:
- Minimize data transfer costs by keeping data within the same AWS region whenever possible.
Optimize Data Transfer:
- Use AWS Direct Connect or AWS Snowball for large data transfers to reduce costs and speed up transfers.
Resource Tagging:
- Implement resource tagging to categorize resources by projects, teams, or purposes. This helps in tracking costs and attributing them accurately.
Lifecycle Policies:
- Implement lifecycle policies for resources like Amazon S3 objects or Amazon EBS volumes to automatically transition or delete resources as they age.
Use CloudFormation or Infrastructure as Code (IaC):
- Manage resources through CloudFormation or other IaC tools. This ensures consistent and controlled deployments, reducing the chance of unnecessary resources.
Optimize Load Balancers:
- Use Application Load Balancers instead of Classic Load Balancers for better cost efficiency and improved performance.
Reserved Capacity for Amazon RDS:
- Use Reserved DB Instances for Amazon RDS to save costs on your database workloads.
Continuous Monitoring and Optimization:
- Regularly review your architecture, usage patterns, and cost reports to identify areas for further optimization.
Training and Education:
- Keep your team updated on AWS best practices for cost optimization to ensure everyone is aligned with cost-saving strategies.
Third-Party Tools:
- Consider using third-party tools like AWS Trusted Advisor, CloudHealth, or CloudCheckr to gain deeper insights into cost optimization opportunities.
Remember that cost optimization is an ongoing process. Your infrastructure and usage patterns may change over time, so it's important to regularly review and adjust your strategies to ensure you're getting the best value from your AWS services.