Using Python with AWS: A Beginner’s Guide

Feb 26, 2023 | Python Tips & Tricks

Amazon Web Services (AWS) is one of the most popular cloud computing platforms in the world.

It offers a wide range of services, from computing and storage to databases and machine learning.

In this blog post, we will be exploring how to use Python with AWS to build scalable and reliable cloud-based applications.

Using Python with AWS

1. Installing the AWS SDK for Python (Boto3)

The first step in using AWS with Python is to install the AWS SDK for Python, also known as Boto3.

Boto3 is the Amazon Web Services (AWS) SDK for Python, which allows Python developers to write software that makes use of services like Amazon S3 and Amazon EC2.

To install Boto3, simply run the following command in your terminal or command prompt:

pip install boto3

2. Setting up an AWS Account and Access Keys

In order to use AWS with Python, you will need to have an AWS account and access keys.

The access keys are used to authenticate your Python code with AWS and allow it to access your AWS resources.

To create an AWS account and access keys, follow these steps:

  1. Go to the AWS website (aws.amazon.com) and sign up for an AWS account.
  2. Log in to the AWS Management Console.
  3. Go to the IAM (Identity and Access Management) dashboard.
  4. Click on the “Users” section and then click the “Create New Users” button.
  5. Enter a username and select the option to generate an access key for each user.
  6. Download the CSV file containing your access keys and store it in a secure location.

3. Configuring Boto3 with Your Access Keys

Once you have your access keys, you need to configure Boto3 with your access keys so that it can access your AWS resources.

This can be done by setting the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables, or by using the aws configure command.

Here’s an example of how to set the environment variables in Python:

import os

os.environ['AWS_ACCESS_KEY_ID'] = 'YOUR_ACCESS_KEY_ID'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'YOUR_SECRET_ACCESS_KEY'

Alternately, you can also set up your AWS credentials in your AWS profile.

This can be done by creating an AWS profile and storing it in the ~/.aws/credentials file on your local machine.

Here’s an example of what the file might look like:

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

4. Using Boto3 with AWS Services

With Boto3 installed and configured, you are now ready to start using AWS services with Python.

In this section, we will go over some examples of how to use Boto3 with some of the most popular AWS services.

Example 1: Working with Amazon S3

Amazon S3 is a simple storage service that allows you to store and retrieve data from anywhere on the web.

In this example, we will use Boto3 to upload a file to Amazon S3 and retrieve it again.

import boto3

# Connect to the S3 service
s3 = boto3.resource('s3')

# Upload a file to S3
s3.Bucket('my-bucket').upload_file('file.txt', 'file.txt')

# Download a file from S3
s3.Bucket('my-bucket').download_file('file.txt', 'downloaded_file.txt')

Another way is by using boto3.client

#alternate ways
import boto3

s3 = boto3.client('s3')

# Upload a file to S3
s3.upload_file('file.txt', 'my-bucket', 'file.txt')

# Download a file from S3
s3.download_file('my-bucket', 'file.txt', 'file.txt')

In this code, we first import the boto3 library and create an S3 client. The upload_file method is then used to upload the file ‘file.txt’ to the ‘my-bucket’ S3 bucket.

Similarly, the download_file method is then used to download the file ‘file.txt’ from the ‘my-bucket’ S3 bucket.

Example 2: Working with Amazon EC2

Amazon Elastic Compute Cloud (EC2) is a web service that provides scalable computing capacity in the cloud.
In this section, we will explore how to use Python and AWS EC2 to launch and manage virtual machines.

import boto3

# Connect to EC2
ec2 = boto3.resource('ec2')

# Launch an EC2 instance
instance = ec2.create_instances(
ImageId='ami-0c55b159cbfafe1f0', # Amazon Linux 2 LTS
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
KeyName='my-ec2-key-pair'
)

# Get the instance ID
instance_id = instance[0].id

# Wait for the instance to be running
instance[0].wait_until_running()

# Get the instance object
instance = ec2.Instance(instance_id)

# Run a command on the instance
response = instance.execute_command(
DocumentName='AWS-RunShellScript',
Parameters={'commands': ['echo "Hello from EC2!"']}
)

# Get the output from the command
output = response['StandardOutputContent']

# Print the output
print(output)

# Terminate the instance
instance.terminate()

In this example, we connect to EC2 using the boto3.resource method.

Next, we launch a new EC2 instance using the create_instances method. We specify the Amazon Linux 2 LTS image (ami-0c55b159cbfafe1f0), specify that we want to launch 1 instance (MinCount=1, MaxCount=1), specify the instance type (InstanceType=’t2.micro’), and specify the name of our EC2 key pair (KeyName=’my-ec2-key-pair‘).

Once the instance is launched, we wait for it to be in a running state using the wait_until_running method.

Next, we run a simple command on the instance using the execute_command method. The command that we run is echo “Hello from EC2!“.

Conclusion

In conclusion, Python and AWS is a powerful combination that offers a wide range of capabilities for businesses and individuals looking to use the cloud for their data processing and storage needs.

From the flexibility of AWS Lambda to the scalability of Amazon EC2, AWS provides a wealth of resources for Python developers.

Whether you’re a beginner or an experienced developer, I hope this post provides a solid foundation for using Python and AWS together.

So, if you’re looking to expand your skills and explore new opportunities in the cloud, Python and AWS is a great place to start.

Hope you liked this post and learned something new today 🙂

Do not forget to follow *only* if you are finding any value add with my posts!

0