Configuring Python 3 on an AWS EC2 Instance running Ubuntu 20.04

Olabode Olugbolagun
4 min readJun 7, 2021

--

Purpose

Your company ABC inc. wants you to begin leveraging using the AWS CLI. First assignment is to configure a programming environment (Python) on an AWS EC2 instance running Ubuntu 20.04 server.

Requirements

  1. AWS console
  2. AWS CLI (download instructions if you do not have AWS CLI installed)

Also, be sure to go through the process of configuring your AWS CLI.

STEPS

  1. Create an IAM User

2. Setup Credentials (Key pair)

3. Create an EC2 instance using the AWS CLI

4. Create a directory called “environment” and create a virtual python environment within it

5. Create a program called hello.py and print file

Create a IAM User

  1. We will use the AWS console to setup your IAM User. Navigate to IAM services and select Users.

2. Add user

3. Provide the user details such as:

  • Username
  • Access type: Select all
  • Password and permissions

All other steps can be skipped and setup at a later time.

Setting up a Key Pair

Still within the AWS console, we need to create a key pair to utilize for when we SSH connect to our EC2 instance.

  1. Navigate to EC2 services and select on Key Pairs
  2. Create key pair

2. Make sure to save the file in a secured location as it will be needed later on.

Establishing an EC2 instance

We need to create an EC2 instance using Ubuntu server 20.04 and will use AWS CLI to do so. We need the instance details and I prefer using the AWS console to obtain the instance details as it’s just much easier than trying to memorize it.

Can be obtained from Step 1 of the instance Launch wizard
  1. To launch a new instance we must run the command

$ aws ec2 run-instances --image-id ami-173d747e --count 1 --instance-type t1.micro --key-name MyKeyPair --security-groups my-sg

Note: Within the AWS console I did update my default security group giving it SSH inbound access

2. To verify if your instance have been created and is running, use the command aws ec2 describe-instance-status

Use a Key Pair to SSH into our instance

Now that we’ve verified our instance is up and running, lets SSH into it. I’ll be using my Windows CMD to SSH. Feel free to use any other connection option of choice.

  1. To connect using your instance’s public DNS name, enter the following command.ssh -i /path/my-key-pair.pem my-instance-user-name@my-instance-public-dns-name

2. Successfully connected!!!

Updates and upgrade all packages on the instance

  1. To stay up to date, let’s update and upgrade all packages for our Ubuntu server before we add our Python program to the environment.

Run these two commands:

sudo apt updatesudo apt upgrade

It may take up to 3–5 minutes for everything to be completed

Installing Python

  1. Before we setup our environment, we have to do some more package installing. This time installing Pip, a tool for installing Python packages, specifically for Python 3.
sudo apt install python3-pip

2. Lets go a step further and install packages and development tools

sudo apt install -y build-essential libssl-dev libffi-dev python3-dev

3. Once installed, verify the installation by checking the pip version.

pip3 --version

Creating Python virtual environment

  1. Installing our virtual environment
sudo apt install -y python3-venv

2. We need to make a directory for the environment

mkdir environment

3. Be sure to change into the environment and from there, create the python virtual environment

python3 -m venv environment

4. Activate the environment

source environment/bin/activate
Here’s my virtual environment example

Create a program file

  1. Lets create a simple “Hello, World!” program using the vim function

2. Print “Hello, World!” in the file and save.

3. Let’s run the program and verify.

Everything has successfully been completed!!!

If you no longer need any of the resources created, go ahead and begin removing it all.

--

--