This article was also published on razcodes.dev
I will outline in this article the steps for creating a simple HTTP server in AWS using the EC2 service, running on Amazon Linux 2.
This article assumes that you already have an AWS account setup, with all the default settings.
Once logged into the AWS console go to Services -> EC2 -> Instances and click on Launch Instance.
Here you will see a list with all the available images that can be launched. We will select the first one (at the time of writing this article) called Amazon Linux 2 AMI.
Make sure t2.micro is selected, as this is part of the free tier and click Next: Configure Instance Details.
On this screen we can leave most things on default, and feel free to click on the info icon located by each option to get more information about what the options do.
The one thing we are going to change is located towards the bottom, under Advanced Details -> User Data. Here, you can include a bash script that will run when the instance is launched, with root privileges. We can use this to run a system update as well as install the run the WebServer. Just copy this script into the text field:
#!/bin/bash yum update -y yum install -y httpd.x86_64 systemctl start httpd.service systemctl enable httpd.service echo "This is $(hostname)" > /var/www/html/index.html
Next: Add Storage - This is where we can select the drive(s) size and type(s) we want for the instance. We can leave everything as default.
Next: Add Tags - Tags can be very useful for billing, inventory and all sorts of other things. For this exercise we do not need any.
Next: Configure Security Groups - This part will define what ports we want to leave open towards the host instance, so we will only deal with ports 22 for SSH and port 80 for HTTP.
- Create a new security group
- Security group name: WebServer
- Description: WebServer SG
SSH is already added, but we will just open that for our machine, so under Source click on the Custom dropdown and select My IP.
Click on Add Rule and select HTTP. This will open port 80 for the web traffic from anywhere, which is what we want.
Review and Launch -> Launch
Create a new key pair, give it a name (ex: myWebServer) and click Download Key Pair.
Launch Instances -> View Instances.
Now you will see you new instance being created. It will take a few minutes to get it up and running, update the system and install httpd.
After a few minutes, with the new instance selected, you can copy the Public DNS or the IP, open a new browser window and visit your new server.
You can click Connect on top, and you can get instructions on how to connect to it using SSH.
Note that because of the way we configured the SSH port to only allow connections from our IP address, when you click connect, the EC2 Instance Connect using the browser will not work, since that will come from a different IP address. If you would like that functionality for playing around, you can go change the security group and allow connections to port 22 from 0.0.0.0/0, but that is not recommended and should only be done for testing purposes.
When you are done with your instance you can select it and under Actions -> Instance State, you can chose:
- Stop : this will stop the instance, you will no longer be charged and start it again when you want to. When you start it back up you will have a different IP address, so the connection string will be different.
- Terminate: this will destroy the instance.