Creating Your Own EC2 Workstation in the AWS Console
Fernando Medina Corey
In this live AWS environment you will create your own EC2 workstation in the AWS console. This will be a useful skill whenever you find yourself working in an AWS environment. Not only will you learn some of the nuances of creating EC2 instances through this process, you'll also be able to do this again in your own AWS environment to create your own AWS workstation that is free from the clutter of a local machine. By the end of this Learning Activity, we will have created our own EC2 instance and signed into it with an SSH key. We will also have used that instance to create an S3 Bucket in our AWS account and used the instance to have the Simple Notification Service send us a text message.
Introduction to Creating Your Own EC2 Workstation
In this lab, we will create our own EC2 workstation in the AWS console. Not only will you learn some of the nuances of creating EC2 instances through this process, you'll also be able to do this again in your own AWS environment to create your own AWS workstation that is free from the clutter of a local machine.
By the end of this lab, we will have created our own EC2 instance and signed in to it with an SSH key. We will also have used that instance to create an S3 bucket in our AWS account and have the Simple Notification Service send us a text message.
To get started, sign in to the AWS environment via the link, username (cloud_user), and password provided. Once you've logged in, navigate to EC2 by searching for it in the AWS services box or clicking All services and clicking EC2 under the Compute header.
Creating Your Own EC2 Workstation in the AWS Console
Click Launch Instance. We need to select a particular AMI with which we want to launch our instance. In this case, select an Amazon Linux AMI that mentions including Python.
Now, we're going to choose the instance type. Select the General purpose t2.micro from the list of instance types, and click Next: Configure Instance Details.
On the Configure Instance Details page, set the IAM role to EC2InstanceCDALabRole. This will give our EC2 instance permissions to take different actions inside AWS. Click Review and Launch.
On the Review Instance Launch page, we can review all our settings for this particular instance (ensuring that it's set as a t2.micro instance), and then click Launch.
Now, we need to make a key pair for our EC2 instance, which we'll be prompted to do in the Select an Existing Key Pair or Create a New Key Pair pop-up. Click the default Choose an Existing Key pair to open the dropdown, and then click Create a New Key Pair. In the Key Pair name box, we can name it whatever we'd like. Let's name it "cdaec2lab" and click Download Key Pair. This will download the .pem file you need to sign in to the AWS instance in the future.
The download should appear in your browser — depending on your settings, it will probably appear inside the Downloads folder on your machine. Check to ensure you have the cda_ec2_lab.pem file by changing into your Downloads folder and listing all the items inside it.
From an open terminal, run:
$ cd Downloads
This will change directories over to your Downloads folder, which is where the .pem file saves automatically.
List out all of the files in the folder:
You should see a file with a .pem extension — this is the key pair you downloaded earlier.
It's vital to make sure you have the .pem file somewhere safe before launching the instance — you will need it to log in to the instance, and there's no way to recreate it without terminating and recreating the instance.
When you're sure you have the .pem file on your local machine, click Launch Instances.
Once the process is completed and we get a success message, we can click View Instances.
In the instances list, we should now see the instance we just created and some of the metadata about it. If the Instance State status is pending, wait for it to change to running so we can access it.
Once the instance is ready (identified by the blue square), click Connect. A pop-up will appear, offering some helpful instructions for how to connect to our instance.
If you're on a Windows machine, click the connect using PuTTY link to learn more about connecting to the instance via PuTTY. On most Unix-based machines, though, you should be able to use the provided commands in the pop-up. Let's go through them now.
First, change the permissions on the .pem file in your Downloads folder:
$ chmod 400 cda_ec2_lab.pem
Log in via SSH, using the command in the pop-up, which should look similar to this:
$ ssh -i "cda_ec2_lab.pem" email@example.com
(Note: Your command will be different than the above because it will have your specific EC2 instance's public-facing address after the @, which you can see in the Public DNS (IPv4) column in AWS.)
A prompt may ask, "Are you sure you want to continue connecting (yes/no)?" Type yes.
This will log you in to your AWS EC2 instance.
Now that we're signed in to our instance, let's attempt to interact with some services that our EC2 instance has permission to interact with.
From your terminal, run:
$ aws dynamodb list-tables
You will receive a response back saying that "You must specify a region. You can also configure your region by running aws configure."
This happens because we have not yet configured which region we want to execute our commands against. We can fix that easily by typing the following command:
$ aws configure
Pass through the next two dialog boxes (AWS Access Key ID [None]: and AWS Secret Access Key [None]:) using the Enter key.
For Default region name: enter us-east-1.
Leave the default output format.
Now, let's run the same command we ran earlier:
$ aws dynamodb list-tables
This time, we get an empty output of "TableNames" because we have no DynamoDB tables created, but the command ran successfully.
Now, let's create an AWS S3 bucket.
First, check to see if there are any S3 buckets created:
$ aws s3 ls
The prompt should return an empty line, since we haven't created any S3 buckets. Let's do that now.
Create a new S3 bucket using the "make buckets" (mb) command (Note: Replace EXAMPLE-NAME here with a new, unique name):
$ aws s3 mb s3://EXAMPLE-NAME
You should see the following response: make_bucket: EXAMPLE-NAME.
Test to ensure the bucket exists using the
aws s3 ls command. The prompt should output your bucket name as you defined it previously.
Now, let's install Boto3.
In your terminal, type:
$ sudo pip install boto3
This uses the pip tool we have installed on this EC2 instance by default to install Boto3, the AWS SDK for Python.
Once that runs, type
python into the terminal to get into the default AWS Python installation on this instance.
Now, import Boto3:
$ import boto3
Now that Boto3 is installed and imported into our Python instance, we can use it to configure AWS's Simple Notification Service (SNS).
To create a client for the SNS service, use the SNS variable:
$ sns = boto3.client('sns')
Once the client is configured, we can set a number to go with it. For the purposes of this guide, we've used a fake number, but you can substitute your own when setting this up for yourself.
$ phone_number = '+15558889999'
The number needs to contain the country code of the country you're interacting with. For the purposes of this guide, we're interacting with the US, so the country code is +1.
Send messages with the SNS client's publish method:
$ sns.publish(Message='Hello from Linux Academy!',PhoneNumber=phone_number)
Once you execute the command with the Enter key, you should see your message appear as a text on your personal phone.
Exit the Python instance with Ctrl-C and Ctrl-D or by typing the
Back in our EC2 instance, we're now free to interact with any other AWS instances we have permissions for.
Congratulations! You've successfully created an EC2 instance inside the AWS console. Happy learning!