π Unlocking AWS Automation: Mastering Boto3 and Lambda Functions with Python
π AWS Automation with Python Boto3 and Lambda Functions
[PART - 1] Introduction
π― Aim: Learn how to automate AWS common tasks using Boto3 and Lambda Functions.
π― Objective of this course:
Cover the core concepts of Boto3 and Lambda.
Understand Boto3 and Lambda concepts with real-time scenarios.
Running Boto3 scripts on your local machine and triggering Lambda functions.
By the end of this course, you will gain the knowledge to apply different concepts of Boto3 and Lambda for different AWS Services.
Pre-requisites: What do you need for this course?
AWS Account: It is great if you have a free tier account.
Good if you have basic Knowledge on AWS Services and Python (Not mandatory).
Knowledge on Any Python IDE (Not mandatory).
π‘ Introduction to Boto3
- Boto3 is the name of the Python SDK/Library/Module/API for AWS.
π§ Boto3 allows us to directly create, update, and delete AWS services from our Python scripts.
π Boto3 is built on the top of the boto core module
π§ Installation:
Python-2.x:
pip install boto3
Python-3.x:
pip3 install boto3
π» Install Python and Boto3 on Windows Machine:
Python-3.7.4: Visit www.python.org.
Set Paths for Python and pip3.
Install boto3 using
pip3 install boto3
.
π§ Install Python and Boto3 on Linux Machine:
Dependencies
yum install gcc openssl-devel bzip2-devel libffi-devel
Download Python
cd /usr/src
wget
https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
tar xzf Python-3.7.4.tgz
cd Python-3.7.4
Configure and Install Python
./configure --enable-optimizations
make altinstall
Set Up Python Binaries
cd /usr/local/bin/
./python3.7 --version
./pip3.7 --version
ln -s /usr/local/bin/python3.7 /bin/python3
python3 --version
ln -s /usr/local/bin/pip3.7 /bin/pip3
pip3 --version
Install Boto3
pip3 install boto3
π§ Boto3 Environment Setup
Setting up your environment to use Boto3 for AWS automation is a crucial first step. Here's a detailed guide to get you started:
1. Configure AWS Credentials:
π οΈ AWS CLI: The AWS Command Line Interface (AWS CLI) is your go-to tool for managing AWS services from the command line.
π₯ Downloading AWS CLI: Guide
π» Configuration:
Login to AWS Management Console and create a new user with programmatic access, granting AdministratorAccess.
Configure access keys/credentials:
aws configure
(Creates DEFAULT profile)
2. First Automation Script: List IAM Users
π Manual Steps:
Step 1: Access AWS Management Console AWS Management Console
Step 2: Navigate to IAM Console
In IAM Console, explore options like:
Users
Groups
Roles
Policies, etc.
import boto3
# Create a session object named 'aws_management_console' using the default profile
aws_management_console = boto3.session.Session(profile_name="default")
# Create an IAM resource object named 'iam_console_resource' using the session
iam_console_resource = aws_management_console.resource('iam')
# Iterate through all IAM users and print their names
for each_user in iam_console_resource.users.all():
print(each_user.name)
π Explanation:
boto3.session.Session(profile_name="default")
: Creates a session object namedaws_management_console
using the default AWS profile. This session object will store configuration information like credentials.aws_management_console.resource('iam')
: Creates an IAM resource object namediam_console_resource
using the session. This resource object allows you to interact with IAM resources.iam_console_resource.users.all()
: Fetches all IAM users using theall()
method provided by the resource object.for each_user in iam_console_resource.users.all():
: Iterates through each IAM user fetched.print(each_
user.name
)
: Prints the name of each IAM user.
π‘ Concepts of Boto3
Session
Resource
Client
Meta
Collections
Waiters
Paginators
πΉ Session
In simple words, it's like the AWS Management Console.
Stores configuration information (Credentials of Default user etcβ¦).
Allows us to create Service, Clients, and Resources.
It creates a default session for us when we need it.
We can create multiple sessions in the same script!
πΉ Resource and Client
We can create particular AWS Service consoles examples: IAM Console, EC2 Console, etc...
You can create an AWS Service console from your Session object.
Region name can be specified after the Profile name.
Example for Resource Object:
Example for Client Object:
π€ Should I choose Resource or Client?
You can choose anyone depending on your use case.
Resource is Higher Level Object oriented service access.
Resource objects are only available for a few AWS Services.
Let us check which AWS Service has a Resource Object!!! - DEMπ
- [
'cloudformation', 'cloudwatch', 'dynamodb', 'ec2', 'glacier', 'iam', 'opsworks', 's3', 'sns', 'sqs']
- Resource Object Available.
- [
Client is Low-Level Service Access.
In simple terms, the output/response in case of Client will be in Dictionary, which needs more effort in implementing boto3 scripts.
Whereas Resource is an object, we can use simple (.) operation.
All operations that we see in AWS Management Console can be done in Client whereas Resource does not guarantee you that. Some operations may not be supported.
If we do not have some operations in Resource we can enter into Client by using the βMetaβ concept. Let us talk about this later! π
Let us see how much effort is needed for both Resource and Client. - DEMπ
output:
Example 1: List all the IAM users in AWS Account using client objects.
import boto3
# Create a session object named 'aws_management_console' using the default profile
aws_management_console = boto3.session.Session(profile_name="default")
# Create an IAM client object named 'iam_console_client' using the session
iam_console_client = aws_management_console.client('iam')
# Retrieve a list of all IAM users
response = iam_console_client.list_users()
# Iterate through all IAM users and print their names
for user in response['Users']:
print(user['UserName'])
π Explanation:
Session Creation:
boto3.session.Session(profile_name="default")
: Creates a session object namedaws_management_console
using the default AWS profile. This session object will store configuration information like credentials.
IAM Client Creation:
aws_management_console.client('iam')
: Creates an IAM client object namediam_console_client
using the session. The client object allows you to interact with the IAM service.
Listing IAM Users:
iam_console_client.list_users()
: Calls thelist_users()
method of the IAM client object to retrieve a list of all IAM users in the AWS account.The response from
list_users()
is stored in the variableresponse
.
Iterating and Printing:
for user in response['Users']:
: Iterates through each IAM user in the list of users returned in the response.print(user['UserName'])
: Prints the name of each IAM user. The username is accessed using the key'UserName'
in the user dictionary.
Example 2: List all the running EC2 Instances in your AWS Account using client objects.
Example 3: List all the IAM users in AWS Account using resource objects.
['cloudformation', 'cloudwatch', 'dynamodb', 'ec2', 'glacier', 'iam', 'opsworks', 's3', 'sns', 'sqs'] - Resource Object Available.
Example 4: List all the running EC2 Instances in your AWS Account using resource objects.