Terraform has revolutionized infrastructure management, but its power comes with the responsibility of ensuring compliance. This guide explores common compliance gaps when using Terraform and provides practical solutions for platform engineers to enhance their security posture and meet regulatory requirements.
Failing to implement consistent and required tagging for resources can lead to non-compliance with organizational policies, difficulties in cost allocation, and challenges in resource management and security audits.
Bad Practice
1resource "aws_instance" "example" {
2 ami = "ami-0c55b159cbfafe1f0"
3 instance_type = "t2.micro"
4}
Good Practice
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Environment = "Production"
Project = "ExampleProject"
Owner = "DevOps Team"
Compliance = "PCI-DSS"
}
}
To ensure consistent and compliant resource tagging:
Misconfigured resources can lead to security vulnerabilities, operational issues, and non-compliance with industry standards or internal policies. This is often a result of inadequate policy enforcement and lack of automated compliance testing.
Bad Practice
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
acl = "public-read"
}
Good Practice
1resource "aws_s3_bucket" "example" {
2 bucket = "my-bucket"
3}
4
5resource "aws_s3_bucket_public_access_block" "example" {
6 bucket = aws_s3_bucket.example.id
7
8 block_public_acls = true
9 block_public_policy = true
10 ignore_public_acls = true
11 restrict_public_buckets = true
12}
13
14resource "aws_s3_bucket_versioning" "example" {
15 bucket = aws_s3_bucket.example.id
16 versioning_configuration {
17 status = "Enabled"
18 }
19}
To maintain compliant resource configurations:
Insufficient logging and monitoring can lead to compliance violations, security blind spots, and difficulties in auditing and incident response. This issue often stems from a lack of standardized logging practices and insufficient monitoring of Terraform operations.
Bad Practice
resource "aws_cloudtrail" "example" {
name = "example-trail"
s3_bucket_name = aws_s3_bucket.example.id
include_global_service_events = false
}
Good Practice
resource "aws_cloudtrail" "example" {
name = "example-trail"
s3_bucket_name = aws_s3_bucket.example.id
include_global_service_events = true
is_multi_region_trail = true
enable_log_file_validation = true
event_selector {
read_write_type = "All"
include_management_events = true
data_resource {
type = "AWS::S3::Object"
values = ["arn:aws:s3:::"]
}
}
}
resource "aws_cloudwatch_log_group" "example" {
name = "example-cloudtrail-logs"
retention_in_days = 365
}
resource "aws_cloudwatch_metric_alarm" "example" {
alarm_name = "terraform-apply-failures"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "FailedTerraformOperations"
namespace = "Custom/Terraform"
period = "300"
statistic = "Sum"
threshold = "0"
alarm_description = "This metric monitors failed Terraform apply operations"
alarm_actions = [aws_sns_topic.example.arn]
}
To ensure adequate logging and monitoring:
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