Set up an Ubuntu-based EC2 Instance with a Virtual Python Environment

How to create an EC2 instance with an Ubuntu 20.04 AMI, then install a Python virtual environment via the AWS CLI.

Jay Van Blaricum
CodeX
5 min readApr 10, 2021

--

Using both the AWS CLI and the AWS console, we will run through the steps necessary to meet seven objectives:

  1. Create a user, download credentials, and set up the AWS CLI.
  2. Create an Ubuntu 20.04 Ec2 instance (t3.small).
  3. Connect and log in to the instance using your keypair.
  4. Refresh updates and upgrade all packages on the instance.
  5. Create a directory called “environment” and create a virtual python environment in that directory.
  6. Activate that virtual environment and confirm the environment is up.
  7. Create a program called hello.py, print “Hello, World!” in that file, exit vim/nano, and run the program.

Step 1: User and CLI Setup

Log in to the AWS Management Console (console), head to the IAM dashboard, and click “Add User.”

Check the box to give the new user programmatic privileges when the option appears in the user setup, and confirm the permissions settings in the user’s Summary tabs.

Create and download a Key Pair for the new user — this credential will be needed to configure the AWS CLI.

If you do not have the AWS CLI installed on your local machine, follow the instructions to do so for your operating system at this user guide page.

Next, to run AWS CLI commands through the new user, you must update the credentials and configuration files for your AWS account in your text editor of choice. But first, create a login profile for the new user:

  • In ~/.aws/credentials, add the new user’s username in brackets, and add the aws_access_key_id and aws_secret_access_key.
  • In ~/.aws/config, add the new user’s profile ID in brackets, and list the source_profile and region.

To use AWS resources as the new user, enter the “export AWS_PROFILE=<new-user>” command.

Step 2: Create an Ubuntu 20.04 EC2 Instance

Back in the AWS console, navigate to the EC2 dashboard and click “Launch Instance.” In this instance, set up step 1, enter “Ubuntu” in the search field to display instance options. Select the Ubuntu Server 20.04 LTS and click “next.”

For instance, type, select “t3.small.” Be sure to select “Enable” public IPv4 address in the configuration step, and design your security group to allow SSH access from the IP range of your choice.

Select or create a key pair for the instance, then click “Launch Instances.”

On the Instances page of the EC2 dashboard, confirm the instance launched successfully and has passed the two status checks.

Step 3: Connect and Login

When the instance is ready, click “Connect” and choose the tab for SSH access. Follow the instructions for logging into your instance via SSH. My input and output looked like this:

Step 4: Update and Upgrade the Ubuntu Server

Update and upgrade the instance and its packages with the Ubuntu commands as shown here:

  • To update, run sudo apt update
  • To upgrade, run sudo apt upgrade (this may take a minute to complete).

Step 5: Environment Directory with Virtual Python Environment

First, to create a new directory, run mkdir environment, then cd into that directory.

Second, install a virtual environment in the new directory using Python’s venv module. The following command should do the trick:

Third, create a path to a directory that will hold the virtual environments and libraries:

Step 6: Activate the Virtual Environment and Confirm

To activate the venv module in your new directory, run:

If activation is successful, you should see your environment name in parens appear in front of your command line, for example:

Step 7: Create and Run “hello.py”

To create a test Python program, use vim, nano, or your text editor of choice to create the file “hello.py.” For example vim hello.py.

In the new Python document, simply enter the Python command “print(‘Hello World!’),” exit the text editor, and run the command:

The output should immediately display ‘Hello World!’

As an example, my CLI history for Steps 5, 6, and 7 looked like this:

Congratulations, you successfully created and ran a Python program in a virtual environment you set up in an Ubuntu EC2 instance!

Thank you for reading!

--

--