← Back to all labs

Create a Web Server using Amazon EC2

I launched an Apache web server on an Amazon EC2 instance, set it up automatically with a User Data script, and managed it through Session Manager instead of SSH.

Amazon Elastic Compute Cloud (EC2) is an AWS service that gives you scalable, resizable virtual servers, with the CPU, memory, storage and networking you choose, to run applications in the cloud.

Architecture Diagram of the Web Server
Figure 1: The architecture diagram for launching the Web Server created from draw.io

Figure 1 illustrates the deployment architecture of the EC2 instance in the Singapore AWS Region, specifically within a public subnet. End users can access the Apache web server through the internet, while the system administrator can remotely manage the instance, install updates, and apply software patches using AWS Systems Manager Session Manager.

How I built it

1. Choosing the AMI

I launched an instance named apache-web-server and chose Amazon Linux 2023 as its Amazon Machine Image (AMI). It is Amazon's own Linux distribution, and it's free, lightweight and optimized for AWS.

An AMI is a preconfigured template with the operating system and software needed to start an instance, much like the operating system on a personal computer. The AMI you pick decides the operating system and starting software of your server.

Name and AMI of EC2 instances
Figure 2: Name and AMI of the EC2 instance

2. Choosing the instance type

I selected t3.micro. The instance type sets the hardware resources of the server. In the name, the letter t is the instance family (General Purpose) and the number 3 is the generation. Newer generations usually bring better performance and efficiency.

General Purpose (T) instances balance CPU, memory and networking, which suits web servers. Other families cover different needs:

Instance type of the EC2
Figure 3: Selecting t3.micro as instance type.

3. Key pair

A key pair is two cryptographic keys that let you connect to an instance, usually over SSH, without a username and password. I chose to continue without a key pair because I connect to the instance with AWS Session Manager, which I turned on in the advanced details.

no keypair
Figure 3: The EC2 instance will be launched without an SSH key pair.

4. Network settings and security group

I used my own VPC, my-VPC, and picked Public Subnet 1, which already auto-assigns a public IP address. I then created a security group named Web Security Group with the description "enable inbound HTTP" and a rule allowing HTTP from anywhere, so everyone can reach the web server.

A security group is a virtual firewall for resources like EC2 instances. Inbound rules control incoming traffic and outbound rules control outgoing traffic. Common ports are 22 for SSH, 80 for HTTP and 443 for HTTPS. Security groups are stateful, which means response traffic is allowed automatically. They only support allow rules, and changes apply right away without restarting the instance.

Network settings
Figure 4: The Network settings and Security Group.

5. Storage

I kept the default storage because this project is a demonstration and needs no extra space.

default storage
Figure 5: Default storage configuration.

6. Advanced details

Under IAM instance profile I selected SSM_EC2, a role I created earlier in IAM. An IAM role gives temporary, secure permissions to AWS resources. This one lets me manage the instance through AWS Systems Manager without a key pair or SSH.

In the User Data field I added a script that installs Apache (httpd) and sets up the web page. User Data runs automatically the first time an instance launches, so the web server is ready as soon as the instance starts, without me connecting to it.

iam ec2 ssm role
Figure 6: Selecting SSM_EC2 IAM role.
apache user-data
Figure 7: Configuring the EC2 instance's user data script.

7. The figure below shows the successful deployment and verification of the web server.

apache user-data
Figure 8: Verifying the Apache HTTP Server (httpd) Status Through AWS Systems Manager Session Manager.
web server interface
Figure 9: Successfully Deployed Apache Web Server.

Summary