Get the FREE Ultimate OpenClaw Setup Guide →

cost-optimization

npx machina-cli add skill agenticdevops/devops-execution-engine/cost-optimization --openclaw
Files (1)
SKILL.md
8.3 KB

Cost Optimization

Cloud cost analysis and optimization strategies.

When to Use This Skill

Use this skill when:

  • Analyzing cloud spending
  • Finding cost savings opportunities
  • Right-sizing resources
  • Investigating cost spikes

AWS Cost Analysis

Current Month Costs

# Total month-to-date
aws ce get-cost-and-usage \
  --time-period Start=$(date +%Y-%m-01),End=$(date +%Y-%m-%d) \
  --granularity MONTHLY \
  --metrics BlendedCost \
  --query 'ResultsByTime[].Total.BlendedCost.Amount' \
  --output text

Cost by Service

aws ce get-cost-and-usage \
  --time-period Start=$(date +%Y-%m-01),End=$(date +%Y-%m-%d) \
  --granularity MONTHLY \
  --metrics BlendedCost \
  --group-by Type=DIMENSION,Key=SERVICE \
  --query 'ResultsByTime[].Groups[].{Service:Keys[0],Cost:Metrics.BlendedCost.Amount}' \
  --output table

Cost Trend (Daily)

aws ce get-cost-and-usage \
  --time-period Start=$(date -d '7 days ago' +%Y-%m-%d),End=$(date +%Y-%m-%d) \
  --granularity DAILY \
  --metrics BlendedCost \
  --query 'ResultsByTime[].{Date:TimePeriod.Start,Cost:Total.BlendedCost.Amount}' \
  --output table

Cost by Tag

# By environment
aws ce get-cost-and-usage \
  --time-period Start=$(date +%Y-%m-01),End=$(date +%Y-%m-%d) \
  --granularity MONTHLY \
  --metrics BlendedCost \
  --group-by Type=TAG,Key=Environment \
  --output table

# By team
aws ce get-cost-and-usage \
  --time-period Start=$(date +%Y-%m-01),End=$(date +%Y-%m-%d) \
  --granularity MONTHLY \
  --metrics BlendedCost \
  --group-by Type=TAG,Key=Team \
  --output table

Find Unused Resources

EC2 Instances

# Stopped instances (still incur EBS costs)
aws ec2 describe-instances \
  --filters "Name=instance-state-name,Values=stopped" \
  --query 'Reservations[].Instances[].[InstanceId,Tags[?Key==`Name`].Value|[0],LaunchTime]' \
  --output table

# Low CPU utilization (last 7 days average < 5%)
aws cloudwatch get-metric-statistics \
  --namespace AWS/EC2 \
  --metric-name CPUUtilization \
  --dimensions Name=InstanceId,Value=i-1234567890 \
  --start-time $(date -d '7 days ago' -u +%Y-%m-%dT%H:%M:%SZ) \
  --end-time $(date -u +%Y-%m-%dT%H:%M:%SZ) \
  --period 604800 \
  --statistics Average

Unattached EBS Volumes

aws ec2 describe-volumes \
  --filters "Name=status,Values=available" \
  --query 'Volumes[].[VolumeId,Size,CreateTime]' \
  --output table

Unused Elastic IPs

aws ec2 describe-addresses \
  --query 'Addresses[?AssociationId==`null`].[PublicIp,AllocationId]' \
  --output table

Old Snapshots

# Snapshots older than 90 days
aws ec2 describe-snapshots \
  --owner-ids self \
  --query "Snapshots[?StartTime<='$(date -d '90 days ago' +%Y-%m-%d)'].[SnapshotId,VolumeSize,StartTime]" \
  --output table

Unused Load Balancers

# ALBs with no targets
aws elbv2 describe-target-groups \
  --query 'TargetGroups[?length(TargetHealthDescriptions)==`0`].[TargetGroupName,LoadBalancerArns]'

Unused RDS Instances

# Check for low connections
aws cloudwatch get-metric-statistics \
  --namespace AWS/RDS \
  --metric-name DatabaseConnections \
  --dimensions Name=DBInstanceIdentifier,Value=mydb \
  --start-time $(date -d '7 days ago' -u +%Y-%m-%dT%H:%M:%SZ) \
  --end-time $(date -u +%Y-%m-%dT%H:%M:%SZ) \
  --period 604800 \
  --statistics Maximum

Kubernetes Cost Analysis

Resource Requests vs Usage

# Current resource requests
kubectl get pods -A -o custom-columns=\
'NAMESPACE:.metadata.namespace,NAME:.metadata.name,CPU_REQ:.spec.containers[*].resources.requests.cpu,MEM_REQ:.spec.containers[*].resources.requests.memory'

# Actual usage
kubectl top pods -A --sort-by=cpu

# Compare requests vs usage
kubectl top pods -A --containers

Over-Provisioned Pods

# Find pods with high requests but low usage
kubectl top pods -A --no-headers | awk '$3 < 10 {print $1, $2, "CPU:", $3}'

Namespace Resource Usage

# By namespace
kubectl top pods -A --no-headers | awk '{ns[$1]+=$3} END {for (n in ns) print n, ns[n]"m"}' | sort -k2 -rn

Right-Sizing Recommendations

EC2 Right-Sizing

# Get recommendations
aws ce get-rightsizing-recommendation \
  --service EC2 \
  --configuration '{"RecommendationTarget":"SAME_INSTANCE_FAMILY","BenefitsConsidered":true}'

General Recommendations

aws ce get-cost-forecast \
  --time-period Start=$(date +%Y-%m-%d),End=$(date -d 'next month' +%Y-%m-01) \
  --metric BLENDED_COST \
  --granularity MONTHLY

Reserved Instances & Savings Plans

RI Coverage

aws ce get-reservation-coverage \
  --time-period Start=$(date -d '30 days ago' +%Y-%m-%d),End=$(date +%Y-%m-%d) \
  --granularity MONTHLY

RI Utilization

aws ce get-reservation-utilization \
  --time-period Start=$(date -d '30 days ago' +%Y-%m-%d),End=$(date +%Y-%m-%d) \
  --granularity MONTHLY

Savings Plans Utilization

aws ce get-savings-plans-utilization \
  --time-period Start=$(date -d '30 days ago' +%Y-%m-%d),End=$(date +%Y-%m-%d)

S3 Cost Optimization

Bucket Sizes

# List bucket sizes
for bucket in $(aws s3api list-buckets --query 'Buckets[].Name' --output text); do
  size=$(aws cloudwatch get-metric-statistics \
    --namespace AWS/S3 \
    --metric-name BucketSizeBytes \
    --dimensions Name=BucketName,Value=$bucket Name=StorageType,Value=StandardStorage \
    --start-time $(date -d '1 day ago' -u +%Y-%m-%dT%H:%M:%SZ) \
    --end-time $(date -u +%Y-%m-%dT%H:%M:%SZ) \
    --period 86400 \
    --statistics Average \
    --query 'Datapoints[0].Average' --output text 2>/dev/null)
  echo "$bucket: ${size:-0} bytes"
done

S3 Storage Lens

# Get storage lens config
aws s3control list-storage-lens-configurations --account-id $(aws sts get-caller-identity --query Account --output text)

Quick Wins Checklist

Immediate Savings

  • Delete stopped EC2 instances (still pay for EBS)
  • Remove unattached EBS volumes
  • Release unused Elastic IPs ($3.60/month each)
  • Delete old snapshots
  • Review and delete unused load balancers
  • Move infrequent S3 data to cheaper storage class

Medium-Term Savings

  • Right-size over-provisioned EC2 instances
  • Use Spot instances for fault-tolerant workloads
  • Implement Reserved Instances for stable workloads
  • Enable S3 Intelligent-Tiering
  • Use Aurora Serverless for variable workloads

Kubernetes Savings

  • Set appropriate resource requests/limits
  • Use cluster autoscaler
  • Implement pod disruption budgets
  • Use spot instances for node groups
  • Right-size persistent volumes

Cost Monitoring

Set Budget Alert

aws budgets create-budget \
  --account-id $(aws sts get-caller-identity --query Account --output text) \
  --budget '{
    "BudgetName": "Monthly-Spend",
    "BudgetLimit": {"Amount": "1000", "Unit": "USD"},
    "BudgetType": "COST",
    "TimeUnit": "MONTHLY"
  }' \
  --notifications-with-subscribers '[{
    "Notification": {
      "NotificationType": "ACTUAL",
      "ComparisonOperator": "GREATER_THAN",
      "Threshold": 80,
      "ThresholdType": "PERCENTAGE"
    },
    "Subscribers": [{"SubscriptionType": "EMAIL", "Address": "alerts@example.com"}]
  }]'

Quick Reference

# This month's total
aws ce get-cost-and-usage --time-period Start=$(date +%Y-%m-01),End=$(date +%Y-%m-%d) --granularity MONTHLY --metrics BlendedCost

# Top 5 services
aws ce get-cost-and-usage --time-period Start=$(date +%Y-%m-01),End=$(date +%Y-%m-%d) --granularity MONTHLY --metrics BlendedCost --group-by Type=DIMENSION,Key=SERVICE --query 'ResultsByTime[].Groups | sort_by(@, &to_number(Metrics.BlendedCost.Amount))[-5:]'

# Unused EBS
aws ec2 describe-volumes --filters "Name=status,Values=available" --query 'Volumes[].VolumeId'

# K8s resource usage
kubectl top nodes && kubectl top pods -A --sort-by=memory | head -20

Related Skills

  • aws-ops: For AWS resource management
  • k8s-debug: For Kubernetes resource analysis
  • terraform-workflow: For infrastructure changes

Source

git clone https://github.com/agenticdevops/devops-execution-engine/blob/main/skills/cost-optimization/SKILL.mdView on GitHub

Overview

Cost Optimization helps you analyze cloud spend and uncover savings opportunities across AWS and Kubernetes. It guides right sizing, detects spikes, and surfaces unused resources to reduce recurring costs.

How This Skill Works

The skill uses AWS Cost Explorer to extract monthly spend, service level costs, trends, and tag based breakdowns. It also surfaces unused resources such as stopped EC2s, unattached volumes, old snapshots, idle load balancers, and low usage RDS connections, and pairs this with Kubernetes cost analysis for right sizing.

When to Use It

  • Analyzing cloud spending across AWS accounts
  • Finding cost savings opportunities by service or tag
  • Right-sizing compute and storage resources
  • Investigating sudden cost spikes or anomalies
  • Identifying unused or idle resources to reclaim spend

Quick Start

  1. Step 1: Ensure AWS CLI is configured with access to cost explorer and kubectl is available if Kubernetes analysis is needed
  2. Step 2: Run cost analysis commands to fetch current month costs, cost by service, and cost by tag
  3. Step 3: Review findings and take actions such as right sizing, removing unused resources, or applying tags for better cost governance

Best Practices

  • Tag resources by environment, team, and cost center for granular reporting
  • Run regular cost reports monthly and monitor daily trends
  • Prioritize right sizing and consider reserved instances or savings plans
  • Audit and remove unused resources like stopped instances and unattached volumes
  • Set up spend alerts to detect anomalous increases early

Example Use Cases

  • Current Month Costs by time period using cost explorer
  • Cost by Service breakdown to see which services dominate spend
  • Cost Trend daily view to spot spikes over the last week
  • Cost by Tag to compare Environment or Team costs
  • Identify unused resources such as stopped EC2s and unattached EBS volumes

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers