main.tf
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# main.tf — GAC compliance fixture
# Produces zero policy violations across all evaluated categories

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

locals {
  baseline_tags = {
    "stack-name"          = "gac-compliance-fixture"
    "application-name"    = "GACFixture"
    "project"             = "DC-Migration-Project"
    "environment"         = "dev"
    "cost-center-opex"    = "271"
    "cost-center-capex"   = "000"
    "data-classification" = "Internal"
    "availability-tier"   = "Tier 1"
    "app-owner"           = "Director IT Infrastructure and Platform Engineering"
    "team"                = "devops"
    "assignment-group"    = "hca-aws-support"
    "owner"               = "[email protected]"
    "business-unit"       = "Infrastructure and Planning"
    "managed-by"          = "terraform"
  }
}

# GAC-025 — Provider with all mandatory default tags
provider "aws" {
  region = "ap-south-1"

  default_tags {
    tags = {
      managed-by = "terraform"
    }
  }
}

# GAC-003 — S3 bucket with versioning enabled
resource "aws_s3_bucket" "good_bucket" {
  bucket = "good-gac-bucket"
  tags   = local.baseline_tags
}

resource "aws_s3_bucket_versioning" "good_versioning" {
  bucket = aws_s3_bucket.good_bucket.id
  versioning_configuration {
    status = "Enabled"
  }
}

# GAC-001 / STR-001 — Public access block
resource "aws_s3_bucket_public_access_block" "good_bucket" {
  bucket                  = aws_s3_bucket.good_bucket.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

# GAC-002 / STR-004 — Server-side encryption
resource "aws_s3_bucket_server_side_encryption_configuration" "good_bucket" {
  bucket = aws_s3_bucket.good_bucket.id
  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm     = "aws:kms"
      kms_master_key_id = "arn:aws:kms:ap-south-1:123456789012:key/abcd1234-a123-456a-a12b-a123b4cd56ef"
    }
  }
}

# STR-007 — Ownership controls
resource "aws_s3_bucket_ownership_controls" "good_bucket" {
  bucket = aws_s3_bucket.good_bucket.id
  rule {
    object_ownership = "BucketOwnerEnforced"
  }
}

# GAC-004 / STR-005 — TLS-only bucket policy
resource "aws_s3_bucket_policy" "good_bucket" {
  bucket = aws_s3_bucket.good_bucket.id
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Sid       = "DenyInsecureTransport"
      Effect    = "Deny"
      Principal = "*"
      Action    = "s3:*"
      Resource = [
        aws_s3_bucket.good_bucket.arn,
        "${aws_s3_bucket.good_bucket.arn}/*"
      ]
      Condition = {
        Bool = { "aws:SecureTransport" = "false" }
      }
    }]
  })
}

# GAC-012 — RDS with adequate backup retention (max 35 days)
# GAC-009 / DAT-002 — Storage encryption
# GAC-011 / DAT-004 — Deletion protection
# DAT-003 / LOM-008 — CloudWatch log exports
resource "aws_db_instance" "good_rds" {
  identifier                      = "good-gac-rds"
  engine                          = "mysql"
  engine_version                  = "8.0"
  instance_class                  = "db.t3.micro"
  allocated_storage               = 20
  username                        = "admin"
  password                        = "Goodpassword123"
  skip_final_snapshot             = true
  backup_retention_period         = 35
  storage_encrypted               = true
  deletion_protection             = true
  enabled_cloudwatch_logs_exports = ["error", "general", "slowquery"]
  tags                            = local.baseline_tags
}

# GAC-014 — IAM policy with scoped permissions
resource "aws_iam_policy" "good_admin_policy" {
  name = "good-admin-policy"
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect   = "Allow"
      Action   = ["s3:GetObject", "s3:PutObject"]
      Resource = "arn:aws:s3:::good-gac-bucket/*"
    }]
  })
  tags = local.baseline_tags
}

# GAC-015 — IAM role with scoped trust principal and specific action
# IAM-004 — Permissions boundary
resource "aws_iam_role" "good_role" {
  name                 = "good-gac-role"
  permissions_boundary = "arn:aws:iam::123456789012:policy/good-permissions-boundary"
  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect    = "Allow"
      Principal = { Service = "ec2.amazonaws.com" }
      Action    = "sts:AssumeRole"
    }]
  })
  tags = local.baseline_tags
}

# GAC-016 — Strong password policy
resource "aws_iam_account_password_policy" "good_password_policy" {
  minimum_password_length      = 14
  require_symbols              = true
  require_numbers              = true
  require_uppercase_characters = true
  require_lowercase_characters = true
  password_reuse_prevention    = 24
  max_password_age             = 90
}

# GAC-022 — CloudFront with modern TLS
resource "aws_cloudfront_distribution" "good_cf" {
  enabled = true

  default_cache_behavior {
    allowed_methods        = ["GET", "HEAD"]
    cached_methods         = ["GET", "HEAD"]
    target_origin_id       = "good-origin"
    viewer_protocol_policy = "redirect-to-https"
    forwarded_values {
      query_string = false
      cookies { forward = "none" }
    }
  }

  viewer_certificate {
    acm_certificate_arn      = "arn:aws:acm:us-east-1:123456789012:certificate/abc-123"
    ssl_support_method       = "sni-only"
    minimum_protocol_version = "TLSv1.2_2021"
  }

  origin {
    domain_name = "example.com"
    origin_id   = "good-origin"
    custom_origin_config {
      http_port              = 80
      https_port             = 443
      origin_protocol_policy = "https-only"
      origin_ssl_protocols   = ["TLSv1.2"]
    }
  }

  restrictions {
    geo_restriction { restriction_type = "none" }
  }

  tags = local.baseline_tags
}

# GAC-023 — EKS with private endpoint only
resource "aws_eks_cluster" "good_eks" {
  name     = "good-eks"
  role_arn = "arn:aws:iam::123456789012:role/eks-role"

  vpc_config {
    subnet_ids              = ["subnet-12345678"]
    endpoint_public_access  = false
    endpoint_private_access = true
  }

  tags = local.baseline_tags
}