Skip to main content

Boto3 MFA Cache

This package patches boto3 to use the AWS CLI credential cache. This uses the same cache as the AWS CLI, which is stored in ~/.aws/cli/cache. This allows you to use MFA with boto3 without having to enter your credentials every time. The environment variable AWS_CREDENTIAL_CACHE can be set to a different path if ~/.aws/cli/cache is not what you want to use.

This pull is the primary reference for this package: https://github.com/boto/botocore/pull/1338

Best Practices

This is designed for use with role-assumption based MFA. That's you if your ~/.aws/config looks something like this:

[default]
role_arn = arn:aws:iam::123456789012:role/MyRole
source_profile = base
region = us-east-2
mfa_serial = arn:aws:iam::123456789012:mfa/my-device
duration_seconds = 43200

This project is only useful on development environments that require MFA. Servers and other automated resources should not require MFA, and thus should not require this package.

python -m pip install boto3-mfa-cache

Methodology

The patch works by loading a .pth file that imports a monkeypatch replacing boto3.setup_default_session with a patched version. The patched version applies the JSONFileCache from botocore (also used by the CLI) to the default credential provider.

import os
import boto3
import botocore.session
from botocore.utils import JSONFileCache

def _setup_default_session_patch(**kwargs):
    cli_cache_path = os.environ.get(
        "AWS_CREDENTIAL_CACHE",
        os.path.join(os.path.expanduser("~"), ".aws", "cli", "cache")
    )
    os.makedirs(cli_cache_path, exist_ok=True)
    botocore_session = botocore.session.Session()
    file_cache = JSONFileCache(cli_cache_path)
    botocore_session.get_component("credential_provider").get_provider("assume-role").cache = file_cache
    boto3.DEFAULT_SESSION = boto3.Session(botocore_session=botocore_session, **kwargs)

boto3.setup_default_session = _setup_default_session_patch

Release files for boto3-mfa-cache 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for boto3-mfa-cache 1.0.0
File Size Uploaded
boto3_mfa_cache-1.0.0.tar.gz 3.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for boto3-mfa-cache 1.0.0
File Interpreter ABI Platform
boto3_mfa_cache-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.7 kB

Release files / boto3_mfa_cache-1.0.0.tar.gz

Download URL boto3_mfa_cache-1.0.0.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1e82d5a38a05e4b94a314c12e16991f3ade365e010d1fe1ed35f465ac2b4335d
BLAKE2b-256 checksum
How to use checksums
c0710f9c1988ac29d033b8498b4b91cd1b1fcc15ae946041c453462350fd6419
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / boto3_mfa_cache-1.0.0-py3-none-any.whl

Download URL boto3_mfa_cache-1.0.0-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
18157c1becbadc86aa8a49eaaccef6cb61016861050e018c1d836c3a3d4035b1
BLAKE2b-256 checksum
How to use checksums
5349d2d409a4e3ca7e39ad30d9f53a999c79ac0686a96076a0dfb15946271ba7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page