AWS Automation Using Python Boto3 & AWS CLI

Objective
To understand and practice AWS automation using Python (Boto3) and AWS CLI, including:
Setting up Python and required packages
Configuring AWS permissions
Accessing AWS services using IAM Role and IAM User
Automating EC2 and S3 operations using Python scripts
Prerequisites
AWS Account
EC2 Instance (Amazon Linux)
IAM access (Role/User)
Environment Details
OS: Amazon Linux
Python Version: Python 3
AWS Services Used: EC2, S3, IAM
Libraries: boto3, AWS CLI
Step 1: Python Installation & Verification
Installed Python 3 using yum package manager:
Verified Python location
Installed Python 3
Checked Python version
Step 2: Installing pip and boto3
Verified pip availability
Installed boto3 using pip
Validated installation without entering Python shell
Commands used:
python3 -c "import boto3"
No errors confirmed that boto3 was installed successfully.
Step 3: AWS Permissions Setup
Option 1: EC2 Instance (IAM Role)
Created an IAM Role for EC2 service
Attached required policies:
EC2 Full Access
S3 Full Access
RDS Full Access (if needed)
Attached the role to EC2 instance
This allowed direct AWS access without using access keys.
Option 2: Local Machine (IAM User)
Created IAM User with programmatic access
Generated Access Key & Secret Key
Attached service-specific policies
Configured AWS CLI using:
aws configure
Entered:
Access Key
Secret Key
Region
Output format
Step 4: AWS CLI Validation
Tested AWS access using CLI commands:
Listed EC2 instances
Listed S3 buckets
Commands:
aws ec2 describe-instancesaws s3 ls
Successful output confirmed proper IAM permissions and configuration.
Step 5: Python Script – List EC2 Instances
Created a Python script using boto3 to:
Connect to EC2 service
Fetch EC2 instance details
Print raw response for understanding data structure
Observed:
Response returned as a dictionary
Instances grouped under
Reservations
Step 6: Python Script – Start Stopped EC2 Instances
Enhanced the script to:
Iterate through EC2 instances
Identify instances in
stoppedstateCollect instance IDs
Start stopped instances automatically
Handled edge case:
- If no stopped instances found, script exits safely
This helped in understanding:
Nested dictionary parsing
Conditional automation logic
Real-world AWS automation use case
Step 7: Python Script – List S3 Buckets
Created another Python script to:
Connect to S3 service
Fetch list of all buckets
Print bucket names one by one
Observed:
Buckets returned as a list of dictionaries
Each bucket contains metadata like name and creation date
Key Learnings
Difference between IAM Role vs IAM User
How AWS CLI and boto3 use the same credentials
Understanding AWS API responses (dict & list structure)
Writing safe automation scripts with validations
Real-world AWS automation using Python
Conclusion
This lab provided hands-on experience with AWS automation using Python boto3 and AWS CLI.
It strengthened my understanding of AWS permissions, scripting logic, and service-level automation, which is essential for real-world cloud and DevOps tasks.

