1+ package main
2+
3+ # Cost Control Policy
4+ # Checks for expensive instance types and configurations
5+
6+ # Get all EC2 instances from terraform plan
7+ ec2_instances[instance] {
8+ instance := input.resource_changes[_]
9+ instance.type == " aws_instance"
10+ }
11+
12+ # Get all RDS instances from terraform plan
13+ rds_instances[instance] {
14+ instance := input.resource_changes[_]
15+ instance.type == " aws_db_instance"
16+ }
17+
18+ # Get all RDS clusters from terraform plan
19+ rds_clusters[cluster] {
20+ cluster := input.resource_changes[_]
21+ cluster.type == " aws_rds_cluster"
22+ }
23+
24+ # List of expensive EC2 instance types
25+ expensive_ec2_types := {
26+ " m5.24xlarge" , " m5.16xlarge" , " m5.12xlarge" ,
27+ " r5.24xlarge" , " r5.16xlarge" , " r5.12xlarge" ,
28+ " c5.24xlarge" , " c5.18xlarge" , " c5.12xlarge" ,
29+ " x1.32xlarge" , " x1.16xlarge" ,
30+ " r4.16xlarge" , " r4.8xlarge" ,
31+ " m4.16xlarge" , " m4.10xlarge" ,
32+ " c4.8xlarge" ,
33+ " p3.16xlarge" , " p3.8xlarge" , " p3.2xlarge" ,
34+ " p2.16xlarge" , " p2.8xlarge" ,
35+ " g3.16xlarge" , " g3.8xlarge"
36+ }
37+
38+ # List of expensive RDS instance types
39+ expensive_rds_types := {
40+ " db.r5.24xlarge" , " db.r5.16xlarge" , " db.r5.12xlarge" ,
41+ " db.r4.16xlarge" , " db.r4.8xlarge" ,
42+ " db.m5.24xlarge" , " db.m5.16xlarge" , " db.m5.12xlarge" ,
43+ " db.m4.16xlarge" , " db.m4.10xlarge" ,
44+ " db.x1.32xlarge" , " db.x1.16xlarge"
45+ }
46+
47+ # High-cost regions (typically more expensive than us-east-1)
48+ high_cost_regions := {
49+ " ap-northeast-1" , " ap-northeast-2" , " ap-southeast-1" , " ap-southeast-2" ,
50+ " eu-central-1" , " eu-west-1" , " eu-west-2" , " eu-west-3" ,
51+ " sa-east-1"
52+ }
53+
54+ # Deny expensive EC2 instance types
55+ deny[msg] {
56+ instance := ec2_instances[_]
57+ expensive_ec2_types[instance.change.after.instance_type]
58+ msg := sprintf (" EC2 instance '%s' uses expensive instance type '%s' - consider using a smaller instance type" , [instance.address, instance.change.after.instance_type])
59+ }
60+
61+ # Deny expensive RDS instance types
62+ deny[msg] {
63+ instance := rds_instances[_]
64+ expensive_rds_types[instance.change.after.instance_class]
65+ msg := sprintf (" RDS instance '%s' uses expensive instance type '%s' - consider using a smaller instance type" , [instance.address, instance.change.after.instance_class])
66+ }
67+
68+ # Deny RDS clusters without deletion protection in production
69+ deny[msg] {
70+ cluster := rds_clusters[_]
71+ cluster.change.after.tags.Environment == " prod"
72+ not cluster.change.after.deletion_protection
73+ msg := sprintf (" RDS cluster '%s' in production does not have deletion protection enabled" , [cluster.address])
74+ }
75+
76+ deny[msg] {
77+ cluster := rds_clusters[_]
78+ cluster.change.after.tags.Environment == " production"
79+ not cluster.change.after.deletion_protection
80+ msg := sprintf (" RDS cluster '%s' in production does not have deletion protection enabled" , [cluster.address])
81+ }
82+
83+ # Warn about missing cost tracking tags
84+ warn[msg] {
85+ instance := ec2_instances[_]
86+ not instance.change.after.tags.CostCenter
87+ msg := sprintf (" EC2 instance '%s' is missing 'CostCenter' tag for cost tracking" , [instance.address])
88+ }
89+
90+ warn[msg] {
91+ instance := rds_instances[_]
92+ not instance.change.after.tags.CostCenter
93+ msg := sprintf (" RDS instance '%s' is missing 'CostCenter' tag for cost tracking" , [instance.address])
94+ }
95+
96+ # Warn about instances in high-cost regions for production workloads
97+ warn[msg] {
98+ instance := ec2_instances[_]
99+ instance.change.after.tags.Environment == " prod"
100+ provider_region := input.configuration.provider_config.aws.expressions.region.constant_value
101+ high_cost_regions[provider_region]
102+ msg := sprintf (" Production EC2 instance '%s' - ensure you're using the most cost-effective region" , [instance.address])
103+ }
104+
105+ warn[msg] {
106+ instance := ec2_instances[_]
107+ instance.change.after.tags.Environment == " production"
108+ provider_region := input.configuration.provider_config.aws.expressions.region.constant_value
109+ high_cost_regions[provider_region]
110+ msg := sprintf (" Production EC2 instance '%s' - ensure you're using the most cost-effective region" , [instance.address])
111+ }
112+
113+ # Warn about dev instances without auto-shutdown
114+ warn[msg] {
115+ instance := ec2_instances[_]
116+ instance.change.after.tags.Environment == " dev"
117+ not instance.change.after.tags.AutoShutdown
118+ msg := sprintf (" Development EC2 instance '%s' should have 'AutoShutdown' tag to reduce costs" , [instance.address])
119+ }
120+
121+ warn[msg] {
122+ instance := ec2_instances[_]
123+ instance.change.after.tags.Environment == " development"
124+ not instance.change.after.tags.AutoShutdown
125+ msg := sprintf (" Development EC2 instance '%s' should have 'AutoShutdown' tag to reduce costs" , [instance.address])
126+ }
0 commit comments