

tfplan example data¶
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | {
"tfplan": {
"format_version": "0.1",
"terraform_version": "0.13.4",
"variables": {
"aws_location": {
"value": "us-east-1"
},
"svr_count": {
"value": "1"
}
},
"planned_values": {
"outputs": {
"scalr_test_private_ip": {
"sensitive": false
},
"scalr_test_public_ip": {
"sensitive": false
}
},
"root_module": {
"resources": [
{
"address": "aws_instance.scalr_test[0]",
"mode": "managed",
"type": "aws_instance",
"name": "scalr_test",
"index": 0,
"provider_name": "registry.terraform.io/hashicorp/aws",
"schema_version": 1,
"values": {
"ami": "ami-024a64a6685d05041",
"credit_specification": [],
"disable_api_termination": null,
"ebs_optimized": null,
"get_password_data": false,
"hibernation": null,
"iam_instance_profile": null,
"instance_initiated_shutdown_behavior": null,
"instance_type": "t3.medium",
"key_name": "PeterG",
"monitoring": null,
"source_dest_check": true,
"subnet_id": "subnet-0ebb1058ad727cfdb",
"tags": null,
"timeouts": null,
"user_data": null,
"user_data_base64": null,
"vpc_security_group_ids": [
"sg-0880cfdc546b123ba"
]
}
},
{
"address": "aws_network_interface.test",
"mode": "managed",
"type": "aws_network_interface",
"name": "test",
"provider_name": "registry.terraform.io/hashicorp/aws",
"schema_version": 0,
"values": {
"description": null,
"private_ips": [
"10.0.0.50"
],
"source_dest_check": true,
"subnet_id": "subnet-0ebb1058ad727cfdb",
"tags": null
}
}
]
}
},
"resource_changes": [
{
"address": "aws_instance.scalr_test[0]",
"mode": "managed",
"type": "aws_instance",
"name": "scalr_test",
"index": 0,
"provider_name": "registry.terraform.io/hashicorp/aws",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"ami": "ami-024a64a6685d05041",
"credit_specification": [],
"disable_api_termination": null,
"ebs_optimized": null,
"get_password_data": false,
"hibernation": 1,
"iam_instance_profile": null,
"instance_initiated_shutdown_behavior": null,
"instance_type": "t3.medium",
"ipv6_addresses": 2,
"key_name": "d1",
"monitoring": null,
"source_dest_check": true,
"subnet_id": "subnet-0ebb1058ad727cfda",
"tags": null,
"timeouts": null,
"user_data": null,
"user_data_base64": null,
"vpc_security_group_ids":
[
"sg-0880cfdc546b123baa",
"sg-0880cfdc546b123baz",
"sg-0880cfdc546b123bag"
]
},
"after_unknown": {
"arn": true,
"associate_public_ip_address": true,
"availability_zone": true,
"cpu_core_count": true,
"cpu_threads_per_core": true,
"credit_specification": [],
"ebs_block_device": true,
"ephemeral_block_device": true,
"host_id": true,
"id": true,
"instance_state": true,
"ipv6_address_count": true,
"ipv6_addresses": true,
"metadata_options": true,
"network_interface": true,
"outpost_arn": true,
"password_data": true,
"placement_group": true,
"primary_network_interface_id": true,
"private_dns": true,
"private_ip": true,
"public_dns": true,
"public_ip": true,
"root_block_device": true,
"secondary_private_ips": true,
"security_groups": true,
"tenancy": true,
"volume_tags": true,
"vpc_security_group_ids": [
false
]
}
}
},
{
"address": "aws_network_interface.test",
"mode": "managed",
"type": "aws_network_interface",
"name": "test",
"provider_name": "registry.terraform.io/hashicorp/aws",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"description": null,
"private_ips": [
"10.0.0.50"
],
"source_dest_check": true,
"subnet_id": "subnet-0ebb1058ad727cfdb",
"tags": null
},
"after_unknown": {
"attachment": true,
"id": true,
"ipv6_address_count": true,
"ipv6_addresses": true,
"mac_address": true,
"outpost_arn": true,
"private_dns_name": true,
"private_ip": true,
"private_ips": [
false
],
"private_ips_count": true,
"security_groups": true
}
}
}
],
"output_changes": {
"scalr_test_private_ip": {
"actions": [
"create"
],
"before": null,
"after_unknown": true
},
"scalr_test_public_ip": {
"actions": [
"create"
],
"before": null,
"after_unknown": true
}
},
"configuration": {
"provider_config": {
"aws": {
"name": "aws",
"expressions": {
"region": {
"references": [
"var.aws_location"
]
}
}
}
},
"root_module": {
"outputs": {
"scalr_test_private_ip": {
"expression": {
"references": [
"aws_instance.scalr_test"
]
}
},
"scalr_test_public_ip": {
"expression": {
"references": [
"aws_instance.scalr_test"
]
}
}
},
"resources": [
{
"address": "aws_instance.scalr_test",
"mode": "managed",
"type": "aws_instance",
"name": "scalr_test",
"provider_config_key": "aws",
"expressions": {
"ami": {
"constant_value": "ami-024a64a6685d05041"
},
"instance_type": {
"constant_value": "t3.medium"
},
"key_name": {
"constant_value": "PeterG"
},
"subnet_id": {
"constant_value": "subnet-0ebb1058ad727cfdb"
},
"vpc_security_group_ids": {
"constant_value": [
"sg-0880cfdc546b123ba"
]
}
},
"schema_version": 1,
"count_expression": {
"references": [
"var.svr_count"
]
}
},
{
"address": "aws_network_interface.test",
"mode": "managed",
"type": "aws_network_interface",
"name": "test",
"provider_config_key": "aws",
"expressions": {
"private_ips": {
"constant_value": [
"10.0.0.50"
]
},
"subnet_id": {
"constant_value": "subnet-0ebb1058ad727cfdb"
}
},
"schema_version": 0
}
],
"variables": {
"aws_location": {},
"svr_count": {}
}
}
}
}
}
|
Example policy using tfrun¶
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 | # Multi provider rule to enforce instance type/size
package terraform
import input.tfplan as tfplan
# Allowed sizes by provider (examples). Edit as required
allowed_types = {
"aws": ["t2.nano", "t2.micro"],
"azurerm": ["Standard_A0", "Standard_A1"],
"google": ["n1-standard-1", "n1-standard-2"]
}
# Attribute names for instance type/size by provider.
# Add additional elements for other resources and clouds
instance_type_key = {
"aws": [ "instance_type" ],
"azurerm": [ "size", "sku" ],
"google": [ "machine_type" ]
}
array_contains(arr, elem) {
arr[_] = elem
}
get_basename(path) = basename{
arr := split(path, "/")
basename:= arr[count(arr)-1]
}
# Extracts the instance type/size
get_instance_type(resource) = instance_type {
# registry.terraform.io/hashicorp/aws -> aws
instance_type := resource.change.after[instance_type_key[resource.type]]
}
deny[reason] {
resource := tfplan.resource_changes[_]
provider_name := get_basename(resource.provider_name)
# Extract array of type attribute names for the provider
provider_types := instance_type_key[provider_name]
# Iterate on each type name
type := provider_types[_]
# Extract the type value
instance_type := resource.change.after[type]
not array_contains(allowed_types[provider_name], instance_type)
reason := sprintf(
"%-60s :: '%s' of '%s' is not allowed",
[resource.address, type, instance_type]
)
}
|