Configure a Python 3 Environment on an AWS EC2 Ubuntu Server 20.04 Instance

  1. Create a user, download credentials, and set up the AWS CLI
  2. Create an Ubuntu Server 20.04 EC2 instance (t2. micro) from AWS CLI.
  3. Connect and log in to the instance using a 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
  8. Print “Hello, World!” in that file
  9. Run the program

Create IAM User

  • User name — Create user name
  • Access type — Select both Programmatic access and AWS Management Console access
  • Console password — Select custom password
  • Require password reset — Uncheck User must create a new password at next sign-in

Create User Groups

  • User Group Name — Create group name
  • Add User to the Group — Select User Name created to grant permissions
  • Attach Permission Policies — Select AmazonEC2FullAccess

Install AWS CLI

curl "" -o "AWSCLIV2.pkg"sudo installer -pkg AWSCLIV2.pkg -target /
which awsaws --version
aws configure
  • AWS Access Key ID: Enter Access key ID
  • AWS Secret Access Key [None]: Enter Secret access key
  • Default region name [None]: us-east-1
  • Default output format [None]: Leave blank

Create Key Pair

aws ec2 create-key-pair --key-name MyPythonKP --query "KeyMaterial" --output text > MyPythonKP.pem
vim MyPythonKP.pem
chmod 400 MyPythonKP.pem

Create Security Group

aws ec2 create-security-group --group-name MyPythonSG --description "My Python Secuirty Group"
aws ec2 authorize-security-group-ingress --group-id sg-010f26d44493f3724 --protocol tcp --port 22 --cidr

Create an Ubuntu Server 20.04 EC2 Instance

aws ec2 run-instances --image-id ami-09e67e426f25ce0d7 --count 1 --instance-type t2.micro --tag-specifications 'ResourceType=instance, Tags=[{ Key=Name,Value=MyInstance}]' --security-group-ids sg-010f26d44493f3724 --key-name MyPythonKPNote: You will need to go into console as if you were creating a new instance to get AMI information for Ubuntu Server 20.04, for the security group provide the information for GroupId you created and for key name provide the key pair name you created.
aws ec2 describe-instances --filters "Name=tag:Name,Values=MyInstance" --query "Reservations[].Instances[].InstanceId"

SSH into Ubuntu Instance

aws ec2 describe-instances --instance-ids i-0f28aaf01dbfb9f6c --query 'Reservations[*].Instances[*].PublicIpAddress' --output text
ssh -i "MyPythonKP.pem" ubuntu@

Install Python

sudo apt updatesudo apt -y upgrade
python3 --version
sudo apt install -y python3-pip
sudo apt install -y build-essential libssl-dev libffi-dev python3-dev

Create Virtual Environment

sudo apt install -y python3-venv
mkdir environment
cd environment
python3 -m venv my_env
ls my_env
source my_env/bin/activate

Create a Python File

print("Hello, World!")

Clean Up

aws ec2 terminate-instances --instance-ids i-0f28aaf01dbfb9f6c
aws ec2 delete-key-pair --key-name MyPythonKP
aws ec2 delete-security-group --group-name MyPythonSG



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store