How to use boto3 on multiple AWS accounts
Post date: Jun 30, 2018 12:31:55 AM
In summary, you can specify the account by using the proper credentials (access_id and secret_access_key) in Session().
Read this article on how boto3 look for your credentials. You can do it through method parameters:
session = boto3.Session(
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN
)
or you can specify the access in the profile file (~/.aws/credentials)
[default]
aws_access_key_id=<something>
aws_secret_access_key=<something>
[my_sandbox]
aws_access_key_id=<something>
aws_secret_access_key=<something>
[my_sandbox_2]
aws_access_key_id=<something>
aws_secret_access_key=<something>
then you can specify "profile_name" in your code like this:
import boto3
...
session = boto3.session.Session(profile_name="my_sandbox")
lambda_func = session.client('lambda',region_name="ap-southeast-1")
...
You can find region name (e.g. "ap-southeast-1") from this page.