skip to Main Content

What is the simplest terraform file you can create to put Apache on and EC2 and give it a public IP on the internet?

I found this way works. But I’m wondering if there is something simpler.

# Network

resource "aws_vpc" "app_vpc" {
  cidr_block = "10.123.0.0/16"
}

resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.app_vpc.id
}

resource "aws_subnet" "public_subnet" {
  vpc_id            = aws_vpc.app_vpc.id
  cidr_block        = "10.123.0.0/24"
  map_public_ip_on_launch = true
  availability_zone = "eu-west-2a"
}

resource "aws_route_table" "public_rt" {
  vpc_id = aws_vpc.app_vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }
}

resource "aws_route_table_association" "public_rt_asso" {
  subnet_id      = aws_subnet.public_subnet.id
  route_table_id = aws_route_table.public_rt.id
}


# Security Group

resource "aws_security_group" "allow-http" {
  name        = "allow-http"
  description = "Allow incoming HTTP traffic"
  vpc_id = aws_vpc.app_vpc.id
  
  ingress {
    description      = "HTTP from VPC"
    from_port        = 80
    to_port          = 80
    protocol         = "tcp"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

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


# EC2

resource "aws_instance" "apache" {
  ami                         = data.aws_ami.ubuntu_ami.id
  instance_type               = "t2.micro"
  subnet_id                   = aws_subnet.public_subnet.id
  vpc_security_group_ids      = [aws_security_group.allow-http.id]
  associate_public_ip_address = true

  user_data = <<-EOF
  #!/bin/bash
  echo "*** Installing apache2"
  sudo apt update -y
  sudo apt install apache2 -y
  echo "*** Completed Installing apache2"
  EOF
}

2

Answers


  1. It seems like your Terraform config is already relatively simplistic. However, if you want to simplify further, you can look into some TF modules, such as this AWS VPC Module to simplify the creation of the VPC. You can also look into similar modules for Security Groups and EC2 Instances.

    Your config is already relatively simple, so I wouldn’t expect a huge reduction in your codebase. You are also introducing an external dependency on the module maintainer(s), which is something to consider. I would also consider looking into different AMIs available on AWS – It might have something already available and configured with apache.

    Login or Signup to reply.
  2. Have you tried this?

    # Network
    resource "aws_vpc" "app_vpc" {
      cidr_block = "10.123.0.0/16"
    }
    
    resource "aws_subnet" "public_subnet" {
      vpc_id            = aws_vpc.app_vpc.id
      cidr_block        = "10.123.0.0/24"
      map_public_ip_on_launch = true
      availability_zone = "eu-west-2a"
    }
    
    resource "aws_instance" "apache" {
      ami                         = data.aws_ami.ubuntu_ami.id
      instance_type               = "t2.micro"
      subnet_id                   = aws_subnet.public_subnet.id
      associate_public_ip_address = true
    
      user_data = <<-EOF
      #!/bin/bash
      echo "*** Installing apache2"
      sudo apt update -y
      sudo apt install apache2 -y
      echo "*** Completed Installing apache2"
      EOF
    }
    
    1. The Internet Gateway and Route Table have been removed as they are not necessary for a basic setup.
    2. The VPC, Subnet, and EC2 instance setup remain to create a basic VPC, a public subnet, and launch an EC2 instance with Apache installed.

    This configuration still accomplishes the goal of creating a VPC with a public subnet and an EC2 instance running Apache but is simplified by removing some of the additional components.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search