|
| 1 | +############################################### |
| 2 | + Policy to check tags on all aws instances |
| 3 | +############################################### |
| 4 | +import "tfplan" |
| 5 | + |
| 6 | +main = rule { |
| 7 | + all tfplan.resources.aws_instance as _, instances { |
| 8 | + all instances as _, r { |
| 9 | + (length(r.applied.tags) else 0) >= 1 |
| 10 | + } |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +################################################## |
| 15 | + Policy to allow certain instance types |
| 16 | +################################################## |
| 17 | +# Imports mock data |
| 18 | +import "tfplan/v2" as tfplan |
| 19 | + |
| 20 | +# Get all AWS instances from all modules |
| 21 | +ec2_instances = filter tfplan.resource_changes as _, rc { |
| 22 | + rc.type is "aws_instance" and |
| 23 | + (rc.change.actions contains "create" or rc.change.actions is ["update"]) |
| 24 | +} |
| 25 | + |
| 26 | +# Mandatory Instance Tags |
| 27 | +mandatory_tags = [ |
| 28 | + "Name", |
| 29 | +] |
| 30 | + |
| 31 | +# Allowed Types |
| 32 | +allowed_types = [ |
| 33 | + "t2.micro", |
| 34 | + "t2.small", |
| 35 | + "t2.medium", |
| 36 | +] |
| 37 | + |
| 38 | +# Rule to enforce "Name" tag on all instances |
| 39 | +mandatory_instance_tags = rule { |
| 40 | + all ec2_instances as _, instance { |
| 41 | + all mandatory_tags as mt { |
| 42 | + instance.change.after.tags contains mt |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +# Rule to restrict instance types |
| 48 | +instance_type_allowed = rule { |
| 49 | + all ec2_instances as _, instance { |
| 50 | + instance.change.after.instance_type in allowed_types |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +# Main rule that requires other rules to be true |
| 55 | +main = rule { |
| 56 | + (instance_type_allowed and mandatory_instance_tags) else true |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +############## |
| 61 | + |
0 commit comments