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

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.

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

print(response)

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

How to update Ubuntu 20.04 LTS to the latest packages

For every new installation of Ubuntu 20.04 LTS the first thing that we need to do is to update Ubuntu and its packages.

I would also recommend updating your Ubuntu Operating System once every month to update the security of your Ubuntu 20.04 LTS.

If you are running Ubuntu Server, the only way to do this is via the Terminal.

If you are running Ubuntu Desktop, then you have the option to use the Terminal or the Software Updater application.

Follow the steps below to update your Ubuntu.



Ubuntu update via Terminal

To update Ubuntu via the Terminal, open a terminal by pressing Ctrl + Alt + t on your keyboard.

Then run the command.

sudo apt update

Enter your password when asked. When you type the password it will not show that you are typing it. That is normal, just type your password and press Enter.

Continue reading How to update Ubuntu 20.04 LTS to the latest packages

How to install VirtualBox Guest Additions in Ubuntu 20.04 LTS Desktop

After you have installed Ubuntu 20.04 LTS Desktop in VirtualBox, the first thing that you will notice is that Ubuntu desktop does not occupy the whole VirtualBox window no matter how you resize the window. To solve this we need to install VirtualBox Guest Additions.

Screen resolution without VirtualBox Guest Additions

Once we install the VirtualBox Guest Additions we will have access to the following features in Ubuntu 20.04 LTS Focal Fossa.

  • Shared clipboards – able to do copy & paste from Host computer to Virtual Machine and vice versa.
  • Shared folders – able to share files via a shared folder from Host computer to Virtual Machine and vice versa.
  • Better video support – able to support higher resolution screens such as Full HD. Virtual Machine screen will resize automatically to the size of the VirtualBox window.
  • Seamless windows – able to make Virtual Machine application windows to appear side-by-side with Host Machine application windows like it was part of the Host Machine.
  • Time synchronization – better time synchronization with the host machine.
  • Automated logins – able to do username and password passing from Host Machine to Virtual Machine.
  • Generic host / guest communication channel – able to control and monitor guest execution.

To know more about Guest Additions, you can read VirtualBox’s documentation.

To install VirtualBox Guest Additions in Ubuntu follow the guide below.



Step-by-step instructions to install VirtualBox Guest Additions in Ubuntu 20.04 LTS Desktop

Installation of build-essential package

VirtualBox Guest Additions requires the build-essential package to be first installed in Ubuntu.

To install the build-essential package, open a terminal by typing Ctrl + Alt + t.

Then run the commands below.

sudo apt update

Enter your password when asked.

Continue reading How to install VirtualBox Guest Additions in Ubuntu 20.04 LTS Desktop

How to Install Ubuntu 20.04 LTS Desktop in VirtualBox

Below is a detailed tutorial on how you can install and run the Ubuntu 20.04 LTS (Focal Fossa) Desktop in Oracle VM VirtualBox.

The tutorial below was run on VirtualBox running in Windows 10. But if you are running VirtualBox in Mac or Linux, the instruction should be the same.


Step-by-step instruction on how to run Ubuntu 20.04 LTS (Focal Fossa) Desktop in VirtualBox

Downloading of the Ubuntu 20.04 LTS Installer ISO Disk Image

Go to ubuntu.com/download/desktop.

On Ubuntu 20.04 LTS click Download.

This will then move to a web page saying “Thank you for downloading Ubuntu Desktop”. Wait for a while then download of ubuntu-20.04.x-desktop-amd64.iso will start automatically.

Continue reading How to Install Ubuntu 20.04 LTS Desktop in VirtualBox

How to use Macbook Big Sur with lid closed

If you want to use your Macbook with the lid closed or in clamshell mode, you will need the following.

  • External Keyboard (USB or Bluetooth)
  • External Mouse (USB or Bluetooth)
  • External Monitor

Then follow the steps below to setup your macOS Big Sur to not sleep when you close the monitor of your Macbook.

Continue reading How to use Macbook Big Sur with lid closed

How to install VirtualBox in Windows 10

If you want to install Oracle VM VirtualBox in your Windows 10 computer, follow the steps below.


Steps to install VirtualBox in Windows 10

On your preferred internet browser, go to https://www.virtualbox.org/wiki/Downloads.

This is the downloads page for the latest VirtualBox Installer.

On the screenshots below, I am using Google Chrome as my internet browser.

Under VirtualBox platform packages, click on Windows hosts.

This will automatically start the download of the installer.

Once the download is finished, run the installer.

This will open the Oracle VM VirtualBox Setup window.

Continue reading How to install VirtualBox in Windows 10

How to disable collapsing to one line for single folders in Visual Studio Code

If you have been using Visual Studio Code, you might have encountered that if you have a folder that only has a single folder inside, Visual Studio Code will automatically collapse both parent and child folders into a single line.

Actually, if you have a chain of subfolders that have only one folder inside, it will merge all folders into a single line, as you can see on the image below.

I used to solve this by adding a dummy file or folder just to have a better folder structure, but even that is annoying since there are extra files that you can see that you do not need.

To solve this collapsing of the folder structure to a single line for single parent/child folder, follow the steps below.

Continue reading How to disable collapsing to one line for single folders in Visual Studio Code