DEV Community

Cover image for Day 04: Terraform Code for Clustered Web Server
Steve Yonkeu
Steve Yonkeu

Posted on

Day 04: Terraform Code for Clustered Web Server

Today we'll dive deeper into Terraform as we continue the challenge of mastering Day 4: Basic Infrastructure. The focus is on developing a more robust understanding of the input variables. Local variables and in deploying increasingly complex infrastructure. These concepts are critical to making Terraform configurations reusable, dynamic, and scalable.

Keywords

  • A cluster is a group of computers. Connected servers (servers) that work together as a system to improve performance. Availability and scalability for any application or task...
  • A web server is software or hardware that stores, processes, and delivers websites or web applications to users over the Internet using protocols such as HTTP or HTTPS.

Key Learnings:

  • Input Variables: Input variables allow us to make our configurations flexible by passing values at runtime. This eliminates hardcoding, making our scripts adaptable for multiple environments.
  • Local Variables: Local variables simplify complex expressions and enable reusable configurations by reducing redundancy.

Activities:

  1. Configurable Web Server: We deployed a web server using input variables to define properties such as instance size, AMI, and tags dynamically.
  2. Clustered Web Server: A significant milestone was deploying a highly available web application using multiple EC2 instances across availability zones for redundancy and reliability.
  3. Documentation Exploration: Diving into Terraform's documentation provided deeper insights into providers, resource blocks, and workflows, reinforcing our understanding of the foundational concepts.

Takeaways:

The experience was challenging and rewarding. The use of high availability setups emphasizes the importance of infrastructure (IaC) as code in modern development practices. Promise to continue traveling.

Terraform Code for Configurable Web Server

provider "aws" {
  region = var.aws_region
}

variable "aws_region" {
  default = "us-east-1"
}

variable "instance_count" {
  default = 1
}

variable "instance_type" {
  default = "t2.micro"
}

variable "ami_id" {
  default = "ami-0c02fb55956c7d316"
}

resource "aws_security_group" "web_sg" {
  name_prefix = "web-sg-"

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

  ingress {
    from_port   = 443
    to_port     = 443
    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"]
  }

  tags = {
    Name = "Web Server SG"
  }
}

resource "aws_instance" "web_server" {
  count         = var.instance_count
  ami           = var.ami_id
  instance_type = var.instance_type
  security_groups = [aws_security_group.web_sg.name]

  tags = {
    Name = "Web Server ${count.index + 1}"
  }

  user_data = <<-EOF
              #!/bin/bash
              sudo yum update -y
              sudo yum install -y httpd
              sudo systemctl start httpd
              sudo systemctl enable httpd
              echo "<h1>Welcome to Terraform Configurable Web Server</h1>" > /var/www/html/index.html
              EOF
}

output "web_server_ips" {
  value = aws_instance.web_server[*].public_ip
}
Enter fullscreen mode Exit fullscreen mode

Terraform Code for Clustered Web Server

provider "aws" {
  region = var.aws_region
}

variable "aws_region" {
  default = "us-east-1"
}

variable "cluster_instance_count" {
  default = 3
}

variable "instance_type" {
  default = "t2.micro"
}

variable "ami_id" {
  default = "ami-0c02fb55956c7d316"
}

resource "aws_security_group" "cluster_sg" {
  name_prefix = "cluster-sg-"

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

  ingress {
    from_port   = 443
    to_port     = 443
    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"]
  }

  tags = {
    Name = "Clustered Web Server SG"
  }
}

resource "aws_instance" "cluster_web_server" {
  count         = var.cluster_instance_count
  ami           = var.ami_id
  instance_type = var.instance_type
  security_groups = [aws_security_group.cluster_sg.name]

  tags = {
    Name = "Cluster Web Server ${count.index + 1}"
  }

  user_data = <<-EOF
              #!/bin/bash
              sudo yum update -y
              sudo yum install -y httpd
              sudo systemctl start httpd
              sudo systemctl enable httpd
              echo "<h1>Welcome to Terraform Clustered Web Server ${count.index + 1}</h1>" > /var/www/html/index.html
              EOF
}

output "cluster_web_server_ips" {
  value = aws_instance.cluster_web_server[*].public_ip
}
Enter fullscreen mode Exit fullscreen mode

Social Media Post

🔥 Deployed a highly available web app today! I'm beginning to enjoy the benefits of IaC.

Let’s keep building and learning. Onward to Day 5!

Top comments (0)