Giter Site home page Giter Site logo

aws-cost-optimization's Introduction

Efficient AWS Cost Management through Stale Resource Detection

Problem :

Sometimes, developers create EC2 instances with volumes attached to them by default. For backup purposes, these developers also create snapshots. However, when they no longer need the EC2 instance and decide to terminate it, they sometimes forget to delete the snapshots created for backup. As a result, they continue to incur costs for these unused snapshots, even though they are not actively using them.

Solution :

We're using AWS to save money on storage costs. We made a Smart Lambda function that looks at our snapshots and our EC2 instances. If Lambda finds a snapshot that isn't connected to any active EC2 instances, it deletes it to save us money. This helps us keep our AWS costs down.

Note :

There are many similar problems like this. For instance, we might attach an Elastic IP to our EC2 instance but forget to delete the Elastic IP after terminating the EC2 instance. In such a case, the Elastic IP continues to incur costs for us.

Steps :

Step 1:

  1. Log into your AWS Console.
  2. Navigate to the EC2 Console.
  3. In the Instances section, select 'Instances,' and then click on 'Launch Instance'.

EC2

  1. Next, navigate to the 'Elastic Block Store' section and select 'Volumes'.

Volume

  1. You will notice that a default volume has already been created for us.
  2. Next, click on 'Snapshots,' and then click the 'Create Snapshot' button. It will prompt you with a page that looks like this.

Snapshot

  1. In Volume ID section choose your default Volume ID created when we create instance.
  2. Next, click 'Next,' provide a name for your Snapshot, and then scroll down and click 'Create Snapshot'.

Snapshot1

Step 2 :

  1. After creating a Snapshot, navigate to the Lambda Console..
  2. You will see some options in the user interface, such as 'Create Function'.
  3. Click on 'Functions'.

Lambda

  1. Select 'Author from Scratch,' then enter the Function name, and choose the latest Python version.
  2. Scroll down and click 'Create Function'.
    6.After creating the function, scroll down, and you will see something like the image below..

Lambda1

  1. Click on the 'Code' section.
  2. Next, clear the existing code and replace it with the 'identify_stale_snapshots.py' code.

lambda2

  1. Click 'Deploy' to save your changes, and then click 'Test.' It will prompt a page that looks like the one given below.

lambda3

  1. Please configure the settings as displayed above and then scroll down. Next, click on 'Create Event'.
  2. Once you've created the event, proceed to the IAM Console(Identity and Access Management) and then navigate policies section to create a new policy.

lambda sp

  1. Select the service as 'EC2' lambda5

  2. In the 'Actions' section, grant permissions for the following actions: DescribeInstances, DescribeVolumes, DescribeSnapshots, DeleteSnapshots.

  3. After Creating the Policies , Navigate to the Lambda Sections.

  4. Next, go to the page of the Lambda function you've created. In the "Permissions" section, click on the role name.

lambda4

  1. Click on 'Add Permissions' and then select 'Attach Policy.'

lambda6

  1. Choose the correct policy you created.

lambda7

  1. Then scroll down and click 'Add Permissions'.
  2. After that, you can go to the Lambda function page and run the code; it will display some outputs as shown below.

lambda11

Step 3 :

  1. You can terminate the EC2 instance to test our Lambda function.
  2. Navigate to the EC2 console and then terminate the EC2 instance.
  3. Return to the Lambda console to test the code; go to the Lambda Function page.
  4. Under the Code section, click 'Test code', it will display an output like this.

lambda10

  1. As expected, our Lambda function deleted the snapshot because it was associated with a volume that couldn't be found.

Additional notes:

We can use CloudWatch to automatically trigger the Lambda function every hour, day, minute, or second. However, this may result in higher costs because our Lambda execution time increases when triggered automatically. Nevertheless, manually triggering this function is a better choice because it allows us to trigger it when needed.

CloudWatch or EventBridge Implementation :

Steps :

  1. Navigate to CloudWatch Console.

cw1 cw2 cw3 cw4

  1. Next, on the following page, configure the schedule pattern as follows:

cw5

  1. Scroll Down and then Click Next.

cw6 cw7

  1. Scroll Down and then Click Next.
  2. On the next page, choose 'None' for the 'Action after Schedule' option.

cw8 cw9 cw10

  1. You have successfully created the scheduler, which will trigger the Lambda function every hour.
  2. However, please note that this setup will incur some costs since the function is triggered continuously every hour. Alternatively, we can configure it to run on specific days and times as needed.

aws-cost-optimization's People

Contributors

mathesh-me avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.