Boto3 Response to JSON String in Python

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

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'])


Running the Python script will result in the long single line output below.

{'Regions': [{'Endpoint': '', '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.

Continue reading Boto3 Response to JSON String in Python

Listing AWS Regions using boto3 Python

If you want to programmatically retrieve the AWS regions one of the best ways to do this is via Python boto3 package.

In boto3, specifically the EC2 client, there is a function named describe_regions. This will retrieve the regions that are in AWS.

See different uses of the describe_regions function below.

Basic code for retrieving the AWS Regions using boto3

Here is a basic code for getting the AWS Regions in boto3.

import boto3
ec2_client = boto3.client('ec2')

response = ec2_client.describe_regions()


response is a dictionary that contains all active regions in the AWS Account.

The output of the Python script above can be seen below. I decided to format the output for easier reading, and show the whole response to easily understand how to manipulate the output.

Continue reading Listing AWS Regions using boto3 Python

How to run Python3 Virtualenv in PowerShell

Below is a step-by-step instruction on how to run Python3 Virtualenv in PowerShell.

The tutorial below assumes that you have already installed python3 and pip3 in your windows computer.

Installation of virtualenv in Python3

In PowerShell, install virtualenv using python pip.

pip3 install virtualenv

Create a virtual environment

virtualenv venv

Error when activating newly created virtual environment

To activate the the Virtual Environment that you have created, you will need to run the command activate.ps1 script inside the Scripts folder. If this is your first time activating virtualenv it will lead to the UnathorizedAccess error File activate.ps1 cannot be loaded because running scripts is disabled on this system.

Continue reading How to run Python3 Virtualenv in PowerShell