How to get started with AWS EC2

Amazon Elastic Compute Cloud (EC2) instances are like virtual servers you can rent from Amazon. The keyword here is Elastic. The EC2 instances come with a feature called Auto Scaling. This is the equivalent of the number of servers stretching automatically when traffic increases and then shrinking back down again when the traffic drops off.

We’ll launch an instance and use it to run an NginX web server which will be accessible from anywhere using a URL. So without further ado, let’s get started.

Starting off

  • Search for EC2 in the AWS search bar. From there click the launch instance button.

  • Now you would see a screen to choose an AMI. AMIs (Amazon Machine Images) are nothing but virtual machine images.

  • For this shot, we’re going to select the Amazon Linux 2 AMI (HVM), SSD Volume Type with the x86 option.

  • On the instance type screen, we select the kind of hardware we want to be allocated to our VMvirtual machine. This is basically like choosing the configuration (CPU, RAM, etc) for our computer which would act as the server. Let’s go with t2.micro here.

  • From here click on review and launch and finally on the next screen click on launch.

Connecting

  • Now to connect to this instance through SSHSecure Shell. We’ll need to create a key pair that acts like a password to access this instance.

  • In the first pulldown on the popup you see, click on “create a new key pair”.

  • Let’s name it as ec2demo. Then click the “download Key pair” button and finally click the “launch instances” button.

  • On the next screen, click on “view instances”.

  • Once you confirm that the instance state is running, click on the checkbox in the first column for this instance and then click on “connect” in the top bar.

  • Go to the SSH client and if you follow the instructions there you should have SSH access to your instance.

What this basically did was use the encoded private key we downloaded to connect via SSH to the instance which already had the associated public key installed.

Installing NginX

  • Now let’s install NginX on this machine. NginX is available from the AWS extras repository.

  • You can confirm this by running the following command:

amazon-linux-extras list | grep nginx
  • To install it, run:
sudo amazon-linux-extras install nginx1
  • We can verify that it was installed successfully by running:
nginx -v
  • Let’s start the webserver by running:
sudo service nginx start
  • You can also confirm that it’s running using:
sudo service nginx status
  • Once the server is up, let’s also confirm that we can see the NginX starter page from within this EC2 instance.
curl localhost
  • This should show you the HTML for the NginX starter page.

Now that we have our EC2 instance all setup, let’s head back to the AWS console. If we select the instance, we’ll see information related to it on the bottom of our screen as shown in the image below:

widget

The Public IPv4 DNS is the URL we can use to see the NginX starter page from any machine.

Security

If you’ve run servers before you’d know that allowing unfiltered internet traffic to your server means exposing it to so many security threats. Amazon uses security groups to solve this. These act as firewalls that allow us to expose only the parts of our server that are configured for public internet traffic.

Since we didn’t configure our security groups while launching the EC2 instance, Amazon assigned the standard security group to it.

  • Click on the security tab next to details.
  • Then click on the link under security groups.
widget

On the next screen, you will see one inbound and one outbound rule. The outbound rule allows our EC2 instance to communicate with anything on the internet. The inbound rule on the other hand says that anyone can connect to our instance using SSH. Click on the edit inbound rules button to add another inbound rule.

We’ll add a rule to allow HTTP traffic from anywhere onto our EC2 instance. Add the rule shown below and then click on “save rules”.

widget

After doing this if you go to the previous URL you should see the NginX starter page.

Please note that you should navigate to http://<url> and not https://<URL>.

Conclusion

Before we wrap up, I want to show you one last thing. Just like we used the Amazon Linux 2 AMI (HVM), SSD Volume Type AMI for this EC2 instance, you can create an AMI from this instance. This would mean that when you launch another instance using this created AMI you wouldn’t have to install NginX again.

To do this, select your instance and go to:

  • Actions → Image and templates → Create image. Just add an image name and leave everything else to its default values.

Now once the AMI is ready and you navigate to the AMIs section using the nav menu on the left, you should be able to launch an instance using this image.

widget

Free Resources

Attributions:
  1. undefined by undefined
  2. undefined by undefined