Skip to content

Lesson 2 of 6

Allow web traffic with a security group

Add a security group resource that opens port 80 from the internet so the server can be reached.

4 minInfrastructure

After this lesson you will be able to add a security group to your Terraform configuration that allows web traffic on port 80 from the internet.

What is a security group?

A security group is a set of firewall rules for a virtual machine in a cloud. It controls which network traffic may reach the machine and which may leave it. Each rule says what kind of traffic is allowed, such as a request to a web server on port 80, and where it may come from.

In this lesson you will write a security group that allows anyone on the internet to connect to port 80 on your future web server. Port 80 is the standard port for unencrypted web traffic. Later lessons will attach this security group to a virtual machine and run a web server on it.

Add the security group resource

Open the file main.tf in your web-server folder. It already contains the Terraform block and the provider block from lesson 1. Add the following resource block after the provider block.

resource "aws_security_group" "web" {
  name        = "web-server-sg"
  description = "Allow inbound HTTP traffic on port 80"

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

  egress {
    description = "Allow all outbound traffic"
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

The resource type is aws_security_group and its local name is web. The name is the name the security group will have in the AWS console. The description explains what the group is for.

The ingress block defines inbound rules. This one allows TCP traffic on port 80 from any IP address. The cidr_blocks list contains 0.0.0.0/0, which means every address on the internet. The egress block allows all outbound traffic. The protocol -1 means all protocols, and ports 0 to 0 mean all ports. This lets the server make requests to the internet, for example to download updates.

Format and validate the configuration

Terraform has a command that formats configuration files in a standard style. Run it now:

terraform fmt

It prints the names of files it changed. If your file was already formatted, it prints nothing.

Next, run terraform validate to check that the configuration is syntactically correct and internally consistent.

terraform validate

You should see a success message. If there is an error, read it carefully and fix the problem in the file.

Plan the change

Before applying any change, you should see what Terraform will do. Run terraform plan.

terraform plan

Terraform shows a plan that says it will create one resource, the security group. It lists the attributes it will set, such as the name, description, and ingress and egress rules. The plan does not create anything yet; it only shows what would happen.

You do not need to apply this plan now. In a later lesson you will add a virtual machine and then apply everything together. For now, the security group is defined and ready.

What you have learned

You have added a security group resource to your Terraform configuration. It allows inbound HTTP traffic on port 80 from any address and allows all outbound traffic. You have formatted and validated the configuration and seen a plan that shows the security group will be created.

Key takeaways

  • A security group is a set of firewall rules for a cloud virtual machine.
  • An ingress rule allows traffic into the machine; an egress rule allows traffic out.
  • Use cidr_blocks = ["0.0.0.0/0"] to allow traffic from any internet address.
  • Run terraform fmt to format your configuration and terraform validate to check it.
  • Always run terraform plan before applying to see what will change.

Try it

Add a second ingress rule to the security group that allows SSH traffic on port 22 from any address. Run terraform fmt and terraform validate. Then run terraform plan and confirm that the plan shows two ingress rules: one for port 80 and one for port 22. After checking, remove the SSH rule so the configuration returns to the state from this lesson, and run terraform fmt and terraform validate again. A good result is that the plan clearly lists both rules when the SSH rule is present, and the final configuration has only the HTTP rule.

Official documentation

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.

    Allow web traffic with a security group | Build a Cloud Web Server with Terraform | Rolebound