https://github.com/dncrypter/efficient-aws-cost-management
This project is focussed on practical problem of Resource management on cloud. where i try to give solution on Efficient AWS Cost Management through Stale Resource Detection
https://github.com/dncrypter/efficient-aws-cost-management
aws-cloudwatch aws-ebs-snapshot aws-ec2-intances aws-elb aws-lambda aws-s3
Last synced: over 1 year ago
JSON representation
This project is focussed on practical problem of Resource management on cloud. where i try to give solution on Efficient AWS Cost Management through Stale Resource Detection
- Host: GitHub
- URL: https://github.com/dncrypter/efficient-aws-cost-management
- Owner: DNcrypter
- Created: 2025-01-02T19:36:07.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-02T19:48:24.000Z (over 1 year ago)
- Last Synced: 2025-01-13T00:36:46.436Z (over 1 year ago)
- Topics: aws-cloudwatch, aws-ebs-snapshot, aws-ec2-intances, aws-elb, aws-lambda, aws-s3
- Language: Python
- Homepage:
- Size: 946 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
## đEfficient AWS Cost Management through Stale Resource Detection
[](https://choosealicense.com/licenses/mit/)
[](https://www.linkedin.com/in/nikhil--chaudhari/)
[](https://medium.com/@nikhil-c)
## đ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 - Creating Snapshot
##### 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â.

##### 4. Next, navigate to the âElastic Block Storeâ section and select âVolumesâ.

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

##### 7. In Volume ID section choose your default Volume ID created when we create instance.
##### 8. Next, click âNext,â provide a name for your Snapshot, and then scroll down and click âCreate Snapshotâ.

### 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â.

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

##### 7. Click on the âCodeâ section.
##### 8. Next, clear the existing code and replace it with the âidentify_stale_snapshots.pyâ code.

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

##### 10. Please configure the settings as displayed above and then scroll down. Next, click on âCreate Eventâ.
##### 11. 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.

##### 12. Select the service as âEC2â

##### 13. In the âActionsâ section, grant permissions for the following actions: DescribeInstances, DescribeVolumes, DescribeSnapshots, DeleteSnapshots.
##### 14. After Creating the Policies , Navigate to the Lambda Sections.
##### 15. Next, go to the page of the Lambda function youâve created. In the âPermissionsâ section, click on the role name.

##### 16. Click on âAdd Permissionsâ and then select âAttach Policy.â

##### 17. Choose the correct policy you created.

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

### 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.

##### 5. 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.




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

##### 3. Scroll Down and then Click Next.


##### 4. Scroll Down and then Click Next.
##### 5. On the next page, choose âNoneâ for the âAction after Scheduleâ option.



##### 6. You have successfully created the scheduler, which will trigger the Lambda function every hour.
##### 7. 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.