Policies API
Note: Sentinel and OPA policies are available in the Terraform Cloud Team & Governance tier.
Policies are rules that Terraform Cloud enforces on Terraform runs. You can use policies to validate that the Terraform plan complies with security rules and best practices. Terraform Cloud policy enforcement lets you use the policy-as-code frameworks Sentinel and Open Policy Agent (OPA) to apply policy checks to Terraform Cloud workspaces. Refer to [Policy Enforcement]((/cloud-docs/policy-enforcement) for more details.
You group policies into policy sets and apply those policy sets to one or more workspaces in your organization. For each run in those workspaces, Terraform Cloud checks the Terraform plan against the policy set and displays the results in the UI.
This page documents the API endpoints to create, read, update, and delete policies in an organization. To manage policy sets, use the Policy Sets API. To manage the policy results, use the Runs API.
Note: We no longer recommend using these endpoints to manage Sentinel policies in Terraform Cloud. For Sentinel, we recommend creating policy sets with the Policy Sets API. This approach lets you integrate with version control systems and upload policy set data and configuration together.
Create a Policy
POST /organizations/:organization_name/policies
Parameter | Description |
---|---|
:organization_name | The organization to create the policy in. The organization must already exist in the system, and the token authenticating the API request must have permission to manage policies. ((More about permissions.) |
[permissions-citation]: #intentionally-unused---keep-for-maintainers)
This creates a new policy object for the organization, but does not upload the actual policy code. After creation, you must use the Upload a Policy endpoint (below) with the new policy's upload path. (This endpoint's response body includes the upload path in its links.upload
property.)
Status | Response | Reason |
---|---|---|
200 | JSON API document (type: "policies" ) | Successfully created a policy |
404 | JSON API error object | Organization not found, or user unauthorized to perform action |
422 | JSON API error object | Malformed request body (missing attributes, wrong types, etc.) |
Request Body
This POST endpoint requires a JSON object with the following properties as a request payload.
Properties without a default value are required.
Key path | Type | Default | Description |
---|---|---|---|
data.type | string | Must be "policies" . | |
data.attributes.name | string | The name of the policy, which can include letters, numbers, - , and _ . You cannot modify this name after creation. | |
data.attributes.description | string | null | Text describing the policy's purpose. This field supports Markdown and appears in the Terraform Cloud UI. |
data.attributes.kind | string | sentinel | The policy-as-code framework for the policy. Valid values are "sentinel" and "opa" . |
data.attributes.query | string | The OPA query to run. Only valid for OPA policies. | |
data.attributes.enforce | array[object] | An array of enforcement configurations that map policy file paths to their enforcement modes. Policies support a single file, so this array should consist of a single element. For Sentinel, if the path in the enforcement map does not match the Sentinel policy (<NAME>.sentinel ), then Terraform Cloud uses the default hard-mandatory enforcement level. For OPA, the default enforcement level is advisory . | |
data.attributes.enforce[].path | string | For Sentinel, must be <NAME>.sentinel , where <NAME> has the same value as data.attributes.name . For OPA, must be <NAME>.rego . | |
data.attributes.enforce[].mode | string | The enforcement level of the policy. For Sentinel, valid values are "hard-mandatory" , "soft-mandatory" , and "advisory" . For OPA, Valid values are "mandatory" and "advisory" . Refer to Managing Policies for details. | |
data.relationships.policy-sets.data[] | array[object] | [] | A list of resource identifier objects to define which policy sets include the new policy. These objects must contain id and type properties, and the type property must be policy-sets . For example,{ "id": "polset-3yVQZvHzf5j3WRJ1","type": "policy-sets" } . |
Sample Payload (Sentinel)
Sample Payload (OPA)
Sample Request
Sample Response (Sentinel)
Sample Response (OPA)
Show a Policy
GET /policies/:policy_id
Parameter | Description |
---|---|
:policy_id | The ID of the policy to show. Use the "List Policies" endpoint to find IDs. |
Status | Response | Reason |
---|---|---|
200 | JSON API document (type: "policies" ) | The request was successful |
404 | JSON API error object | Policy not found or user unauthorized to perform action |
Sample Request
Sample Response
Upload a Policy
PUT /policies/:policy_id/upload
Parameter | Description |
---|---|
:policy_id | The ID of the policy to upload code to. Use the "List Policies" endpoint (or the response to a "Create Policy" request) to find IDs. |
This endpoint uploads code to an existing Sentinel or OPA policy.
Note: This endpoint does not use JSON-API's conventions for HTTP headers and body serialization.
Note: This endpoint limits the size of uploaded policies to 10MB. If a larger payload is uploaded, an HTTP 413 error will be returned, and the policy will not be saved. Consider refactoring policies into multiple smaller, more concise documents if you reach this limit.
Request Body
This PUT endpoint requires the text of a valid Sentinel or OPA policy with a Content-Type
of application/octet-stream
.
- Refer to Defining Sentinel Policies for details about writing Sentinel code.
- Refer to Defining OPA Policies for details about writing OPA code.
Sample Payload
Sample Request
Update a Policy
PATCH /policies/:policy_id
Parameter | Description |
---|---|
:policy_id | The ID of the policy to update. Use the "List Policies" endpoint to find IDs. |
This endpoint can update the enforcement mode of an existing policy. To update the policy code itself, use the upload endpoint.
Status | Response | Reason |
---|---|---|
200 | JSON API document (type: "policies" ) | Successfully updated the policy |
404 | JSON API error object | Policy not found, or user unauthorized to perform action |
422 | JSON API error object | Malformed request body (missing attributes, wrong types, etc.) |
Request Body
This PATCH endpoint requires a JSON object with the following properties as a request payload.
Properties without a default value are required.
Key path | Type | Default | Description |
---|---|---|---|
data.type | string | Must be "policies" . | |
data.attributes.description | string | null | Text describing the policy's purpose. This field supports Markdown and appears in the Terraform Cloud UI. |
data.attributes.query | string | The OPA query to run. This is only valid for OPA policies. | |
data.attributes.enforce | array[object] | An array of enforcement configurations that map policy file paths to their enforcement modes. Policies support a single file, so this array should consist of a single element. For Sentinel, if the path in the enforcement map does not match the Sentinel policy (<NAME>.sentinel ), then Terraform Cloud uses the default hard-mandatory enforcement level. For OPA, the default enforcement level is advisory . | |
data.attributes.enforce[].path | string | For Sentinel, must be <NAME>.sentinel , where <NAME> has the same value as data.attributes.name . For OPA, must be <NAME>.rego . | |
data.attributes.enforce[].mode | string | hard-mandatory (Sentinel) , advisory (OPA) | The enforcement level of the policy. For Sentinel, valid values are "hard-mandatory" , "soft-mandatory" , and "advisory" . For OPA, valid values are "mandatory" and "advisory" . Refer to Managing Policies for details. |
Sample Payload
Sample Request
Sample Response
List Policies
GET /organizations/:organization_name/policies
Parameter | Description |
---|---|
:organization_name | The organization to list policies for. |
Status | Response | Reason |
---|---|---|
200 | Array of JSON API documents (type: "policies" ) | Success |
404 | JSON API error object | Organization not found, or user unauthorized to perform action |
Query Parameters
This endpoint supports pagination with standard URL query parameters; remember to percent-encode [
as %5B
and ]
as %5D
if your tooling doesn't automatically encode URLs.
Parameter | Description | |
---|---|---|
page[number] | Optional. If omitted, the endpoint will return the first page. | |
page[size] | Optional. If omitted, the endpoint will return 20 policies per page. | |
search[name] | Optional. Allows searching the organization's policies by name. | |
filter[kind] | Optional. If specified, restricts results to those with the matching policy kind value. Valid values are sentinel and opa |
Sample Request
Sample Response
Delete a Policy
DELETE /policies/:policy_id
Parameter | Description |
---|---|
:policy_id | The ID of the policy to delete. Use the "List Policies" endpoint to find IDs. |
Status | Response | Reason |
---|---|---|
204 | Nothing | Successfully deleted the policy |
404 | JSON API error object | Policy not found, or user unauthorized to perform action |
Sample Request
Available Related Resources
The GET endpoints above can optionally return related resources, if requested with the include
query parameter. The following resource types are available:
Resource Name | Description |
---|---|
policy_sets | Policy sets that any returned policies are members of. |