Configure a Python 3 Environment on an AWS EC2 Ubuntu Server 20.04 Instance
The following will be performed as part of this project:
- Create a user, download credentials, and set up the AWS CLI
- Create an Ubuntu Server 20.04 EC2 instance (t2. micro) from AWS CLI.
- Connect and log in to the instance using a Keypair
- Refresh updates and upgrade all packages on the instance
- Create a directory called “environment” and create a virtual python environment in that directory.
- Activate that virtual environment and confirm the environment is up
- Create a program called hello.py
- Print “Hello, World!” in that file
- Run the program
Create IAM User
User will require programmatic and console access.
1. Navigate IAM in AWS Console
2. Click on Users
3. Click on Add 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
4. Click Next for Permissions, we will skip for now
5. Click Next for Tags, no tags required
6. Click Next for Review
7. Click Create User
8. Click on Download .csv, we will need this information.
Create User Groups
1. Navigate to User Groups, we will create a group to grant our new user permissions.
2. Click Create Group
- User Group Name — Create group name
- Add User to the Group — Select User Name created to grant permissions
- Attach Permission Policies — Select AmazonEC2FullAccess
3. Click Create Group
Install AWS CLI
The following steps are what you will use if installing on MacOS Terminal, but you could find direction here if you wish to use other options or platforms.
1. Open Terminal
2. Install AWS CLI using the following commands.
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"sudo installer -pkg AWSCLIV2.pkg -target /
3. Verify the installation.
which awsaws --version
4. AWS Configurations.
aws configure
5. The following question will be prompted, reference .csv file dowloaded.
- 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
1. Create Key Pair MyPythonKP using the following command.
aws ec2 create-key-pair --key-name MyPythonKP --query "KeyMaterial" --output text > MyPythonKP.pem
2. Use VIM or another text editor to verify MyPythonKP.pem contains your private key.
vim MyPythonKP.pem
3. To ensure your key is not publicly viewable run the following command.
chmod 400 MyPythonKP.pem
Create Security Group
Security Group will be created for our instance that will allow SSH from 0.0.0.0/0 in order to access our site. This is not necessarily best practice so feel free to restrict SSH to only your IP. You may do a quick online search that will provide you with directions.
1. Create Security Group MyPythonSG using following command.
aws ec2 create-security-group --group-name MyPythonSG --description "My Python Secuirty Group"
2. If command runs successfully the GroupId for the Security Group will be displayed. You will need this information so make a note of it.
3. Run the following command to update the inbound rules for the Security Group. For the group-id make sure you use your GroupID created.
aws ec2 authorize-security-group-ingress --group-id sg-010f26d44493f3724 --protocol tcp --port 22 --cidr 0.0.0.0/0
Create an Ubuntu Server 20.04 EC2 Instance
1. Create Ubuntu 20.04 EC2 instance using the following command.
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.
2. After successfully running the command it will output a JSON template with the new instance information. Verify everything looks accurate.
3. To confirm your EC2 instance was created run the following command filtering by Tag to only view new instance created.
aws ec2 describe-instances --filters "Name=tag:Name,Values=MyInstance" --query "Reservations[].Instances[].InstanceId"
4. You will see your instance id.
SSH into Ubuntu Instance
1. Run the following command to find the Public IP of our instance.
aws ec2 describe-instances --instance-ids i-0f28aaf01dbfb9f6c --query 'Reservations[*].Instances[*].PublicIpAddress' --output text
2. Run the following command to SSH into our Ubuntu instance using our key pair.
ssh -i "MyPythonKP.pem" ubuntu@3.84.143.71
3. We have now successfully connected to our EC2 Ubuntu Server.
Install Python
We will need to run some updates and upgrades to out Ubuntu Server.
1. Run the following commands individually.
sudo apt updatesudo apt -y upgrade
2. Now check the the python3 version to see if ti was installed.
python3 --version
3. Install pip, which will install and manage programming packages used for development.
sudo apt install -y python3-pip
4. Install packages and development tools.
sudo apt install -y build-essential libssl-dev libffi-dev python3-dev
Create Virtual Environment
1. Install venv.
sudo apt install -y python3-venv
2. Make a directory for your virtual environment.
mkdir environment
3. Change into the environment directory.
cd environment
4. Create the virtual environment.
python3 -m venv my_env
5. List the newly created my_env directory to see the contents.
ls my_env
6. Activate the virtual environment.
source my_env/bin/activate
7. Notice that the prefix now included (my_env). This lets you know that the environment is active.
Create a Python File
1. Let’s create a simple “Hello, World!” print function using VIM.
vim hello.py
2. Press i on your keyboard to enter insert mode and type the following.
print("Hello, World!")
3. Press the Esc key to take you out of insert mode, then type :wq to save and return to the terminal.
4. Let’s run our program to verify.
python hello.py
5. You should see the words “Hello, World!” printed on the screen.
Clean Up
We have successfully ran our created our virtual environment and ran our python program. Now we may delete everything created since it will no longer be needed. This could be done from the Console or Terminal.
1. Delete original instance created.
aws ec2 terminate-instances --instance-ids i-0f28aaf01dbfb9f6c
2. Delete the key pair if you don’t have any more use for it.
aws ec2 delete-key-pair --key-name MyPythonKP
4. Delete Security Group created.
aws ec2 delete-security-group --group-name MyPythonSG
5. Delete test user created from AWS Console.