A simple utility library providing helper functions for RDS
Project description
RDS Utilities
This is a simple utility library providing some helpful functions for interacting with RDS. Specifically, these functions are useful when utilizing the RDS Data API. The RDS Data API allows you to connect to an RDS instance directly using the AWS SDK without needing any additional database drivers or libraries (such as psycopg2). I found that the syntax for utilizing the RDS Data API within boto3 was not entirely intuitive so I made these wrapper functions to make adoption of this API a little easier. This library is also meant to serve as reference material for my blog post on Python fundamentals and how publishing libraries works. But I hope the functions can be at least nominally useful to some.
Available Functions
Currently, there are four utility functions exposed through this library:
- execute_query
- fetch_query
- format_parameters
- get_xacct_session
Here is what each function does:
execute_query(session, rds_endpoint_arn, secret_arn, database, query, parameters):
Executes a query against the specified RDS instance. All arguments except parameters
are required. It does not matter whether the query returns a result set or not. This
function returns the raw response from boto3's execute_sql function, as this function
is a wrapper for that command. As such, there may be keys named records and
numberOfRecordsUpdated. Note that the top level sqlStatementResults is not present
because we pass in includeResultMetadata=True to the underlying function.
sessionis a instance of a boto3 sessionrds_endpoint_arnis a string identifying the particular RDS clustersecret_arnis a string identifying an AWS Secrets Manager secret containing valid database credentialsdatabaseis the name of the database within the RDS cluster to targetqueryis a string containing the SQL query you wish to executeparametersis an optional dictionary of parameters to pass into the query
For example, if your query is SELECT * FROM employees WHERE employee_id = :id LIMIT 1, you
might set the value of parameters to something like {"id": 1}.
fetch_query(session, rds_endpoint_arn, secret_arn, database, query, parameters):
Similar to the execute_query function, except this function expects a result set
(in other words, this is useful for SELECT queries but not for INSERT or UPDATE queries).
The function also reformats the output into a clean list of rows (instead of the raw boto3 response).
It will collapse nested keys like stringValue or doubleValue in the response and
utilize the column names as dictionary keys on each row.
In other words, if you pass in a query like this: SELECT employee_id, name, title FROM employees LIMIT 3
then you might get a response like this:
[
{"employee_id": 1, "name": "Alice", "title": "Senior Engineer"},
{"employee_id": 2, "name": "Bob", "title": "Junior Engineer"},
{"employee_id": 3, "name": "Sam", "title": "Manager"}
]
format_parameters(parameters): Transforms a dictionary of parameters into the format
which boto3 utilizes when calling its underlying functions. For instance, if you pass in
a value like {"id": 1, "name": "Alice"} the output of this function will be as follows:
[
{
"name": "id",
"value": {"doubleValue": 1}
},
{
"name": "name",
"value": {"stringValue": "Alice"}
}
]
This function is automatically invoked by both execute_query and fetch_query so you
should not need to call it directly. It is exposed nonetheless.
get_xacct_session(session, assume_role_arn): Instantiates a boto3 service within a secondary AWS account using assume-role. This is useful when an application living in one AWS account needs to access an RDS instance living in a separate AWS account. This of course requires that you set up the appropriate IAM roles in both accounts first. Here is an example of how you might use this:
import os
import boto3
from rds_utils import fetch_query, get_xacct_session
session1 = boto3.Session(profile_name="account_a", region_name="us-east-1")
session2 = get_xacct_session(session1, "arn:aws:iam::123456789012:role/account_b_rds_role")
results = fetch_query(
session2,
os.getenv("RDS_ENDPOINT_ARN"),
os.getenv("RDS_SECRET_ARN"),
os.getenv("RDS_DATABASE_NAME"),
"SELECT * FROM employees WHERE employee_id = :id",
{"id": 1},
)
print(results)
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 rds_utils-0.1.0.tar.gz.
File metadata
- Download URL: rds_utils-0.1.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25e97782fdefd891491dc24d3ce8448c03b64bd11451eb6859dde6de6d69b096
|
|
| MD5 |
5984d020e9f1715e45e0e2ee539d0d38
|
|
| BLAKE2b-256 |
10a7b35184e89180557390bc0c68e2dcfa3a7a2bc2c18421222e200d97f383ad
|
File details
Details for the file rds_utils-0.1.0-py3-none-any.whl.
File metadata
- Download URL: rds_utils-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5974db4fe160013d78c323efeba38ea2e4eb4d3c42725e5472f144b1c2740b4
|
|
| MD5 |
0367c66a49da901a69cb5958b82d2a5f
|
|
| BLAKE2b-256 |
37138832ea077bf59fcd98f8664f609e23647a530583a0e8509369c3e2a053ab
|