DEV Community

Cover image for Day 03: Deploying Basic Infrastructure with Terraform
Steve Yonkeu
Steve Yonkeu

Posted on

Day 03: Deploying Basic Infrastructure with Terraform

Day 3: Progress Update 🚀

Today marks Day 3 of our challenge, and I couldn’t be more excited about the progress so far! 🎉 It’s a small step, but as Neil Armstrong once said:

"That’s one small step for [a] man, one giant leap for mankind."

Each day brings us closer to mastering the fundamentals, and today’s milestone is a huge win for Terraform beginners.

We’ve successfully completed and opened our Pull Request (PR) for today, which includes an architectural diagram and a Terraform code snippet. The architecture reflects the deployment of a simple web server in a cloud environment, showcasing how Infrastructure as Code (IaC) simplifies and automates the process.

Here’s a quick sample of the Terraform code we worked on:

provider "aws" {
  region  = "us-east-1"
  profile = "terraform_learner"
}

resource "aws_vpc" "day_3" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_support   = true
  enable_dns_hostnames = true
  tags = {
    Name = "day_3_vpc"
  }
}

resource "aws_internet_gateway" "day_3" {
  vpc_id = aws_vpc.day_3.id
  tags = {
    Name = "day_3_igw"
  }
}

resource "aws_subnet" "public" {
  vpc_id                  = aws_vpc.day_3.id
  cidr_block              = "10.0.1.0/24"
  map_public_ip_on_launch = true
  availability_zone       = "us-east-1a"
  tags = {
    Name = "day_3_public_subnet"
  }
}

resource "aws_route_table" "public" {
  vpc_id = aws_vpc.day_3.id
  tags = {
    Name = "day_3_public_route_table"
  }
}

resource "aws_route" "internet_access" {
  route_table_id         = aws_route_table.public.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.day_3.id
}

resource "aws_route_table_association" "public_subnet_association" {
  subnet_id      = aws_subnet.public.id
  route_table_id = aws_route_table.public.id
}

resource "aws_security_group" "web_sg" {
  vpc_id = aws_vpc.day_3.id

  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 = "day_3_web_sg"
  }
}

resource "aws_instance" "learn_tf_ec2" {
  ami                    = "ami-0c02fb55956c7d316"
  instance_type          = "t2.micro"
  subnet_id              = aws_subnet.public.id
  vpc_security_group_ids = [aws_security_group.web_sg.id]

  tags = {
    Name = "day_3_ec2"
  }

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

output "webserver_ip" {
  value       = aws_instance.learn_tf_ec2.public_ip
  description = "Public IP of the EC2 instance"
}
Enter fullscreen mode Exit fullscreen mode

Ingress and Egress

Aspect Ingress Egress
Direction Traffic entering a resource. Traffic leaving a resource.
Use Case Control what can access the resource. Control what the resource can access.
Examples HTTP requests to a web server. A server accessing an external API.
Configuration Focus on source addresses. Focus on destination addresses.

Output:

Output

The journey has just begun, but the excitement of creating something from scratch keeps building! Let’s keep pushing forward—Day 4, here we come! 💪

Top comments (0)