Creating a Python 3 Environment on an EC2 Instance with AWS CLI

Topic created · 1 Posts · 19 Views
  • For this task, you will write a secret note in Python 3 after connecting to a newly created EC2 Instance through AWS CLI. An AWS account is needed, along with AWS CLI downloaded on your local system. Provided is a link to guide you through the download process for AWS CLI for different operating systems:

    Step 1: Create an Ubuntu 20.04 Ec2 Instance (t3.small)

    In Terminal, using the code below, change the Key Pair Name, Security Group IDs, and Subnet ID’(s) to match your account details.

    aws ec2 run-instances --image-id ami-09e67e426f25ce0d7 --count 1 --instance-type t3.small --key-name <KEY\_PAIR\_NAME> --security-group-ids <SG\_ID> --subnet-id <SUBNET\_ID, SUBNET\_ID>

    The output from the code above will create a json script, validating the creation of the Ubuntu 20.04 EC2 Instance.

    Check in the CLI that the creation has been successful using the following code:

    aws ec2 describe-instances --filters "Name=instance-type,Values=t3.small" --query "Reservations[].Instances[].InstanceId"

    The AWS console is another resource to check the success of the created EC2 Instance.

    Step 2: Connect and Log In to the Instance Using Your Key-Pair

    In the console, select the instance to start a connection. Select the “Connect” button, seen below.

    Follow the steps below to SSH into your EC2 Instance through Terminal.

    Be sure that the inbound security group settings has Port 22 open, for that is essential when attempting to SSH into the EC2 Instance through Terminal.

    Tip: remember to “cd” into the directory of which your key-pair was downloaded, and proceed connection from there.

    cd Downloads  
    chmod 400 <KEY\_PAIR\_NAME>.pem   
    ssh -I "<KEY\_PAIR\_NAME>.pem" [email protected]<EC2\_IP>

    Step 3: Refresh Updates and Upgrade All Packages on the Instance

    To update, use the command below:

    sudo apt-get update

    To upgrade all packages, use the command below:

    sudo apt-get upgrade

    Step 4: Create “environment” Directory and a Virtual Python Environment in “environment” Directory

    mkdir environment   
    cd environment   
    sudo apt-install python3-venv  
    python3 -m venv env

    Step 5: Activate Virtual Environment and Confirm Environment is Up

    source env/bin/activate  
    which python

    Parenthesis in front validate environment activation

    Step 6: Create a program called

    You are allowed to create whatever program you’d like, we are so used to seeing “Hello, World!” as our example, so this was a little twist on the task.


    Step 7: Print “What’s Up, World!” in that File

    Remember, apostrophes need to be handled appropriately when using Python. Escape characters can be used, or the text can be wrapped with double quotes to use the apostrophe.

    print('What\\'s Up, World!') #example of using escape character  
    print("What's Up, World!") #example of wrapping with double quotes

    Step 8: Exit vim and Run the Program!


    By typing “exit” on the command line, the connection between the EC2 Instance will be broken. Once that happens, be sure to clean up the environment to avoid any unnecessary charges by AWS! This can be done by terminating (through CLI or console) the EC2 Instance that was created during this project.

    terminate-instances --instance-ids <ID\_HERE>

    Thanks for trying this exercise with me! Hope you learned something new along the way. Find me on LinkedIn —

Log in to reply