Package: boto3-extensions
Boto3 ExtensionsOverviewThis module adds more resource files to the Boto3 library and includes some functionality enhancements.Quick StartFirst, install the library:$pipinstallboto3_extensionsFollow the Boto3 docs on setting up your region and credentials (https://github.com/boto/boto3).Then, from a Python interpreter:importboto3importboto3_extensionsr=boto3.resource('cloudtrail',region_name='us-east-1')fortrailinr.trails.all():print(trail.trail_arn)Resource FilesThe following resource files areaddedto the Boto3 library.acmautoscalingcloudfrontcloudtrailcurdatapipelinedirectconnectelasticacheelbelbv2emrgluehealthkinesislambdardsredshiftroute53supportThe following resource files areupdatedin the Boto3 library.dynamodbec2iams3RefreshableAssumeRoleProviderIf 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')fortrailinr.trails.all():print(trail.trail_arn)ARN PatchIt 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.importboto3importboto3_extensionsfromimpimportreloadboto3_extensions.arn_patch_boto3()reload(boto3)r=boto3.resource('rds',region_name='us-east-1')fordbinr.db_instances.all():print(db.arn)Session CachingThere is basic Session caching builtin that simply stores theSessionfor a each assumed Role into a dict, but if you require another caching mechanism you can monkey patch theboto3_extensions.Cacheclass, as long as you supply theCache.get()andCache.set()methods.importboto3importboto3_extensionsfromboto3_extensionsimportConnectionManagerclassMyCache:def__init__(self):self._cache={}defget(self,role_arn,role_session_name):print("inside MyCache.get()")returnself._cache.get((role_arn,role_session_name),None)defset(self,role_arn,role_session_name,session):print("inside MyCache.set()")self._cache[(role_arn,role_session_name)]=sessionboto3_extensions.Cache=MyCacheconnections=ConnectionManager()connections.get_session(role_arn="arn:aws:iam::012345678912:role/test_role",role_session_name="testing")insideMyCache.get()insideMyCache.set()connections.get_session(role_arn="arn:aws:iam::012345678912:role/test_role",role_session_name="testing")insideMyCache.get()In the above we monkey patch theCacheclass and callget_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 runget_session()a second time and as theSessionis now cached we only see a cache get resulting in a cache hit.Getting HelpPlease raise issue ticket inside our Bitbucket repo:https://bitbucket.org/atlassian/boto3_extensions/issuesLicenseCopyright (c) 2023 Atlassian US., Inc.
Apache 2.0 licensed, seeLICENSEfile.DevelopingTo develop this library, you will need to install the following:Python 3.7Poetry (https://poetry.eustace.io/docs/)# Install development environment and setup pre-commit hooks$makesetup