How To SetUp an EC2 Instance Using The CLI And Installing A Virtual Python Environment

Hello, thanks for joining me again! In this lab we will learn how to set up a Python Environment through the AWS Command Line Interface (CLI). If you are wondering what the AWS CLI is and what it is used for, please check out my lab titled “Creating an Autoscaling Group From The CLI”. In that lab, I go over what the AWS CLI is and the benefits of using it instead of the AWS Console GUI. If you are not familiar with Python, I strongly encourage you to check out my last blog “How To Create A Brand Name Generator Using Python”. That lab goes over what Python is and what it is used for in greater depth.

*Some words of advice before we get started- For any information that you will need to to log into AWS or fill in a command, secure it in a file so only you can access it. This will also make it easier for you to reference instead of constantly searching for the information.

Alright, now that I have that out of the way, let’s get this party started!

Step 1: Create a new user

1a) Login to the AWS console and create a new user by going to the search bar and typing in IAM. Once there, select the blue icon labeled “Add User”.

1b) Once you give the user a name, make sure that you check the box to give him Programmatic access as well as AWS Management Console access. It is good practice to give the user a custom password with the box checked that forces the user to create a new password at next sign in but it is not a requirement for this lab.

1c) Once you have successfully created your user, make sure to save the Access key ID as well as the Secret access key to your notepad or somewhere secure. These will be needed to successfully login to the AWS CLI under this user.

And also, this VPC and User will be torn down by the time this blog is posted to avoid anyone trying to login, lol.

1d) Now things get fun. Log on to your CLI and enter the command “aws configure”. This is where the previous information is needed. Use the information that you saved to successfully configure your AWS CLI with your new user credentials. Once that is complete, follow me to the next step. Just FYI, I blocked out my access key and secret key for this Lab but yours will fully display in the CLI.

Step 2: Configure your VPC from the CLI

2a) Use the command “aws ec2 describe-vpcs” to make sure that you are using the correct VPC that you want to configure. This is just to make sure that everything is good to go before you proceed.

2b) Next step is to create a security group for your VPC. This will be needed for your EC2 instance to allow you to SSH into it to download your Python environment. Use the following command to create your security group. It should return back with the security group ID.

aws ec2 create-security-group -group-name project5 -description “whatever you want to name it” -vpc-id yourvpcid

2c) Once the Security Group is configured, use this next command to open port 22. Port 22 allows you to SSH into your EC2 once you attach this Security Group to it. I also opened port 80 out of habit but it is not required for this lab. You will know that you are successful when it doesn’t give you a return. You can always check the console to make sure that your commands are actually going through. Here is the command:

aws ec2 authorize-security-group-ingress -group-id yoursecuritygroup -protocol tcp -port whateveryouwanttoopen cidr yourvpcid

Step 3: Create your EC2

3a) Alright, once you get to this step, go back over to the AWS console and select the AMI that you wish to choose. Grab the AMI for the instance and put it in your notepad since it will be needed for this following command. I personally chose Ubuntu. Keep in mind that whatever image you choose might have it’s own commands.

3b) Once you get to this step you will then create your instance using the information from the AWS EC2 console. Also, make sure that the keypair that you chose is saved to your computer under a folder that you can access. Create the instance with the following command:

aws ec2 run-instances -image-id theamiyouchose -instance-type whateversizeyouwant -key-name yourkey

Use this command to attach your security group to it:

aws ec2 modify-instance-attribut -instance-id yourinstanceid -groups yoursecuritygroupid

Step 4: Login to your Instance

4a) Once you get to this step, go to the AWS Console and type in EC2 into the search bar. Once there, click on Instances and select the Instance ID that you just created and then select “Connect”.

4b) Once you select connect, then select SSH client and your screen should look like what I posted directly above. You will need to copy the example that AWS created for you and place it into your CLI. But before you do, change directory into where you have your key pair document saved using the CLI command “cd” and then type in your folders name.

4c) Once you are in that directory, paste the instance connect example into your CLI. Your CLI should then begin connecting and your final line should be the private IPv4 address for your Instance awaiting your next command. Once you see that, you were successful in connecting to it.

Step 5: Download Python environment to your Instance

5a) Alright, we’re almost done. The next step is to download the Python environment to your Instance. But before you do that, it’s good practice to update and upgrade your Instance. Keep in mind, your sudo commands will change depending on what Instance you chose.

  • Use the command “sudo apt update” to update your Instance
  • Follow up with “sudo apt upgrade” to upgrade your Instance

Then create a path in your Instance to download all of the packages and libraries from the environment. You can use the command “mkdir” and then whatever name you want to give the folder. Once that is complete, change directory into that folder and then use the following command to install the Python environment:

sudo apt install python3-venv

5b) To activate the environment, type in:

source whateveryounamedyourfolder/bin/activate

Step 6: Create and Run your Python Test Program

6a) In this final step, we will be creating a test program through Python. You can use whatever text editor that you prefer to create the “hello.py” program but I personally chose (and would recommend for this lab) to keep it simple and use Vim. You can run Vim and create the program by simply typing in the command “vim hello.py”.

6b) You will then be brought to this screen to enter your code. Here is where you can type in whatever you want the text editor to execute upon you commanding it. To begin to insert your code, simply hit “i” to enter the Insert mode. Once you are in the insert mode, simply type in print(“Hello World!”). You can save this code by hitting the escape key on your keyboard. To save it, type the semi colon “:” and then type in wq and hit enter. This saves your code to the file to be executed once you enter the command.

6c) Now type in “python3 hello.py” to make python execute your file. You will immediately see Python print “Hello World!” to the screen.

This concludes my lab. Thanks for sticking with me!

Level Up In Tech Student currently learning AWS

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