install python // in video 3.7.
4 version installed
-------
python.org windows web based installer
customize install , install location C:\Python37
C:\Python37\python --version //if environment variables not set & u
want to run python commands
env->system variables->path (edit)->new (C:\Python37)
python --version
env->system variables->path (edit)->new (C:\Python37\Scripts) //for
pip3 setting env variable
pip3 --version
pip3 install boto3
install boto
------
python2 users : pip install boto3
python3 users : pip3 install boto3
on top of botocore , boto3 is built
-----------------------------install python 3.x & boto3 on amazonlinux2 server
on amazon linux2
python --version //u get 2.7
python3 --version //no python3
sudo su root
//on google install python on rhel7 (https://tecadmin.net/install-
python-3-7-on-centos/)
yum install gcc openssl-devel bzip2-devel libffi-devel
cd /usr/src
wget wget https://www.python.org/ftp/python/3.7.11/Python-3.7.11.tgz
tar xzf Python-3.7.11.tgz
cd Python-3.7.11
./configure --enable-optimizations
make altinstall //make altinstall is used to
prevent replacing the default python binary file /usr/bin/python.
rm -rf Python-3.7.4*
rm /usr/src/Python-3.7.11.tgz
python3.7 -v
// by default python installed under /usr/local/bin
cd /usr/local/bin/
ls
ls -lrt
./python3.7 -version
./pip3.7 -version
//creating soft links(like shortcut) under /bin location
ln -s /usr/local/bin/python3.7 /bin/python3
python3 --version
ln -s /usr/local/bin/pip3.7 /bin/pip3
pip3 --version
python //default 2.7 python on amazon linux
python3 //3.7 version
import boto3 // boto3 is not default module for python
pip3 install boto3 //installing boto3 for python3 only
python3
import boto3 //no errors , so boto3 installed in python3
on aws , create iam user (with s3 full access) , download iam user access keys
pip3 install awscli //configure programmatic access keys
aws configure --profile s3cliuser //iam user name same as cli
profile name best practice
//boto3 by default consider us-east-1 as default region so define u r own region
echo %HOMEPATH%
cd .aws
dir //ls
type config //storing aws credentials inside config file . cat
config
type credentials //cat command
//aws configure ovverides previous configuration in .aws folder . so use --profile
option while configuring aws
aws configure --profile root
aws configure --profile s3cliuser
aws configure --profile ec2cliuser
---------------
echo %HOMEPATH%
cd .aws
dir //ls
type config //storing aws credentials inside config file . cat
config
type credentials
------------ aws configure on amazonlinux2
pip3 install awscli --user //installed on current user directory , ec2-user
or any other linux user
ls -a
cd .aws/
cat credentials
----------------- list all iam users using boto3
aws iam list-users //using aws cli
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")
for each in iam_con_re.users.all():
print(each_user.name)
# use commented lines when using python interpreter (>>>)or in .py file
# print("--------------------------")
# print(iam_con_cli.list_users())
# print(iam_con_cli.list_users()['Users'])
#for each in iam_con_cli.list_users()['Users']:
# print(each)
print("--------------------------")
for each in iam_con_cli.list_users()['Users']:
print(each['UserName'])