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.
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
| Field | Type | Description |
|---|---|---|
resource.account | string | Account ID the resource is in. |
resource.region | string | Region. Empty for account-wide resources such as IAM roles. |
resource.type | string | CloudFormation type, such as AWS::EC2::VPC. Empty when the layer's type has no mapping. |
resource.source_type | string | The layer's own type name, such as codedeploy:deploymentconfig, when the CloudFormation type is unknown. |
resource.tf_types | list(string) | Terraform resource types that map to the CloudFormation type. |
resource.arn | string | ARN, when the layer gives one. |
resource.id | string | The layer's resource ID, such as AWS Config's resourceId. |
resource.name | string | Name, or the name in the ARN when the layer gives none. |
resource.tags | map(string, string) | Tags. An empty map when the resource has none, so "k" in resource.tags works. |
resource.attrs | map(string, string) | Identifiers a layer read, such as is_default or role. Read one with resource.attrs.?key.orValue(""). |
resource.layer | string | The first layer that reported the resource, such as config. |
resource.sources | list(string) | Every layer that reported the resource, in precedence order. |
resource.parents | list(Related) | Parent resources. |
resource.relationships | list(Related) | Every related resource, the parents included. |
resource.created | string | Creation time in RFC 3339, when the layer knows it. |
Related (parents and relationships)
| Field | Type | Description |
|---|---|---|
related.type | string | CloudFormation type. |
related.id | string | Resource ID. |
related.arn | string | ARN, from the relationship or the inventory. |
related.name | string | Name, from the inventory. |
related.relationship | string | The relationship as the layer names it, such as Is attached to Instance. |
related.tags | map(string, string) | Tags, from the inventory. |
related.attrs | map(string, string) | Identifiers, from the inventory. |
related.found | bool | Whether the inventory has the related resource. Name, tags and attrs are empty when it doesn't. |
unit
| Field | Type | Description |
|---|---|---|
unit.repo | string | Repository of the unit that manages the resource. |
unit.path | string | Unit path in the repository. |
unit.workspace | string | Workspace. |
unit.address | string | Resource address in the state, such as aws_iam_role.app. |
Functions
| Function | Returns |
|---|---|
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 library | comparisons, in, size, startsWith, endsWith, contains, matches (RE2), and the macros exists, all, exists_one, map and filter. |
| The CEL strings extension | lowerAscii, upperAscii, split, replace, substring, trim, indexOf, join and the others of cel-go ext.Strings. |
| Optional values | map.?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:
"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.