Use Amazon CloudFront for dynamic Websites -1
Tags: s3, cloudfront, ec2, elasticloadbalancing
Published: 2023-09-25 12:54:52

# What is Amazon CloudFront?

Please read my previous article **[Understanding Amazon CloudFront](https://dev.to/aws-builders/understanding-amazon-cloudfront-674)**

- ***Note:*** CloudFront allows you to configure cache control and Time-To-Live (TTL) settings, giving you control over how long content is cached at the edge locations. 

- This allows you to balance the freshness of content with cache hit rates.
- CloudFront has features similar to dynamic site acceleration, a method used to improve online content delivery.
- CloudFront accelerates the delivery of dynamic content by moving it closer to the user to minimize internet hops involved in retrieving the content.

- You create a CloudFront distribution to tell CloudFront where you want content to be delivered from, and the details about how to track and manage content delivery.

Please visit my **[GitHub Repository for S3 articles](https://github.com/awsmine/S3_projects)** on various topics being updated on constant basis.

_**Let’s get started!**_

I have divided this article into 3 parts for understanding this process better.

# Objectives:

***Part 1***
**1.** Sign into AWS Management Console.

**2.** Launch two EC2 Instances ***`my-EC2`*** using Bash script.

***Part 2***
**3.** Create a Target Group ***`my-TG`***.

**4.** Create an application Load Balancer.

***Part 3***
**5.** Create the CloudFront Distribution.

**6.** Test the distribution.

# Pre-requisites:
- AWS user account with admin access, not a root account.


# Resources Used:

- [What is Amazon EC2?](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html)

- [What is Amazon CloudFront?](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Introduction.html)

- [Working with distributions](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-working-with.html)

- [What is Elastic Load Balancing?](https://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/what-is-load-balancing.html)

# Steps for implementation to this project:

## 1. Sign into AWS Management Console

- Make sure you're in the N. Virginia (us-east-1) region

## 2. Launch two EC2 Instances ***`my-EC2`*** using Bash script.

##### 1.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eklyl1qpoqo43bx3sa4s.png)

##### 2.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqdimh6cdux7bjal6sfb.png)

##### 3. Proceed without a key pair.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4yp9268wr69p4ixqkhfc.png)

##### 4. Under Network Settings, choose Create New Security group.
 - Check the Allow SSH traffic from
 - Check the Allow HTTP traffic from the internet.
 - Check the Allow HTTPs traffic from the internet.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usdn1tqfov59arq6lbfe.png)

##### 5. Under, User data: section, copy and paste the script to install Apache webserver and php and copy the php file from S3 bucket to the server path.

- user data

```
#!/bin/bash

# Switch to the superuser to execute commands with elevated privileges
sudo su

# Update the package repository and install Apache web server (httpd) and PHP
yum update -y
yum install httpd php -y

# Start the Apache web server
systemctl start httpd

# Enable the Apache web server to start automatically on system boot
systemctl enable httpd

# Change the current directory to the web server's root directory
cd /var/www/html/

# Download the sample PHP webpage to the web server's root directory
wget https://s3.amazonaws.com/joshiawssolutions.com/index.php

# Restart the Apache web server to apply the changes and serve the PHP webpage
systemctl restart httpd
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/md50l16ew82tke7ml297.png)

##### 6. Under Summary, Enter number of instances as 2.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kupnlilqoxwthqryn7fk.png)

##### 7. Launch Instance

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4qxjyedw7nb1d5ofus1.png)

##### 8. View the PHP file in the webserver:
- 1. click on the 1st instance ID and copy the IPv4 Public IP of this instance.

```
http://<IPv4 public IP>/index.php

http://34.207.132.4/index.php
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hua3txgdvf62kguhta1i.png)

- 2. click on the 2nd instance ID and copy the IPv4 Public IP of this instance.

```
http://<IPv4 public IP>/index.php

http://34.227.87.42/index.php
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53ldct758evkoer5kxj7.png)