Lesson 5 of 10
Infrastructure as Code Security
Infrastructure as Code (IaC) allows security to be automated, version-controlled, and consistently applied across environments. However, insecure IaC templates can deploy misconfigurations at scale - a poorly written Terraform file can create thousands of publicly exposed resources.
Terraform Security Best Practices
# Terraform - secure infrastructure patterns
# INSECURE: Open security group (all traffic from anywhere)
resource "aws_security_group" "web" {
ingress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # ← Open to the whole internet - DANGEROUS
}
}
# SECURE: Minimal necessary access
resource "aws_security_group" "web" {
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # HTTPS from anywhere - acceptable for public web
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = [aws_security_group.bastion.id] # SSH only from bastion
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] # Allow all outbound (restrict if possible)
}
}
# S3 bucket with encryption and access controls
resource "aws_s3_bucket" "data" {
bucket = "company-sensitive-data"
}
resource "aws_s3_bucket_server_side_encryption_configuration" "data" {
bucket = aws_s3_bucket.data.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms" # KMS encryption (not S3 AES256)
kms_master_key_id = aws_kms_key.s3.arn
}
}
}
resource "aws_s3_bucket_public_access_block" "data" {
bucket = aws_s3_bucket.data.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true # Prevent any public access
}IaC Security Scanning
# Automated security scanning for IaC templates
# tfsec - Terraform security scanner
pip install tfsec
tfsec ./terraform/
# OUTPUT:
# HIGH: Security group allows unrestricted access to port 22
# MEDIUM: S3 bucket does not have versioning enabled
# LOW: Resource does not have tags applied
# Checkov - multi-format IaC scanner (Terraform, CloudFormation, Kubernetes, Dockerfiles)
pip install checkov
checkov -d ./terraform/ # Scan Terraform
checkov -d ./kubernetes/ # Scan Kubernetes manifests
checkov -f Dockerfile # Scan Dockerfile
# Example Checkov output:
# FAILED for resource: aws_s3_bucket.data
# Check: CKV_AWS_18: Ensure the S3 bucket has access logging enabled
# FAILED for resource: aws_security_group.web
# Check: CKV_AWS_25: Ensure aws_security_group_rule does not open all ports
# Terrascan - cloud-native IaC scanner
terrascan scan -i terraform -d ./terraform/
# Integrate in CI/CD (GitHub Actions):
# - name: Run tfsec
# uses: aquasecurity/tfsec-action@v1.0.0
# with:
# soft_fail: false # Fail the pipeline
# severity: HIGH # Only fail on HIGH and CRITICALCommon Mistakes
- Storing Terraform state with sensitive data in unencrypted S3 - Terraform state contains all resource details including passwords; use S3 with KMS encryption and DynamoDB state locking
- Not tagging resources for cost allocation and security - untagged resources are difficult to associate with teams/owners for security reviews and cost management
- Importing manually-created resources without reviewing their security - 'terraform import' brings resources under IaC control but doesn't change configuration; review all imported resources for security issues
Tip
Tip
Practice Infrastructure as Code Security in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
The foundational security model: Confidentiality (privacy), Integrity (trust), and Availability (uptime).
Practice Task
Note
Practice Task - (1) Write a working example of Infrastructure as Code Security from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Infrastructure as Code Security is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.
Key Takeaways
- Infrastructure as Code (IaC) allows security to be automated, version-controlled, and consistently applied across environments.
- Storing Terraform state with sensitive data in unencrypted S3 - Terraform state contains all resource details including passwords; use S3 with KMS encryption and DynamoDB state locking
- Not tagging resources for cost allocation and security - untagged resources are difficult to associate with teams/owners for security reviews and cost management
- Importing manually-created resources without reviewing their security - 'terraform import' brings resources under IaC control but doesn't change configuration; review all imported resources for security issues
Finished this lesson?