boto3 library

Go to this path to cover boto3 concepts

E:\Projects\Intelliconnect_Infrastructure_project\PYTHON-BOTO

chevron-rightmethodshashtag

boto3 (AWS SDK for Python)

🔹 General

Method
Description

boto3.client()

Low-level service client

boto3.resource()

High-level service resource (e.g., S3)

boto3.session.Session()

Create a custom session

🔹 Common AWS Services (Client-based usage)

🔸 S3

pythonCopyEdits3 = boto3.client('s3')
Method
Description

s3.upload_file()

Uploads file to S3 bucket

s3.download_file()

Downloads file from S3

s3.list_buckets()

Lists all buckets

s3.create_bucket()

Creates a new bucket

s3.put_object()

Puts object in bucket

s3.get_object()

Gets object from bucket

s3.delete_object()

Deletes an object

🔸 EC2

pythonCopyEditec2 = boto3.client('ec2')
Method
Description

ec2.describe_instances()

Lists EC2 instances

ec2.start_instances()

Starts stopped instances

ec2.stop_instances()

Stops running instances

ec2.terminate_instances()

Terminates instances

🔸 IAM

pythonCopyEditiam = boto3.client('iam')
Method
Description

iam.create_user()

Creates a new IAM user

iam.attach_user_policy()

Attaches policy to user

iam.list_users()

Lists all IAM users

chevron-rightexamplehashtag

Here are real-world DevOps/SRE scenarios using the boto3 (AWS SDK for Python) methods — grouped by General, S3, EC2, and IAM:


✅ General

🔹 boto3.client()Use AWS CLI-like Low-Level Access

import boto3

# Create low-level S3 client
s3 = boto3.client("s3")
response = s3.list_buckets()
print("S3 Buckets:", [b["Name"] for b in response["Buckets"]])

🔹 boto3.resource()High-Level Object-Oriented S3 Interface

import boto3

s3 = boto3.resource("s3")
bucket = s3.Bucket("my-app-backups")

for obj in bucket.objects.all():
    print("Found object:", obj.key)

🔹 boto3.session.Session()Use Custom Profile or Region

import boto3

session = boto3.session.Session(profile_name="prod", region_name="us-west-2")
s3 = session.client("s3")

print("Buckets in us-west-2:", s3.list_buckets())

🔸 S3

🔹 s3.upload_file()Upload Log File to Central Bucket

import boto3

s3 = boto3.client("s3")
s3.upload_file("server.log", "my-devops-bucket", "logs/server.log")

print("Log uploaded to S3")

🔹 s3.download_file()Download Daily Backup from S3

import boto3

s3 = boto3.client("s3")
s3.download_file("backup-bucket", "db/backup.sql", "backup.sql")

print("Backup downloaded")

🔹 s3.list_buckets()Audit Bucket List

import boto3

s3 = boto3.client("s3")
buckets = s3.list_buckets()

for b in buckets["Buckets"]:
    print("Bucket:", b["Name"])

🔹 s3.create_bucket()Create Temporary Bucket for Testing

import boto3

s3 = boto3.client("s3")
s3.create_bucket(Bucket="temporary-devops-bucket")
print("Bucket created")

🔹 s3.put_object()Store Config File

import boto3

s3 = boto3.client("s3")
s3.put_object(Bucket="config-bucket", Key="app/config.yaml", Body="debug: false")

print("Config uploaded")

🔹 s3.get_object()Read S3 File for Config Validation

import boto3

s3 = boto3.client("s3")
response = s3.get_object(Bucket="config-bucket", Key="app/config.yaml")

config_content = response["Body"].read().decode("utf-8")
print("Config content:", config_content)

🔹 s3.delete_object()Delete Outdated Report

import boto3

s3 = boto3.client("s3")
s3.delete_object(Bucket="reports-bucket", Key="old-report.csv")

print("Old report deleted")

🔸 EC2

🔹 ec2.describe_instances()List All Running Instances

import boto3

ec2 = boto3.client("ec2")
instances = ec2.describe_instances()

for res in instances["Reservations"]:
    for inst in res["Instances"]:
        print("Instance ID:", inst["InstanceId"], "| State:", inst["State"]["Name"])

🔹 ec2.start_instances()Start Instance at 9 AM

import boto3

ec2 = boto3.client("ec2")
ec2.start_instances(InstanceIds=["i-0123456789abcdef0"])

print("Instance started")

🔹 ec2.stop_instances()Auto Shutdown at Night

import boto3

ec2 = boto3.client("ec2")
ec2.stop_instances(InstanceIds=["i-0123456789abcdef0"])

print("Instance stopped")

🔹 ec2.terminate_instances()Remove Unused Dev Machine

import boto3

ec2 = boto3.client("ec2")
ec2.terminate_instances(InstanceIds=["i-0123456789abcdef0"])

print("Instance terminated")

🔸 IAM

🔹 iam.create_user()Add New Developer Account

import boto3

iam = boto3.client("iam")
iam.create_user(UserName="devuser")

print("User created")

🔹 iam.attach_user_policy()Grant S3 Full Access

import boto3

iam = boto3.client("iam")
iam.attach_user_policy(
    UserName="devuser",
    PolicyArn="arn:aws:iam::aws:policy/AmazonS3FullAccess"
)

print("Policy attached")

🔹 iam.list_users()Audit IAM Users

import boto3

iam = boto3.client("iam")
users = iam.list_users()

for u in users["Users"]:
    print("IAM User:", u["UserName"])

Would you like this as:

  • ✅ A PDF cheatsheet

  • ✅ A Jupyter notebook

  • ✅ A single image for quick sharing? Let me know!

Last updated