In the world of infrastructure as code (IaC), Terraform has become a powerful tool for managing cloud resources. However, with great power comes great responsibility, especially when it comes to security. This guide explores common pitfalls in resource exposure and provides practical solutions for platform engineers to enhance their Terraform security posture.
One of the most critical security risks in Terraform configurations is the unintended exposure of resources to the public internet. This can lead to unauthorized access, data breaches, and potential exploitation of unprotected cloud assets[1].
Bad Practice
1resource "aws_s3_bucket" "example" {
2 bucket = "my-public-bucket"
3 acl = "public-read"
4}
5
6resource "aws_security_group" "example" {
7 name = "allow_all"
8 description = "Allow all inbound traffic"
9
10 ingress {
11 from_port = 0
12 to_port = 0
13 protocol = "-1"
14 cidr_blocks = ["0.0.0.0/0"]
15 }
16}
Good Practice
resource "aws_s3_bucket" "example" {
bucket = "my-private-bucket"
}
resource "aws_s3_bucket_public_access_block" "example" {
bucket = aws_s3_bucket.example.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_security_group" "example" {
name = "restricted_access"
description = "Allow specific inbound traffic"
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["10.0.0.0/8"]
}
}
To mitigate the risk of exposing resources publicly:
Inadequate security group configurations can lead to overly permissive firewall rules, exposing services to unnecessary risks. This issue often stems from misconfigured access controls and a lack of proper network segmentation
Bad Practice
resource "aws_security_group" "wide_open" {
name = "wide_open"
description = "Allow all traffic"
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
Good Practice
1resource "aws_security_group" "web_server" {
2 name = "web_server"
3 description = "Allow inbound traffic for web server"
4
5 ingress {
6 description = "HTTPS from VPC"
7 from_port = 443
8 to_port = 443
9 protocol = "tcp"
10 cidr_blocks = [aws_vpc.main.cidr_block]
11 }
12
13 egress {
14 from_port = 0
15 to_port = 0
16 protocol = "-1"
17 cidr_blocks = ["0.0.0.0/0"]
18 }
19
20 tags = {
21 Name = "allow_web_traffic"
22 }
23}
To ensure robust security group configurations:
Using default VPC configurations without customization can lead to insecure network architectures and exposed management interfaces. This practice often results in inadequate network isolation and potential security vulnerabilities3.
Bad Practice
terraform {
backend "local" {
path = "terraform.tfstate"
}
}
Good Practice
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-west-2"
encrypt = true
dynamodb_table = "terraform-locks"
}
}
To enhance security when working with VPCs:
By addressing these common pitfalls and implementing secure practices, platform engineers can significantly enhance the security posture of their Terraform-managed infrastructure. Remember, security is an ongoing process that requires constant vigilance and adaptation to new threats and best practices.
Terraform has become an essential tool for managing cloud infrastructure as code. However, with great power comes great responsibility, especially when it comes to access management and authentication. In this guide, we'll explore common pitfalls and provide secure solutions to help platform engineers enhance their Terraform security posture.
Citations:
[1] https://spacelift.io/blog/terraform-security
[2] https://docs.aws.amazon.com/pdfs/prescriptive-guidance/latest/terraform-aws-provider-best-practices/terraform-aws-provider-best-practices.pdf
[3] https://stackoverflow.com/questions/44056551/preventing-terraform-aws-vpc-from-creating-default-resources
[4] https://www.wiz.io/academy/terraform-security-best-practices
[5] https://cycode.com/blog/7-terraform-security-best-practices/
[6] https://zeet.co/blog/terraform-security