Core SDK Mechanics Best Practices
The mechanics of construction, endpoints, timeouts, and connections decide whether your AWS code is fast and predictable or quietly wasteful.
Busque em todas as páginas da documentação
The mechanics of construction, endpoints, timeouts, and connections decide whether your AWS code is fast and predictable or quietly wasteful.
Walk this list once now, then use it as a review checklist before shipping any client-heavy integration.
@aws-sdk/client-<service> per service to keep bundles and cold starts small.Build the client outside the hot path and reuse the same instance.
# --- Python (boto3) ---
import boto3
# Module scope: created once, reused by every handler invocation.
S3 = boto3.client("s3", region_name="us-east-1")
def handler(event, context):
return S3.list_buckets()["Buckets"]// --- TypeScript (AWS SDK v3) ---
import { S3Client, ListBucketsCommand } from "@aws-sdk/client-s3";
// Module scope: created once, reused by every handler invocation.
const s3 = new S3Client({ region: "us-east-1" });
export const handler = async () => {
const out = await s3.send(new ListBucketsCommand({}));
return out.Buckets ?? [];
};region_name / region so endpoint resolution and signing do not depend on ambient defaults.aws partition. Build ARNs from context, since GovCloud and China use different partition segments.Set region explicitly and let resolution do the rest.
# --- Python (boto3) ---
import boto3
from botocore.config import Config
# Region pinned; FIPS via a flag, not a hardcoded hostname.
cfg = Config(use_fips_endpoint=True)
s3 = boto3.client("s3", region_name="us-west-2", config=cfg)// --- TypeScript (AWS SDK v3) ---
import { S3Client } from "@aws-sdk/client-s3";
// Region pinned; FIPS via a flag, not a hardcoded hostname.
const s3 = new S3Client({ region: "us-west-2", useFipsEndpoint: true });max_pool_connections (boto3) or maxSockets (SDK v3) so requests do not queue for a free connection.Replace the long defaults with values matched to the call.
# --- Python (boto3) ---
import boto3
from botocore.config import Config
cfg = Config(connect_timeout=3, read_timeout=10, max_pool_connections=50)
ddb = boto3.client("dynamodb", region_name="us-east-1", config=cfg)// --- TypeScript (AWS SDK v3) ---
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { NodeHttpHandler } from "@smithy/node-http-handler";
import { Agent } from "node:https";
const ddb = new DynamoDBClient({
region: "us-east-1",
requestHandler: new NodeHttpHandler({
connectionTimeout: 3000,
requestTimeout: 10000,
httpsAgent: new Agent({ maxSockets: 50 }),
}),
});max_attempts / maxAttempts instead of unlimited retries that stack latency.ClientError (boto3) or typed exceptions (SDK v3) and branch on the error code, not the message.ResponseMetadata.RequestId (boto3) or $metadata.requestId (SDK v3) for AWS support and correlation.after-call or SDK v3 deserialize.Because rebuilding a client per call silently discards its connection pool and config, forcing a cold handshake every time. Reuse keeps calls fast and cheap.
Yes. Ambient region defaults differ across machines and environments. Pinning it makes endpoint resolution and signing deterministic and reproducible.
A short connect timeout (1 to 3 seconds) and a read timeout sized to the operation. Raise the read timeout only for large transfers.
Match it to your expected concurrent in-flight requests per client. Too small and calls queue; too large wastes memory and file descriptors.
Usually no. Both SDKs retry transient and throttling errors with backoff. Tune retry mode and max attempts rather than reimplementing it.
For local testing against tools like LocalStack, or to reach a specific VPC endpoint. Avoid it in portable production code, where it disables resolution.
So the SDK gives up and your code handles the failure before the platform kills the function, letting you log and respond cleanly.
Rebuilding clients and leaving default timeouts. Both add latency and resource use that do not show up until load or an incident exposes them.
For business logic, retries, or error handling. Those belong at call sites or in the SDK's retry layer, not in a per-request hook.
Yes. Request-side lifecycle stages run per attempt, so a retried request passes through them again. Keep hooks idempotent.
Import only @aws-sdk/client-<service> for the services you use. The modular v3 packages keep bundle and cold-start size down.
The client's configured region. That is another reason to pin it, since an incorrect region produces signature failures.
Stack versions: This page was written for boto3 1.43.x (Python 3.10+) and the AWS SDK for JavaScript v3 (Node.js 18+).
Revisado por Chris St. John·Última atualização: 23 de jul. de 2026