-1

I have the following script to list trails from CloudTrail:

import boto3
import os

os.environ['AWS_DEFAULT_REGION'] = 'us-east-2'
current_session = boto3.session.Session(profile_name='production')
client = current_session.client('cloudtrail')
response = client.list_trails()
print(response)

This only gives me the list in us-east-1.

I have tried setting the variable by passing it as an argument to the session and also setting it as env var on command line but it only looks at us-east-1.

Any suggestions?

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
user_mda
  • 18,148
  • 27
  • 82
  • 145

2 Answers2

1

I suspect your profile does not have a region associated to it. For this reason, the session instantiation is using us-east-1 as a default.

To fix this, explicitly specify the region name in the session instantiation:

current_session = boto3.session.Session(profile_name='production', region_name='us-east-2')
Paolo
  • 21,270
  • 6
  • 38
  • 69
0

Define it in session config which has the following:

  • aws_access_key_id - A specific AWS access key ID.
  • aws_secret_access_key - A specific AWS secret access key.
  • region_name
  • The AWS Region where you want to create new connections. profile_name - The profile to use when creating your session.

So just add region_name in your example. See: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/session.html#session-configurations

AlizaminJ
  • 95
  • 1
  • 5