When working to programmatically check or configure your AWS infrastructure in Python, we need to use Boto3. Most of the time we will need to check the output of the Boto3 Client by printing it to the terminal. Unfortunately, printing directly the boto3 response is not really easy to understand.
The best way to work with the boto3 response is to convert it to a JSON string before printing it to the terminal.
- Printing Boto3 Client response
- Converting Boto3 Response to JSON String
- Fixing TypeError: Object of type datetime is not JSON serializable
- Advantage of printing JSON Strings in Lambda functions and CloudWatch Logs
Printing Boto3 Client response
To see how we can output boto3 client’s response to the terminal we will initially use the describe_regions function of the boto3 EC2 client.
See the Python script below.
import boto3
client = boto3.client('ec2')
response = client.describe_regions(RegionNames=['us-east-1'])
print(response)
Running the Python script will result in the long single line output below.
{'Regions': [{'Endpoint': 'ec2.us-east-1.amazonaws.com', 'RegionName': 'us-east-1', 'OptInStatus': 'opt-in-not-required'}], 'ResponseMetadata': {'RequestId': '9437271e-6132-468f-b19d-535f9d7bda09', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '9437271e-6132-468f-b19d-535f9d7bda09', 'cache-control': 'no-cache, no-store', 'strict-transport-security': 'max-age=31536000; includeSubDomains', 'content-type': 'text/xml;charset=UTF-8', 'content-length': '449', 'date': 'Sat, 03 Apr 2021 08:30:15 GMT', 'server': 'AmazonEC2'}, 'RetryAttempts': 0}}
In my screen it will look like this since it automatically wraps the output.

With the output above, it is hard to understand or search for the specific part of the response that you are looking for.
Note: The response object of the boto3 client is a dictionary (dict
) type. The examples below will work even if you change response object to any dictionary object.
Converting Boto3 Response to JSON String
To easier understand the returned response of the boto3 client it is best to convert it to a JSON string before printing it. To change the response to a JSON string use the function json.dumps
.