Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 intoTerraform'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=80to_port=80protocol="tcp"cidr_blocks=["0.0.0.0/0"]}ingress{from_port=443to_port=443protocol="tcp"cidr_blocks=["0.0.0.0/0"]}egress{from_port=0to_port=0protocol="-1"cidr_blocks=["0.0.0.0/0"]}tags={Name="Web Server SG"}}resource"aws_instance""web_server"{count=var.instance_countami=var.ami_idinstance_type=var.instance_typesecurity_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 modeExit 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=80to_port=80protocol="tcp"cidr_blocks=["0.0.0.0/0"]}ingress{from_port=443to_port=443protocol="tcp"cidr_blocks=["0.0.0.0/0"]}egress{from_port=0to_port=0protocol="-1"cidr_blocks=["0.0.0.0/0"]}tags={Name="Clustered Web Server SG"}}resource"aws_instance""cluster_web_server"{count=var.cluster_instance_countami=var.ami_idinstance_type=var.instance_typesecurity_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 modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

If you can't code, document it
  • Location
    0.0.0.0/0
  • Education
    University of ... (kidding Self Thought)
  • Pronouns
    He/Him/His
  • Work
    Backend and Cloud Engineer
  • Joined

More fromSteve Yonkeu

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp