- Poll: How reliable are ChatGPT and Bing Chat? - Tue, May 23 2023
- Pip install Boto3 - Thu, Mar 24 2022
- Install Boto3 (AWS SDK for Python) in Visual Studio Code (VS Code) on Windows - Wed, Feb 23 2022
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
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.
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.
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.