$npx -y skills add wshobson/agents --skill cost-optimizationOptimize cloud costs across AWS, Azure, GCP, and OCI through resource rightsizing, tagging strategies, reserved instances, and spending analysis. Use when reducing cloud expenses, analyzing infrastructure costs, or implementing cost governance policies.
| 1 | # Cloud Cost Optimization |
| 2 | |
| 3 | Strategies and patterns for optimizing cloud costs across AWS, Azure, GCP, and OCI. |
| 4 | |
| 5 | ## Purpose |
| 6 | |
| 7 | Implement systematic cost optimization strategies to reduce cloud spending while maintaining performance and reliability. |
| 8 | |
| 9 | ## When to Use |
| 10 | |
| 11 | - Reduce cloud spending |
| 12 | - Right-size resources |
| 13 | - Implement cost governance |
| 14 | - Optimize multi-cloud costs |
| 15 | - Meet budget constraints |
| 16 | |
| 17 | ## Cost Optimization Framework |
| 18 | |
| 19 | ### 1. Visibility |
| 20 | |
| 21 | - Implement cost allocation tags |
| 22 | - Use cloud cost management tools |
| 23 | - Set up budget alerts |
| 24 | - Create cost dashboards |
| 25 | |
| 26 | ### 2. Right-Sizing |
| 27 | |
| 28 | - Analyze resource utilization |
| 29 | - Downsize over-provisioned resources |
| 30 | - Use auto-scaling |
| 31 | - Remove idle resources |
| 32 | |
| 33 | ### 3. Pricing Models |
| 34 | |
| 35 | - Use reserved capacity |
| 36 | - Leverage spot/preemptible instances |
| 37 | - Implement savings plans |
| 38 | - Use committed use discounts |
| 39 | |
| 40 | ### 4. Architecture Optimization |
| 41 | |
| 42 | - Use managed services |
| 43 | - Implement caching |
| 44 | - Optimize data transfer |
| 45 | - Use lifecycle policies |
| 46 | |
| 47 | ## AWS Cost Optimization |
| 48 | |
| 49 | ### Reserved Instances |
| 50 | |
| 51 | ``` |
| 52 | Savings: 30-72% vs On-Demand |
| 53 | Term: 1 or 3 years |
| 54 | Payment: All/Partial/No upfront |
| 55 | Flexibility: Standard or Convertible |
| 56 | ``` |
| 57 | |
| 58 | ### Savings Plans |
| 59 | |
| 60 | ``` |
| 61 | Compute Savings Plans: 66% savings |
| 62 | EC2 Instance Savings Plans: 72% savings |
| 63 | Applies to: EC2, Fargate, Lambda |
| 64 | Flexible across: Instance families, regions, OS |
| 65 | ``` |
| 66 | |
| 67 | ### Spot Instances |
| 68 | |
| 69 | ``` |
| 70 | Savings: Up to 90% vs On-Demand |
| 71 | Best for: Batch jobs, CI/CD, stateless workloads |
| 72 | Risk: 2-minute interruption notice |
| 73 | Strategy: Mix with On-Demand for resilience |
| 74 | ``` |
| 75 | |
| 76 | ### S3 Cost Optimization |
| 77 | |
| 78 | ```hcl |
| 79 | resource "aws_s3_bucket_lifecycle_configuration" "example" { |
| 80 | bucket = aws_s3_bucket.example.id |
| 81 | |
| 82 | rule { |
| 83 | id = "transition-to-ia" |
| 84 | status = "Enabled" |
| 85 | |
| 86 | transition { |
| 87 | days = 30 |
| 88 | storage_class = "STANDARD_IA" |
| 89 | } |
| 90 | |
| 91 | transition { |
| 92 | days = 90 |
| 93 | storage_class = "GLACIER" |
| 94 | } |
| 95 | |
| 96 | expiration { |
| 97 | days = 365 |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | ``` |
| 102 | |
| 103 | ## Azure Cost Optimization |
| 104 | |
| 105 | ### Reserved VM Instances |
| 106 | |
| 107 | - 1 or 3 year terms |
| 108 | - Up to 72% savings |
| 109 | - Flexible sizing |
| 110 | - Exchangeable |
| 111 | |
| 112 | ### Azure Hybrid Benefit |
| 113 | |
| 114 | - Use existing Windows Server licenses |
| 115 | - Up to 80% savings with RI |
| 116 | - Available for Windows and SQL Server |
| 117 | |
| 118 | ### Azure Advisor Recommendations |
| 119 | |
| 120 | - Right-size VMs |
| 121 | - Delete unused resources |
| 122 | - Use reserved capacity |
| 123 | - Optimize storage |
| 124 | |
| 125 | ## GCP Cost Optimization |
| 126 | |
| 127 | ### Committed Use Discounts |
| 128 | |
| 129 | - 1 or 3 year commitment |
| 130 | - Up to 57% savings |
| 131 | - Applies to vCPUs and memory |
| 132 | - Resource-based or spend-based |
| 133 | |
| 134 | ### Sustained Use Discounts |
| 135 | |
| 136 | - Automatic discounts |
| 137 | - Up to 30% for running instances |
| 138 | - No commitment required |
| 139 | - Applies to Compute Engine, GKE |
| 140 | |
| 141 | ### Preemptible VMs |
| 142 | |
| 143 | - Up to 80% savings |
| 144 | - 24-hour maximum runtime |
| 145 | - Best for batch workloads |
| 146 | |
| 147 | ## OCI Cost Optimization |
| 148 | |
| 149 | ### Flexible Shapes |
| 150 | |
| 151 | - Scale OCPUs and memory independently |
| 152 | - Match instance sizing to workload demand |
| 153 | - Reduce wasted capacity from fixed VM shapes |
| 154 | |
| 155 | ### Commitments and Budgets |
| 156 | |
| 157 | - Use annual commitments for predictable spend |
| 158 | - Set compartment-level budgets with alerts |
| 159 | - Track monthly forecasts with OCI Cost Analysis |
| 160 | |
| 161 | ### Preemptible Capacity |
| 162 | |
| 163 | - Use preemptible instances for batch and ephemeral workloads |
| 164 | - Keep interruption-tolerant autoscaling groups |
| 165 | - Mix with standard capacity for critical services |
| 166 | |
| 167 | ## Tagging Strategy |
| 168 | |
| 169 | ### AWS Tagging |
| 170 | |
| 171 | ```hcl |
| 172 | locals { |
| 173 | common_tags = { |
| 174 | Environment = "production" |
| 175 | Project = "my-project" |
| 176 | CostCenter = "engineering" |
| 177 | Owner = "team@example.com" |
| 178 | ManagedBy = "terraform" |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | resource "aws_instance" "example" { |
| 183 | ami = "ami-12345678" |
| 184 | instance_type = "t3.medium" |
| 185 | |
| 186 | tags = merge( |
| 187 | local.common_tags, |
| 188 | { |
| 189 | Name = "web-server" |
| 190 | } |
| 191 | ) |
| 192 | } |
| 193 | ``` |
| 194 | |
| 195 | **Reference:** See `references/tagging-standards.md` |
| 196 | |
| 197 | ## Cost Monitoring |
| 198 | |
| 199 | ### Budget Alerts |
| 200 | |
| 201 | ```hcl |
| 202 | # AWS Budget |
| 203 | resource "aws_budgets_budget" "monthly" { |
| 204 | name = "monthly-budget" |
| 205 | budget_type = "COST" |
| 206 | limit_amount = "1000" |
| 207 | limit_unit = "USD" |
| 208 | time_period_start = "2024-01-01_00:00" |
| 209 | time_unit = "MONTHLY" |
| 210 | |
| 211 | notification { |
| 212 | comparison_operator = "GREATER_THAN" |
| 213 | threshold = 80 |
| 214 | threshold_type = "PERCENTAGE" |
| 215 | notification_type = "ACTUAL" |
| 216 | subscriber_email_addresses = ["team@example.com"] |
| 217 | } |
| 218 | } |
| 219 | ``` |
| 220 | |
| 221 | ### Cost Anomaly Detection |
| 222 | |
| 223 | - AWS Cost Anomaly Detection |
| 224 | - Azure Cost Management alerts |
| 225 | - GCP Budget alerts |
| 226 | - OCI Budgets and Cost Analysis |
| 227 | |
| 228 | ## Architecture Patterns |
| 229 | |
| 230 | ### Pattern 1: Serverless First |
| 231 | |
| 232 | - Use Lambda/Functions for event-driven |
| 233 | - Pay only for execution time |
| 234 | - Auto-scaling included |
| 235 | - No idle costs |
| 236 | |
| 237 | ### Pattern 2: Right-Sized Databases |
| 238 | |
| 239 | ``` |
| 240 | Development: t3.small RDS |
| 241 | Staging: t3.large RDS |
| 242 | Production: r6g.2x |