Skip to main content

Website "Hello World" with Amazon Web Services (AWS)

Introduction

This article explains the foundations on how to start a new website or web application.
The website will be hosted on the cloud, for a very effective cost.
At the end of this article, the reader will be able to:
  • Open an account on Amazon Web Services (AWS) and start an Elastic Compute Could (EC2) instance
  • Configure the different parameters of the EC2 instance (connectivity, security, network)
  • Create a domain name
  • Link the domain name to the EC2 instance
  • Run a Hello World website, and access it from anywhere

Required Knowledge 

A very basic knowledge is required in these areas:
  • HTTP
  • Unix
  • SSH

Create an EC2 Instance in AWS

The first step of the process, is to open an account with AWS: https://aws.amazon.com/
This should be quite straightforward, by clicking on Sign-Up or "Create a Free Account". The credit card information needs to be entered for verification, but the first year of basic usage will be free of charge, this is a great way to get started without incurring any infrastructure cost. A few minutes after the registration, the account creation is completed, and it is time to login to the AWS console: https://console.aws.amazon.com/. The interesting option there is to "Launch a virtual machine" with EC2.


The next step is to choose the operating system - in this tutorial, the chosen one will be Ubuntu Server.
The instance type will be a "t2.micro" - the important thing to note is that it is "Free-Tier Eligible", meaning that the usage will be free for the next twelve months. If a more powerful instance type is needed, chances are it won't be free-tier eligible. However, the "t2.micro" specs are more than enough to get started.
Then, a "key pair" needs to be created. A key pair is a private key and public key (that's why it is called a "pair"). The private key needs to be immediately downloaded, and stored in a secure location. Indeed, connecting to the EC2 instance will require the key pair to be available, there is no option to have a password based login. It needs to be stored in a secure location, as anyone in possession of the private key will be able to login to the EC2 instance.
Finally, the EC2 instance is created, and can be managed in the EC2 console.
The public IP address can be found under "IPv4 Public IP", it needs to be noted down for the next steps. It is now time to log in the new instance.

Login to the EC2 Instance

SSH Command

On Unix based systems, this is very simple:
ssh -i /path/my-key-pair.pem ${ipv4}

Where ${ipv4} is the public IP address of the EC2 instance.

Putty

Putty is a popular SSH client which can run on Windows OS. There is an additional step to be performed: Putty can't directly use the .pem private key file generated by Amazon, it needs to be converted in Putty format.
Putty comes with an utility called PUTTYgen, which can help to convert the private key to the Putty format.
It is as simple as loading the .pem file, and then saving the private key (when loading the .pem file, "All Files" needs to be chosen as by default only .ppk files are shown.


Now to login to the instance, two parameters need to be setup (see below screenshots):
  • The IP address of the instance
  • The private key

Troubleshooting

It might be possible that the connection can't be established, even though all the parameters of the SSH client are setup properly.
This can be due to the instance security group policy.
The security group name can be found on the main page of the EC2 console:


In the security groups section, the allowed IP address must be set correctly to the client IP address - AWS should be able to compute it properly.
Alternatively, all IP addresses can be allowed, but this is not recommended.



  

Default Username

The default username to use is not always well documented. For Ubuntu, the default username is simply "ubuntu". It might be different for another OS (e.g. "ec2-user" or "root").
The "Connect" button in the EC2 console gives instructions on how to connect, along with the default user.



Install a Web Server

A quick way to have a web server up and running is to install nginx:
sudo apt-get install nginx

To make sure it works:
wget -O -  http://localhost

Some HTML output should be written to the console.

However, trying to connect to the web server from the client machine fails (not the EC2 instance but any other machine).
For example, entering the URL: http://11.22.33.44 in a web browser will probably result in a connection refused error (where "11.22.33.44" needs to be replaced with the real IPv4 of the instance).

The reason is that the default HTTP port (80) is not opened for inbound requests in the EC2 instance security group (c.f. "Troubleshooting" section above).
The port 80 needs to be enabled for all IP addresses - it is indeed wanted that everyone should be able to access the website.


Once done, accessing the instance URL via a web browser finally works!
All the infrastructure has been setup to start coding and deploying a (simple) website / web application.

For some OS, there might be an additional firewall to be configured, and the port 80 must also be opened on the firewall - this should not be needed for Ubuntu.

Create a Domain Name

The website is now up and running - however, accessing it using its IP address is far from being user friendly. It would be far better to give it a name such as "mywebsite.com".

A domain name is an identification string, used to identify various internet resources, such as computer hosts. Within the Internet Protocol, a host is identified by an IP address, which is just a set of numbers separated by dots. This is not a really user-friendly identification method though, it is much easier to remember google.com rather than (123.45.67.89).
Creating a domain name is relatively simple, this can be done in a domain name registrar, such as GoDaddy or NameCheap. The registrar will simply reserve the domain name, which can then be associated to a numeric IP address - so that the name can be used in place of the IP address.

For this tutorial, NameCheap will be used to show the creation of a domain name - but similar steps apply to other registrars (I am not affiliated to NameCheap, I just found it cheaper than other registrars in my case).

The first step is to create a new account in http://www.namecheap.com/, and then search for the needed domain name and purchase it (domain names using popular words will be more expensive).

Once it is purchased, the domain name needs to be "mapped" to the EC2 instance IP address - so that web browsers will know that when requesting a page from "mywebsite.com", it will have to send the request on the correct host (the EC2 instance just created).

This is being done by setting up DNS records.

In namecheap.com, the configuration settings can be found in "Advanced DNS".




There are two recommended settings are - they are two "A-records".
An A-record simply maps a sub-domain to an IP address.

For example in the above screenshot, if the purchased domain was "mywebsite.com":
  1. A-record - {Host: @, Value: 11.22.33.44}. This means: map the name "mywebsite.com" to the IP address 11.22.33.44, so that a request (from a web browser for example) to http://mywebsite.com is equivalent to http://11.22.33.44 (the @ symbol represents the purchased domain name).
  2. A-record - {Host: www, Value: 11.22.33.44}. This means: map the sub-domain "www" to the IP address 11.22.33.44, so that a request (from a web browser for example) to http://www.mywebsite.com is equivalent to http://11.22.33.44
After saving the configuration, the changes my take up to 48 hours to be reflected.
This type of domain name configuration is not specific to namecheap.com - it is the same for other domain name registrars.

Conclusion

This article has presented:
  • How to create a minimal running web server on AWS - for no cost
  • How to create a domain name and associate it to the web server host
This lays the foundation on how to get a website up and running, the next step being obviously building the website / web application itself.
As the web application grows, there will be other concerns, such as security, scalability, which are out of scope of this document.
I hope this article was useful - any comment / feedback is much appreciated!


Comments

Popular posts from this blog

Testing Against External Processes with pytest-xprocess

Introduction With the advent of micro-services or simply componentization of complex applications, more often than not it is a good idea to write at least few integration tests to make sure that different components can interact in the intended way. However, these components may run in their own process, may take some time to start-up, and may require other processes to be run first. This article aims to give some ideas on how to solve the following problems, using pytest:  How to start an external process before the tests run, and make sure tests start running only once the process is fully initialized? Starting an external process is usually resource and time consuming, how can the whole test suite be setup to run the external process only once before all tests run, and shutdown only once all tests are completed? Given a system under test (SUT), which depends on a process A, which in turn depends on a process B, how can all the processes be start in the correct order befor...

Python Virtual Environment 101

What is a Virtual Environment? A Python Virtual Environment, or more commonly called "virtualenv", is a tool that is included in the recent Python distributions. Its purpose is to create a virtual and isolated Python environment, that can be modified at will and then simply discarded. "Python environment" refers to the Python binaries, the standard Python libraries, as well as any third party library that is later installed. When installing a Python distribution from https://www.python.org/ or from a package manager, this installation is referred as the "global environment" or "global installation". Why is a Virtual Environment needed? Except for the simplest cases, most Python projects require some dependencies to third party libraries - third party in the sense that they are not included in the Python official distribution. Typically, these libraries are installed in the "site-packages" folder of the global Python installatio...