nagurameer
nagurameer

Reputation: 58

How to specify aws region within boto program?

I want to list the ec2 instances in aws account with boto module.I am getting the issue as

"You must specify a region".Here is the program.

import boto3
ec2 = boto3.resource('ec2')
for instance in ec2.instances.all()
print instance.id, instance.state

I didn't specify any default region. How can I specify it programmatically ?

Upvotes: 4

Views: 10006

Answers (2)

John Hanley
John Hanley

Reputation: 81454

Since you are using the resource interface, your code will look like this:

import boto3
ec2 = boto3.resource('ec2', region_name = 'us-west-2')
for instance in ec2.instances.all()
    print instance.id, instance.state

Upvotes: 12

vishal
vishal

Reputation: 1874

This is how your code would look like using client instead of resource

 import boto3
 ec2 = boto3.client('ec2',region_name='us-west-1')
 a = ec2.describe_instances()
 for i in a['Reservations']:
    for j in i['Instances']:
       print "state of the instance"+j['InstanceId']+" is:"+j['State']['Name']

This is just one of the methods.There are a lot more methods.Try it yourself

Upvotes: 7

Related Questions