Tutorial: AWS CLI EC2 Tutorial
The industrial revolution has brought us a degree of wealth that would have seemed unimaginable to our ancestors and it’s all thanks to the phenomenon known as economy of scale.
IKEA factories can pump out a desks much faster than an individual building them by hand. In the same sense, we can obtain more computational resources (i.e. a server with better specs) at the same cost by using the infrastructure of tech giants instead of purchasing the hardware ourselves.
The current world leaders in cloud computing are Amazon Web Services (AWS) proceeded by Microsoft’s Azure then the Google Cloud Platform.
In this article, we will create a AWS EC2 instance in the cloud.
If you haven’t already, you’re going to want to install the AWS Command Line Interface (CLI).
The installation should have already added
C:\Program Files\Amazon\AWSCLI\bin\ to your
PATH variable. Try running
aws --version, if it returns a value, your installation was successful. If you run into problems try opening a new shell.
Configure The AWS CLI
AWS Access Key ID and AWS Secret Access Key
These are your account credentials, protect them at all costs. If someone were to get their hands on them, they could use computing resources and run your bill up thousands of dollars.
Default region name
If you select us-east-2 as the default region then all the commands will make API calls to the data centers in that region unless specified otherwise.
If you don’t know the name to your region, you can list them by running the following line.
aws ec2 describe-regions --output table
Default output format
The output of
aws commands can be in json (default), text, or table format.
In essence, a security group is a virtual firewall. You can set rules to limit access to your instance.
**aws ec2 create-security-group --group-name example --description "this is an example"**
Be sure to copy the GroupID somewhere because we’ll need it when we go to create the instance.
The value of the CIDR range corresponds with the range of IP addresses that are allows to access your instance. A value of all 0s means that any computer connected to the Internet can access your instance. Theoretically, if someone were to get hold of your SSH key, they could get into your VM. For better security, replace the
0.0.0.0/0 with the range of IP addresses you’ll use to connect to your instance.
At minimum, we need to open port 22 (SSH protocol) so that you can connect to your instance.
**aws ec2 authorize-security-group-ingress --group-name example --protocol tcp --port 22 --cidr _0.0.0.0/0_**
The proceeding line creates a 2048-bit RSA key pair with the specified name. The aws ec2 command stores the public key and outputs the private key for you to save to a file. The .pem file will be passed in as an argument when you go to connect to your instance.
**aws ec2 create-key-pair --key-name test-key --query 'KeyMaterial' --output text > test-key.pem**
In a Windows Command prompt, use double quotes instead of single quotes.
Note: Make sure you are using a Windows Command Prompt and not Powershell as you may run into issues.
Amazon Machine Image
If you’ve had any prior experience working with virtual machines, you know that they require an image. An EC2 instance is no different. When creating a EC2 instance from the command line, we specify the operating system using the amazon machine image (AMI) ID.
In this tutorial, we’ll be using the Amazon Linux 2 AMI however there are actually quite a few available in the free tier.
To get the image ID corresponding to the Amazon Linux 2 AMI, run the proceeding code.
aws ec2 describe-images --owners amazon --filters 'Name=name,Values=amzn2-ami-hvm-2.0.????????-x86_64-gp2' 'Name=state,Values=available' --output json | jq -r '.Images | sort_by(.CreationDate) | last(.).ImageId'
To get the command that retrieves the latest amazon machine image for a specific distribution, visit the following link.
You can have the latest version of windows at home but if it’s running on a decade old computer chances are that you’re slamming your keyboard wondering why it’s so slow.
All this to say, the operating system is completely independent of the underlying hardware that runs it. In the context of AWS, every type of an instance has its own set of specs. These specs include CPUs, RAM, networking capacity and storage.
t2.micro is the only instance type available in the free tier.
Elastic Compute Cloud or EC2 provides scalable computing capacity in the Amazon Web Services (AWS) cloud.
**aws ec2 run-instances --image-id ami-_xxxxxxxx_ --security-group-ids xxxxxxxxx --instance-type _t2.micro_ --key-name example-key**
You can list your instances with
aws ec2 describe-instances.
It can take several minutes to create the instance. Once the it’s up and running, you can use the following command to get the public IP address.
**aws ec2 describe-instances --instance-ids i-_0787e4282810ef9cf_ --query 'Reservations.Instances.PublicIpAddress'**
Finally, we can connect to our EC2 instance over the Internet.
ssh -i example-key.pem firstname.lastname@example.org
Where the user is the default user name associated with the AMI you used to launch the instance. For an Amazon Linux AMI, it is
If you’re on Windows, you’ll need to use putty. Putty doesn’t accept keys in .pem format so we’re going to have to use puttygen to convert the key into .ppk format.
Open puttygen and click load.
By default, puttygen will only look for files that end in .ppk, make sure you’re looking for all files.
After loading the .pem file into puttygen, hit
Save private key. Save the key in .ppk format.
Now under the SSH tab in putty, select the .ppk file.
Now you’re ready to connect to your instance using putty.
Once you’re done with your VM. You can enter
exit to exit out of your session.
Amazon won’t charge usage for a stopped instance. However, they do charge you for Amazon EBS volume usage.
Run the following code to terminate your EC2 instance.
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0