Production SDK Configuration Basics
Nine examples to get you started with production-ready SDK configuration - six basic and three intermediate.
Busque em todas as páginas da documentação
Nine examples to get you started with production-ready SDK configuration - six basic and three intermediate.
The SDK defaults are tuned for getting started, not for a service under load. Before you ship, make region, credentials, retries, and timeouts explicit so behavior is predictable across every environment.
pip install boto3 (1.43.x, Python 3.10+).npm install @aws-sdk/client-sts @aws-sdk/credential-providers (SDK v3, Node 18+). Install only the @aws-sdk/client-* packages you use.Never rely on the ambient default region in production code.
# --- Python (boto3) ---
import boto3
s3 = boto3.client("s3", region_name="us-east-1")
print(s3.meta.region_name)// --- TypeScript (AWS SDK v3) ---
import { S3Client } from "@aws-sdk/client-s3";
const s3 = new S3Client({ region: "us-east-1" });
console.log(await s3.config.region());region_name / region at client construction, not per call.Related: SDK Config Precedence: Env, Profile, Code & Defaults - where region is resolved from.
Clients are thread-safe and pool TCP connections, so construct them once.
# --- Python (boto3) ---
import boto3
# Module-level: created once, reused across requests.
DDB = boto3.client("dynamodb", region_name="us-east-1")
def get_item(key):
return DDB.get_item(TableName="orders", Key=key)// --- TypeScript (AWS SDK v3) ---
import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb";
// Module scope: created once, reused across requests.
const ddb = new DynamoDBClient({ region: "us-east-1" });
export const getItem = (Key: Record<string, any>) =>
ddb.send(new GetItemCommand({ TableName: "orders", Key }));Pick a retry mode and a max attempt count instead of accepting the default.
# --- Python (boto3) ---
import boto3
from botocore.config import Config
cfg = Config(retries={"max_attempts": 5, "mode": "adaptive"})
s3 = boto3.client("s3", region_name="us-east-1", config=cfg)// --- TypeScript (AWS SDK v3) ---
import { S3Client } from "@aws-sdk/client-s3";
import { AdaptiveRetryStrategy } from "@smithy/util-retry";
const s3 = new S3Client({
region: "us-east-1",
maxAttempts: 5,
retryStrategy: new AdaptiveRetryStrategy(async () => 5),
});adaptive mode adds client-side rate limiting on throttling responses.max_attempts counts the initial try plus retries.Bound how long a single request can hang so a stuck socket cannot stall your service.
# --- Python (boto3) ---
import boto3
from botocore.config import Config
cfg = Config(connect_timeout=3, read_timeout=10, retries={"max_attempts": 3})
s3 = boto3.client("s3", region_name="us-east-1", config=cfg)// --- TypeScript (AWS SDK v3) ---
import { S3Client } from "@aws-sdk/client-s3";
import { NodeHttpHandler } from "@smithy/node-http-handler";
const s3 = new S3Client({
region: "us-east-1",
requestHandler: new NodeHttpHandler({ connectionTimeout: 3000, requestTimeout: 10000 }),
});Be explicit about where credentials come from rather than trusting the ambient chain.
# --- Python (boto3) ---
import boto3
# Local/dev: a named profile from the shared config files.
session = boto3.Session(profile_name="payments-dev")
s3 = session.client("s3", region_name="us-east-1")// --- TypeScript (AWS SDK v3) ---
import { S3Client } from "@aws-sdk/client-s3";
import { fromIni } from "@aws-sdk/credential-providers";
// Local/dev: a named profile from the shared config files.
const s3 = new S3Client({ region: "us-east-1", credentials: fromIni({ profile: "payments-dev" }) });Related: The Credential Provider Chain, End to End - what the chain resolves when you do not pin a source.
Log who you are before serving traffic so misconfiguration fails loudly.
# --- Python (boto3) ---
import boto3
ident = boto3.client("sts", region_name="us-east-1").get_caller_identity()
print("Running as", ident["Arn"], "in account", ident["Account"])// --- TypeScript (AWS SDK v3) ---
import { STSClient, GetCallerIdentityCommand } from "@aws-sdk/client-sts";
const id = await new STSClient({ region: "us-east-1" }).send(new GetCallerIdentityCommand({}));
console.log("Running as", id.Arn, "in account", id.Account);get_caller_identity needs no special permission and never throttles in practice.Build every client through a single helper so retries, timeouts, and region stay consistent.
# --- Python (boto3) ---
import boto3
from botocore.config import Config
_CFG = Config(
region_name="us-east-1",
retries={"max_attempts": 5, "mode": "adaptive"},
connect_timeout=3,
read_timeout=10,
)
def client(service: str):
return boto3.client(service, config=_CFG)
s3, ddb = client("s3"), client("dynamodb")// --- TypeScript (AWS SDK v3) ---
import { S3Client } from "@aws-sdk/client-s3";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { NodeHttpHandler } from "@smithy/node-http-handler";
const shared = {
region: "us-east-1",
maxAttempts: 5,
requestHandler: new NodeHttpHandler({ connectionTimeout: 3000, requestTimeout: 10000 }),
};
export const s3 = new S3Client(shared);
export const ddb = new DynamoDBClient(shared);region_name inside Config, so a single object covers region too.Prefer regional endpoints and never disable certificate verification.
# --- Python (boto3) ---
import boto3
from botocore.config import Config
cfg = Config(
region_name="us-east-1",
s3={"addressing_style": "virtual"},
# verify defaults to True - never set verify=False in production.
)
s3 = boto3.client("s3", config=cfg)// --- TypeScript (AWS SDK v3) ---
import { S3Client } from "@aws-sdk/client-s3";
const s3 = new S3Client({
region: "us-east-1",
forcePathStyle: false, // prefer virtual-hosted-style endpoints
// TLS verification is on by default - do not disable it.
});Catch the no-credentials case at startup with a clear, actionable error.
# --- Python (boto3) ---
import boto3
from botocore.exceptions import NoCredentialsError
try:
boto3.client("sts", region_name="us-east-1").get_caller_identity()
except NoCredentialsError:
raise SystemExit("No AWS credentials resolved - check profile, env, or role.")// --- TypeScript (AWS SDK v3) ---
import { STSClient, GetCallerIdentityCommand } from "@aws-sdk/client-sts";
import { CredentialsProviderError } from "@smithy/property-provider";
try {
await new STSClient({ region: "us-east-1" }).send(new GetCallerIdentityCommand({}));
} catch (e) {
if (e instanceof CredentialsProviderError)
throw new Error("No AWS credentials resolved - check profile, env, or role.");
throw e;
}NoCredentialsError; SDK v3 raises CredentialsProviderError.Related: Production Configuration Best Practices - the full pre-launch checklist.
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