Using Terraform To Deploy a Web Server and Run a Bootstrap Script

Terraform isn’t just for big, end-to-end projects. As a Cloud or DevOps engineer, you’ll often work on smaller but critical tasks like creating and connecting a database server, setting up a security group, or running a bootstrap script on a web server.

For beginners, these tasks are great practice because they reflect what you’ll do in enterprise environments. I created a step-by-step tutorial to walk you through some of the daily tasks you might encounter. 

This tutorial walks you through deploying a simple AWS setup using Terraform. You’ll create a database server, a web server with a fixed IP address, configure security groups, and run a bootstrap script on the web server.

Scenario

As the DevOps engineer on duty, your supervisor has asked you to complete the following tasks:

  1. Create a database (DB) server and output its private IP.
  2. Create a web server with a fixed public IP.
  3. Create a security group for the web server, opening ports 80 (HTTP) and 443 (HTTPS).
  4. Run a provided bootstrap script on the web server.

Step 1: Create a Database Server and Output Its Private IP

  1. Create the project folders
  • Open VS Code.
  • Create a folder named WebServer.
  • Inside it, create another folder named dbserverchallenge.

You’ll find all the code source for this tutorial in this repo folder: Web Server.

2. Add the server script

  • Inside dbserverchallenge, create a file named server-script.sh 
  • And copy the bootstrap script into it.

3. Create the Terraform configuration

  • Create a main.tf file in the same folder.
  • Add the following Terraform code to launch an EC2 instance for the database server:

Step 2: Create a Web Server with a Fixed Public IP

1. Update the instance configuration

  • Create another EC2 instance for the web server.
  • In your main.tf, add:

2. Create an Elastic IP

  • Add the following resource to allocate a fixed public IP for the web server:

Step 3: Create a Security Group for the Web Server

I’ll create a security group that allows inbound HTTP (80) and HTTPS (443) traffic.

1. Define variables (optional)

Add variables for the ports:

2. Create the security group

Add this to main.tf:

3. Attach the security group to the web server

Update the aws_instance.web resource.

4. Output the public IP

Add this output at the end of your main.tf

Step 4: Deploy with Terraform

  • From your terminal, navigate to the project directory and run:
terraform init
terraform apply
Terraform will:
  • Initialize the project.
  • Deploy your database server, web server, Elastic IP, and security group.

If you run into issues with the user_data script:

  • Double-check that the server-script.sh file has executable permissions:

    chmod +x server-script.sh
  • Verify that the file path matches what you reference in Terraform.

You’ve successfully:

  1. Built a database server and web server using Terraform.
  2. Attached a fixed IP to the web server.
  3. Configured security groups for secure traffic.
  4. Automated server setup with a bootstrap script.

Leave a Reply

Your email address will not be published. Required fields are marked *