How to connect to AWS by boto3

How to connect to AWS by boto3

Boto3 is an API used to communicate with AWS Cloud to manage services like EC2 or S3.
This is AWS SDK for python which allows to create software for managing AWS.

1. Preparation

Boto3 is using credentials to authenticate with AWS.
Credentials include items such as aws_access_key_id, aws_secret_access_key, and aws_session_token. aws_ssession_token is optional.

aws_access_key_id and aws_secret_key can be generated in IAM -> Users -> <your user name>.

Boto3 is looking for credentials throuht a list of possible locatins and stop as soon as it finds credentials.

The order is as follow:

  1. Passing credentials as parameters in the boto.client() method
  2. Passing credentials as parameters when creating a Session object
  3. Environment variables
  4. Shared credential file (~/.aws/credentials)
  5. AWS config file (~/.aws/config)
  6. Assume Role provider
  7. Boto2 config file (/etc/boto.cfg and ~/.boto)
  8. Instance metadata service on an Amazon EC2 instance that has an IAM role configured.

For example set variables in : ~/.aws/credentials: 

[default]
aws_access_key_id = FKIAAJ2NTK42RD67GWQS
aws_secret_access_key = /+VAgUhGbftkye+rNdCf9S48lV3ILbB7eQ8QUPsK

and ~/.aws/credentials:

[default]
region = us-west-2

or set variables in ~/.bash_profile:

export AWS_ACCESS_KEY_ID="FKIAHJ2NTK42RD67GWQS"
export AWS_SECRET_ACCESS_KEY="/+VAgUhGbftkye+rNdCf9S98lV3ILbB7eQ8QUPsK"
export AWS_REGION="us-west-2"

2. Getting all instances from all regions

Now using boto3 in Python we can list all instances in all regions from our AWS account:

import boto3

session = boto3.Session()
regions = session.get_available_regions('ec2')

for region in regions:
print(f'Region: {region}')
ec2client = session.client('ec2', region)
ec2instances = ec2client.describe_instances(Filters=[ \
{ 'Name': 'instance-state-name', 'Values': [ 'running' ] } ])
for reservation in ec2instances['Reservations']:
for instance in reservation['Instances']:
print(f"Instance: {instance['InstanceId']}")

print(f'\n')

Output:

/usr/local/bin/python3.7 /Users/user/PYPROJECTS/BOTO/ec2.py
Region: ap-northeast-1
Instance: i-0dda53f3087f816f3
Instance: i-03c83687d063a318c
Instance: i-0c098bc7562636f63
Instance: i-0c19acd2fb7419a03
Instance: i-02513179d6b79011a
Instance: i-09a921a07569e6670

Region: ap-northeast-2

Region: ap-south-1
Instance: i-0999f94a19da500c5
Instance: i-0ea8e9a110625f042
Instance: i-0d66b7bd12dab2bdd
Instance: i-0995a801888d04b5b

...

Leave a Reply