Using Terraform to deploy a basic web server on AWS

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 an…


This content originally appeared on DEV Community and was authored by Chibuzo Nwobia

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
  • Install AWS CLI:
  sudo apt-get install awscli
  aws configure

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

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
}

Step 3: Initialize Terraform

Run the following command to initialize your Terraform working directory:

terraform init

Step 4: Apply the Terraform Configuration

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

terraform apply --auto-approve

--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

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

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.


This content originally appeared on DEV Community and was authored by Chibuzo Nwobia


Print Share Comment Cite Upload Translate Updates
APA

Chibuzo Nwobia | Sciencx (2024-08-21T15:30:32+00:00) Using Terraform to deploy a basic web server on AWS. Retrieved from https://www.scien.cx/2024/08/21/using-terraform-to-deploy-a-basic-web-server-on-aws/

MLA
" » Using Terraform to deploy a basic web server on AWS." Chibuzo Nwobia | Sciencx - Wednesday August 21, 2024, https://www.scien.cx/2024/08/21/using-terraform-to-deploy-a-basic-web-server-on-aws/
HARVARD
Chibuzo Nwobia | Sciencx Wednesday August 21, 2024 » Using Terraform to deploy a basic web server on AWS., viewed ,<https://www.scien.cx/2024/08/21/using-terraform-to-deploy-a-basic-web-server-on-aws/>
VANCOUVER
Chibuzo Nwobia | Sciencx - » Using Terraform to deploy a basic web server on AWS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/21/using-terraform-to-deploy-a-basic-web-server-on-aws/
CHICAGO
" » Using Terraform to deploy a basic web server on AWS." Chibuzo Nwobia | Sciencx - Accessed . https://www.scien.cx/2024/08/21/using-terraform-to-deploy-a-basic-web-server-on-aws/
IEEE
" » Using Terraform to deploy a basic web server on AWS." Chibuzo Nwobia | Sciencx [Online]. Available: https://www.scien.cx/2024/08/21/using-terraform-to-deploy-a-basic-web-server-on-aws/. [Accessed: ]
rf:citation
» Using Terraform to deploy a basic web server on AWS | Chibuzo Nwobia | Sciencx | https://www.scien.cx/2024/08/21/using-terraform-to-deploy-a-basic-web-server-on-aws/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.