Skip to content

CEL fields and functions for rules ​

An ownership rule's match is a CEL expression that returns a boolean. scan evaluates it for each resource that no state manages, and whois --live and codify --select use the same variables. The built-in rule packs are written the same way.

yaml
rules:
  - name: datadog-integration
    category: other-iac
    owner: datadog
    match: 'resource.type == "AWS::IAM::Role" && resource.name.startsWith("DatadogIntegrationRole")'
    reason: created by the Datadog CloudFormation quick start
  - name: sandbox-experiments
    category: ignored
    match: 'resource.account == "111111111111" && resource.tags.?owner.orValue("") == "sandbox"'
    reason: temporary experiments, review in Q1
    expires: "2027-03-31"

Variables ​

A rule sees two variables: resource, the live resource, and unit, the unit whose state manages it. unit is empty for an unmanaged resource, so most rules read resource only. Each entry of resource.parents and resource.relationships is a Related object.

resource ​

FieldTypeDescription
resource.accountstringAccount ID the resource is in.
resource.regionstringRegion. Empty for account-wide resources such as IAM roles.
resource.typestringCloudFormation type, such as AWS::EC2::VPC. Empty when the layer's type has no mapping.
resource.source_typestringThe layer's own type name, such as codedeploy:deploymentconfig, when the CloudFormation type is unknown.
resource.tf_typeslist(string)Terraform resource types that map to the CloudFormation type.
resource.arnstringARN, when the layer gives one.
resource.idstringThe layer's resource ID, such as AWS Config's resourceId.
resource.namestringName, or the name in the ARN when the layer gives none.
resource.tagsmap(string, string)Tags. An empty map when the resource has none, so "k" in resource.tags works.
resource.attrsmap(string, string)Identifiers a layer read, such as is_default or role. Read one with resource.attrs.?key.orValue("").
resource.layerstringThe first layer that reported the resource, such as config.
resource.sourceslist(string)Every layer that reported the resource, in precedence order.
resource.parentslist(Related)Parent resources.
resource.relationshipslist(Related)Every related resource, the parents included.
resource.createdstringCreation time in RFC 3339, when the layer knows it.
FieldTypeDescription
related.typestringCloudFormation type.
related.idstringResource ID.
related.arnstringARN, from the relationship or the inventory.
related.namestringName, from the inventory.
related.relationshipstringThe relationship as the layer names it, such as Is attached to Instance.
related.tagsmap(string, string)Tags, from the inventory.
related.attrsmap(string, string)Identifiers, from the inventory.
related.foundboolWhether the inventory has the related resource. Name, tags and attrs are empty when it doesn't.

unit ​

FieldTypeDescription
unit.repostringRepository of the unit that manages the resource.
unit.pathstringUnit path in the repository.
unit.workspacestringWorkspace.
unit.addressstringResource address in the state, such as aws_iam_role.app.

Functions ​

FunctionReturns
in_state(tf_type, id)true when a state in the run manages a resource of Terraform type tf_type with that ID, such as in_state("aws_lambda_function", resource.name).
The CEL standard librarycomparisons, in, size, startsWith, endsWith, contains, matches (RE2), and the macros exists, all, exists_one, map and filter.
The CEL strings extensionlowerAscii, upperAscii, split, replace, substring, trim, indexOf, join and the others of cel-go ext.Strings.
Optional valuesmap.?key gives an optional value, and .orValue(default) reads it with a default.

Missing keys ​

Reading a map key that doesn't exist, such as resource.tags["owner"] on an untagged resource, is an evaluation error, and the rule doesn't match. Test the key first, or read it as an optional value:

text
"owner" in resource.tags && resource.tags["owner"] == "sandbox"
resource.tags.?owner.orValue("") == "sandbox"
resource.relationships.exists(r, r.type == "AWS::EC2::VPC" && r.attrs.?is_default.orValue("") == "true")

A rule whose expression doesn't compile, or doesn't return a boolean, fails the run with exit 2, and the error names the rule and the column. tfcensus rules test checks rules against a sample resource without calling AWS.