Boto3 Lambda Python
Boto3 Lambda Python
4 version installed
-------
python.org windows web based installer
customize install , install location C:\Python37
install boto
------
python2 users : pip install boto3
python3 users : pip3 install boto3
ln -s /usr/local/bin/pip3.7 /bin/pip3
pip3 --version
on aws , create iam user (with s3 full access) , download iam user access keys
---------------
echo %HOMEPATH%
cd .aws
dir //ls
type config //storing aws credentials inside config file . cat
config
type credentials
using boto3
-------
import boto3
aws_mag_con=boto3.session.Session(profile_name="root") //get into aws
console . we need to provide aws credentials using profile name is aws cli profile
created when aws configure used
iam_con=aws_mag_con.resource('iam') // to get into iam console. can use
resource , client options both same
// aws_mag_con.resource('any_aws_service_name_which_u_want_to_work_with')
for each_user in iam_con.users.all(): //to see all iam user
& for to see all groups for each_user in iam_con.groups.all()
print(each_user.name)
------list_iam_users.py
import boto3
aws_mag_con=boto3.session.Session(profile_name="root")
iam_con=aws_mag_con.resource('iam')
for each_user in iam_con.users.all():
print(each_user.name)
--------
python list_iam_users.py
---------list_s3_buckets.py (to list all s3 buckets)
import boto3
aws_mag_con=boto3.session.Session(profile_name="root")
iam_con=aws_mag_con.resource('s3')
for each_buk in iam_con.buckets.all():
print(each_buk.name)
--------
python3 list_s3_buckets.py
which python3 //copy output & paste in our .py scripts to tell which
python version should run your script / shebangline
vi list_s3_buckets.py
----------
#!/usr/bin/python3
import boto3
aws_mag_con=boto3.session.Session(profile_name="root")
iam_con=aws_mag_con.resource('s3')
for each_buk in iam_con.buckets.all():
print(each_buk.name)
-----------
chmod +x list_s3_buckets.py //execution permissions for script .py
file
./list_s3_buckets.py
-------------
concepts of boto3 (session , resource , client , meta , collections , waiters,
paginators)
session stores credentials / configuration information , allows us to create
service clients , resources , creates default session
resource , client
---------
we can create particular aws service console like iam console , ec2 console , sns
console on session object . aws_mag_con.resource('s3')
aws_mag_con_root=boto3.session.Session(profile_name="root")
iam_con_re=aws_mag_con.resource(service_name='iam' ,region_name="us-east-2")
//region_name , service_name , profile_name are optional . when u want to override
default region (aws configure region ) u can provide here
to use any service from boto3 , we can use resource , client
on windows cmd
--------
python
import boto3
aws_mag_con_root=boto3.session.Session(profile_name="root")
dir(aws_mag_con_root) // to list all operations available
on object aws_mag_con_root
print(aws_mag_con_root.get_available_resources()) //resource provide access
to limited aws services but client available to all aws services
-----
resource is higher-level object . result is an object
client is low -level service access . result is like dictionary
---------------- for changes.py (to see client , resource changes )
import boto3
aws_mag_con_root=boto3.session.Session(profile_name="root")
aws_mag_con_root=boto3.session.Session(profile_name="s3cliuser")
iam_con_re=aws_mag_con.resource(service_name='iam' ,region_name="us-east-2")
iam_con_cli=aws_mag_con.client(service_name='iam' ,region_name="us-east-2")
print("--------------------------")