---
title: Validate an IaC custom rule
description: Compile a Rego rule for an IaC (Infrastructure as Code) platform.
breadcrumbs: Docs > API > Validate an IaC custom rule
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Validate an IaC custom rule

## v2 (latest)

{% alert level="warning" %}
**Note**: This endpoint is in Preview and is subject to change.
If you have any feedback, contact [Datadog support](https://docs.datadoghq.com/help.md).
{% /alert %}

| Datadog site      | API endpoint                                                                               |
| ----------------- | ------------------------------------------------------------------------------------------ |
| app.datadoghq.com | POST https://api.datadoghq.com/api/v2/security/code_security/iac_custom_rules/validate     |
| us3.datadoghq.com | POST https://api.us3.datadoghq.com/api/v2/security/code_security/iac_custom_rules/validate |
| us5.datadoghq.com | POST https://api.us5.datadoghq.com/api/v2/security/code_security/iac_custom_rules/validate |
| app.datadoghq.eu  | POST https://api.datadoghq.eu/api/v2/security/code_security/iac_custom_rules/validate      |
| ap1.datadoghq.com | POST https://api.ap1.datadoghq.com/api/v2/security/code_security/iac_custom_rules/validate |
| ap2.datadoghq.com | POST https://api.ap2.datadoghq.com/api/v2/security/code_security/iac_custom_rules/validate |
| uk1.datadoghq.com | POST https://api.uk1.datadoghq.com/api/v2/security/code_security/iac_custom_rules/validate |
| app.ddog-gov.com  | POST https://api.ddog-gov.com/api/v2/security/code_security/iac_custom_rules/validate      |
| us2.ddog-gov.com  | POST https://api.us2.ddog-gov.com/api/v2/security/code_security/iac_custom_rules/validate  |

### Overview

Compile a Rego rule for an IaC (Infrastructure as Code) platform.
When the request has a `sample_file`, the operation also runs the rule on that file and returns what it detects.
This operation doesn't store anything, so use it to check a rule before you create or update it.
`is_valid` is `true` when the rule compiles and runs without errors.
An empty `findings` list with a `sample_file` means that the rule matches nothing in that file.
When the scanner doesn't finish within 30 seconds, the operation returns `200` with `is_valid` set to `false`
and an entry in `errors` whose `code` is `scanner_timeout`.

This endpoint requires the `appsec_vm_write` permission.

### Request

#### Body Data (required)

{% tabs %}
{% tab label="Model" %}
| Parent field | Field                   | Type   | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| ------------ | ----------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|              | data [*required*]       | object | The rule to validate.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| data         | attributes [*required*] | object | The rule to validate.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| attributes   | platform [*required*]   | enum   | The IaC (Infrastructure as Code) platform of the files that the rule scans. - `ansible`: Ansible playbooks. - `cicd`: GitHub Actions workflows. - `cloudformation`: AWS CloudFormation templates. - `docker_compose`: Docker Compose files. - `dockerfile`: Dockerfiles. - `kubernetes`: Kubernetes manifests. - `terraform`: Terraform configuration files. List operations filter on it with `filter[platform]=terraform`. Allowed enum values: `ansible`, `cicd`, `cloudformation`, `docker_compose`, `dockerfile`, `kubernetes`, `terraform`. |
| attributes   | rego_query [*required*] | string | The Rego source of the rule, in plain text, at most 75,000 bytes in UTF-8.                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| attributes   | sample_file             | string | An IaC file to run the rule on, in plain text, at most 75,000 bytes in UTF-8. Omit it to only compile the rule.                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| data         | type [*required*]       | enum   | The type of the request. The value is always `iac_custom_rule_validation_request`. Allowed enum values: `iac_custom_rule_validation_request`. Default: `iac_custom_rule_validation_request`.                                                                                                                                                                                                                                                                                                                                                      |
{% /tab %}

{% tab label="Example" %}
```json
{
  "data": {
    "attributes": {
      "platform": "terraform",
      "rego_query": "package datadog\n\nDatadogPolicy contains result if {\n\tsome i, doc in input.document\n\tsome type, resources in doc.resource\n\tsome name, resource in resources\n\ttype == \"aws_s3_bucket\"\n\tnot resource.server_side_encryption_configuration\n\tresult := {\n\t\t\"documentId\": doc.id,\n\t\t\"resourceType\": type,\n\t\t\"resourceName\": name,\n\t\t\"searchKey\": sprintf(\"resource.%s.%s\", [type, name]),\n\t}\n}",
      "sample_file": "resource \"aws_s3_bucket\" \"my_bucket\" {\n  bucket = \"my-bucket\"\n}"
    },
    "type": "iac_custom_rule_validation_request"
  }
}
```
{% /tab %}
{% /tabs %}

### Response

{% tabs %}
{% tab label="200" %}
OK

{% tabs %}
{% tab label="Model" %}
The result of the validation of an IaC custom rule.

| Parent field | Field                      | Type     | Description                                                                                                                                                                              |
| ------------ | -------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|              | data [*required*]          | object   | The result of the validation.                                                                                                                                                            |
| data         | attributes [*required*]    | object   | The outcome of the validation.                                                                                                                                                           |
| attributes   | errors [*required*]        | [object] | The compilation and runtime errors, ordered by position. Empty when the rule is valid.                                                                                                   |
| errors       | code [*required*]          | string   | The error code, for example `rego_parse_error`, `rego_runtime_error`, or `scanner_timeout` when the scanner doesn't finish within 30 seconds.                                            |
| errors       | end_column [*required*]    | int64    | The column where the error ends, starting at `1`. `0` when the position is unknown.                                                                                                      |
| errors       | end_line [*required*]      | int64    | The line where the error ends, starting at `1`. `0` when the position is unknown.                                                                                                        |
| errors       | message [*required*]       | string   | The error message.                                                                                                                                                                       |
| errors       | start_column [*required*]  | int64    | The column where the error starts, starting at `1`. `0` when the position is unknown.                                                                                                    |
| errors       | start_line [*required*]    | int64    | The line where the error starts, starting at `1`. `0` when the position is unknown.                                                                                                      |
| attributes   | findings [*required*]      | [object] | The resources of `sample_file` that the rule detects. Empty when the request has no `sample_file`, or when the rule matches nothing.                                                     |
| findings     | end_line [*required*]      | int64    | The last line of the finding in `sample_file`, starting at `1`.                                                                                                                          |
| findings     | resource_name [*required*] | string   | The name of the detected resource, as the rule reports it.                                                                                                                               |
| findings     | resource_type [*required*] | string   | The type of the detected resource, as the rule reports it.                                                                                                                               |
| findings     | search_key [*required*]    | string   | The search key that the rule reports, which locates the detected value in `sample_file`.                                                                                                 |
| findings     | start_line [*required*]    | int64    | The first line of the finding in `sample_file`, starting at `1`.                                                                                                                         |
| attributes   | is_valid [*required*]      | boolean  | `true` when the rule compiles, and runs on `sample_file` without errors when the request has one. `false` when `errors` isn't empty.                                                     |
| data         | id [*required*]            | string   | The ID of this validation result, a random UUID (universally unique identifier).                                                                                                         |
| data         | type [*required*]          | enum     | The type of the result. The value is always `iac_custom_rule_validation_result`. Allowed enum values: `iac_custom_rule_validation_result`. Default: `iac_custom_rule_validation_result`. |
{% /tab %}

{% tab label="Example" %}
```json
{
  "data": {
    "attributes": {
      "errors": [
        {
          "code": "rego_parse_error",
          "end_column": 9,
          "end_line": 3,
          "message": "unexpected eof token",
          "start_column": 2,
          "start_line": 3
        }
      ],
      "findings": [
        {
          "end_line": 3,
          "resource_name": "my_bucket",
          "resource_type": "aws_s3_bucket",
          "search_key": "resource.aws_s3_bucket.my_bucket",
          "start_line": 1
        }
      ],
      "is_valid": true
    },
    "id": "2ec5acf6-359f-4436-bf06-4cc19051d353",
    "type": "iac_custom_rule_validation_result"
  }
}
```
{% /tab %}
{% /tabs %}
{% /tab %}

{% tab label="400" %}
Bad Request

{% tabs %}
{% tab label="Model" %}
API error response.

| Parent field | Field               | Type     | Description                                                                     |
| ------------ | ------------------- | -------- | ------------------------------------------------------------------------------- |
|              | errors [*required*] | [object] | A list of errors.                                                               |
| errors       | code                | string   | An application-specific error code.                                             |
| errors       | detail              | string   | A human-readable explanation specific to this occurrence of the error.          |
| errors       | meta                | object   | Non-standard meta-information about the error                                   |
| errors       | source              | object   | References to the source of the error.                                          |
| source       | header              | string   | A string indicating the name of a single request header which caused the error. |
| source       | parameter           | string   | A string indicating which URI query parameter caused the error.                 |
| source       | pointer             | string   | A JSON pointer to the value in the request document that caused the error.      |
| errors       | status              | string   | Status code of the response.                                                    |
| errors       | title               | string   | Short human-readable summary of the error.                                      |
{% /tab %}

{% tab label="Example" %}
```json
{
  "errors": [
    {
      "code": "invalid_request",
      "detail": "Missing required attribute in body",
      "meta": {},
      "source": {
        "header": "Authorization",
        "parameter": "limit",
        "pointer": "/data/attributes/title"
      },
      "status": "400",
      "title": "Bad Request"
    }
  ]
}
```
{% /tab %}
{% /tabs %}
{% /tab %}

{% tab label="403" %}
Forbidden

{% tabs %}
{% tab label="Model" %}
API error response.

| Field               | Type     | Description       |
| ------------------- | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |
{% /tab %}

{% tab label="Example" %}
```json
{
  "errors": [
    "Bad Request"
  ]
}
```
{% /tab %}
{% /tabs %}
{% /tab %}

{% tab label="404" %}
Not Found

{% tabs %}
{% tab label="Model" %}
API error response.

| Parent field | Field               | Type     | Description                                                                     |
| ------------ | ------------------- | -------- | ------------------------------------------------------------------------------- |
|              | errors [*required*] | [object] | A list of errors.                                                               |
| errors       | code                | string   | An application-specific error code.                                             |
| errors       | detail              | string   | A human-readable explanation specific to this occurrence of the error.          |
| errors       | meta                | object   | Non-standard meta-information about the error                                   |
| errors       | source              | object   | References to the source of the error.                                          |
| source       | header              | string   | A string indicating the name of a single request header which caused the error. |
| source       | parameter           | string   | A string indicating which URI query parameter caused the error.                 |
| source       | pointer             | string   | A JSON pointer to the value in the request document that caused the error.      |
| errors       | status              | string   | Status code of the response.                                                    |
| errors       | title               | string   | Short human-readable summary of the error.                                      |
{% /tab %}

{% tab label="Example" %}
```json
{
  "errors": [
    {
      "code": "invalid_request",
      "detail": "Missing required attribute in body",
      "meta": {},
      "source": {
        "header": "Authorization",
        "parameter": "limit",
        "pointer": "/data/attributes/title"
      },
      "status": "400",
      "title": "Bad Request"
    }
  ]
}
```
{% /tab %}
{% /tabs %}
{% /tab %}

{% tab label="429" %}
Too many requests

{% tabs %}
{% tab label="Model" %}
API error response.

| Field               | Type     | Description       |
| ------------------- | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |
{% /tab %}

{% tab label="Example" %}
```json
{
  "errors": [
    "Bad Request"
  ]
}
```
{% /tab %}
{% /tabs %}
{% /tab %}
{% /tabs %}

### Code Example

{% tabs %}
{% tab label="Curl" %}
```bash
# Set your Datadog site and credentials
export DD_SITE="datadoghq.com" # Or us3.datadoghq.com, us5.datadoghq.com, datadoghq.eu, ap1.datadoghq.com, ap2.datadoghq.com, uk1.datadoghq.com, ddog-gov.com, us2.ddog-gov.com
# Use a Personal Access Token or Service Access Token
export DD_BEARER_TOKEN="<PERSONAL_ACCESS_TOKEN OR SERVICE_ACCESS_TOKEN>"

curl -X POST "https://api.${DD_SITE}/api/v2/security/code_security/iac_custom_rules/validate" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${DD_BEARER_TOKEN}" \
-d @- << EOF
{
  "data": {
    "attributes": {
      "platform": "terraform",
      "rego_query": "package datadog\n\nDatadogPolicy contains result if {\n\tsome i, doc in input.document\n\tsome type, resources in doc.resource\n\tsome name, resource in resources\n\ttype == \"aws_s3_bucket\"\n\tnot resource.server_side_encryption_configuration\n\tresult := {\n\t\t\"documentId\": doc.id,\n\t\t\"resourceType\": type,\n\t\t\"resourceName\": name,\n\t\t\"searchKey\": sprintf(\"resource.%s.%s\", [type, name]),\n\t}\n}",
      "sample_file": "resource \"aws_s3_bucket\" \"my_bucket\" {\n  bucket = \"my-bucket\"\n}"
    },
    "type": "iac_custom_rule_validation_request"
  }
}
EOF
```
{% /tab %}
{% /tabs %}
