boto_response_formatter is decorator that convert standard boto3 function response (returned as python list) in flattened JSON or tabular CSV formats for [list of supported services and functions] (https://github.com/awslabs/boto-formatter/blob/main/docs/supported_services.md). You can output the response to print, file or send flattened columnar JSON list to another function to continue your process.
boto_formatter simplifies the process and reduce the need of writing custom codebase potentially of 100s of line of code to 4-5 lines of code for simple use cases like generating list of lambda functions or list of cloudfront distriubtions with all the attributes that AWS Python SDK provides.
You simply add decorator to your python function (The function which is returning list from boto3 function) and it will convert the boto3 return list to flatten JSON or comma separate values (CSV).
By adding decorator @boto_response_formatter to a function as example shown below in list_policies_fmt() function the response of the function will be converted to .csv . Generated csv response will also be saved in a file iam_list_polices_.csv in a output folder located in the same directory of invoking python script. You can also notice the difference in lines of code when using boto_formatter and without boto_formatter to achieve the same result of parsing and flattening the json response.
import boto3
from boto_formatter.core_formatter import boto_response_formatter
# With boto_formatter
@boto_response_formatter(service_name="iam", function_name="list_policies", format_type="csv", output_to="file" ,pagination="yes")
def list_policies_fmt():
client = boto3.client('iam')
paginator = client.get_paginator('list_policies')
result = []
for page in paginator.paginate():
result.append(page)
return result
# Without boto_formatter
def list_policies_without_boto_formatter():
client = boto3.client('iam')
paginator = client.get_paginator('list_policies')
result = []
for page in paginator.paginate():
for role in page['Policies']:
json_obj = {}
json_obj['PolicyName'] = role['PolicyName']
json_obj['PolicyId'] = role['PolicyId']
json_obj['Arn'] = role['Arn']
json_obj['Path']=str(role['Path'])
json_obj['DefaultVersionId'] = str(role['DefaultVersionId'])
json_obj['AttachmentCount'] = str(role['AttachmentCount'])
json_obj['PermissionsBoundaryUsageCount'] = str(role['PermissionsBoundaryUsageCount'])
json_obj['IsAttachable'] = str(role['IsAttachable'])
if "Description" in role.keys():
json_obj['Description'] = str(role['Description'])
json_obj['CreateDate'] = str(role['CreateDate'])
json_obj['UpdateDate'] = str(role['UpdateDate'])
if "Tags" in role.keys():
json_obj['Tags_Key'] = role['Tags']['Key']
json_obj['Tags_Value'] = role['Tags']['Value']
result.append(json_obj)
return result
list_policies_fmt()
list_policies_without_boto_formatter()
boto-formatter is distributed on PyPI. Easiest way to install it is with pip
Create a virtual environment (optional):
python3 -m venv .venv
Activate enviornment:
source .venv/bin/activate
Install boto-formatter and boto3:
pip install boto-formatter
pip install boto3
Run boto-formatter code:
import boto3
from boto_formatter.core_formatter import boto_response_formatter
# With boto_formatter
@boto_response_formatter(service_name="iam", function_name="list_policies", format_type="csv", output_to="file" ,pagination="yes")
def list_policies_fmt():
client = boto3.client('iam')
paginator = client.get_paginator('list_policies')
result = []
for page in paginator.paginate():
result.append(page)
return result
For building Installation from source code click here
Please click on each function to see the usage/example
Click on service to see the usage
Service | Functions |
---|---|
General usage | General usage |
accessanalyzer |
|
apigateway |
|
budgets |
|
cloudfront |
|
cloudtrail |
|
cloudwatch |
|
codecommit |
|
dynamodb |
|
ec2 |
|
ecs |
|
efs |
|
eks |
|
elasticache |
|
elbv2 |
|
emr-serverless |
|
emr |
|
iam |
|
kms |
|
lambda |
|
organizations |
|
rds |
|
redshift-serverless |
|
redshift |
|
route53 |
|
route53domains |
|
s3 |
|
sns |
|
sqs |
|
This library is licensed under the MIT-0 License. See the LICENSE file.
- When the format_type is selected as "csv" ;boto_formatter will skip the columns which contains "," in value.
- Majority of the cases Library returns formatted response of all the attributes that Python SDK provides. However, it doesn't assure 100 % coverage of all the attributes that Python SDK provides.
- Library is not designed for latency-based requirements. So, if you have high latency requirements, please evaluate library in lower enviornments (dev,QA) before using in high latency-based environment.