Package: boto3-assume
boto3-assumeboto3-assumehas one simple goal. Easily createboto3/aioboto3assume role sessions with automatic credential refreshing.InstallationInstall with pip:$ pip install boto3-assumeIt doesn't come withboto3oraioboto3by default,
but if you want to install them with the package it can be done as extras.$ pip install boto3-assume[aioboto,boto3]TutorialThere are only 2 functionsassume_role_sessionandassume_role_aio_sessionFor boto3:importboto3fromboto3_assumeimportassume_role_sessionassume_session=assume_role_session(source_session=boto3.Session(),# You must pass in a boto3 session that automatically refreshes!RoleArn="arn:aws:iam::123412341234:role/my_role",RoleSessionName="my-role-session")# Create clients, and their credentials will auto-refresh when expired!sts_client=assume_session.client("sts",region_name="us-east-1")print(sts_client.get_caller_identity())# {#     "UserId": "EXAMPLEID",#     "Account": "123412341234",#     "Arn": "arn:aws:sts::123412341234:role/my_role",#     "ResponseMetadata": {#         "RequestId": "asdfqwfqwfasdfasdfasfsdf",#         "HTTPStatusCode": 200,#         "HTTPHeaders": {#             "server": "amazon.com",#             "date": "Tue, 27 Jun 2023 00:00:00 GMT"#         },#         "RetryAttempts": 0#     }# }Foraioboto3:importasyncioimportaioboto3fromboto3_assumeimportassume_role_aio_session# since this uses "Deferred" credentials you don't need to call this within a coroutine or context managerassume_session=assume_role_session(source_session=aioboto3.Session(),# You must pass in an aioboto3 session that automatically refreshes!RoleArn="arn:aws:iam::123412341234:role/my_role",RoleSessionName="my-role-session")asyncdefmain():# Create clients, and their credentials will auto-refresh when expired!asyncwithassume_session.client("sts",region_name="us-east-1")assts_client:print(awaitsts_client.get_caller_identity())# {#     "UserId": "EXAMPLEID",#     "Account": "123412341234",#     "Arn": "arn:aws:sts::123412341234:role/my_role",#     "ResponseMetadata": {#         "RequestId": "asdfqwfqwfasdfasdfasfsdf",#         "HTTPStatusCode": 200,#         "HTTPHeaders": {#             "server": "amazon.com",#             "date": "Tue, 27 Jun 2023 00:00:00 GMT"#         },#         "RetryAttempts": 0#     }# }asyncio.run(main())Under the hood aboto3/aioboto3sts client will be created andassume_rolecalled to get/refresh credentials.If you want you can also specify extra kwargs for thests client, and for theassume_rolecall.NOTE: The "sts" service is already specified for the client.RoleArnandRoleSessionNameare used in the assume role call.importboto3fromboto3_assumeimportassume_role_sessionfrombotocore.configimportConfigassume_session=assume_role_session(source_session=boto3.Session(),# You must pass in a boto3 session that automatically refreshes!RoleArn="arn:aws:iam::123412341234:role/my_role",RoleSessionName="my-role-session",sts_client_kwargs={"region_name":"us-east-1","config":Config(retries={"total_max_attempts":10,"mode":"adaptive"})},assume_role_kwargs={"DurationSeconds":900})DevelopmentInstall the package in editable mode with dev dependencies.(venv) $ pip install -e .[dev,all]noxis used to manage various dev functions.
Start with(venv) $ nox --helppyenvis used to manage python versions.
To run the nox tests for applicable python version you will first need to install them.
In the root project dir run:(venv) $ pyenv installChangelogChangelog forboto3-assume.
All notable changes to this project will be documented in this file.The format is based onKeep a Changelog,
and this project adheres toSemantic Versioning.[0.1.1] - 2023-06-28Fixed- Formatting for Changelog, README[0.1.0] - 2023-06-28Initial Release.