Jacob Amar
Jacob Amar

Reputation: 73

How to make a default configuration for boto3

I'm trying to create a tool for our helpdesk team to have a minimal interface to restart our production servers in AWS. I'm using the AWS boto3 module and I'm using multiple regions, do I need to write a config file?

How do I tell boto3 where is the config file?

What I tried so far only succeed when I'm telling boto3 to use one region but we are using a few regions in our servers.

What I tried so far was:

ec2 = boto3.resource('ec2',region_name=("eu-west-1a","eu-west-1c"))

and it didn't succeed. What I should do?

Upvotes: 0

Views: 798

Answers (1)

Roelant
Roelant

Reputation: 5119

It is important to separate the concept of region which is something like eu-west-1, us-gov-east-1 and availability zones likes eu-west-1a. The regions indicate a general location, like a country or a state. Within each region there are generally 3 availability zones, which are specified with letters, and indicate geographical different locations. This allows you to guarantee higher availability. Read more on the internet like here.

Regions in python

You can fix this on the python side using the suggestion of @Orest Gulman:

ec2_euwest = boto3.resource('ec2', region_name="eu-west-1") 
ec2_usgoveast1 = boto3.resource('ec2', region_name="us-gov-east-1")

Regions by configuration

But you can also solve it in user\.aws\credentials by specifying

[default]
aws_access_key_id=<yourkeyid>
aws_secret_access_key=<yoursecretkey>

[euwest] 
role_arn=<ifwanted>
source_profile=default 
region=eu-west-1

[usgov]
role_arn=<ifwanted>
source_profile=default 
region=us-gov-east-1

And then connect using different sessions

 session = boto3.session.Session(profile_name='euwest')
 ec2_euwest1 = session.resource('ec2')

Availabilty zones in python

To create an ec2 instance in a specified availability zone, you do this at startup time.

 session = boto3.session.Session(profile_name='euwest')
 ec2_euwest = session.resource('ec2')
 ec2_euwest.create_instances(
     ImageId='ami-00b6a8a2bd28daf19',
     MinCount=1,
     MaxCount=2,
     InstanceType='t2.micro',
     KeyName='ec2-keypair'
     Placement={
        'AvailabilityZone': 'eu-west-1c',
     }
 )

Upvotes: 2

Related Questions