In my previous post, I explained how to install Boto3 with VS Code on Windows. Even though all components involved are cross-platform, the installation of the AWS SDK with VS Code on macOS is a bit different.

Install Python 3 ^

Macs come preinstalled with Python. However, it is the old Python 2.7, which does not work with VS Code. Thus, we need to install Python 3 before we can install Boto3. First, download either the Intel installer if you have an Intel Mac or the universal installer for an M1 Mac.

Pip install Boto3 ^

Python 3 on macOS also installs pip3, so we don't have to install that. However, instead of pip install boto3 we have to use the command below because Python 2.7 is still installed with the old pip.

pip3 install boto3
Pip Install Boto3 on macOS

Pip Install Boto3 on macOS

If you didn't install the AWS CLI on your Mac, you have to add the configuration files to your home directory in the .aws folder:

mkdir ~/.aws

Then, create the config file where you specify the AWS region you want to work with and the output format the AWS CLI uses.

[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 get your 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 on macOS ^

Download VS Code from Microsoft and unzip the file. There is no installer; you can just move the VS Code app to your Applications folder and then launch VS Code.

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

Disable telemetry in VS Code

Disable telemetry in VS Code

Install the Microsoft Python extension ^

Next, you have to install the Python extension for VS Code. The extension enables you to easily run and debug Python in VS Code. Click the Extension icon on the left sidebar and then search for Python. The first hit should be the Microsoft extension, which is the one that we need.

Install the Microsoft Python extension for Visual Studio Code

Install the Microsoft Python extension for Visual Studio Code

Testing Boto3 ^

To test whether your Boto3 installation in VS Code works, you can run the code below, which lists all 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 with the file extension .py. Then, click the debug icon in the upper right corner of VS Code to run the Python script.

Subscribe to 4sysops newsletter!

Testing Boto3 in Visual Studio Code on macOS

Testing Boto3 in Visual Studio Code on macOS

avatar
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