0
0
AWScloud~5 mins

Cost optimization pillar in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Managing cloud costs is important to avoid paying more than needed. The cost optimization pillar helps you use resources efficiently and save money while keeping your applications running well.
When you want to reduce your monthly cloud bill without affecting performance
When you need to choose the right type of cloud resources for your workload
When you want to track and monitor your cloud spending regularly
When you want to automate turning off unused resources to save money
When you want to plan your cloud usage to avoid surprises in costs
Commands
This command fetches your AWS cost data for May 2024, showing how much you spent in total. It helps you understand your current spending.
Terminal
aws ce get-cost-and-usage --time-period Start=2024-05-01,End=2024-05-31 --granularity MONTHLY --metrics BlendedCost
Expected OutputExpected
{ "ResultsByTime": [ { "TimePeriod": { "Start": "2024-05-01", "End": "2024-05-31" }, "Total": { "BlendedCost": { "Amount": "123.45", "Unit": "USD" } } } ] }
--time-period - Specifies the date range for the cost data
--granularity - Sets the detail level of the cost data (DAILY, MONTHLY)
--metrics - Chooses which cost metric to show
This command lists all running EC2 virtual machines. Knowing which instances run helps identify unused or oversized resources to save costs.
Terminal
aws ec2 describe-instances --filters Name=instance-state-name,Values=running
Expected OutputExpected
{ "Reservations": [ { "Instances": [ { "InstanceId": "i-0abcd1234efgh5678", "InstanceType": "t3.medium", "State": {"Name": "running"} } ] } ] }
--filters - Filters instances by their state to show only running ones
This command stops a running EC2 instance that is not needed right now, helping reduce costs by not paying for compute time when idle.
Terminal
aws ec2 stop-instances --instance-ids i-0abcd1234efgh5678
Expected OutputExpected
{ "StoppingInstances": [ { "InstanceId": "i-0abcd1234efgh5678", "CurrentState": {"Name": "stopping"}, "PreviousState": {"Name": "running"} } ] }
--instance-ids - Specifies which instance to stop
This command lists your S3 storage buckets. Reviewing storage helps find unused or large buckets that can be cleaned up to save storage costs.
Terminal
aws s3 ls
Expected OutputExpected
2024-05-15 10:00:00 my-app-logs 2024-05-15 10:05:00 my-backups 2024-05-15 10:10:00 my-temp-files
Key Concept

If you remember nothing else from this pattern, remember: regularly check your cloud usage and stop or resize resources you don't need to save money.

Common Mistakes
Ignoring unused resources that keep running
You keep paying for resources even when they are not used, wasting money
Regularly list running resources and stop or delete those not needed
Not monitoring cost data regularly
You miss spikes or unexpected charges that can grow your bill
Use cost reports and alerts to track spending every month
Choosing large instance types without checking workload needs
Oversized resources cost more but may not improve performance
Match instance size to actual workload requirements and adjust as needed
Summary
Use AWS Cost Explorer CLI to check your monthly cloud spending.
List running EC2 instances to find resources that can be stopped or resized.
Stop unused EC2 instances to reduce compute costs.
List S3 buckets to identify storage that can be cleaned up.