How to Create an AWS Security Group in Terraform - Securing AWS Resources With Terraform Security Groups

  • Home /
  • Blog Posts /
  • How to Create an AWS Security Group in Terraform - Securing AWS Resources with Terraform Security Groups

Security groups are a critical component for securing resources in AWS. This guide will show you how to create and manage security groups in AWS using Terraform, a popular infrastructure-as-code tool .

Overview of AWS Security Groups

Security groups act as a firewall to control inbound and outbound traffic to AWS resources like EC2 instances, RDS databases, ELB load balancers, etc. They operate at the instance level, not the subnet level.

Some key characteristics of security groups:

  • Default deny all inbound, allow all outbound
  • Stateful - Return traffic automatically allowed
  • Supports allow rules only, not deny
  • VPC scoped - can’t cross VPC boundaries

Benefits of Managing Security Groups in Terraform

There are several benefits to using Terraform to configure AWS security groups:

  • Infrastructure as Code - Terraform scripts serve as documented source of truth for security group rules. Changes are made to scripts, not manually via AWS console.
  • Prevention of Configuration Drift - Terraform plans/applies ensure security groups don’t drift from desired state.
  • Built-in Dependency Management - Terraform automatically handles dependencies between security groups and other resources.
  • Validation - Terraform validates syntax and detects errors prior to applying changes.
  • Multi-provider - Terraform can manage security groups across AWS, Azure, GCP and other providers.

How to Create a Security Group in Terraform

Below is a simple example for creating a security group in Terraform:

resource "aws_security_group" "app_sg" {
  name        = "app-sg"
  description = "Allow traffic for application"
  vpc_id      = aws_vpc.main.id
  
  ingress {
    from_port   = 80 
    to_port     = 80
    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 = "Application Security Group"
  }
}

This creates a security group named “app-sg” that allows inbound TCP traffic on port 80 and all outbound traffic.

Some key parameters:

  • name - Name tag for the security group.
  • description - Optional description.
  • vpc_id - ID of the VPC to associate this security group with.
  • ingress/egress - Rules to allow inbound/outbound traffic.
  • protocol, from_port, to_port, cidr_blocks - Specify source/destination for traffic.

For complete documentation of all available arguments for the aws_security_group resource, refer to the Terraform AWS Provider Docs .

Tips for Effective Security Groups

Here are some tips for creating effective security groups in Terraform:

  • Least privilege - Start with deny all inbound, allow minimum required.
  • Scope to resource - Avoid using overly broad cidr ranges.
  • Stateful rules - No need to allow return outbound traffic.
  • Target resources - Apply security group rules only to necessary resources.
  • Separate concerns - Use different security groups for front-end, back-end, data layers.

By leveraging Terraform to implement robust security groups, you can effectively secure AWS resources and infrastructure.