Tuesday, 22 July 2025

Boto3

 

🧠 Understanding Boto3: Overview

Boto3 is the official AWS SDK for Python, used to interact with AWS services like S3, EC2, Lambda, DynamoDB, etc.


⚙️ 1. boto3.client() vs boto3.resource() vs boto3.session()

boto3.client(service_name, ...)

  • Low-level client.

  • Maps 1:1 to AWS service APIs.

  • Returns response dicts (JSON-like).

  • Example: client('s3')

boto3.resource(service_name, ...)

  • High-level abstraction.

  • Uses Python objects.

  • Easier for common operations (like bucket.upload_file(...))

⚠️ Not available for all AWS services.

boto3.session.Session(...)

  • Used to manage configuration: profiles, credentials, and regions.

  • You can have multiple sessions, for example for multi-account or multi-region setups.


🔁 When to Use What

FeatureUse client()Use resource()Use session()
Needs raw API access✅ Yes❌ No❌ Use session.client()
Object-based actions❌ Too verbose✅ Ideal✅ For multi-profile access
Working across profiles❌ Only default or env vars❌ Same✅ Fully supported
Need flexibility✅ Advanced control❌ Less control✅ Multi-region and credential flexibility


✅ Example 1: Using boto3.client to List S3 Buckets


import boto3 s3_client = boto3.client('s3', region_name='us-east-1') def list_buckets(): response = s3_client.list_buckets() for bucket in response['Buckets']: print(f"- {bucket['Name']}")

Why use client?
We want direct access to AWS API to fetch raw data like bucket names.


✅ Example 2: Using boto3.resource to Upload File to S3


import boto3 s3 = boto3.resource('s3') bucket = s3.Bucket('my-bucket-name') def upload_file(): bucket.upload_file(Filename='file.txt', Key='uploaded_file.txt')

Why use resource?
This is a high-level operation (upload_file) which is easier with resource than calling put_object() manually with client.


✅ Example 3: Using boto3.session for Multiple Profiles

Let's say you have 2 AWS profiles: dev and prod.


import boto3 def get_instance_count(profile_name): session = boto3.Session(profile_name=profile_name) ec2 = session.client('ec2', region_name='us-east-1') instances = ec2.describe_instances() total = sum(len(reservation['Instances']) for reservation in instances['Reservations']) print(f"{profile_name} has {total} EC2 instance(s).") get_instance_count('dev') get_instance_count('prod')

Why use session?
Each session uses its own credentials and region. Useful for multi-account management.


✅ Example 4: Using boto3.session to Assume Role into Another Account


import boto3 def assume_role_and_list_s3(role_arn): base_session = boto3.Session() sts_client = base_session.client('sts') assumed_role = sts_client.assume_role( RoleArn=role_arn, RoleSessionName='CrossAccountSession' ) credentials = assumed_role['Credentials'] temp_session = boto3.Session( aws_access_key_id=credentials['AccessKeyId'], aws_secret_access_key=credentials['SecretAccessKey'], aws_session_token=credentials['SessionToken'] ) s3 = temp_session.client('s3') buckets = s3.list_buckets() for b in buckets['Buckets']: print(b['Name']) assume_role_and_list_s3("arn:aws:iam::123456789012:role/SomeRole")

Why use session?
You can create temporary sessions with assumed roles — essential in enterprise, multi-account setups.


🧪 Quick Summary Table

Use CaseMethod UsedWhy?
List S3 bucketsboto3.client()Raw API for precise data
Upload files to S3boto3.resource()High-level object methods
Switch between dev and prod accountsboto3.session()Supports multiple profiles
Cross-account access with STS assume roleboto3.session()Use temporary credentials via STS

🧰 Pro Tip

Use session.client() or session.resource() like this:


session = boto3.Session(profile_name='dev') s3_client = session.client('s3')

It gives you flexibility + cleaner multi-env support.



✅ 1. S3 File Operations (Upload, List, Download)

python
import boto3 # High-level resource s3 = boto3.resource('s3') bucket_name = 'my-demo-bucket' # Upload a file s3.Bucket(bucket_name).upload_file('local.txt', 'uploaded.txt') # List objects for obj in s3.Bucket(bucket_name).objects.all(): print(f'File in bucket: {obj.key}') # Download a file s3.Bucket(bucket_name).download_file('uploaded.txt', 'downloaded.txt')

✅ Use resource for S3 when you want file operations, cleaner syntax, and auto-pagination.


✅ 2. EC2: Launch Instance, List Instances

python
import boto3 ec2 = boto3.resource('ec2') # Launch new EC2 instance instances = ec2.create_instances( ImageId='ami-0c55b159cbfafe1f0', InstanceType='t2.micro', MinCount=1, MaxCount=1 ) print("Launched instance ID:", instances[0].id) # List running instances for instance in ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]): print(instance.id, instance.instance_type, instance.state['Name'])

boto3.resource('ec2') is great for managing instances in an object-oriented way.


✅ 3. EKS: List Clusters and Get Cluster Info

python
import boto3 eks = boto3.client('eks') # List all EKS clusters response = eks.list_clusters() print("Clusters:", response['clusters']) # Get details for a specific cluster cluster_info = eks.describe_cluster(name='my-eks-cluster') print("Cluster status:", cluster_info['cluster']['status'])

❗ EKS supports only client, not resource.


✅ 4. Lambda: List and Invoke a Function

python
import boto3 import json lambda_client = boto3.client('lambda') # List Lambda functions functions = lambda_client.list_functions() for func in functions['Functions']: print(func['FunctionName']) # Invoke a function response = lambda_client.invoke( FunctionName='my-function-name', InvocationType='RequestResponse', Payload=json.dumps({'key1': 'value1'}), ) print("Function output:", response['Payload'].read().decode())

client is required for AWS Lambda.


✅ 5. DynamoDB: Add Item, Query Table

python
import boto3 dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('MyTable') # Put item table.put_item(Item={'id': '123', 'name': 'John Doe'}) # Get item response = table.get_item(Key={'id': '123'}) print(response['Item'])

resource is perfect for table access in DynamoDB.


No comments:

Post a Comment