DEV Community

Chibuzo Nwobia
Chibuzo Nwobia

Posted on

Using Terraform to deploy a basic web server on AWS

Introduction:

Deploying a web server on AWS using Terraform offers a powerful way to automate infrastructure management. By defining your infrastructure as code, you can easily create, update, and manage AWS resources. This guide will walk you through the step-by-step process of setting up Terraform, configuring AWS, and deploying a basic web server. Whether you're new to Terraform or looking to refine your skills, this guide provides clear instructions to help you get started.

To deploy a basic web server on AWS using Terraform follow the steps below:

Step 1: Install Terraform and AWS CLI

Ensure Terraform and AWS CLI are installed on your system and configured with your AWS credentials. If not, you can install and configure them using these commands:

  • Install Terraform:
  sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
  curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
  sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
  sudo apt-get update && sudo apt-get install terraform
Enter fullscreen mode Exit fullscreen mode
  • Install AWS CLI:
  sudo apt-get install awscli
  aws configure
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Terraform Configuration

Create a directory for your Terraform configuration files and navigate to it:

mkdir terraform-web-server
cd terraform-web-server
Enter fullscreen mode Exit fullscreen mode

Now, create a main.tf file with the following content:

# Specify the AWS provider
provider "aws" {
  region = "us-east-1"  # Replace with your desired region
}

# Create a Security Group to allow HTTP and SSH access

resource "aws_security_group" "web_sg" {
  name        = "web-sg"
  description = "Allow HTTP and SSH inbound traffic"

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

# Create an EC2 instance
variable "key_pair_name" {
  description = "The name of the key pair to use fr the EC2 insance"
  type        = string
}

resource "aws_instance" "web_server" {
  ami           = "ami-066784287e358dad1" 
  instance_type = "t2.micro"
  key_name      = var.key_pair_name  # Replace with your key pair

  # Use the security group

  vpc_security_group_ids = [aws_security_group.web_sg.id]

  # User data to install a basic web server
  user_data = <<-EOF
              #!/bin/bash
              sudo yum update -y
              sudo yum install -y httpd
              sudo systemctl start httpd
              sudo systemctl enable httpd
              echo "Hello, Chibuzo's demo page!" > /var/www/html/index.html
            EOF

  tags = {
    Name = "Terraform-Web-Server"
  }
}

# Output the public IP of the instance

output "instance_public_ip" {
  value = aws_instance.web_server.public_ip
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Initialize Terraform

Run the following command to initialize your Terraform working directory:

terraform init
Enter fullscreen mode Exit fullscreen mode

Step 4: Apply the Terraform Configuration

Run the following command to create the resources defined in your configuration:

terraform apply --auto-approve
Enter fullscreen mode Exit fullscreen mode

--auto-approve automatically approves the applied configuration hence reducing the need to make a user input.

applied_config

Step 5: Access the Web Server

Once the deployment is complete, Terraform will output the public IP of the EC2 instance.

public_ip

You can access the web server by visiting http://<instance_public_ip> in your web browser. You should see a message saying "Hello, Chibuzo's demo page!".

demo_page

Step 6: Clean Up Resources

When you're done, you can destroy the resources created by Terraform using the following command:

terraform destroy --auto-approve
Enter fullscreen mode Exit fullscreen mode

Terraform will now remove all the resources it created.

If in the process of trying to create an instance, you do not have a valid key-pair, you can create one using the following commands:

How to Create a Key Pair

  1. Via AWS CLI: You can also create a key pair using the AWS CLI:
   aws ec2 create-key-pair --key-name my-key-pair --query 'KeyMaterial' --output text > my-key-pair.pem
Enter fullscreen mode Exit fullscreen mode

This will create a key pair named my-key-pair and save the private key to a file named my-key-pair.pem.

Make sure the private key file (my-key-pair.pem) is accessible and has the correct permissions (e.g., chmod 400 my-key-pair.pem) for SSH access.

Conclusion:

With your web server successfully deployed using Terraform, you've taken a significant step in automating infrastructure management. From installing Terraform and AWS CLI to configuring security groups and launching an EC2 instance, you now have the knowledge to manage cloud infrastructure efficiently. By following the cleanup steps, you can also ensure that resources are properly decommissioned when no longer needed. This approach not only streamlines your workflow but also enhances the scalability and reliability of your deployments.

Top comments (0)