DEV Community

Cover image for Project 2: Cloud Fun Facts Generator Project with Terraform
Ulhas Narwade
Ulhas Narwade

Posted on Originally published at builder.aws.com

Project 2: Cloud Fun Facts Generator Project with Terraform

Part 2 of 12 — 12 Hands-On AWS Projects

Introduction

Learning sticks better when you build something fun. In this project I will build a small web app that shows a random cloud or tech fun fact every time you open it, and I will put it on AWS with Terraform. It is a small project, but it teaches the full loop: write the code, provision the server, deploy the page, and share the link with friends.

What you will build

  • A fun facts web page in plain HTML and JavaScript
  • A button that shows a new random fact on every click
  • An EC2 instance hosting the page (with an S3 option below)
  • The whole setup provisioned with Terraform code
  • A public link you can share with anyone

Architecture

Terraform provisions the server, the server hosts the page, and the browser shows a random fact.

Prerequisites

  • An AWS account (the free tier works for this project)
  • Terraform installed on your laptop
  • AWS CLI configured with your access key (run aws configure)
  • A key pair created in the AWS console
  • A text editor and basic comfort with the terminal

Steps

1. Build the web page

Create a file called index.html. It holds a short list of facts in JavaScript and picks one at random:

<!DOCTYPE html>
<html>
<head>
  <title>Cloud Fun Facts</title>
</head>
<body>
  <h1>Cloud Fun Fact</h1>
  <p id="fact"></p>
  <button onclick="newFact()">New Fact</button>
  <script>
    const facts = [
      "The first AWS service, S3, launched in 2006.",
      "A single Amazon S3 bucket can hold unlimited objects.",
      "The cloud runs in data centres in more than 30 regions around the world.",
      "EC2 lets you rent a virtual server in minutes, and stop paying when you stop it.",
      "Terraform lets you build and destroy the same setup again and again."
    ];
    function newFact() {
      const i = Math.floor(Math.random() * facts.length);
      document.getElementById("fact").innerText = facts[i];
    }
    newFact();
  </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Style it simply with a little CSS if you like — a big font, a calm background, and a clear button are enough.

2. Write the Terraform code

Create a main.tf file. It launches one EC2 instance and uses a user data script to install a web server and copy your page onto it:

provider "aws" {
  region = "ap-south-1"
}

resource "aws_instance" "facts_app" {
  ami           = "ami-0c614dee691244d1c"
  instance_type = "t2.micro"
  key_name      = "my-key"

  user_data = <<-EOF
    #!/bin/bash
    yum update -y
    yum install -y httpd
    systemctl start httpd
    systemctl enable httpd
  EOF

  tags = {
    Name = "cloud-fun-facts"
  }
}
Enter fullscreen mode Exit fullscreen mode

Note: the AMI ID above is an example — pick the current Amazon Linux AMI ID for your region from the console. Add a security group that allows SSH on port 22 and HTTP on port 80.

3. Upload the page

After the instance is running, copy your page to it:

scp -i my-key.pem index.html ec2-user@<Public-IP>:/home/ec2-user/
ssh -i my-key.pem ec2-user@<Public-IP>
sudo cp /home/ec2-user/index.html /var/www/html/
Enter fullscreen mode Exit fullscreen mode

Replace <Public-IP> with your instance public IP and my-key.pem with your key file name.

4. Apply the Terraform code

From the project folder, run:

terraform init
terraform plan
terraform apply
Enter fullscreen mode Exit fullscreen mode

Type yes when asked. Terraform creates the instance and the security group.

5. Open the app

Open http://<Public-IP> in your browser. You should see your fun fact and the button. Click it a few times and watch the facts change.

6. Make it your own

Add your own facts to the list in index.html, upload the file again, and refresh the page. That is the whole deploy loop — edit, upload, refresh.

S3 option

If you prefer no server at all, you can host the same page on S3 static website hosting instead of EC2. Upload index.html to a bucket, turn on static website hosting, and open the bucket website address. The page works the same way.

What you learned

  • How a static site works on a web server
  • How a user data script sets up a server at launch
  • How Terraform turns a manual setup into repeatable code
  • How a security group opens only port 80 to the world
  • How a tiny app still teaches the full deploy loop

Cleanup

Do not leave the instance running. From the project folder, run:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Type yes when asked. If you used the S3 option instead, empty the bucket and delete it from the console.

Try next

In Project 3, we go one level deeper — connecting to your Linux EC2 instance over SSH with Git Bash, Command Prompt, PuTTY and MobaXterm.


About Cloud Messenger — Learn • Build • Visualize • Troubleshoot • Grow | cloudmessenger.in | linkedin.com/in/cloud-messenger | github.com/Cloud-Messenger | #buildwithCloudMessenger

Top comments (0)