Python-Boto3-Automation(AWS)

·

2 min read

Note:

# Boto3 Install for Windows:
Download & Install Python latest version in our laptop from www.python.org
set path in Env Variables:
Right Click on This PC ---> Properties ---> Advanced System settings ---> Environment Variables ---> select path in System Variables Box, Edit & Add(New) our installed python & pip3 paths
In CMD Prompt:
python --version ---> pip3 --version
pip install boto3    # for python-2.x versions
pip3 install boto3    # for python-3.x versions
python ---> import boto3    # to check in CMD

# Boto3 Install for AWS Linux EC2(Redhat/CentOS):
sudo su
yum install gcc openssl-devel bzip2-devel libffi-devel
cd /usr/src
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
tar xvzf Python-3.7.4.tgz
rm -rf Python-3.7.4.tgz
cd Python-3.7.4
./configure --enable-optimizations
make altinstall
cd /usr/local/bin/
ln -s /usr/local/bin/python3.7 /bin/python3
ln -s /usr/local/bin/pip3.7 /bin/pip3
pip3 install boto3
python3 ---> import boto3 ---> exit()

In AWS(IAM) create any user with admin access role and create access, secret access keys
Install AWS CLI in CMD(windows):
pip3 install awscli
aws configure --profile <name>
#aws configure --profile raghav
give your above IAM user access key, secret access key, default region
you can check your details as below:
cd .aws ---> dir ---> check inside config, credentials files
type config
type credentials

Install AWS CLI in AWS Linux EC2:
pip3 install awscli    # for root user
pip3 install awscli --user    # for normal user
aws configure --profile raghav
give your above IAM user access key, secret access key, default region
cd .aws/ ----> ls ----> cat config ---> cat credentials
# 1st Script:
mkdir boto3_scripts && cd boto3_scripts
vim list_iam_users.py    # add the below script
#!/usr/bin/python3
import boto3
aws_mgmnt_console=boto3.session.Session(profile_name="root")
iam_console=aws_mgmnt_console.resource('iam')
for each_user in iam_console.users.all():
    print(each_user.name)
# Escape :wq!    (to save & exit)
python list_iam_users.py

# 2nd script:
cd boto3_scripts
vim list_s3_bkts.py    # add the below script
#!/usr/bin/python3
import boto3
aws_mgmnt_console=boto3.session.Session(profile_name="root")
s3_console=aws_mgmnt_console.resource('s3')
for each_buk in s3_console.buckets.all():
    print(each_buk.name)
# Escape :wq!    (to save & exit)
python list_s3_bkts.py    # (or)
chmod +x list_s3_bkts.py
./list_s3_bkts.py
vbvbs
vfsbgkjs
djfksg
fdhskf
fjkdsfks

Note: sdfgbjldshfkl