Cost Optimization
AWS Cost Optimization: A Practical Guide for Production
AWS bills rarely grow because of one big mistake. They grow through many small defaults that nobody revisited: a NAT gateway carrying traffic it did not need to, instances sized for a launch-day estimate, log groups that never expire, snapshots from servers that no longer exist.
That is good news, because small defaults can be fixed safely. On a production environment I managed, applying the practices below reduced operating cost by approximately 25 to 30 percent. Your result depends on where you start, so treat that as an example of what is possible, not a promise.
Start by measuring
Do not change anything until you know where the money goes.
- Open Cost Explorer and group by service, then by usage type, then by tag or account.
- Find the top five line items. They usually account for most of the bill.
- Check data transfer specifically. It hides inside EC2, NAT Gateway and CloudFront line items and is often a surprise.
- Turn on AWS Budgets and Cost Anomaly Detection now, so that future surprises reach you quickly.
Tagging makes this repeatable. Apply at least Environment, Project and Owner tags through your infrastructure code. The Terraform best practices article shows how to set them once with default_tags.
1. Cut network cost with VPC endpoints
Workloads in private subnets usually reach AWS services through a NAT gateway. A NAT gateway charges per hour and per gigabyte processed, so heavy traffic to S3, ECR or other AWS services adds up quickly.
Gateway endpoints for S3 and DynamoDB have no hourly or data charge. They are usually the easiest saving available:
resource "aws_vpc_endpoint" "s3" {
vpc_id = aws_vpc.main.id
service_name = "com.amazonaws.${var.region}.s3"
vpc_endpoint_type = "Gateway"
route_table_ids = aws_route_table.private[*].id
}
Interface endpoints (for ECR, CloudWatch, Secrets Manager and others) do charge per hour and per gigabyte, but the data rate is lower than NAT. They pay off when traffic is high, and they also keep traffic off the public internet, which helps security. Check the numbers for your traffic before adding many of them, especially in several availability zones.
Also review cross-AZ traffic. Chatty services spread across zones pay for every gigabyte that crosses a zone boundary.
2. Right-size before anything else
Right-sizing means matching resources to measured use, not to the original estimate.
- EC2 and RDS. Look at CPU, memory and network over at least two weeks. AWS Compute Optimizer gives recommendations, but memory metrics need the CloudWatch agent.
- ECS tasks. Compare reserved CPU and memory against actual use. Tasks sized “to be safe” are often 2 to 4 times larger than needed.
- Kubernetes. Compare pod requests against real usage. Over-requested pods waste node capacity, which you pay for.
Reduce in steps, watch the metrics after each change, and keep a rollback ready. Do this before buying commitments, or you will commit to capacity you do not need.
3. Commit to the baseline
Once usage is right-sized, cover the steady part with Savings Plans or Reserved Instances. They trade flexibility for a lower rate.
- Commit only to what you will run continuously, measured over several months.
- Compute Savings Plans are the more flexible option. They apply across instance families, regions and services such as Fargate and Lambda.
- Leave the variable top of your usage on demand or on Spot.
4. Use Spot where interruption is acceptable
Spot capacity is significantly cheaper than on demand, but AWS can reclaim it with two minutes’ notice. It suits stateless, fault-tolerant workloads: batch jobs, workers, CI runners and many web tiers behind a load balancer.
On Kubernetes, Karpenter provisions right-sized nodes on demand and can mix Spot and on-demand capacity:
apiVersion: karpenter.sh/v1
kind: NodePool
metadata:
name: general
spec:
template:
spec:
requirements:
- key: karpenter.sh/capacity-type
operator: In
values: ["spot", "on-demand"]
nodeClassRef:
group: karpenter.k8s.aws
kind: EC2NodeClass
name: default
disruption:
consolidationPolicy: WhenEmptyOrUnderutilized
consolidateAfter: 1m
limits:
cpu: "200"
Spread across several instance types, set pod disruption budgets, and handle interruption signals so a reclaimed node does not drop traffic. Keep critical, stateful or singleton workloads on on-demand capacity.
5. Set log retention
CloudWatch log groups keep data forever by default. Storage and ingestion both cost money, and most logs lose value after a few weeks.
resource "aws_cloudwatch_log_group" "api" {
name = "/app/api"
retention_in_days = 30
}
Choose retention by purpose: short for debug logs, longer where audit or compliance requires it, and archive to S3 when you must keep logs cheaply. Also reduce ingestion at the source by lowering log verbosity in production and dropping health-check noise. Do not delete logs that regulations require you to keep.
6. Expire snapshots and apply S3 lifecycle rules
Storage that nobody owns keeps billing. Two rules prevent most of it.
Snapshots. Use AWS Backup or Data Lifecycle Manager to create snapshots on a schedule and delete them after a retention period. Review old snapshots of volumes and AMIs that no longer exist.
S3 lifecycle rules. Move data to cheaper storage classes as it ages and delete what you no longer need:
resource "aws_s3_bucket_lifecycle_configuration" "logs" {
bucket = aws_s3_bucket.logs.id
rule {
id = "tier-and-expire"
status = "Enabled"
filter {}
transition {
days = 30
storage_class = "STANDARD_IA"
}
transition {
days = 90
storage_class = "GLACIER"
}
expiration {
days = 365
}
noncurrent_version_expiration {
noncurrent_days = 30
}
abort_incomplete_multipart_upload {
days_after_initiation = 7
}
}
}
If access patterns are unpredictable, S3 Intelligent-Tiering moves objects automatically. Remember that small objects and early deletion can add charges in some storage classes, so check the pricing page for your data.
7. Make savings stick
Cost work fails when it is a one-off project.
- Keep budgets and anomaly alerts active, with a named owner.
- Review the bill monthly for 30 minutes.
- Add cost to code review. A new always-on resource is a decision with a price.
- Schedule non-production environments to shut down at night and on weekends.
- Delete what you no longer use, and do it regularly.
The order that works
- Measure and tag.
- Quick wins with no risk: endpoints, retention, snapshots and lifecycle rules.
- Right-size, in steps, with monitoring.
- Introduce Spot and Karpenter for suitable workloads.
- Buy commitments for the stable baseline.
- Set budgets, alerts and a monthly review.
Common mistakes
- Buying Savings Plans before right-sizing.
- Putting stateful workloads on Spot.
- Deleting logs or backups that compliance requires.
- Optimizing compute while data transfer is the real cost.
- Treating cost as a project instead of a habit.
Need help?
Cost optimization is one of my strongest areas of delivery. I review your usage, apply safe changes and set up the visibility to keep costs down. See cloud cost optimization or AWS cloud consulting, or discuss your project.
Related articles
- OpenTelemetry and Observability: A Practical Guide
- ECS vs EKS: Choosing the Right Container Platform on AWS