AWS SDK for Python (boto3)
Official AWS SDK for Python. Extremely stable API — no breaking changes since v1.0. Two client styles: low-level client (boto3.client()) and high-level resource (boto3.resource()). Credential resolution follows a fixed chain. Region must be specified or set in environment — no global default. Released daily with new AWS service additions.
Warnings
- gotcha Region is not globally defaulted. Calling boto3.client('s3') without region_name and without AWS_DEFAULT_REGION raises botocore.exceptions.NoRegionError. This is the most common source of 'it works on my machine' failures when deploying to CI/CD.
- gotcha NoCredentialsError means the credential chain was exhausted — no credentials found anywhere. Common in CI/CD when AWS_ACCESS_KEY_ID is set but AWS_SECRET_ACCESS_KEY is missing, or when ~/.aws/credentials exists locally but not in the deployment environment.
- gotcha boto3.client() and boto3.resource() return different objects with different APIs. S3 client uses client.put_object(), resource uses s3.Object().put(). Mixing client and resource patterns causes AttributeError.
- gotcha Pinning boto3 without pinning botocore (or vice versa) causes version conflicts. boto3 and botocore ship together daily and must stay version-compatible.
- gotcha ClientError contains all AWS service errors. The error code is at e.response['Error']['Code'], not as a Python exception type. Catching specific AWS errors requires checking this string.
- gotcha boto3.setup_default_session() modifies global state and is not thread-safe. In multi-threaded or async applications, concurrent calls with different credentials will interfere.
Install
-
pip install boto3 -
pip install boto3[crt]
Imports
- boto3.client (low-level)
import boto3 # Explicit credentials (not recommended for production) client = boto3.client( 's3', region_name='us-east-1', aws_access_key_id='KEY', aws_secret_access_key='SECRET' ) # Preferred: let credential chain resolve client = boto3.client('s3', region_name='us-east-1') - boto3.Session (multi-profile)
import boto3 session = boto3.Session( profile_name='my-profile', region_name='eu-west-1' ) client = session.client('dynamodb')
Quickstart
import boto3
from botocore.exceptions import ClientError, NoCredentialsError
# Credential chain: env vars → ~/.aws/credentials → IAM role → ...
# Set AWS_DEFAULT_REGION or pass region_name explicitly
client = boto3.client('s3', region_name='us-east-1')
try:
# List buckets
response = client.list_buckets()
for bucket in response['Buckets']:
print(bucket['Name'])
except NoCredentialsError:
print('No AWS credentials found')
except ClientError as e:
print(f"Error: {e.response['Error']['Code']}: {e.response['Error']['Message']}")