Skip to main content

Boto3 Extensions

Overview

This module adds more resource files to the Boto3 library and includes some functionality enhancements.

Quick Start

First, install the library:

$ pip install boto3_extensions

Follow the Boto3 docs on setting up your region and credentials (https://github.com/boto/boto3).

Then, from a Python interpreter:

import boto3
import boto3_extensions

r = boto3.resource('cloudtrail', region_name='us-east-1')
for trail in r.trails.all():
    print(trail.trail_arn)

Resource Files

The following resource files are added to the Boto3 library.

  • acm
  • autoscaling
  • cloudfront
  • cloudtrail
  • cur
  • datapipeline
  • directconnect
  • elasticache
  • elb
  • elbv2
  • emr
  • glue
  • health
  • kinesis
  • lambda
  • rds
  • redshift
  • route53
  • support

The following resource files are updated in the Boto3 library.

  • dynamodb
  • ec2
  • iam
  • s3

RefreshableAssumeRoleProvider

If your code needs to AssumeRole into another role before performing actions against the AWS API (be it in the same or another AWS account), you run the risk that the credentials you are using expire during their use. You can either add code to your application to constantly check the credential expiry time or using this extension offload the credential refresh to boto3 itself. By using the ConnectionManager in boto3_extensions not only will it automattically assumeRole when the credentials get below 15 mins left, but it will also cache the credentials. This means that if your application is calling boto3 to get credentials for another role more than once the ConnectionManager will cache the first call and then hand out the same session for the subsequent calls.

role_arn = 'arn:aws:iam::1234567890:role/test-role'
role_session_name = 'test'
connections = boto3_extensions.ConnectionManager(region_name='us-east-1')
session = connections.get_session(role_arn=role_arn, role_session_name=role_session_name)

r = session.resource('cloudtrail', region_name='us-east-1')
for trail in r.trails.all():
    print(trail.trail_arn)

ARN Patch

It would be nice to have a consistent way to get the ARN of resources. The ARN patch feature of boto3_extensions allows you to get the arn from resources via an arn attribute.

import boto3
import boto3_extensions
from imp import reload
boto3_extensions.arn_patch_boto3()
reload(boto3)

r = boto3.resource('rds', region_name='us-east-1')
for db in r.db_instances.all():
    print(db.arn)

Session Caching

There is basic Session caching builtin that simply stores the Session for a each assumed Role into a dict, but if you require another caching mechanism you can monkey patch the boto3_extensions.Cache class, as long as you supply the Cache.get() and Cache.set() methods.

import boto3
import boto3_extensions
from boto3_extensions import ConnectionManager

class MyCache:
    def __init__(self):
        self._cache = {}

    def get(self, role_arn, role_session_name):
        print("inside MyCache.get()")
        return self._cache.get((role_arn, role_session_name), None)

    def set(self, role_arn, role_session_name, session):
        print("inside MyCache.set()")
        self._cache[(role_arn, role_session_name)] = session

boto3_extensions.Cache = MyCache
connections = ConnectionManager()

connections.get_session(role_arn="arn:aws:iam::012345678912:role/test_role", role_session_name="testing")
inside MyCache.get()
inside MyCache.set()

connections.get_session(role_arn="arn:aws:iam::012345678912:role/test_role", role_session_name="testing")
inside MyCache.get()

In the above we monkey patch the Cache class and call get_session() twice. The output shows that the first time we have a cache get which results in a cache miss and thena cache set after the credential is retrieved from STS. We then run get_session() a second time and as the Session is now cached we only see a cache get resulting in a cache hit.

Getting Help

Please raise issue ticket inside our Bitbucket repo: https://bitbucket.org/atlassian/boto3_extensions/issues

License

Copyright (c) 2023 Atlassian US., Inc. Apache 2.0 licensed, see LICENSE file.

Developing

To develop this library, you will need to install the following:

# Install development environment and setup pre-commit hooks
$ make setup

With thanks from Atlassian

Metadata

Release files for boto3-extensions 0.23.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-extensions 0.23.0
File Size Uploaded Provenance
boto3_extensions-0.23.0.tar.gz 37.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for boto3-extensions 0.23.0
File Interpreter ABI Platform
boto3_extensions-0.23.0-py3-none-any.whl Python 3 none any Details

Total release size: 96.3 kB

Release files / boto3_extensions-0.23.0.tar.gz

Download URL boto3_extensions-0.23.0.tar.gz
Size 37.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ec029d69ce210221fe9e692c0afa4db11b9505f5c54559b5053fdf67323f42e3
BLAKE2b-256 checksum
How to use checksums
0864a98bfb56b209ffbdc52e02c57e76d786f38397422932c96033234c0b95be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.8.18 Linux/5.10.101

Release files / boto3_extensions-0.23.0-py3-none-any.whl

Download URL boto3_extensions-0.23.0-py3-none-any.whl
Size 58.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
846c44c505e588e451a5f360ce71d5b86545e12881bfcedc7a46ca259e21ad1f
BLAKE2b-256 checksum
How to use checksums
810bbb28edb4d83ad5f647cc81c78e0979fa9a44a19804f167f7df856be7a029
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.8.18 Linux/5.10.101

Security for boto3-extensions 0.23.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.23.0 This release

2 release files No provenance

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