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.
796 lines
36 KiB
HTML
796 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>cloud-aws-databases</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/cloud-aws-databases">
|
|
<link rel="alternate" type="text/markdown" href="/cloud-aws-databases.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/cloud-aws-databases.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>cloud-aws-databases</h1><p class="lede">AWS database services - RDS, DynamoDB, ElastiCache, Aurora, migration, backup, and optimization</p><h1>AWS Databases</h1>
|
|
<p><strong>Scope</strong>: AWS databases - RDS, DynamoDB, ElastiCache, Aurora Serverless, migration strategies, backup and recovery <strong>Lines</strong>: ~350 <strong>Last Updated</strong>: 2025-10-25 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<hr>
|
|
<h2>When to Use This Skill</h2>
|
|
<p>Activate this skill when:</p>
|
|
<ul><li>Deploying managed relational databases with RDS</li><li>Building NoSQL applications with DynamoDB</li><li>Implementing caching with ElastiCache (Redis/Memcached)</li><li>Setting up Aurora Serverless for variable workloads</li><li>Migrating databases to AWS</li><li>Configuring database backups and point-in-time recovery</li><li>Optimizing database performance and read replicas</li><li>Troubleshooting database connectivity or performance issues</li></ul>
|
|
<h2>Core Concepts</h2>
|
|
<h3>Concept 1: RDS (Relational Database Service)</h3>
|
|
<p><strong>RDS engines</strong>:</p>
|
|
<ul><li><strong>PostgreSQL</strong>: Full-featured, JSON support, extensions</li><li><strong>MySQL</strong>: Popular, good ecosystem</li><li><strong>Aurora</strong>: AWS-optimized, 5x MySQL / 3x PostgreSQL performance</li><li><strong>MariaDB</strong>: MySQL fork, additional features</li><li><strong>Oracle</strong>: Commercial, enterprise features</li><li><strong>SQL Server</strong>: Microsoft, Windows integration</li></ul>
|
|
<pre><code class="lang-python">import boto3
|
|
|
|
rds = boto3.client('rds')
|
|
|
|
def create_rds_instance():
|
|
"""Create RDS PostgreSQL instance with Multi-AZ"""
|
|
|
|
response = rds.create_db_instance(
|
|
DBInstanceIdentifier='myapp-db',
|
|
DBInstanceClass='db.t3.medium', # 2 vCPU, 4 GB RAM
|
|
Engine='postgres',
|
|
EngineVersion='15.4',
|
|
MasterUsername='dbadmin',
|
|
MasterUserPassword='SecurePassword123!', # Example only - use AWS Secrets Manager in production
|
|
AllocatedStorage=100, # GB
|
|
StorageType='gp3', # General purpose SSD
|
|
StorageEncrypted=True,
|
|
MultiAZ=True, # High availability
|
|
DBSubnetGroupName='my-db-subnet-group',
|
|
VpcSecurityGroupIds=['sg-0123456789abcdef0'],
|
|
BackupRetentionPeriod=7, # Days
|
|
PreferredBackupWindow='03:00-04:00', # UTC
|
|
PreferredMaintenanceWindow='sun:04:00-sun:05:00',
|
|
EnableCloudwatchLogsExports=['postgresql'],
|
|
Tags=[
|
|
{'Key': 'Name', 'Value': 'myapp-db'},
|
|
{'Key': 'Environment', 'Value': 'production'}
|
|
]
|
|
)
|
|
|
|
print(f"Creating RDS instance: {response['DBInstance']['DBInstanceIdentifier']}")
|
|
|
|
return response['DBInstance']['Endpoint']['Address']
|
|
|
|
def create_read_replica(source_db_id):
|
|
"""Create read replica for scaling reads"""
|
|
|
|
response = rds.create_db_instance_read_replica(
|
|
DBInstanceIdentifier=f'{source_db_id}-replica-1',
|
|
SourceDBInstanceIdentifier=source_db_id,
|
|
DBInstanceClass='db.t3.medium',
|
|
PubliclyAccessible=False,
|
|
Tags=[
|
|
{'Key': 'Name', 'Value': f'{source_db_id}-replica'},
|
|
{'Key': 'Role', 'Value': 'read-replica'}
|
|
]
|
|
)
|
|
|
|
print(f"Creating read replica: {response['DBInstance']['DBInstanceIdentifier']}")</code></pre>
|
|
<h3>Concept 2: DynamoDB</h3>
|
|
<p><strong>DynamoDB concepts</strong>:</p>
|
|
<ul><li><strong>Tables</strong>: Primary key (partition + sort key)</li><li><strong>Indexes</strong>: GSI (global), LSI (local)</li><li><strong>Capacity modes</strong>: On-demand vs provisioned</li><li><strong>Streams</strong>: Change data capture</li></ul>
|
|
<pre><code class="lang-python">import boto3
|
|
from boto3.dynamodb.conditions import Key, Attr
|
|
from datetime import datetime
|
|
|
|
dynamodb = boto3.resource('dynamodb')
|
|
|
|
def create_dynamodb_table():
|
|
"""Create DynamoDB table with indexes"""
|
|
|
|
table = dynamodb.create_table(
|
|
TableName='Users',
|
|
KeySchema=[
|
|
{'AttributeName': 'userId', 'KeyType': 'HASH'}, # Partition key
|
|
],
|
|
AttributeDefinitions=[
|
|
{'AttributeName': 'userId', 'AttributeType': 'S'},
|
|
{'AttributeName': 'email', 'AttributeType': 'S'},
|
|
{'AttributeName': 'createdAt', 'AttributeType': 'S'}
|
|
],
|
|
GlobalSecondaryIndexes=[
|
|
{
|
|
'IndexName': 'email-index',
|
|
'KeySchema': [
|
|
{'AttributeName': 'email', 'KeyType': 'HASH'}
|
|
],
|
|
'Projection': {'ProjectionType': 'ALL'},
|
|
'ProvisionedThroughput': {
|
|
'ReadCapacityUnits': 5,
|
|
'WriteCapacityUnits': 5
|
|
}
|
|
},
|
|
{
|
|
'IndexName': 'created-index',
|
|
'KeySchema': [
|
|
{'AttributeName': 'createdAt', 'KeyType': 'HASH'}
|
|
],
|
|
'Projection': {'ProjectionType': 'KEYS_ONLY'},
|
|
'ProvisionedThroughput': {
|
|
'ReadCapacityUnits': 5,
|
|
'WriteCapacityUnits': 5
|
|
}
|
|
}
|
|
],
|
|
BillingMode='PROVISIONED',
|
|
ProvisionedThroughput={
|
|
'ReadCapacityUnits': 10,
|
|
'WriteCapacityUnits': 10
|
|
},
|
|
StreamSpecification={
|
|
'StreamEnabled': True,
|
|
'StreamViewType': 'NEW_AND_OLD_IMAGES'
|
|
},
|
|
Tags=[
|
|
{'Key': 'Environment', 'Value': 'production'}
|
|
]
|
|
)
|
|
|
|
# Wait for table to be created
|
|
table.wait_until_exists()
|
|
print(f"Created table: {table.table_name}")
|
|
|
|
return table
|
|
|
|
# CRUD operations
|
|
table = dynamodb.Table('Users')
|
|
|
|
def create_user(user_id, email, name):
|
|
"""Create user item"""
|
|
table.put_item(
|
|
Item={
|
|
'userId': user_id,
|
|
'email': email,
|
|
'name': name,
|
|
'createdAt': datetime.utcnow().isoformat(),
|
|
'status': 'active'
|
|
}
|
|
)
|
|
|
|
def get_user(user_id):
|
|
"""Get user by ID"""
|
|
response = table.get_item(Key={'userId': user_id})
|
|
return response.get('Item')
|
|
|
|
def query_by_email(email):
|
|
"""Query using GSI"""
|
|
response = table.query(
|
|
IndexName='email-index',
|
|
KeyConditionExpression=Key('email').eq(email)
|
|
)
|
|
return response['Items']
|
|
|
|
def update_user(user_id, name):
|
|
"""Update user with atomic increment"""
|
|
response = table.update_item(
|
|
Key={'userId': user_id},
|
|
UpdateExpression='SET #name = :name, updatedAt = :timestamp ADD loginCount :inc',
|
|
ExpressionAttributeNames={'#name': 'name'},
|
|
ExpressionAttributeValues={
|
|
':name': name,
|
|
':timestamp': datetime.utcnow().isoformat(),
|
|
':inc': 1
|
|
},
|
|
ReturnValues='ALL_NEW'
|
|
)
|
|
return response['Attributes']</code></pre>
|
|
<h3>Concept 3: ElastiCache</h3>
|
|
<p><strong>Redis vs Memcached</strong>:</p>
|
|
<ul><li><strong>Redis</strong>: Data structures, persistence, replication, pub/sub</li><li><strong>Memcached</strong>: Simple key-value, multi-threaded, faster for simple caching</li></ul>
|
|
<pre><code class="lang-python">import boto3
|
|
import redis
|
|
|
|
elasticache = boto3.client('elasticache')
|
|
|
|
def create_redis_cluster():
|
|
"""Create ElastiCache Redis cluster"""
|
|
|
|
response = elasticache.create_replication_group(
|
|
ReplicationGroupId='myapp-redis',
|
|
ReplicationGroupDescription='Redis cluster for myapp',
|
|
Engine='redis',
|
|
EngineVersion='7.0',
|
|
CacheNodeType='cache.t3.medium',
|
|
NumCacheClusters=2, # Primary + 1 replica
|
|
AutomaticFailoverEnabled=True,
|
|
MultiAZEnabled=True,
|
|
CacheSubnetGroupName='my-cache-subnet-group',
|
|
SecurityGroupIds=['sg-0123456789abcdef0'],
|
|
AtRestEncryptionEnabled=True,
|
|
TransitEncryptionEnabled=True,
|
|
SnapshotRetentionLimit=5,
|
|
SnapshotWindow='03:00-05:00',
|
|
Tags=[
|
|
{'Key': 'Name', 'Value': 'myapp-redis'},
|
|
{'Key': 'Environment', 'Value': 'production'}
|
|
]
|
|
)
|
|
|
|
print(f"Creating Redis cluster: {response['ReplicationGroup']['ReplicationGroupId']}")
|
|
|
|
# Use Redis client
|
|
def connect_to_redis(endpoint, port=6379):
|
|
"""Connect to ElastiCache Redis"""
|
|
|
|
# For encrypted connections
|
|
client = redis.Redis(
|
|
host=endpoint,
|
|
port=port,
|
|
decode_responses=True,
|
|
ssl=True,
|
|
ssl_cert_reqs=None # For self-signed certs
|
|
)
|
|
|
|
return client
|
|
|
|
# Caching pattern
|
|
def get_user_cached(user_id, redis_client):
|
|
"""Get user with Redis caching"""
|
|
|
|
cache_key = f'user:{user_id}'
|
|
|
|
# Check cache
|
|
cached = redis_client.get(cache_key)
|
|
if cached:
|
|
return json.loads(cached)
|
|
|
|
# Cache miss - fetch from DB
|
|
user = get_user_from_db(user_id)
|
|
|
|
# Store in cache (1 hour TTL)
|
|
redis_client.setex(
|
|
cache_key,
|
|
3600,
|
|
json.dumps(user)
|
|
)
|
|
|
|
return user</code></pre>
|
|
<h3>Concept 4: Aurora Serverless</h3>
|
|
<p><strong>Aurora Serverless use cases</strong>:</p>
|
|
<ul><li>Variable workloads (dev/test environments)</li><li>Unpredictable traffic patterns</li><li>Multi-tenant applications</li><li>Infrequent usage (pauses when idle)</li></ul>
|
|
<pre><code class="lang-python">def create_aurora_serverless_cluster():
|
|
"""Create Aurora Serverless v2 cluster"""
|
|
|
|
response = rds.create_db_cluster(
|
|
DBClusterIdentifier='myapp-aurora',
|
|
Engine='aurora-postgresql',
|
|
EngineVersion='15.4',
|
|
MasterUsername='dbadmin',
|
|
MasterUserPassword='SecurePassword123!', # Example only - use AWS Secrets Manager in production
|
|
DatabaseName='myapp',
|
|
DBSubnetGroupName='my-db-subnet-group',
|
|
VpcSecurityGroupIds=['sg-0123456789abcdef0'],
|
|
ServerlessV2ScalingConfiguration={
|
|
'MinCapacity': 0.5, # ACUs (Aurora Capacity Units)
|
|
'MaxCapacity': 2.0
|
|
},
|
|
EnableHttpEndpoint=True, # Data API
|
|
StorageEncrypted=True,
|
|
BackupRetentionPeriod=7,
|
|
Tags=[
|
|
{'Key': 'Name', 'Value': 'myapp-aurora'},
|
|
{'Key': 'Type', 'Value': 'serverless'}
|
|
]
|
|
)
|
|
|
|
cluster_id = response['DBCluster']['DBClusterIdentifier']
|
|
|
|
# Create serverless instance
|
|
rds.create_db_instance(
|
|
DBInstanceIdentifier=f'{cluster_id}-instance-1',
|
|
DBInstanceClass='db.serverless',
|
|
Engine='aurora-postgresql',
|
|
DBClusterIdentifier=cluster_id
|
|
)
|
|
|
|
print(f"Created Aurora Serverless cluster: {cluster_id}")</code></pre>
|
|
<hr>
|
|
<h2>Patterns</h2>
|
|
<h3>Pattern 1: Database Migration with DMS</h3>
|
|
<p><strong>When to use</strong>: Migrate databases to AWS with minimal downtime</p>
|
|
<pre><code class="lang-python">import boto3
|
|
|
|
dms = boto3.client('dms')
|
|
|
|
def create_dms_replication():
|
|
"""Create DMS replication instance and task"""
|
|
|
|
# Create replication instance
|
|
replication_response = dms.create_replication_instance(
|
|
ReplicationInstanceIdentifier='myapp-migration',
|
|
ReplicationInstanceClass='dms.t3.medium',
|
|
AllocatedStorage=100,
|
|
VpcSecurityGroupIds=['sg-0123456789abcdef0'],
|
|
MultiAZ=False,
|
|
EngineVersion='3.4.7',
|
|
PubliclyAccessible=False
|
|
)
|
|
|
|
# Wait for instance to be available
|
|
waiter = dms.get_waiter('replication_instance_available')
|
|
waiter.wait(
|
|
Filters=[
|
|
{'Name': 'replication-instance-id', 'Values': ['myapp-migration']}
|
|
]
|
|
)
|
|
|
|
# Create source endpoint (on-premises database)
|
|
source_endpoint = dms.create_endpoint(
|
|
EndpointIdentifier='source-postgres',
|
|
EndpointType='source',
|
|
EngineName='postgres',
|
|
ServerName='onprem-db.example.com',
|
|
Port=5432,
|
|
DatabaseName='myapp',
|
|
Username='migration_user',
|
|
Password='migration_password' # Example only - use AWS Secrets Manager in production
|
|
)
|
|
|
|
# Create target endpoint (RDS)
|
|
target_endpoint = dms.create_endpoint(
|
|
EndpointIdentifier='target-rds',
|
|
EndpointType='target',
|
|
EngineName='postgres',
|
|
ServerName='myapp-db.abc123.us-east-1.rds.amazonaws.com',
|
|
Port=5432,
|
|
DatabaseName='myapp',
|
|
Username='dbadmin',
|
|
Password='SecurePassword123!' # Example only - use AWS Secrets Manager in production
|
|
)
|
|
|
|
# Create migration task
|
|
dms.create_replication_task(
|
|
ReplicationTaskIdentifier='myapp-full-load',
|
|
SourceEndpointArn=source_endpoint['Endpoint']['EndpointArn'],
|
|
TargetEndpointArn=target_endpoint['Endpoint']['EndpointArn'],
|
|
ReplicationInstanceArn=replication_response['ReplicationInstance']['ReplicationInstanceArn'],
|
|
MigrationType='full-load-and-cdc', # Full load + ongoing replication
|
|
TableMappings=json.dumps({
|
|
'rules': [
|
|
{
|
|
'rule-type': 'selection',
|
|
'rule-id': '1',
|
|
'rule-name': 'include-all',
|
|
'object-locator': {
|
|
'schema-name': 'public',
|
|
'table-name': '%'
|
|
},
|
|
'rule-action': 'include'
|
|
}
|
|
]
|
|
})
|
|
)
|
|
|
|
print("Created DMS replication task")</code></pre>
|
|
<h3>Pattern 2: Connection Pooling</h3>
|
|
<p><strong>Use case</strong>: Manage database connections efficiently</p>
|
|
<pre><code class="lang-python">import psycopg2
|
|
from psycopg2 import pool
|
|
|
|
# Create connection pool
|
|
db_pool = psycopg2.pool.SimpleConnectionPool(
|
|
minconn=1,
|
|
maxconn=20,
|
|
host='myapp-db.abc123.us-east-1.rds.amazonaws.com',
|
|
port=5432,
|
|
database='myapp',
|
|
user='dbadmin',
|
|
password='SecurePassword123!' # Example only - use AWS Secrets Manager in production
|
|
)
|
|
|
|
def execute_query(query, params=None):
|
|
"""Execute query using connection from pool"""
|
|
|
|
conn = None
|
|
try:
|
|
# Get connection from pool
|
|
conn = db_pool.getconn()
|
|
cursor = conn.cursor()
|
|
|
|
# Execute query
|
|
cursor.execute(query, params)
|
|
|
|
# Fetch results
|
|
if cursor.description:
|
|
results = cursor.fetchall()
|
|
else:
|
|
results = None
|
|
|
|
# Commit transaction
|
|
conn.commit()
|
|
|
|
return results
|
|
|
|
except Exception as e:
|
|
if conn:
|
|
conn.rollback()
|
|
raise e
|
|
|
|
finally:
|
|
# Return connection to pool
|
|
if conn:
|
|
db_pool.putconn(conn)
|
|
|
|
# Lambda function with connection pooling
|
|
connection_pool = None
|
|
|
|
def lambda_handler(event, context):
|
|
"""Lambda with persistent connection pool"""
|
|
global connection_pool
|
|
|
|
# Initialize pool once (outside handler reuses)
|
|
if not connection_pool:
|
|
connection_pool = create_connection_pool()
|
|
|
|
# Use pooled connection
|
|
results = execute_query_pooled(connection_pool, "SELECT * FROM users LIMIT 10")
|
|
|
|
return {
|
|
'statusCode': 200,
|
|
'body': json.dumps({'users': results})
|
|
}</code></pre>
|
|
<h3>Pattern 3: DynamoDB Batch Operations</h3>
|
|
<p><strong>Use case</strong>: Efficient bulk reads/writes</p>
|
|
<pre><code class="lang-python">def batch_write_items(table_name, items):
|
|
"""Batch write up to 25 items at a time"""
|
|
|
|
dynamodb = boto3.resource('dynamodb')
|
|
table = dynamodb.Table(table_name)
|
|
|
|
# DynamoDB limits batch to 25 items
|
|
with table.batch_writer() as batch:
|
|
for item in items:
|
|
batch.put_item(Item=item)
|
|
|
|
print(f"Batch wrote {len(items)} items")
|
|
|
|
def batch_get_items(table_name, keys):
|
|
"""Batch get up to 100 items at a time"""
|
|
|
|
dynamodb = boto3.resource('dynamodb')
|
|
|
|
response = dynamodb.batch_get_item(
|
|
RequestItems={
|
|
table_name: {
|
|
'Keys': keys,
|
|
'ConsistentRead': True
|
|
}
|
|
}
|
|
)
|
|
|
|
items = response['Responses'][table_name]
|
|
|
|
# Handle unprocessed keys
|
|
while response.get('UnprocessedKeys'):
|
|
response = dynamodb.batch_get_item(
|
|
RequestItems=response['UnprocessedKeys']
|
|
)
|
|
items.extend(response['Responses'][table_name])
|
|
|
|
return items</code></pre>
|
|
<h3>Pattern 4: Database Backup and Restore</h3>
|
|
<p><strong>Use case</strong>: Point-in-time recovery and snapshots</p>
|
|
<pre><code class="lang-python">def create_rds_snapshot(db_instance_id):
|
|
"""Create manual snapshot"""
|
|
|
|
snapshot_id = f"{db_instance_id}-{datetime.utcnow().strftime('%Y%m%d-%H%M%S')}"
|
|
|
|
response = rds.create_db_snapshot(
|
|
DBSnapshotIdentifier=snapshot_id,
|
|
DBInstanceIdentifier=db_instance_id,
|
|
Tags=[
|
|
{'Key': 'Type', 'Value': 'manual'},
|
|
{'Key': 'CreatedBy', 'Value': 'automation'}
|
|
]
|
|
)
|
|
|
|
print(f"Creating snapshot: {snapshot_id}")
|
|
|
|
return snapshot_id
|
|
|
|
def restore_from_snapshot(snapshot_id, new_instance_id):
|
|
"""Restore database from snapshot"""
|
|
|
|
response = rds.restore_db_instance_from_db_snapshot(
|
|
DBInstanceIdentifier=new_instance_id,
|
|
DBSnapshotIdentifier=snapshot_id,
|
|
DBInstanceClass='db.t3.medium',
|
|
PubliclyAccessible=False,
|
|
MultiAZ=True
|
|
)
|
|
|
|
print(f"Restoring {new_instance_id} from {snapshot_id}")
|
|
|
|
def point_in_time_restore(source_db_id, target_db_id, restore_time):
|
|
"""Restore to specific point in time"""
|
|
|
|
response = rds.restore_db_instance_to_point_in_time(
|
|
SourceDBInstanceIdentifier=source_db_id,
|
|
TargetDBInstanceIdentifier=target_db_id,
|
|
RestoreTime=restore_time, # datetime object
|
|
DBInstanceClass='db.t3.medium'
|
|
)
|
|
|
|
print(f"Restoring {target_db_id} to {restore_time}")</code></pre>
|
|
<hr>
|
|
<h2>Quick Reference</h2>
|
|
<h3>Database Service Selection</h3>
|
|
<p>| Use Case | Service | Type | Best For | |----------|---------|------|----------| | Relational, ACID | RDS | SQL | Structured data, transactions | | Key-value, high scale | DynamoDB | NoSQL | Serverless, millisecond latency | | Caching, sessions | ElastiCache | In-memory | Performance optimization | | Variable workload | Aurora Serverless | SQL | Cost optimization | | Graph data | Neptune | Graph | Relationships, social networks | | Time series | Timestream | Time series | IoT, metrics, logs |</p>
|
|
<h3>RDS Instance Sizing</h3>
|
|
<pre><code>Workload Type | Instance Class | Example | vCPU | RAM
|
|
-------------------|----------------|---------------|------|-------
|
|
Dev/test | db.t3.micro | db.t3.micro | 2 | 1 GB
|
|
Small production | db.t3.medium | db.t3.medium | 2 | 4 GB
|
|
Medium production | db.m5.large | db.m5.large | 2 | 8 GB
|
|
Large production | db.r5.xlarge | db.r5.xlarge | 4 | 32 GB
|
|
Memory-intensive | db.r5.4xlarge | db.r5.4xlarge | 16 | 128 GB</code></pre>
|
|
<h3>Key Guidelines</h3>
|
|
<pre><code>✅ DO: Enable Multi-AZ for production RDS instances
|
|
✅ DO: Use read replicas to scale read traffic
|
|
✅ DO: Enable automated backups (7-35 days retention)
|
|
✅ DO: Use connection pooling for Lambda functions
|
|
✅ DO: Enable encryption at rest and in transit
|
|
✅ DO: Use IAM database authentication when possible
|
|
✅ DO: Monitor performance with CloudWatch
|
|
|
|
❌ DON'T: Use DynamoDB scans for large tables (use queries)
|
|
❌ DON'T: Expose databases publicly (use VPC endpoints)
|
|
❌ DON'T: Ignore read replica lag for critical queries
|
|
❌ DON'T: Use provisioned capacity without monitoring
|
|
❌ DON'T: Store large objects in DynamoDB (use S3 + pointers)</code></pre>
|
|
<hr>
|
|
<h2>Anti-Patterns</h2>
|
|
<h3>Critical Violations</h3>
|
|
<pre><code class="lang-python"># ❌ NEVER: Create new connection per Lambda invocation
|
|
def lambda_handler(event, context):
|
|
conn = psycopg2.connect(
|
|
host='db.example.com',
|
|
database='myapp',
|
|
user='dbadmin',
|
|
password='password'
|
|
)
|
|
# New connection every time = connection exhaustion
|
|
|
|
# ✅ CORRECT: Reuse connection across invocations
|
|
connection = None
|
|
|
|
def lambda_handler(event, context):
|
|
global connection
|
|
|
|
if not connection or connection.closed:
|
|
connection = psycopg2.connect(...)
|
|
|
|
# Reuse connection
|
|
cursor = connection.cursor()
|
|
cursor.execute("SELECT * FROM users")</code></pre>
|
|
<p>❌ <strong>New connection per invocation</strong>: Exhausts database connections, high latency</p>
|
|
<p>✅ <strong>Correct approach</strong>: Initialize outside handler, reuse across warm invocations</p>
|
|
<h3>Common Mistakes</h3>
|
|
<pre><code class="lang-python"># ❌ Don't scan entire DynamoDB table
|
|
response = table.scan()
|
|
items = response['Items']
|
|
# Consumes read capacity, expensive, slow
|
|
|
|
# ✅ Correct: Use query with key condition
|
|
response = table.query(
|
|
IndexName='email-index',
|
|
KeyConditionExpression=Key('email').eq(user_email)
|
|
)
|
|
items = response['Items']
|
|
# Efficient, targeted read</code></pre>
|
|
<p>❌ <strong>DynamoDB scan</strong>: High latency, expensive, consumes capacity</p>
|
|
<p>✅ <strong>Better</strong>: Use query with partition key, add GSI if needed</p>
|
|
<hr>
|
|
<h2>Related Skills</h2>
|
|
<ul><li><code>aws-lambda-functions.md</code> - Lambda integration with databases</li><li><code>aws-storage.md</code> - S3 for database backups and large objects</li><li><code>aws-networking.md</code> - VPC endpoints for private database access</li><li><code>aws-iam-security.md</code> - IAM database authentication and permissions</li></ul>
|
|
<hr>
|
|
<p><strong>Last Updated</strong>: 2025-10-25 <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="/cloud-aws-databases.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
|
</footer>
|
|
</div></body></html> |