mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
Every page now loads /webmcp.js, scripts/webmcp.ts with its types stripped (npm run webmcp). Where the browser has document.modelContext it registers two read-only tools: search_skills over the published skills index, and get_skill, which returns a skill's markdown checked against the index digest. The markdown alternate link moves from build.py to site.py, so CI holds every page to it. The API catalog gains the site itself (llms.txt and the skills index) and hanzo.ai/agents; the live discovery check now fetches every catalog link. tests/site.mjs drives the tools through a recording modelContext, and checks a browser without one loads clean.
853 lines
36 KiB
HTML
853 lines
36 KiB
HTML
<!doctype html>
|
|
<html lang="en"><head>
|
|
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>infrastructure-cost-optimization</title>
|
|
<meta name="color-scheme" content="dark">
|
|
<style>
|
|
/* Committed to one visual world: a dark instrument panel. No light variant —
|
|
the dot field and the hairline rules only read on black, and a naive
|
|
inversion would wash both out. Every colour is painted explicitly so the page
|
|
never borrows a host background. */
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
|
|
|
|
:root{
|
|
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
|
|
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
|
|
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
|
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
|
|
--font-zen-pixel-square:'Zen Pixel Square',monospace;
|
|
--mono:var(--font-zen-mono);
|
|
--sans:var(--font-zen-sans);
|
|
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
|
|
--r:999px; --r-soft:14px;
|
|
}
|
|
*{box-sizing:border-box}
|
|
html{-webkit-text-size-adjust:100%}
|
|
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
|
|
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
|
|
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
|
|
|
|
/* The dot field. A halftone made of one repeating radial-gradient, faded out
|
|
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
|
|
/* No overflow clip here: the dot field is inset to the box already, and a clip
|
|
would cut off the copy-prompt menu. */
|
|
.hero{position:relative;border-bottom:1px solid var(--line)}
|
|
.hero::before{content:"";position:absolute;inset:0;
|
|
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
|
|
background-size:7px 7px;
|
|
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
pointer-events:none}
|
|
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
|
|
|
|
nav{border-bottom:1px solid var(--line)}
|
|
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
|
|
justify-content:space-between;align-items:center;gap:1rem}
|
|
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--ink);text-decoration:none;
|
|
display:inline-flex;align-items:center;gap:.6rem}
|
|
.brand i{font-style:normal;color:var(--faint)}
|
|
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
|
|
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--soft);text-decoration:none}
|
|
nav a.up:hover{color:var(--ink)}
|
|
|
|
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
|
|
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
|
|
h1 em{font-style:normal;color:var(--faint)}
|
|
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
|
|
|
|
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
|
|
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
|
|
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
|
|
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
|
|
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
|
|
line-height:1.2;font-weight:400}
|
|
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
|
|
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
|
|
.btn.solid:hover{background:#d8d8d4}
|
|
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
|
|
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
|
|
|
|
/* Copy-prompt selector. Each row copies the prompt for its own target. */
|
|
.copy{position:relative;display:inline-flex}
|
|
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
|
|
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
|
|
padding:.4rem;display:flex;flex-direction:column;
|
|
box-shadow:0 12px 32px rgba(0,0,0,.75)}
|
|
.menu[hidden]{display:none}
|
|
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
|
|
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
|
|
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
|
|
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
|
|
white-space:nowrap}
|
|
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
|
|
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
|
|
.mi:hover .mark{color:var(--ink)}
|
|
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
|
|
letter-spacing:.1em;text-transform:uppercase}
|
|
|
|
/* Works-with bar: one scrolling row, name and mark per agent. */
|
|
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
|
|
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
|
|
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
|
|
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
|
|
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
|
|
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
|
|
display:flex;align-items:center}
|
|
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
|
|
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
|
|
-webkit-overflow-scrolling:touch;
|
|
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
|
|
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
|
|
.bar::-webkit-scrollbar{display:none}
|
|
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
|
|
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
|
|
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
|
|
text-decoration:none;background:var(--ground)}
|
|
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
|
|
.agent .mark{width:.95rem;height:.95rem}
|
|
|
|
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
|
|
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
|
|
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
|
|
p{margin:0 0 1rem;max-width:70ch}
|
|
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
|
|
li{margin:.22rem 0}
|
|
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
|
|
text-decoration-color:var(--line-hi)}
|
|
a:hover{text-decoration-color:var(--ink)}
|
|
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
|
|
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
|
|
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
|
|
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
|
|
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
|
|
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
|
|
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
|
|
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
|
|
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
|
|
display:block;overflow-x:auto}
|
|
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
|
|
|
|
.curl{display:block;background:var(--panel);border:1px solid var(--line);
|
|
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
|
|
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
|
|
white-space:pre;line-height:1.9}
|
|
.curl b{color:var(--ink);font-weight:400}
|
|
.curl i{font-style:normal;color:var(--faint)}
|
|
|
|
/* index */
|
|
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
|
|
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
|
|
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
|
|
#q:focus{outline:none;border-color:var(--soft)}
|
|
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
|
|
text-transform:uppercase;margin:0 0 2rem}
|
|
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
|
|
padding-bottom:.45rem}
|
|
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
|
|
border-radius:var(--r-soft);text-decoration:none}
|
|
.row:hover{background:#0a0a0a}
|
|
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
|
|
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
|
|
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
|
|
color:var(--faint);font-size:.85rem}
|
|
footer a{color:var(--soft)}
|
|
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
|
|
margin:0 0 2rem;font-size:.85rem}
|
|
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
|
|
border-bottom:1px solid var(--line);padding-bottom:.4rem}
|
|
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
|
|
.fmap a:hover{text-decoration:underline}
|
|
.fmap div{color:var(--faint);line-height:1.5}
|
|
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
|
|
|
|
@media (max-width:640px){
|
|
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
|
|
.btns{gap:.5rem}
|
|
.btn{padding:.75rem 1.15rem;font-size:.68rem}
|
|
.copy,.copy .btn{width:100%;justify-content:center}
|
|
.menu{left:0;right:0;min-width:0}
|
|
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
|
|
.strip b{border-right:0;padding-right:0}
|
|
.bar{width:100%}
|
|
}
|
|
</style>
|
|
<link rel="icon" href="/favicon.svg">
|
|
<link rel="canonical" href="https://hanzoskills.com/infrastructure-cost-optimization">
|
|
<link rel="alternate" type="text/markdown" href="/infrastructure-cost-optimization.md">
|
|
<script type="module" src="/webmcp.js"></script>
|
|
</head>
|
|
<!-- Reading this with a machine? Every page here is also plain markdown.
|
|
This one: https://hanzoskills.com/infrastructure-cost-optimization.md
|
|
Index: https://hanzoskills.com/llms.txt
|
|
Corpus: https://hanzoskills.com/all.md
|
|
Append .md to any path for the markdown twin. -->
|
|
<body>
|
|
<nav><div class="in">
|
|
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
|
|
<a class="up" href="/">All skills</a>
|
|
</div></nav>
|
|
|
|
<div class="wrap">
|
|
<h1>infrastructure-cost-optimization</h1><p class="lede">Cloud costs are growing unexpectedly</p><h1>Cost Optimization</h1>
|
|
<p><strong>Scope</strong>: Cloud cost optimization - Resource right-sizing, reserved instances, spot instances, monitoring <strong>Lines</strong>: 394 <strong>Last Updated</strong>: 2025-10-18 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<hr>
|
|
<h2>When to Use This Skill</h2>
|
|
<p><strong>Activate when</strong>:</p>
|
|
<ul><li>Cloud costs are growing unexpectedly</li><li>Planning infrastructure budgets</li><li>Optimizing existing workloads</li><li>Implementing cost-aware architectures</li><li>Setting up cost monitoring and alerts</li><li>Choosing between deployment options</li></ul>
|
|
<p><strong>Prerequisites</strong>:</p>
|
|
<ul><li>Access to cloud provider billing console</li><li>Understanding of current infrastructure</li><li>CloudWatch/monitoring access (AWS)</li><li>Billing reports enabled</li><li>Cost allocation tags implemented</li></ul>
|
|
<p><strong>Common scenarios</strong>:</p>
|
|
<ul><li>Monthly cost exceeded budget</li><li>Idle resources consuming budget</li><li>Over-provisioned instances</li><li>Inefficient storage patterns</li><li>Unoptimized database usage</li><li>Poor auto-scaling configuration</li></ul>
|
|
<hr>
|
|
<h2>Core Concepts</h2>
|
|
<h3>1. Resource Right-Sizing</h3>
|
|
<pre><code class="lang-python"># AWS Lambda optimization
|
|
# Problem: Over-provisioned memory
|
|
|
|
# ❌ Expensive: 3008 MB memory
|
|
def lambda_handler(event, context):
|
|
# Function uses ~512 MB
|
|
process_data(event)
|
|
return {'statusCode': 200}
|
|
|
|
# Memory: 3008 MB
|
|
# Cost: ~$0.0000166667 per 100ms
|
|
# Actual usage: 512 MB
|
|
|
|
# ✅ Optimized: Right-sized memory
|
|
def lambda_handler(event, context):
|
|
process_data(event)
|
|
return {'statusCode': 200}
|
|
|
|
# Memory: 512 MB
|
|
# Cost: ~$0.0000008333 per 100ms
|
|
# Savings: ~95% reduction
|
|
|
|
# Testing different memory configurations
|
|
import json
|
|
|
|
def test_memory_configs():
|
|
"""Test Lambda with different memory configs"""
|
|
configs = [128, 256, 512, 1024, 2048, 3008]
|
|
results = {}
|
|
|
|
for memory_mb in configs:
|
|
# Update Lambda configuration
|
|
lambda_client.update_function_configuration(
|
|
FunctionName='my-function',
|
|
MemorySize=memory_mb
|
|
)
|
|
|
|
# Run performance test
|
|
execution_time, cost = run_test()
|
|
results[memory_mb] = {
|
|
'execution_time': execution_time,
|
|
'cost': cost,
|
|
'cost_per_invocation': cost / 1000
|
|
}
|
|
|
|
return results</code></pre>
|
|
<h3>2. EC2 Right-Sizing</h3>
|
|
<pre><code class="lang-bash"># Analyze CloudWatch metrics for right-sizing
|
|
aws cloudwatch get-metric-statistics \
|
|
--namespace AWS/EC2 \
|
|
--metric-name CPUUtilization \
|
|
--dimensions Name=InstanceId,Value=i-1234567890abcdef0 \
|
|
--start-time 2024-01-01T00:00:00Z \
|
|
--end-time 2024-01-31T23:59:59Z \
|
|
--period 3600 \
|
|
--statistics Average,Maximum
|
|
|
|
# If average CPU < 30%, consider smaller instance type
|
|
# If max memory < 50%, consider less memory
|
|
|
|
# Current: t3.xlarge (4 vCPU, 16 GB RAM) - $0.1664/hour
|
|
# Average CPU: 20%, Memory: 6 GB
|
|
# Recommendation: t3.large (2 vCPU, 8 GB RAM) - $0.0832/hour
|
|
# Savings: ~50% reduction</code></pre>
|
|
<h3>3. Reserved Instances and Savings Plans</h3>
|
|
<pre><code class="lang-python"># Calculate savings with Reserved Instances
|
|
def calculate_ri_savings(instance_type, hours_per_month=730):
|
|
"""
|
|
Calculate potential savings with Reserved Instances
|
|
|
|
Pricing examples (us-east-1):
|
|
t3.medium: $0.0416/hour on-demand
|
|
t3.medium: $0.0250/hour 1-year RI (40% savings)
|
|
t3.medium: $0.0166/hour 3-year RI (60% savings)
|
|
"""
|
|
|
|
on_demand_cost = {
|
|
't3.micro': 0.0104,
|
|
't3.small': 0.0208,
|
|
't3.medium': 0.0416,
|
|
't3.large': 0.0832,
|
|
't3.xlarge': 0.1664,
|
|
}
|
|
|
|
# 1-year Reserved Instance (40% discount)
|
|
ri_1yr_cost = on_demand_cost[instance_type] * 0.6
|
|
|
|
# 3-year Reserved Instance (60% discount)
|
|
ri_3yr_cost = on_demand_cost[instance_type] * 0.4
|
|
|
|
monthly_on_demand = on_demand_cost[instance_type] * hours_per_month
|
|
monthly_ri_1yr = ri_1yr_cost * hours_per_month
|
|
monthly_ri_3yr = ri_3yr_cost * hours_per_month
|
|
|
|
return {
|
|
'on_demand': monthly_on_demand,
|
|
'ri_1yr': monthly_ri_1yr,
|
|
'ri_3yr': monthly_ri_3yr,
|
|
'savings_1yr': monthly_on_demand - monthly_ri_1yr,
|
|
'savings_3yr': monthly_on_demand - monthly_ri_3yr,
|
|
'savings_1yr_percent': ((monthly_on_demand - monthly_ri_1yr) / monthly_on_demand) * 100,
|
|
'savings_3yr_percent': ((monthly_on_demand - monthly_ri_3yr) / monthly_on_demand) * 100,
|
|
}
|
|
|
|
# Example: 5 x t3.large instances running 24/7
|
|
savings = calculate_ri_savings('t3.large', 730)
|
|
print(f"Monthly cost - On-Demand: ${savings['on_demand']:.2f}")
|
|
print(f"Monthly cost - 1-year RI: ${savings['ri_1yr']:.2f}")
|
|
print(f"Monthly savings with RI: ${savings['savings_1yr']:.2f} ({savings['savings_1yr_percent']:.1f}%)")</code></pre>
|
|
<h3>4. Spot Instances</h3>
|
|
<pre><code class="lang-python"># Use Spot Instances for fault-tolerant workloads
|
|
import boto3
|
|
|
|
ec2 = boto3.client('ec2')
|
|
|
|
# Request Spot Instances
|
|
response = ec2.request_spot_instances(
|
|
InstanceCount=5,
|
|
LaunchSpecification={
|
|
'ImageId': 'ami-0c55b159cbfafe1f0',
|
|
'InstanceType': 't3.medium',
|
|
'KeyName': 'my-key',
|
|
'SecurityGroupIds': ['sg-12345678'],
|
|
'IamInstanceProfile': {
|
|
'Arn': 'arn:aws:iam::123456789012:instance-profile/worker-role'
|
|
},
|
|
'UserData': base64.b64encode(user_data.encode()).decode()
|
|
},
|
|
SpotPrice='0.0200', # Max price (70% of on-demand)
|
|
Type='one-time'
|
|
)
|
|
|
|
# Spot Instances savings:
|
|
# t3.medium on-demand: $0.0416/hour
|
|
# t3.medium spot (average): ~$0.0125/hour
|
|
# Savings: ~70%
|
|
|
|
# Use Cases:
|
|
# - Batch processing
|
|
# - CI/CD workers
|
|
# - Data analysis
|
|
# - Machine learning training
|
|
# - Stateless web workers
|
|
|
|
# NOT suitable for:
|
|
# - Databases
|
|
# - Long-running stateful services
|
|
# - Time-critical applications</code></pre>
|
|
<h3>5. Auto-Scaling Optimization</h3>
|
|
<pre><code class="lang-python"># Auto Scaling policy for cost optimization
|
|
import boto3
|
|
|
|
autoscaling = boto3.client('autoscaling')
|
|
|
|
# Target tracking scaling policy
|
|
response = autoscaling.put_scaling_policy(
|
|
AutoScalingGroupName='web-asg',
|
|
PolicyName='target-tracking-cpu',
|
|
PolicyType='TargetTrackingScaling',
|
|
TargetTrackingConfiguration={
|
|
'PredefinedMetricSpecification': {
|
|
'PredefinedMetricType': 'ASGAverageCPUUtilization'
|
|
},
|
|
'TargetValue': 70.0, # Target 70% CPU utilization
|
|
'ScaleInCooldown': 300, # Wait 5 min before scaling down
|
|
'ScaleOutCooldown': 60 # Wait 1 min before scaling up
|
|
}
|
|
)
|
|
|
|
# Scheduled scaling for predictable patterns
|
|
autoscaling.put_scheduled_action(
|
|
AutoScalingGroupName='web-asg',
|
|
ScheduledActionName='scale-down-evening',
|
|
Recurrence='0 18 * * *', # 6 PM UTC
|
|
MinSize=2,
|
|
MaxSize=5,
|
|
DesiredCapacity=2
|
|
)
|
|
|
|
autoscaling.put_scheduled_action(
|
|
AutoScalingGroupName='web-asg',
|
|
ScheduledActionName='scale-up-morning',
|
|
Recurrence='0 6 * * *', # 6 AM UTC
|
|
MinSize=5,
|
|
MaxSize=20,
|
|
DesiredCapacity=10
|
|
)</code></pre>
|
|
<h3>6. Storage Optimization</h3>
|
|
<pre><code class="lang-python"># S3 Lifecycle policies for cost optimization
|
|
import boto3
|
|
|
|
s3 = boto3.client('s3')
|
|
|
|
lifecycle_config = {
|
|
'Rules': [
|
|
{
|
|
'Id': 'archive-old-logs',
|
|
'Status': 'Enabled',
|
|
'Prefix': 'logs/',
|
|
'Transitions': [
|
|
{
|
|
'Days': 30,
|
|
'StorageClass': 'STANDARD_IA' # Infrequent Access
|
|
},
|
|
{
|
|
'Days': 90,
|
|
'StorageClass': 'GLACIER_IR' # Glacier Instant Retrieval
|
|
},
|
|
{
|
|
'Days': 365,
|
|
'StorageClass': 'DEEP_ARCHIVE' # Deep Archive
|
|
}
|
|
],
|
|
'Expiration': {
|
|
'Days': 2555 # Delete after 7 years
|
|
}
|
|
},
|
|
{
|
|
'Id': 'delete-temp-files',
|
|
'Status': 'Enabled',
|
|
'Prefix': 'temp/',
|
|
'Expiration': {
|
|
'Days': 7
|
|
}
|
|
},
|
|
{
|
|
'Id': 'abort-incomplete-multipart',
|
|
'Status': 'Enabled',
|
|
'Prefix': '',
|
|
'AbortIncompleteMultipartUpload': {
|
|
'DaysAfterInitiation': 7
|
|
}
|
|
}
|
|
]
|
|
}
|
|
|
|
s3.put_bucket_lifecycle_configuration(
|
|
Bucket='my-bucket',
|
|
LifecycleConfiguration=lifecycle_config
|
|
)
|
|
|
|
# S3 Storage Costs (per GB/month):
|
|
# Standard: $0.023
|
|
# Standard-IA: $0.0125 (45% savings)
|
|
# Glacier Instant Retrieval: $0.004 (83% savings)
|
|
# Glacier Flexible Retrieval: $0.0036 (84% savings)
|
|
# Glacier Deep Archive: $0.00099 (96% savings)</code></pre>
|
|
<h3>7. Database Cost Optimization</h3>
|
|
<pre><code class="lang-python"># RDS optimization strategies
|
|
|
|
# 1. Right-size instance
|
|
# Current: db.r5.2xlarge (8 vCPU, 64 GB) - $1.344/hour
|
|
# If CPU < 30% and memory < 40GB
|
|
# Recommended: db.r5.xlarge (4 vCPU, 32 GB) - $0.672/hour
|
|
# Savings: 50%
|
|
|
|
# 2. Use Aurora Serverless v2 for variable workloads
|
|
import boto3
|
|
|
|
rds = boto3.client('rds')
|
|
|
|
# Aurora Serverless v2
|
|
response = rds.create_db_cluster(
|
|
DBClusterIdentifier='my-cluster',
|
|
Engine='aurora-postgresql',
|
|
EngineVersion='14.6',
|
|
ServerlessV2ScalingConfiguration={
|
|
'MinCapacity': 0.5, # Min ACUs (Aurora Capacity Units)
|
|
'MaxCapacity': 16.0 # Max ACUs
|
|
}
|
|
)
|
|
|
|
# Savings example:
|
|
# Traditional RDS: db.r5.large 24/7 = $0.336/hour * 730 = $245/month
|
|
# Aurora Serverless v2: Average 2 ACU * $0.12/ACU/hour * 730 = $175/month
|
|
# Savings: ~29%
|
|
|
|
# 3. Schedule stop/start for dev/test databases
|
|
def schedule_database_stop_start():
|
|
"""Stop dev databases outside business hours"""
|
|
|
|
# Stop at 6 PM
|
|
lambda_client.create_rule(
|
|
Name='stop-dev-databases',
|
|
ScheduleExpression='cron(0 18 * * ? *)',
|
|
State='ENABLED',
|
|
Targets=[{
|
|
'Arn': 'arn:aws:lambda:us-east-1:123456789012:function:stop-databases',
|
|
'Id': 'stop-dev-dbs'
|
|
}]
|
|
)
|
|
|
|
# Start at 8 AM
|
|
lambda_client.create_rule(
|
|
Name='start-dev-databases',
|
|
ScheduleExpression='cron(0 8 * * ? *)',
|
|
State='ENABLED',
|
|
Targets=[{
|
|
'Arn': 'arn:aws:lambda:us-east-1:123456789012:function:start-databases',
|
|
'Id': 'start-dev-dbs'
|
|
}]
|
|
)
|
|
|
|
# Savings: 14 hours/day * 5 days/week = ~40% reduction</code></pre>
|
|
<hr>
|
|
<h2>Patterns</h2>
|
|
<h3>Cost Monitoring with CloudWatch</h3>
|
|
<pre><code class="lang-python"># Set up billing alerts
|
|
import boto3
|
|
|
|
cloudwatch = boto3.client('cloudwatch', region_name='us-east-1') # Must be us-east-1
|
|
sns = boto3.client('sns')
|
|
|
|
# Create SNS topic for alerts
|
|
topic_response = sns.create_topic(Name='billing-alerts')
|
|
topic_arn = topic_response['TopicArn']
|
|
|
|
# Subscribe email
|
|
sns.subscribe(
|
|
TopicArn=topic_arn,
|
|
Protocol='email',
|
|
Endpoint='[email protected]'
|
|
)
|
|
|
|
# Create billing alarm
|
|
cloudwatch.put_metric_alarm(
|
|
AlarmName='MonthlyBillingExceeds1000',
|
|
AlarmDescription='Alert when monthly bill exceeds $1000',
|
|
ActionsEnabled=True,
|
|
AlarmActions=[topic_arn],
|
|
MetricName='EstimatedCharges',
|
|
Namespace='AWS/Billing',
|
|
Statistic='Maximum',
|
|
Dimensions=[
|
|
{
|
|
'Name': 'Currency',
|
|
'Value': 'USD'
|
|
}
|
|
],
|
|
Period=21600, # 6 hours
|
|
EvaluationPeriods=1,
|
|
Threshold=1000.0,
|
|
ComparisonOperator='GreaterThanThreshold'
|
|
)</code></pre>
|
|
<h3>Cost Allocation Tags</h3>
|
|
<pre><code class="lang-python"># Implement comprehensive tagging strategy
|
|
import boto3
|
|
|
|
def tag_resources():
|
|
"""Apply cost allocation tags to all resources"""
|
|
|
|
ec2 = boto3.client('ec2')
|
|
|
|
# Tag EC2 instances
|
|
ec2.create_tags(
|
|
Resources=['i-1234567890abcdef0'],
|
|
Tags=[
|
|
{'Key': 'Environment', 'Value': 'production'},
|
|
{'Key': 'Project', 'Value': 'web-app'},
|
|
{'Key': 'Team', 'Value': 'platform'},
|
|
{'Key': 'CostCenter', 'Value': '12345'},
|
|
{'Key': 'Owner', 'Value': '[email protected]'}
|
|
]
|
|
)
|
|
|
|
# Enable cost allocation tags in AWS Billing Console
|
|
# This allows filtering and grouping costs by tag
|
|
|
|
# Cost allocation reports by tag
|
|
def generate_cost_report_by_tag(tag_key='Project'):
|
|
"""Generate cost report grouped by tag"""
|
|
|
|
ce = boto3.client('ce') # Cost Explorer
|
|
|
|
response = ce.get_cost_and_usage(
|
|
TimePeriod={
|
|
'Start': '2024-01-01',
|
|
'End': '2024-01-31'
|
|
},
|
|
Granularity='MONTHLY',
|
|
Metrics=['UnblendedCost'],
|
|
GroupBy=[
|
|
{
|
|
'Type': 'TAG',
|
|
'Key': tag_key
|
|
}
|
|
]
|
|
)
|
|
|
|
return response['ResultsByTime']</code></pre>
|
|
<h3>Lambda Cost Optimization</h3>
|
|
<pre><code class="lang-python"># Optimize Lambda for cost
|
|
def optimize_lambda_cost():
|
|
"""Best practices for Lambda cost optimization"""
|
|
|
|
# 1. Right-size memory (affects CPU allocation)
|
|
# Test different configurations to find optimal
|
|
|
|
# 2. Reduce package size
|
|
# - Use Lambda Layers for shared dependencies
|
|
# - Remove unused dependencies
|
|
# - Use tree-shaking for Node.js
|
|
|
|
# 3. Minimize cold starts
|
|
# - Keep functions warm with scheduled invocations (if cost-effective)
|
|
# - Use provisioned concurrency for critical functions
|
|
|
|
# 4. Connection reuse
|
|
# Initialize clients outside handler
|
|
dynamodb = boto3.resource('dynamodb')
|
|
table = dynamodb.Table('users')
|
|
|
|
def lambda_handler(event, context):
|
|
# Connection is reused across warm invocations
|
|
table.put_item(Item={'id': '123', 'name': 'Alice'})
|
|
|
|
# 5. Batch operations
|
|
# Process multiple records per invocation
|
|
# Reduces number of invocations
|
|
|
|
# 6. Use appropriate timeout
|
|
# Don't set timeout higher than needed
|
|
# Default: 3 seconds (adjust based on actual needs)
|
|
|
|
# Lambda pricing:
|
|
# $0.20 per 1M requests
|
|
# $0.0000166667 per GB-second
|
|
|
|
# Example savings:
|
|
# 10M requests/month at 128 MB, 200ms avg
|
|
# Current cost: 10M * $0.20/1M + 10M * 0.2s * 0.125GB * $0.0000166667/GB-s = $2 + $4.17 = $6.17
|
|
# With optimization (100ms): $2 + $2.08 = $4.08
|
|
# Savings: ~34%</code></pre>
|
|
<h3>Multi-Region Cost Optimization</h3>
|
|
<pre><code class="lang-python"># Choose cost-effective regions
|
|
REGIONAL_PRICING = {
|
|
'us-east-1': { # N. Virginia (cheapest)
|
|
'ec2_t3_medium': 0.0416,
|
|
'lambda_gb_second': 0.0000166667,
|
|
's3_standard': 0.023
|
|
},
|
|
'us-west-2': { # Oregon
|
|
'ec2_t3_medium': 0.0416,
|
|
'lambda_gb_second': 0.0000166667,
|
|
's3_standard': 0.023
|
|
},
|
|
'eu-west-1': { # Ireland
|
|
'ec2_t3_medium': 0.0456, # ~10% more expensive
|
|
'lambda_gb_second': 0.0000166667,
|
|
's3_standard': 0.023
|
|
},
|
|
'ap-southeast-1': { # Singapore
|
|
'ec2_t3_medium': 0.0528, # ~27% more expensive
|
|
'lambda_gb_second': 0.0000185185,
|
|
's3_standard': 0.025
|
|
}
|
|
}
|
|
|
|
def calculate_regional_savings(instances_count, region_from, region_to):
|
|
"""Calculate savings by switching regions"""
|
|
|
|
current_cost = REGIONAL_PRICING[region_from]['ec2_t3_medium'] * 730 * instances_count
|
|
new_cost = REGIONAL_PRICING[region_to]['ec2_t3_medium'] * 730 * instances_count
|
|
|
|
savings = current_cost - new_cost
|
|
savings_percent = (savings / current_cost) * 100
|
|
|
|
return {
|
|
'current_monthly_cost': current_cost,
|
|
'new_monthly_cost': new_cost,
|
|
'monthly_savings': savings,
|
|
'savings_percent': savings_percent
|
|
}
|
|
|
|
# Example: Moving 10 t3.medium instances from Singapore to N. Virginia
|
|
savings = calculate_regional_savings(10, 'ap-southeast-1', 'us-east-1')
|
|
print(f"Monthly savings: ${savings['monthly_savings']:.2f} ({savings['savings_percent']:.1f}%)")</code></pre>
|
|
<h3>Idle Resource Detection</h3>
|
|
<pre><code class="lang-python"># Identify and terminate idle resources
|
|
import boto3
|
|
from datetime import datetime, timedelta
|
|
|
|
def find_idle_resources():
|
|
"""Identify idle resources consuming costs"""
|
|
|
|
ec2 = boto3.client('ec2')
|
|
cloudwatch = boto3.client('cloudwatch')
|
|
|
|
# Find EC2 instances with low CPU usage
|
|
instances = ec2.describe_instances(
|
|
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]
|
|
)
|
|
|
|
idle_instances = []
|
|
end_time = datetime.utcnow()
|
|
start_time = end_time - timedelta(days=7)
|
|
|
|
for reservation in instances['Reservations']:
|
|
for instance in reservation['Instances']:
|
|
instance_id = instance['InstanceId']
|
|
|
|
# Get average CPU over last 7 days
|
|
response = cloudwatch.get_metric_statistics(
|
|
Namespace='AWS/EC2',
|
|
MetricName='CPUUtilization',
|
|
Dimensions=[{'Name': 'InstanceId', 'Value': instance_id}],
|
|
StartTime=start_time,
|
|
EndTime=end_time,
|
|
Period=86400, # 1 day
|
|
Statistics=['Average']
|
|
)
|
|
|
|
if response['Datapoints']:
|
|
avg_cpu = sum(d['Average'] for d in response['Datapoints']) / len(response['Datapoints'])
|
|
|
|
if avg_cpu < 5: # Less than 5% CPU
|
|
idle_instances.append({
|
|
'instance_id': instance_id,
|
|
'avg_cpu': avg_cpu,
|
|
'instance_type': instance['InstanceType'],
|
|
'tags': {tag['Key']: tag['Value'] for tag in instance.get('Tags', [])}
|
|
})
|
|
|
|
return idle_instances
|
|
|
|
# Find unused EBS volumes
|
|
def find_unattached_volumes():
|
|
"""Find EBS volumes not attached to instances"""
|
|
|
|
ec2 = boto3.client('ec2')
|
|
|
|
volumes = ec2.describe_volumes(
|
|
Filters=[{'Name': 'status', 'Values': ['available']}]
|
|
)
|
|
|
|
unattached = []
|
|
for volume in volumes['Volumes']:
|
|
unattached.append({
|
|
'volume_id': volume['VolumeId'],
|
|
'size': volume['Size'],
|
|
'type': volume['VolumeType'],
|
|
'monthly_cost': volume['Size'] * 0.10 # ~$0.10/GB/month for gp3
|
|
})
|
|
|
|
return unattached</code></pre>
|
|
<hr>
|
|
<h2>Quick Reference</h2>
|
|
<h3>AWS Cost Optimization Checklist</h3>
|
|
<pre><code>[ ] Right-size EC2 instances based on CloudWatch metrics
|
|
[ ] Purchase Reserved Instances for steady-state workloads
|
|
[ ] Use Spot Instances for fault-tolerant workloads
|
|
[ ] Implement S3 lifecycle policies
|
|
[ ] Delete unattached EBS volumes
|
|
[ ] Delete old snapshots
|
|
[ ] Use S3 Intelligent-Tiering for unpredictable access patterns
|
|
[ ] Enable S3 Transfer Acceleration only when needed
|
|
[ ] Use CloudFront for content delivery
|
|
[ ] Optimize Lambda memory allocation
|
|
[ ] Use DynamoDB On-Demand for unpredictable workloads
|
|
[ ] Schedule stop/start for dev/test resources
|
|
[ ] Set up billing alerts
|
|
[ ] Implement cost allocation tags
|
|
[ ] Review and delete unused resources monthly
|
|
[ ] Use AWS Cost Explorer for analysis
|
|
[ ] Enable AWS Compute Optimizer recommendations</code></pre>
|
|
<h3>Quick Cost Comparison</h3>
|
|
<pre><code>Compute:
|
|
- Lambda: $0.20/1M requests + $0.0000166667/GB-second
|
|
- Fargate: $0.04048/vCPU/hour + $0.004445/GB/hour
|
|
- EC2 t3.medium: $0.0416/hour (on-demand), $0.025/hour (RI)
|
|
- EC2 Spot: ~70% discount from on-demand
|
|
|
|
Storage:
|
|
- S3 Standard: $0.023/GB/month
|
|
- S3 Intelligent-Tiering: $0.023/GB + $0.0025/1000 objects
|
|
- S3 Glacier: $0.004/GB/month
|
|
- EBS gp3: $0.08/GB/month
|
|
|
|
Database:
|
|
- RDS db.t3.medium: $0.068/hour
|
|
- Aurora Serverless v2: $0.12/ACU/hour
|
|
- DynamoDB: On-Demand $1.25/million writes, $0.25/million reads</code></pre>
|
|
<hr>
|
|
<h2>Anti-Patterns</h2>
|
|
<h3>Critical Violations</h3>
|
|
<pre><code class="lang-python"># ❌ NEVER: Leave resources running 24/7 in dev/test
|
|
# Cost: t3.medium * 730 hours = $30.37/month
|
|
# If only used 8 hours/day, 5 days/week = ~$9/month
|
|
# Waste: ~$21/month per instance
|
|
|
|
# ✅ CORRECT: Schedule stop/start
|
|
def lambda_handler(event, context):
|
|
ec2 = boto3.client('ec2')
|
|
|
|
if event['action'] == 'stop':
|
|
ec2.stop_instances(InstanceIds=event['instance_ids'])
|
|
elif event['action'] == 'start':
|
|
ec2.start_instances(InstanceIds=event['instance_ids'])</code></pre>
|
|
<pre><code class="lang-python"># ❌ NEVER: Store everything in S3 Standard
|
|
# If 1 TB of logs accessed once per month
|
|
# S3 Standard: $23/month
|
|
# S3 Glacier: $4/month + retrieval costs
|
|
# Waste: ~$19/month
|
|
|
|
# ✅ CORRECT: Use lifecycle policies
|
|
# See Storage Optimization section above</code></pre>
|
|
<pre><code class="lang-python"># ❌ NEVER: Over-provision Lambda memory
|
|
# 3008 MB when function uses 512 MB
|
|
# Waste: ~95% of memory cost
|
|
|
|
# ✅ CORRECT: Use AWS Lambda Power Tuning
|
|
# https://github.com/alexcasalboni/aws-lambda-power-tuning</code></pre>
|
|
<hr>
|
|
<h2>Related Skills</h2>
|
|
<p><strong>Infrastructure</strong>:</p>
|
|
<ul><li><code>aws-serverless.md</code> - Lambda, DynamoDB cost optimization</li><li><code>terraform-patterns.md</code> - Infrastructure as Code for cost tracking</li><li><code>kubernetes-basics.md</code> - Resource requests/limits for cost control</li><li><code>cloudflare-workers.md</code> - Low-cost edge computing alternative</li></ul>
|
|
<p><strong>Monitoring</strong>:</p>
|
|
<ul><li>CloudWatch for metrics and billing alerts</li><li>AWS Cost Explorer for detailed analysis</li><li>AWS Budgets for spending limits</li></ul>
|
|
<p><strong>Standards from CLAUDE.md</strong>:</p>
|
|
<ul><li>Always define resource limits</li><li>Use cost allocation tags</li><li>Monitor costs proactively</li><li>Right-size before deploying</li><li>Use serverless when appropriate</li><li>Schedule dev/test resources</li></ul>
|
|
<hr>
|
|
<p><strong>Last Updated</strong>: 2025-10-18 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<footer>
|
|
<div class="fmap">
|
|
<div><b>Build</b>
|
|
<a href="https://hanzo.app">hanzo.app</a> app builder
|
|
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
|
|
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
|
|
<div><b>Run</b>
|
|
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
|
|
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
|
|
<div><b>Account</b>
|
|
<a href="https://hanzo.id">hanzo.id</a> sign in
|
|
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
|
|
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
|
|
<div><b>Company</b>
|
|
<a href="https://hanzo.ai">hanzo.ai</a> canonical
|
|
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
|
|
</div>
|
|
<div class="fend"><a href="/infrastructure-cost-optimization.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
|
</footer>
|
|
</div></body></html> |