Image description

Navigate to the AWS Management Console and locate the VPC service.

Image description

Click Create VPC.

Image description

Select VPC and more.

Image description

In the NAT gateways section, select 1 per AZ.

Image description

We're ready to create the network. Click Create VPC.

Image description

Image description

Image description

Image description

Browse to the Security Groups part of the Amazon EC2 service.

Image description

Notice the default security group that is already present, then proceed to click Create security group to define a new custom security group for our resources.

Image description

Image description

In the Inbound rules section click Add rule

Image description

Image description

Image description

Next, repeat the process to create an Security Group

Image description

Image description

Image description

Confirm the 2 security groups have been created.

Browse to the IAM service.

Image description

To grant permissions, we create IAM policies. These policies can then be assigned to an IAM role. IAM roles can be assumed by trusted entities, such as an EC2 instance, to access permitted AWS resources. An EC2 Instance Profile is a container that holds an IAM Role and attaches it to an EC2 instance.

Image description

Create a new IAM role and associate it with the EC2 instance profile for the web server.

Select Roles, then click Create role.

Image description

Attach IAM policies to the role being created. Instead of defining the policies manually, an existing AWS managed policy will be used to save time.

Select AWS Service. Choose EC2 for the service or use case.

Image description

Select EC2 Role for AWS Systems Manager and click Next

Image description

Confirm that the AmazonSSMManagedInstanceCore policy has been added to the role and click Next

Name the role WebServerInstanceProfile . Scroll to the bottom and click Create role.

Image description

Browse to the EC2 service.

Image description

Click Launch Instance.

Image description

Name the server mywebserver

Image description

Customers have the flexibility to launch Amazon EC2 instances with a wide selection of operating systems and pre-configured images.

For our simple web server, we'll select the Amazon Linux 2023 AMI (Amazon Machine Image) in the 64-bit (x86) architecture.

Image description

Let's select the t2.micro instance type, which provides 1 vCPU and 1 GiB of memory.

Normally, you'd create a key pair to enable secure SSH access to the EC2 instance. But in this case, we'll skip the key pair since we'll be using AWS Systems Manager to connect, rather than direct SSH.

Image description

In Network settings, click the Edit button to configure the EC2 instance's networking. Associate the new instance with the Amazon VPC and private subnet we set up earlier.

For Firewall (security groups), choose Select existing security group and pick the Web Server Security Group you created earlier. This configuration will allow incoming traffic on port 80 from the public subnets.

Image description

Expand Advanced details. Under IAM instance profile, choose WebServerInstanceProfile. This is the instance profile we created earlier, which will allow us to privately connect to the server.

Image description

!/bin/bash

yum update -y

Install Session Manager agent

yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
systemctl enable amazon-ssm-agent

Install and start the php web server

dnf install -y httpd wget php-json php
chkconfig httpd on
systemctl start httpd
systemctl enable httpd

Install AWS SDK for PHP

wget https://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.zip
unzip aws.zip -d /var/www/html/sdk
rm aws.zip

Install the web pages for our lab

if [ ! -f /var/www/html/index.html ]; then
rm index.html
fi
cd /var/www/html
wget https://ws-assets-prod-iad-r-iad-ed304a55c2ca1aee.s3.us-east-1.amazonaws.com/2aa53d6e-6814-4705-ba90-04dfa93fc4a3/index.php

Update existing packages

dnf update -y

We want the server to run a script on boot that installs the necessary PHP web server components. We can accomplish this by specifying User data.

Enter the code above into the User data field.

Image description

Click Launch Instance to complete the configuration and launch the new web server.

Image description

Once the instance is launched, you'll see a success message. Click on the underlined Amazon EC2 instance ID to navigate back to the EC2 dashboard.

Wait until the Status check shows 2/2 checks passed.

In the Amazon EC2 dashboard, select the web server instance. You'll notice it only has a private IP address, not a public one.

Ensure the web server instance is still selected and click Connect.

Image description

Select the Session Manager tab and click Connect.

Image description

Take a moment to marvel at the web server shell, then proceed to run the following commands:

echo -n 'Private IPv4 Address: ' && ifconfig enX0 | grep -i mask | awk '{print $2}'| cut -f2 -d: && \
echo -n 'Public IPv4 Address: ' && curl checkip.amazonaws.com

Image description

Excellent, there's the private IP address you noted in step 1 This means we successfully connected to the right instance without exposing the SSH protocol. However, you'll also notice there is a Public IP address - this is the Elastic IP allocated for the NAT Gateway. The NAT Gateway allows resources in the private subnet, like our web server, to communicate with the Internet.

Browse to the EC2 service.

From the navigation menu, click on the Load Balancers link, then click Create load balancer.

Image description

The Application Load Balancer (ALB) operates at the application layer, providing advanced traffic routing capabilities, in contrast to other load balancer options like the Network Load Balancer which functions at the network layer.

Click Create under Application Load Balancer.

Image description

Configure the Application load balancer with the following basic and network settings:
Image description

Image description

In the Security Groups section, delete the default group and add the Load Balancer Security Group you created earlier. This configuration will allow incoming traffic on port 80 from the Internet.

Image description

Leave the default settings of HTTP and port 80 in the Listeners and routing section, then click Create target group.

A target group defines the targets (e.g. EC2 instances) that the load balancer will route traffic to. Configure the new target group with the following settings:

Image description

Image description

Image description

Click Next to proceed to the register targets settings.
Image description

Image description

Select mywebserver and click include as pending below. This will configure the load balancer to route web traffic from the Internet to the EC2 web server instance

Image description

Click Create target group to finalize the setup, then close the browser tab to return to the load balancer configuration

Image description

In the Listeners and routing section, click the refresh button and select the WebServerTargetGroup we just created.

Image description

Leave the remaining settings as default and click Create load balancer

Image description

Browse to the Amazon S3 service.

Image description

Click Create Bucket

Image description

Let's give the S3 bucket a fun and unique name. Think of something clever or memorable. It needs to be globally unique.

Image description

Leave the other settings as the defaults, then click Create bucket.

Image description

Image description

Click on the name of the S3 bucket you created, then click Upload.

Image description

Image description

Use the S3 console to upload the files you downloaded. Click Add files

Image description

Browse to the IAM service.

Image description

Navigate to Roles, then search for and click on the WebServerInstanceProfile role.

Image description

Image description

Under Permission policies, click Add permissions and select Attach policies

Image description

Search for s3. Select the AmazonS3ReadOnlyAccess AWS managed policy and click Add permissionsExpand the AmazonS3ReadOnlyAccess AWS managed policy attached to the role, and review the JSON policy definition.

Image description

Browse to the EC2 service.

Image description

Image description

Then, build the Auto Scaling Group, which can run alongside the existing Amazon EC2 instance without the need to delete it.

Image description

Follow image

Image description

Image description

Image description

Image description

Image description

Using load balancer DNS Link

Image description

Image description