Day-18-Aws

Mastering AWS Cost Optimization: A Comprehensive Guide for DevOps and Cloud Engineers

TLDR: This blog post explores the critical concept of AWS cost optimization, detailing its importance for organizations transitioning to cloud infrastructure. It provides a practical demonstration of a project that utilizes AWS Lambda functions to identify and delete stale resources, thereby reducing unnecessary cloud costs. The post also includes insights into the responsibilities of DevOps engineers in managing cloud expenses effectively.

In the ever-evolving landscape of cloud computing, cost optimization has emerged as a crucial aspect for organizations leveraging cloud services. This blog post delves into the concept of AWS cost optimization, particularly focusing on its significance for DevOps and Cloud Engineers. We will explore a practical project that demonstrates how to effectively manage cloud costs using AWS Lambda functions.

Create ec2 instance → volume and create snapshot with same volume id
search → ec2 → resources (card) → instances → launch instance
name : test-ec2
os : ubuntu
AMI : v22.04
instance type: t2.micro
key value pair : aws_login (selct from dropdown) create key value
Configure storage : 8gb
----> launch instance
automatically create volume for it —> click on created instance → storage(tab) → volume → id
EC2 Dashboard (sidebar) → resources (card) → volume → id —>[both volume id is same → above and this one]
EC2 Dashboard (sidebar) → resources (card) → snapshot →[resource type : volume → volume id : →description : test → create snapshot]
scenario : if i delete instance → automatically delete volume → automatically delete snapshot
real-world : if i delete instance → automatically delete volume → manually delete snapshot → so create lambda fn to delete unwanted snapshot
create lambda fn to delete unwanted snapshot
search → lambda → create fn →
author from scratch → fn name : cost-optimization-ebs-snapshot → create fn
tabs (code,test,monitor,conf,aliases,version)
code(tab) →code source : lambda_handler (python fn name) —> copy the code and paste it here → save and deploy → test [create new event → name: test → event sharing setting : private → save] → test ——> (if you are triggering lambda fn with cloudwatch then test is not required) → giving error
configuration(tab) → edit → timeout : 10 sec —→ (default execution time is 3 sec) → save
add permission → create policy and attach that policy to lambda fn
configuration(tab) → permissions (sidebar) → role name url → right click and open on new tab
it opens IAM → roles → permission → add permission → attach policy → create policy
select service : ec2 → filter by snapshot : DescribeSnapshots ,DeleteSnapshots → resources : all —> next
policy details [name: cost-optimization-ebs] → create policy
configuration(tab) → permissions (sidebar) → role name url → right click and open on new tab
it opens IAM → roles → permission → add permission → attach policy → attach policy → filter created policy : cost-optimization-ebs → add permission
run the script in lambda fn → gives error
it opens IAM → roles → permission → add permission → attach policy → create policy
select service : ec2 → filter by snapshot : DescribeVolumes ,DescribeInstances → resources : all —> next
policy details [name: ec2-permission] → create policy
it opens IAM → roles → permission → add permission → attach policy → attach policy → filter created policy : ec2-permission → add permission
run the script in lambda fn → now the script got executed → but snapshot is still there not deleted yet
if you delete the ec2 instance now → automatically delete volume → automatically delete snapshot
python code explanation
list all running ec2 instance
list all volume attach to running ec2 instance
if volume id is not present in snapshot → volume is deleted → delete snapshot
if volume id is present in snapshot , but volume is there but not attached to ec2 instance → delete snapshot
cloudwatch + lambda fn
search → cloudwatch → events(sidebar) → rules → create rule
Rule details[name :ebssnapshot-rule → description: → event bus: default → rule type : schedule] →continue Event Bridge Scheduler
schedule details[name : ebssnapshot-rule → description: → schedule group : default]
schedule pattern[occurance : one-time → date and time: → start date : → end date:] —> next

Why Organizations Move to the Cloud

Organizations are increasingly migrating to cloud platforms for two primary reasons:

  1. Reducing Infrastructure Overhead: Setting up and maintaining an on-premises data center requires significant resources, including hardware, software, and a dedicated team of system administrators.

  2. Optimizing Cloud Costs: While cloud services can reduce initial setup costs, they can also lead to unexpected expenses if not managed properly. This is where cost optimization becomes essential.

The Importance of Cost Optimization

Once an organization transitions to the cloud, the expectation is that costs will decrease. However, without proper management, cloud expenses can escalate. For instance, developers may create resources such as EC2 instances, EBS volumes, and S3 buckets, but if these resources are not monitored and managed, they can lead to unnecessary charges.

Common Scenarios Leading to Increased Costs

  • Stale Resources: Resources that are created but no longer in use, such as unmonitored EBS snapshots or S3 buckets filled with outdated data, can accumulate costs over time.

  • Inefficient Resource Management: Developers may forget to delete resources after use, leading to ongoing charges for storage and compute resources.

The Role of DevOps Engineers in Cost Optimization

DevOps engineers play a pivotal role in ensuring that cloud costs remain manageable. Their responsibilities include:

  • Monitoring cloud resources to identify stale or unused assets.

  • Implementing automated solutions to delete or notify teams about unnecessary resources.

  • Utilizing tools and scripts to streamline cost management processes.

Project Overview: Automating Cost Optimization with AWS Lambda

In this section, we will outline a project that demonstrates how to automate the identification and deletion of stale resources using AWS Lambda functions. This project will focus on EBS snapshots as a case study.

Project Architecture

The architecture for this project is straightforward:

  1. AWS Lambda Functions: We will write Python code within Lambda functions to interact with AWS APIs.

  2. Boto3 Library: This Python library will be used to communicate with AWS services, allowing us to list and manage EBS snapshots.

  3. CloudWatch Events: We can schedule the Lambda function to run at regular intervals, ensuring continuous monitoring of cloud resources.

Step-by-Step Implementation

  1. Setting Up the Lambda Function: Create a new Lambda function in the AWS Management Console and configure it to use the Boto3 library.

  2. Writing the Python Code: The code will perform the following tasks:

    • List all EBS snapshots.

    • Identify snapshots that are not associated with any active EC2 instances.

    • Delete stale snapshots based on predefined criteria.

  3. Testing the Function: Execute the Lambda function to ensure it correctly identifies and deletes stale snapshots.

Example Code Walkthrough

Here is a simplified version of the Python code that could be used in the Lambda function:

import boto3

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')
    snapshots = ec2.describe_snapshots()['Snapshots']
    for snapshot in snapshots:
        # Logic to check if snapshot is stale
        if is_stale(snapshot):
            ec2.delete_snapshot(SnapshotId=snapshot['SnapshotId'])
            print(f'Deleted snapshot: {snapshot['SnapshotId']}')

def is_stale(snapshot):
    # Implement logic to determine if snapshot is stale
    return True  # Placeholder for actual logic
  1. Granting Permissions: Ensure that the Lambda function has the necessary permissions to describe and delete snapshots by attaching the appropriate IAM policies.

  2. Scheduling with CloudWatch: Set up a CloudWatch rule to trigger the Lambda function at regular intervals, such as daily or weekly.

Conclusion

AWS cost optimization is a vital practice for organizations utilizing cloud services. By implementing automated solutions like the one demonstrated in this project, DevOps and Cloud Engineers can effectively manage cloud costs and ensure that resources are used efficiently. This not only helps in reducing expenses but also contributes to better resource management and operational efficiency.

As cloud environments continue to grow, the importance of cost optimization will only increase. By staying proactive and leveraging automation, organizations can navigate the complexities of cloud costs and maximize their investment in cloud technologies.