I’ve been wanting to for a while start writing policies for our Terraform Cloud deployments.
Yesterday I started to pick up Sentinel and run over some of the tutorials but being honest, this structure and finding all the syntax key words and meanings is turning out to be very hard.
As Indicated above the end goal for this piece of work is to have a policy, I can attach to workspaces that will identify if non approved providers are being used.
I thought I would start simple and just see if I can pass on a single provider such as aws but I’m having no luck.
Mock I have is
providers = {
"aws": {
"alias": "",
"config": {
"region": {
"references": [
"var.region",
],
},
},
"full_name": "registry.terraform.io/hashicorp/aws",
"module_address": "",
"name": "aws",
"provider_config_key": "aws",
"version_constraint": ">= 3.26.0",
},
"random": {
"alias": "",
"config": {},
"full_name": "registry.terraform.io/hashicorp/random",
"module_address": "",
"name": "random",
"provider_config_key": "random",
"version_constraint": "3.0.1",
},
}
Code I’m currently trying is
# Imports mock data
import "tfconfig/v2" as tfconfig
# Retrive all providers
providers = tfconfig.providers
#confirm the provider matches
allowed_providers = rule {
all providers as _, provider{
provider.provider_config_key is "aws"
}
}
# Main rule that requires other rules to be true
main = rule {
(allowed_providers) else true
}
It doesn’t matter what I put in " provider.provider_config_key is “aws” " it always comes back as false and I can’t work out why as it seems correct.
What has I got wrong here, ta ?