A wrappper for boto3 to help access and test usage of common aws services.
Project description
Skymantle Boto Buddy
A wrapper for boto3 to access common aws serverless services primarily used for aws Lambda. By default the wrapper is dependent on using boto3 configuration through environment variables for setting credentials for accessing aws resources. It's also possible to provide a boto3.Session object for setting credentials.
When used within the context of an aws lambda function, no credentials are required and instances of boto3 resource and clients are created during lambda initialization when importing helpers. The library determines its running in the context of a lambda function but looking for the AWS_LAMBDA_FUNCTION_NAME environment variable.
The boto3 client and resource objects are cached but it is possible to also get uncached instances or cache can be disabled globally by setting the BOTO_BUDDY_DISABLE_CACHE environment variable. Supported values are 1, true, yes and on.
Installation
To install use:
pip3 install skymantle_boto_buddy
Using skymantle_boto_buddy will not include the boto3 dependency, useful when part of a lambda function and the lambda runtime version or a layer is used. To include boto3 use:
pip3 install skymantle_boto_buddy[boto3]
Usage
The library provides the following functions.
- Package
get_boto3_clientget_boto3_resource
- DynamoDb
get_dynamodb_resourceget_tableput_item_simplifiedupdate_item_simplifiedget_itemdelete_itemqueryquery_no_paging
- S3
get_s3_clientget_s3_resourceget_bucketget_object_signed_urlput_object_signed_urlget_objectget_object_bytesget_object_jsonget_object_csv_readerupload_fileobjput_objectdelete_objectdelete_objectscopylist_objects_v2execute_sql_query_simplified
- SSM
get_ssm_clientget_parameterget_parameter_decrypted
- CloudFormation
get_cloudformation_clientdescribe_stacksget_stack_outputs
- STS
get_sts_clientget_caller_identityget_caller_account
- StepFunction
get_stepfunction_clientstart_executionstart_sync_executionstart_with_wait_for_completiondescribe_execution
- Logs
get_logs_client
Examples
- running inside a lambda function or using environment variable credentials
from skymantle_boto_buddy import dynamodb
dynamodb.put_item_simplified("table_name", {"PK": "some_key", "Description": "Some description"})
- providing a Session and specifying a profile named
developer
from boto3 import Session
from skymantle_boto_buddy import dynamodb
session = Session(profile_name="developer")
dynamodb.put_item_simplified("table_name", {"PK": "some_key", "Description": "Some description"}, session=session)
- Get a version of the s3 client that is not cached
from boto3 import Session
from skymantle_boto_buddy import EnableCache, s3
s3_client = get_s3_client(enable_cache=EnableCache.NO)
- unit test a function with patching (also possible to use packages like moto)
# my_file.py
from skymantle_boto_buddy import dynamodb
def some_function():
# ...
item = dynamodb.get_item("table_name", {"PK": "some_key"})
return item
# my_test.py
from unittest.mock import MagicMock
import pytest
from pytest_mock import MockerFixture
import my_file
@pytest.fixture()
def mock_dynamodb(mocker: MockerFixture) -> MagicMock:
mock = mocker.patch("my_file.dynamodb")
return mock
def test_some_function(mock_dynamodb):
mock_dynamodb.get_item.return_value = {"PK": "some_pk", "Name": "some value"}
result = my_file.some_function()
mock_dynamodb.get_item.assert_called_with("table_name", {"PK": "some_key"})
assert result == {"PK": "some_pk", "Name": "some value"}
Source Code Dev Notes
The following project commands are supported:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file skymantle_boto_buddy-0.3.0.tar.gz.
File metadata
- Download URL: skymantle_boto_buddy-0.3.0.tar.gz
- Upload date:
- Size: 18.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1605e6302f3e909242fca91c2e69e32866319ec9f3903ab10e0b4bba0f05471c
|
|
| MD5 |
24d2556ead5ee387e4121ad245ca7a5d
|
|
| BLAKE2b-256 |
97274ab24b2cd4a6412e8e00099059dbfced9bf062b2442cc0250643fb248030
|
File details
Details for the file skymantle_boto_buddy-0.3.0-py3-none-any.whl.
File metadata
- Download URL: skymantle_boto_buddy-0.3.0-py3-none-any.whl
- Upload date:
- Size: 18.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
876be793bcec0d61b64707733706a4112d21d7167f1a5e9372d3448d64522946
|
|
| MD5 |
9a512c688902a80881ba577a24922649
|
|
| BLAKE2b-256 |
c5d90c54fe6c67e84bbf6df7fec139c23341e2364ebb83626eea098084ebdb41
|