AWS SDK Fundamentals Best Practices
These are the habits that separate a quick script from code you can trust in production.
Busca en todas las páginas de la documentación
These are the habits that separate a quick script from code you can trust in production.
Walk the list once now, then treat it as a review checklist before shipping any AWS integration.
Authorization header when debugging.Let the default chain do the work rather than passing keys by hand.
# --- Python (boto3) ---
import boto3
# No keys in code: the default chain finds env vars, config, or an IAM role.
s3 = boto3.client("s3", region_name="us-east-1")// --- TypeScript (AWS SDK v3) ---
import { S3Client } from "@aws-sdk/client-s3";
// No keys in code: the default provider chain resolves credentials.
const s3 = new S3Client({ region: "us-east-1" });@aws-sdk/client-<service> per service to keep bundles and Lambda cold starts small.ClientError in boto3 and typed exception classes in SDK v3, and branch on the error code.ResponseMetadata.RequestId (boto3) or $metadata.requestId (SDK v3) so AWS support can trace the call.Match the specific error and read its code.
# --- Python (boto3) ---
from botocore.exceptions import ClientError
try:
s3.get_object(Bucket="my-bucket", Key="missing.txt")
except ClientError as err:
code = err.response["Error"]["Code"]
rid = err.response["ResponseMetadata"]["RequestId"]
print(code, rid)// --- TypeScript (AWS SDK v3) ---
import { GetObjectCommand, NoSuchKey } from "@aws-sdk/client-s3";
try {
await s3.send(new GetObjectCommand({ Bucket: "my-bucket", Key: "missing.txt" }));
} catch (err) {
if (err instanceof NoSuchKey) console.log("NoSuchKey");
else console.log((err as { $metadata?: { requestId?: string } }).$metadata?.requestId);
}get_paginator (boto3) or paginate<Operation> helpers (SDK v3) instead of managing continuation tokens by hand.Prefix, KeyConditionExpression, or FilterExpression so you fetch only what you need.BatchGetItem and BatchWriteItem over one call per item to cut requests and latency.region_name / region (or AWS_REGION) so behavior does not depend on ambient defaults.aws partition. Build ARNs from the request context, since GovCloud and China use different partition segments.Because rebuilding a client per call is a common, silent waste. Construction sets up connection pools and configuration, so reusing one client across requests is cheaper and faster.
Hardcoded keys leak into source control and logs, are hard to rotate, and grant standing access. The default credential chain uses environment, config, and rotating IAM role credentials instead.
Usually no. Both SDKs retry transient and throttling errors with backoff by default. Tune the retry mode and max attempts instead of reimplementing retries yourself.
Only when an operation returns a single, bounded result with no continuation token. Any list operation that can return a token should use a paginator.
It uniquely identifies a call on the AWS side. When you open a support case or debug an intermittent failure, the request id is what lets AWS trace exactly what happened.
Ambient region defaults differ across machines and environments. Pinning the region makes behavior reproducible and avoids calls silently going to the wrong region.
Yes. Over-broad permissions turn a minor credential leak into a major incident. Scoping actions to what the code calls is a baseline habit, not an advanced one.
Unbounded loops and tight polling. They turn one logical action into thousands of billed requests, so use waiters, batching, and events instead.
The v3 SDK is modular. Importing only @aws-sdk/client-<service> keeps your bundle and Lambda cold-start size small, which the monolithic approach cannot.
Only for local testing against tools like LocalStack. In production, overriding disables endpoint resolution and freezes the partition, which breaks portability.
In boto3, catch ClientError and read err.response["Error"]["Code"]. In SDK v3, match typed exception classes with instanceof, such as NoSuchKey.
Outside the handler function, at module scope. Warm invocations then reuse the same client instead of constructing a new one on every request.
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 actualización: 23 jul 2026