Skip to content

awslabs/boto-formatter

boto_formatter

What is boto _formatter?

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.

How it works?

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()

Installation

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

Check Addtional examples here

Usage

Please click on each function to see the usage/example

Click on service to see the usage

Service Functions
General usage General usage
accessanalyzer
  • 1.list_analyzers
  • 2.list_findings
apigateway
  • 3.get_rest_apis
budgets
  • 4.describe_budgets
cloudfront
  • 5.list_distributions
  • 6.list_functions
cloudtrail
  • 7.list_trails
cloudwatch
  • 8.list_dashboards
  • 9.list_metrics
codecommit
  • 10.list_repositories
dynamodb
  • 11.list_tables
ec2
  • 12.describe_addresses
  • 13.describe_flow_logs
  • 14.describe_instances
  • 15.describe_network_acls
  • 16.describe_route_tables
  • 17.describe_security_groups
  • 18.describe_security_group_rules
  • 19.describe_snapshots
  • 20.describe_subnets
  • 21.describe_transit_gateways
  • 22.describe_volumes
  • 23.describe_vpcs
  • 24.describe_vpc_endpoints
  • 25.describe_vpc_peering_connections
  • 26.describe_vpn_connections
ecs
  • 27.list_clusters
  • 28.list_services
  • 29.list_tasks
efs
  • 30.describe_file_systems
eks
  • 31.describe_cluster
  • 32.list_clusters
  • 33.list_fargate_profiles
elasticache
  • 34.describe_cache_clusters
elbv2
  • 35.describe_load_balancers
emr-serverless
  • 36.list_applications
  • 37.list_job_runs
emr
  • 38.list_clusters
  • 39.list_instance_fleets
  • 40.list_notebook_executions
  • 41.list_studios
iam
  • 42.list_users
  • 43.list_access_keys
  • 44.list_account_aliases
  • 45.list_attached_group_policies
  • 46.list_attached_role_policies
  • 47.list_attached_user_policies
  • 48.list_group_policies
  • 49.list_groups
  • 50.list_policies
  • 51.list_roles
kms
  • 52.list_keys
lambda
  • 53.list_functions
  • 54.list_layers
organizations
  • 55.list_accounts
  • 56.list_policies
rds
  • 57.describe_db_clusters
  • 58.describe_db_instances
  • 59.describe_db_security_groups
  • 60.describe_db_snapshots
  • 61.describe_global_clusters
redshift-serverless
  • 62.list_namespaces
  • 63.list_workgroups
redshift
  • 64.describe_clusters
route53
  • 65.list_cidr_blocks
  • 66.list_hosted_zones
  • 67.list_hosted_zones_by_vpc
  • 68.list_vpc_association_authorizations
route53domains
  • 69.list_domains
  • 70.list_prices
s3
  • 71.create_bucket
  • 72.list_buckets
  • 73.list_multipart_uploads
  • 74.list_objects_v2
sns
  • 75.list_subscriptions
  • 76.list_topics
sqs
  • 77.list_queues

License

This library is licensed under the MIT-0 License. See the LICENSE file.

Considerations

  • 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.