📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials AWS Solutions Architect AWS SDKs and boto3

AWS SDKs and boto3

4 min read
Use boto3 to integrate AWS services into Python applications. Learn authentication best practices and error handling patterns.

AWS SDKs — Integrate AWS into Your Code

AWS SDKs let your applications talk to AWS services programmatically. The most popular is boto3 for Python. With boto3, your application can upload to S3, query DynamoDB, send SNS notifications, and manage EC2 — all from code.

Teacher Note: SDKs are like a set of pre-built LEGO connectors. Instead of building the connection from scratch, you use the ready-made connector. boto3 handles authentication, retries, pagination, and error handling automatically.

boto3 Quick Start

import boto3

# Create a client (low-level API)
s3 = boto3.client('s3')
response = s3.list_buckets()
for bucket in response['Buckets']:
    print(bucket['Name'])

# Create a resource (high-level OOP API)
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket('my-bucket')
bucket.upload_file('local-file.txt', 'remote-key.txt')

# DynamoDB example
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('users')
table.put_item(Item={'user_id': '123', 'name': 'Alice', 'age': 30})
response = table.get_item(Key={'user_id': '123'})
print(response['Item'])

Authentication in Code — Best Practices

EnvironmentRecommended Auth MethodWhy
EC2 InstanceIAM Role attached to instanceAuto-rotating temporary credentials
Lambda FunctionIAM Role attached to functionSame — auto-rotating, no key management
Local DevelopmentAWS CLI configured profileUse your developer credentials
CI/CD PipelineOIDC (GitHub Actions) or IAM RoleNo long-term keys in pipeline config
NEVERHard-coded access keys in codeKeys get committed to Git and leaked

Error Handling and Retries

import boto3
from botocore.exceptions import ClientError, NoCredentialsError

s3 = boto3.client('s3')
try:
    s3.download_file('my-bucket', 'file.txt', '/tmp/file.txt')
except ClientError as e:
    error_code = e.response['Error']['Code']
    if error_code == '404':
        print('File not found in S3')
    elif error_code == '403':
        print('Access denied - check IAM permissions')
except NoCredentialsError:
    print('AWS credentials not configured')
Exam Tip: boto3 automatically retries on transient errors (throttling, network issues) using exponential backoff. Configure retry behaviour with botocore.config. For high-throughput applications, use connection pooling by reusing the client object.