Package: boto-session-manager
Welcome toboto_session_managerDocumentationAboutboto_session_managerboto_session_manageris a light weight, zero dependency python library that simplify managing your AWS boto3 session in your application code. It bring auto complete and type hint to the defaultboto3SDK, and provide smooth development experience with the following goodies:boto3 Client auto completeCached boto3 ClientAssume IAM role in application codeSet temporary credential for AWS CliAdditionally, if you useboto3-stubsand you didpip install"boto3-stubs[all]", thenboto_session_managercomes with the auto complete and type hint for all boto3 methods out-of-the-box, without any extra configuration (such asexplicit type annotations)FeatureBoto Client Auto CompleteProvide an Enum class to access the aws service name to create boto client.fromboto_session_managerimportBotoSesManager,AwsServiceEnumbsm=BotoSesManager()s3_client=bsm.s3_clientOne click to jump to the documentation:Client method auto complete:Arguments type hint:Note: you have to dopip install"boto3-stubs[all]"to enable “Client method auto complete” and “Arguments type hint” features.Cached ClientOnce an boto session is defined, each AWS Service client should be created only once in most of the case.boto_session_manager.BotoSesManager.get_client(service_name)allow you to fetch the client object from cache if possible.fromboto_session_managerimportBotoSesManager,AwsServiceEnumbsm=BotoSesManager()s3_client1=bsm.get_client(AwsServiceEnum.S3)s3_client2=bsm.get_client(AwsServiceEnum.S3)assertid(s3_client1)=id(s3_client2)Or you can just do:bsm.s3_client.list_buckets()# it cache the client when neededAssume RoleCreate another boto session manager based on an assumed IAM role. Allow you to check if it is expired and maybe renew later.bsm_assumed=bsm.assume_role("arn:aws:iam::111122223333:role/your-assume-role-name")sts_client=bsm_assumed.get_client(AwsServiceEnum.sts)print(sts_client.get_caller_identity())print(bsm_assumed.is_expired())From1.5.1, it adds support for auto-refreshable assumed role (Beta). Note that it is usingAssumeRoleCredentialFetcherandDeferredRefreshableCredentialsfrom botocore, which is not public API officially supported by botocore. This API may be unstable.bsm_assumed=bsm.assume_role("arn:aws:iam::111122223333:role/your-assume-role-name",duration_seconds=900,auto_refresh=True,)# even though the duration seconds is only 15 minutes,# but it can keep running for 1 hour.tick=60sleep=60foriinrange(tick):time.sleep(sleep)print("elapsed{}seconds".format((i+1)*sleep))print("Account id ={}".format(bsm_new.sts_client.get_caller_identity()["Account"]))AWS CLI context managerYou explicitly defined a boto session manager that is not the same as the default one used by your AWS CLI. Theboto_session_manager.BotoSesManager.awscli()context manager can temporarily set your default AWS CLI credential as the same as the one you defined, and automatically revert it back.# explicitly define a boto session managerbsm=BotoSesManager(profile_name="my_aws_profile",)withbsm.awscli():# now the default AWS CLI credential is the same as the ``bsm`` you definedHere’s a more detailed example:importosfromboto_session_managerimportBotoSesManagerdefprint_default_aws_cli_credential():print("AWS_ACCESS_KEY_ID =",os.environ.get("AWS_ACCESS_KEY_ID"))print("AWS_SECRET_ACCESS_KEY =",os.environ.get("AWS_SECRET_ACCESS_KEY"))print("AWS_SESSION_TOKEN =",os.environ.get("AWS_SESSION_TOKEN"))print("AWS_REGION =",os.environ.get("AWS_REGION"))print("--- before ---")print_default_aws_cli_credential()bsm=BotoSesManager(profile_name="aws_data_lab_open_source_us_east_1")withbsm.awscli():print("--- within awscli() context manager ---")print_default_aws_cli_credential()print("--- after ---")print_default_aws_cli_credential()# --- before ---# AWS_ACCESS_KEY_ID = None# AWS_SECRET_ACCESS_KEY = None# AWS_SESSION_TOKEN = None# AWS_REGION = None# --- within awscli() context manager ---# AWS_ACCESS_KEY_ID = ABCDEFG...# AWS_SECRET_ACCESS_KEY = ABCDEFG...# AWS_SESSION_TOKEN = ABCDEFG...# AWS_REGION = us-east-1# --- after ---# AWS_ACCESS_KEY_ID = None# AWS_SECRET_ACCESS_KEY = None# AWS_SESSION_TOKEN = None# AWS_REGION = NoneInstallboto_session_manageris released on PyPI, so all you need is:$pipinstallboto_session_managerTo upgrade to latest version:$pipinstall--upgradeboto_session_manager