Multi AWS Regions and Multi AWS Accounts Deployment

Initially, a project is developed in one region and in one AWS account. However, we also plan to deploy this solution to other AWS regions and accounts in the future. To enable this, we have created a Python module named boto_ses.py, which defines a variable bsm (short for “boto session manager”) that uses the us-east-1 region and the AWS CLI profile of the POC AWS account. All of the deployment scripts import this module and use bsm for deployment. This allows us to easily deploy the solution to any new region and account by simply changing the profile_name and region_name arguments.

# -*- coding: utf-8 -*-

import pynamodb_mate as pm
from s3pathlib import context
from boto_session_manager import BotoSesManager

from .runtime import IS_LOCAL, IS_CI, IS_LAMBDA

# environment aware boto session manager
if IS_LAMBDA:  # put production first
    bsm = BotoSesManager(
        region_name="us-east-1",
    )
elif IS_LOCAL:
    bsm = BotoSesManager(
        profile_name="my_aws_profile",
        region_name="us-east-1",
    )
elif IS_CI:
    bsm = BotoSesManager(
        region_name="us-east-1",
    )
else:  # pragma: no cover
    raise NotImplementedError

# Set default s3pathlib boto session
context.attach_boto_session(boto_ses=bsm.boto_ses)

# Set default pynamodb boto session
with bsm.awscli():
    pynamodb_connection = pm.Connection()