Boto3 is a Python software development kit (SDK) offered by Amazon to developers so that they can easily interact with various services of Amazon Cloud. For example, if a developers want to use Python code to manage AWS resources or configuration of services like EC2 instances, DynamoDB, and others, can use the Boto3 library. It helps automate tasks and build more complex applications using AWS infrastructure. This Python package is easily available to install using the Python package manager called PIP.
Let’s see the steps involved in the installation of Boto3 on Amazon Linux 2023.
1. Access AL2023 Terminal
You may already have access to your Amazon Linux 2023, if not then first connect your Instance from either the Ec2 Dashboard or using the local terminal with SSH. After that run the system update command, given below:
It will not only install the latest available security update but also rebuild the package manager’s cache.
sudo dnf update
2. Check Python3 is available
By default, the Python3 is available on Amazon Linux 2023 to use. Therefore, let’s first confirm whether it is available or not, and then accordingly we proceed. The simple way to confirm is by checking its version.
python3 -V
If you get the output displaying the Python version and other details, it means the Python3 is already on your Amazon Linux. However, in case you get the command not found output then use the commands given below to install it.
sudo dnf install python3 -y
3. Get PIP3 on Amazon Linux 2023
Now, if your system doesn’t have the python3 already then use the given command to get the packages for its installation.
Check the Version
After completing the installation, we can confirm the PIP3 version using:
pip3 -V
To upgrade PIP2 use:
pip3 install pip --upgrade
5. Install Boto3 on Amazon Linux 2023
PIP is the package manager for Python and it will get installed automatically as you set up Python 3, so to get the Boto3 we simply need to use PIP3.
pip3 install boto3
However, if you don’t want to install the library globally but instead in an isolated environment just for the application you are developing then create a Python3 environment.
your_app is the directory of the app for which we are creating an environment.
python3 -m venv your_app/env
To activate the created environment for your current bash session, use:
source ~/your_app/env/bin/activate
After that to install the Boto3, use:
pip3 install pip --upgrade
pip3 install boto3
Now, switch to the Python command line and import the Boto3 library to start developing your application.
For example: You can import it into your Python code and start using it to interact with AWS services, for example, to list all S3 buckets:
import boto3
# Create an S3 client
s3 = boto3.client('s3')
# List all buckets
response = s3.list_buckets()
# Print the bucket names
for bucket in response['Buckets']:
print(bucket['Name'])
If you want to exit the environment then simply type:
deactivate
(optional) However, if Boto3 is installed in a Python environment, to use it, every time developers need to source or activate it in bash, as manually we did above. So, those who want their bash to switch automatically to the created environment, need to add its path in their Bashrc file:
echo "source ${HOME}/your_app/env/bin/activate" >> ${HOME}/.bashrc
Replace your_app with your actual app directory name.
To refresh the Bashrc file use:
source ~/.bashrc
Other Articles: