Skip to main content

Easily create aioboto3 assume role sessions with automatic credential refreshing.

Project description

aioboto3-assume

Easily create aioboto3 assume role sessions with automatic credential refreshing.

NOTE - For boto3 support, see boto3-assume.

Installation

Install with pip:

$ pip install aioboto3-assume

Tutorial

import aioboto3
from aioboto3_assume import assume_role

assume_session = assume_role(
    source_session=aioboto3.Session(), # You must pass in an aioboto3 session that automatically refreshes!
    assume_role_kwargs={
        "RoleArn": "arn:aws:iam::123412341234:role/my_role",
        "RoleSessionName": "my-role-session"
    }
)


async def main():
    # Create clients, and their credentials will auto-refresh when expired!
    async with assume_session.client("sts", region_name="us-east-1") as sts_client:
        print(await 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
        #     }
        # }

asyncio.run(main())

Under the hood an aioboto3 sts client will be created and assume_role called to get/refresh credentials.

You can pass the kwargs parameters as so:

  • assume_role_kwargs - Keyword arguments to pass when calling assume_role with a boto3 STS client.
    • Must at least provide RoleArn and RoleSessionName as outlined in the boto3 docs.
  • sts_client_kwargs - Kwargs to pass when creating the boto3 low level client for STS
    • By default only the service argument will be passed as "sts".
    • Note that you should not pass in the service_name or credentials here.
  • target_session_kwargs - Keyword arguments to pass when creating a the new target boto3 Session
    • By default no arguments are passed.
    • Note that you should only pass in region_name or aws_account_id or other variables that will not effect credentials or credential refreshing.

A more complex example:

import aioboto3
from aioboto3_assume import assume_role
from botocore.config import Config

assume_session = assume_role(
    source_session=aioboto3.Session(), 
    assume_role_kwargs={
        "RoleArn": "arn:aws:iam::123412341234:role/my_role",
        "RoleSessionName": "my-role-session",
        "DurationSeconds": 900,
        "Tags": [
            {
                "Key": "MyKey",
                "Value": "MyValue"
            }
        ]
    },
    sts_client_kwargs={
        "config": Config(
            retries={
                "total_max_attempts": 10,
                "mode": "adaptive"
            }
        )
    },
    target_session_kwargs={
        "region_name": "us-east-1"
    }
)

Development

Install the package in editable mode with dev dependencies.

(venv) $ pip install -e .[dev]

nox is used to manage various dev functions. Start with

(venv) $ nox --help

pyenv is 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 install

Changelog

Changelog for aioboto3-assume. All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[0.1.1] - 2026-01-14

Fixed

  • type hints and docstrings on assume_role
  • README formatting

[0.1.0] - 2026-01-13

Initial Release. Split from boto3-assume package.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioboto3_assume-0.1.1rc1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aioboto3_assume-0.1.1rc1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file aioboto3_assume-0.1.1rc1.tar.gz.

File metadata

  • Download URL: aioboto3_assume-0.1.1rc1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for aioboto3_assume-0.1.1rc1.tar.gz
Algorithm Hash digest
SHA256 01936c9aa65775479469a196afc2b2e64beab2d612c169b90b36d19281c4f450
MD5 589038ea07978c95756a3a8a06f80f06
BLAKE2b-256 08d5737eacd0f38f82cd6d76ab7f1e1028a2ff229616b923ee646c30bf814746

See more details on using hashes here.

File details

Details for the file aioboto3_assume-0.1.1rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for aioboto3_assume-0.1.1rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 5c8568ef79b505416f0d1d32a3c5cb8d711382b3f4aef30299ebaaf055502451
MD5 11f4fe4771afb632368c7963c903f91f
BLAKE2b-256 e5cd63dd6b75f41e151d25e88bbf8a193f81912c69d77f29c6066ecb68a02e5a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page