In this guide, I show you how to install Boto3, the AWS SDK for Python, in Microsoft's free Visual Studio Code (VS Code) on a Windows machine. This will allow you to automate in Amazon's cloud with the most popular scripting language on the planet.

Amazon's cloud is still the undisputed market leader. Python is the most popular programming language (Python just sacked C and Java), and VS Code is most likely the most used free Integrated Development Environment (IDE). Thus, it makes sense to bring all three together. This allows you to manage and automate AWS right from VS Code with an easy-to-learn scripting language with an enormous number of code samples and a lot of support on the web.

Install Python on Windows ^

First, we need to install Python on Windows. You can install Python from the Store, but I prefer the real thing. Download the latest stable release of Python. I don't recommend working with the latest release. Windows is already unstable enough. You don't want to add more experimental software to your computer.

I used the 64-bit Python installer on Windows 11. Be sure select Add Python 3.10 to Path. That way, Windows will always automatically find the Python installation when you execute a Python script.

Install Python on Windows 11

Install Python on Windows 11

You have to reboot after the installation to add PATH. To verify that everything worked, run python -V to display the installed Python version number.

Display Python version number

Display Python version number

Install PIP on Windows ^

Pip is Python's package manager, and we need it to install Boto3. First, download the get-pip.py Python script to your computer from the pip documentation page. Then, install pip with this command:

python get-pip.py

After the installation, you can check which pip version you installed with pip -V.

Install pip on Windows

Install pip on Windows

Install Boto3 on Windows ^

Now you can install Boto3 on Windows with this command:

pip install boto3
Install Boto3 with pip on Windows

Install Boto3 with pip on Windows

If you didn't install the AWS CLI on your Windows machine, you have to add the configuration and the credentials file to your home folder (C:Users\YourUsername). First, create a new folder called .aws and then create two new files, config and credentials.

In the config file, specify the AWS region and the output format. For instance:

[default]
region=us-west-2
output=json

The credentials file looks like this:

[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

You can find the AWS access keys (access key ID and secret access key) after you sign into the AWS Console by clicking Security Credentials in the upper right corner under your user name.

Install VS Code ^

If you haven't installed VS Code yet, you can download it from Microsoft here. I used the 64-bit installer on Windows 11.

By the way, if you don't want Microsoft to harvest your private data, I highly recommend that you disable telemetry altogether. To do so, in VS Code, click File > Preferences > Telemetry Settings and then choose Off as Telemetry Level.

Disable telemetry in Visual Studio Code

Disable telemetry in Visual Studio Code

Next, install Microsoft's Python extension. In VS Code, click in the left sidebar, then Extension, and then search for "Python." You will see many Python extensions, but the first one should be the one from Microsoft. That's the one we need here.

Install the Python extension in Visual Studio Code

Install the Python extension in Visual Studio Code

Testing Boto3 ^

Now, you are ready to run your first Python script using Boto3. The sample Python script below lists all of your EC2 instances in AWS.

import boto3
ec2 = boto3.resource('ec2')
for instance in ec2.instances.all():
     print(
         "Id: {0}\nPlatform: {1}\nType: {2}\nPublic IPv4: {3}\nAMI: {4}\nState: {5}\n".format(
         instance.id, instance.platform, instance.instance_type, instance.public_ip_address, instance.image.id, instance.state
         )
     )

Create a new file in VS Code and save it using the file extension .py. Then, you can run the Python script by simply clicking the debug button icon in the upper left corner of VS Code. VS Code will display the output in the Terminal window.

Subscribe to 4sysops newsletter!

Running a Python script with Boto3 in Visual Studio Code

Running a Python script with Boto3 in Visual Studio Code

Congrats, you are now ready to become a DevOps engineer. 😉

0 Comments

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account