Boman Avong
7 min readJul 4, 2021

--

image credit: Kwame Sarpong

Install Python 3 and Configure a Programming Environment on an AWS EC2 Instance Running Ubuntu 20.04 server.

With AWS, available services allow us to build architectures that are scalable, resilient, and highly available and managed by configuring automated environments using powerful tools like python. In this project we will be covering how to configure a programming environment on an AWS EC2 instance running Ubuntu 20.04 server.

Objectives:

1. Create a user (under your root/main account), download credentials (be sure to save the credential file), and set up the AWS CLI

2. Create an Ubuntu 20.04 Ec2 instance (t2.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

8. Print “Hello, World!” in that file

9. Exit vim and run the program!

Prerequisites:

  1. AWS CLI and credentials
  2. IAM AWS Account
  3. A way to connect SSH, Terminal, or PuTTy. For this project I will SSH into the Ubuntu box using Windows
  4. Ubuntu 20.04 Ec2 instance (t3.small)

Project 3 Steps:

  1. Create a user(under your root/main account), download credentials(be sure to save them), and set up the AWS CLI

a. Create an account on AWS Management Console here, if you already have an account skip this step. Next create a user by navigating to the IAM section and add a new user. AWS IAM allows account administrators to create users for every employee and grant them access to a limited set of services, actions, and resources. The use of root account for daily operations is highly discouraged.

b. Create a user name and at a minimum add the Programmatic access. This will Enables an access key ID and secret access key for the AWS API, CLI, SDK, and other development tools.

c. Add permissions by adding the new user to groups. Add tags and finally review and create the user. Download .csv containing security credentials and save to a folder.

Fig 1. Creating Linux EC2 Instance in AWS.

e. To setup AWS CLI navigate to https://aws.amazon.com/cli/ and download the installer for your operating system. I will be using the 64 bit AWS CLI for this project. Navigate to the downloaded file, click on it and run.

Fig 2. Downloading AWS CLI

f. In this step, we will configure AWS CLI. The first thing we will do is to verify that AWS has been installed by running aws — version on the command prompt. To configure AWS CLI, run aws — configure. You will be prompted for AWS Access and Secret Key which we already from the .csv file downloaded. Choose the region closest to you, in my own case it is us-east-1 and finally set the default output format to json.

g. To verify AWS CLI is working properly, you can run a simple command like aws s3 ls which list available s3 buckets available.

2. Create an Ubuntu 20.04 Ec2 instance (t2.small)

a. Once logged in to AWS Management Console, click on EC2 instance and click “Lunch Instance”. Select Ubuntu Server 20.04 LTS (HVM) which is the version we will be using for this project.

Fig 4. Selecting Ubuntu Server

b. Select the t2.micro, ensure you are using the Free tier eligible version to avoid charges. Click “Review and Launch”. On the next stage, accept default settings and ensure security groups is set to SSH since we will be using that port number later in the project to remotely log into the Linux machine.

Fig 5. Configuring Ubuntu Server t2.micro

c. Lastly, create and download a new key pair or use an existing key pair for the new Linux EC2 instance. A key pair consists of a public key that AWS stores, and a private key file that you store. Together, they allow you to connect to your instance securely.

Fig 6. Creating EC2 Key Pairs

d. View the instance created and while not required, you can edit the EC2 instance name by clicking on the “Name”. Do not use any spacing when naming the EC2 instance. You will also notice that the key pair created is assigned to the newly created EC2 instance. Wait to completely initialize, click on the EC2 instance and launch.

Fig 7. Configured Linux EC2 Instance

3. Connect and log in to the instance using your keypair

a. To accomplish this task to SSH into your Amazon EC2 instance from Windows command prompt, you will need to configure your Windows environment using the steps outlined here.

b. Once your Windows environment has been configured change your directory in command line to point to the folder where the .pem key is stored. Click on the EC2 instance on the AWS Console Management and click on connect. Copy the already provided SSH command and paste in command prompt and connect using the key pair credentials.

Fig 7. Obtaining EC2 Key Pairs in AWS
Fig 8. Connecting AWS EC2 Instance with SSH in Windows
Fig 9. Connected AWS EC2 Instance

4. Refresh updates and upgrade all packages on the instance

a. Now that we are logged in, we will run some commands to update our newly created EC2 instance.

b. Run sudo apt-get update to update the list of available packages and their versions, but it does not install or upgrade any packages. Then run sudo apt-get upgrade install newer versions of the packages you already have. After updating the lists, the package manager knows about available updates for the software you have installed. This is why you first want to run the update command.

Fig 10. Running Upgrades and Updates on Ubuntu EC2 Instance.

5. Create a directory called “environment” and create a virtual python environment in that directory.

a. To create virtual environments in Ubuntu, first install the required dependency package by running command: sudo apt install python3-venv

Now we can create virtual environments by running command: python3 -m venv environment. Running the command above will create a virtual environment called “environment” in my home directory. If you want to use any other version of Python, you can replace “python3” above with full path to the location of your alternative Python binary.

Fig 11. Creating a virtual python environment

6. Activate that virtual environment and confirm the environment is up

a. To activate the custom environment created above, run command: source environment/bin/activate. Where “environment” is the name assigned to my virtual environment. (see above screenshot)

Once the virtual environment is activated, shell prompt will change to reflect the current virtual environment you are using.

b. To verify the environment is setup and running properly, run “sys.path” command inside python interpreter, you can see that virtual environment is working properly. Python will now look for packages inside the custom environment you have just created.

Ensure you import the sys module in python program by using “import sys” before running any functions to avoid getting any errors.

7. Create a program called hello.py

a. We are almost there, this where we use vim and some python scripting.

Vim is an open-source and freely available text editor that is designed for both application GUI as well as for command-line. If you don’t have vim installed you run the command sudo apt-get install vim for Debian based Linux OS. If you are using CentosOS use sudo yum install vim. Verify vim is installed by running vim — version.

Fig 12. Installing and Configuring vim
Fig 13. Verifying installed vim

b. Change directory to the python environment/folder you created. In my own case it “environment”. Create a file by executing touch hello.py.

c. Use ls -l to view the properties of the file. Ensure the file is executable, if not run chmod +x hello.py.

8. Print “Hello, World!” in that file

a. The next stage will be to write our python script using vim. Type the command vim hello.py and open the text editor.

b. Inside the text editor, change the mode to “INSERT” mode by pressing “i” on the keyboard. Type the python script shown below or any code you want to run. Always start the script with !#/usr/bin/env python. This ensures our script runs inside the environment we created.

!#/usr/bin/env python

print(“Hello World”)

9. Exit vim and run the program!

a. Exit out of the insert mode my using the escape key. Save the file by using :wq and return to the the terminal.

b. Run the python script by typing ./hello.py

Fig 14. Running python script “Hello World”

You should see “Hello World” displayed if your python syntax is correct.

On a final note, kindly direct all comments, suggestions or corrections to my inbox. Thanks for reading!!!!!

--

--