Code Security Integration with Terraform Cloud
Protect your infrastructure by detecting potential issues in your Terraform An infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. configuration and plan before applying the changes to production. You can integrate Code Security with Terraform Cloud or Terraform Enterprise. This integration is based on an AWS
Amazon® Web Services. Public cloud platform that offers global compute, storage, database, application and other cloud services. lambda function which is being triggered by the Run Task at the relevant stage.
Terraform Cloud Integration Types
Code Security can be integrated with the Pre-plan stage or the Post-plan stage of the Terraform run:
Pre-plan stage
This stage takes place right before the plan stage.
In this stage,Code Security scans your Terraform configuration deployed in this run for misconfigurations.
Post-plan stage
This stage takes place between the plan and apply stages.
In this stage ,Code Security scans the generated plan of the current run for potential issues before applying the changes to your live infrastructure.
You can read more about run tasks in Terraform Cloud here and here.
Integration Environment Variables
Variable |
Required |
Description |
---|---|---|
|
Yes |
Your Spectral DSN retrieved from SpectralOps |
|
Yes |
If Spectral finds issues - how should we handle the run? The policies are based on the Spectral issue severity - critical / high / medium / low / informational (Valid values: "Fail on any issue" / "Fail on low and above" / "Fail on medium and above" / "Fail on high and above" / "Fail on critical only" / "Always Pass") |
|
No |
A key that will be used for securing your Run Task by validating the request payload signature, should be identical to the HMAC key you set will set in the Run Task |
|
No |
User key created by Terraform (required for pre-plan run task) - can be created here |
Configuration

-
Use one of these methods to create the required AWS resources:
-
Use this Terraform module (set the
integration_type
param value toterraform
). -
Launch the CloudFormation stack.
-
-
Add all the required environment variables, including
SPECTRAL_DSN
,CHECK_POLICY
andHMAC_KEY
. -
Optional - If you plan to create a pre-plan Run Task:
-
Create a user API key in Terraform
-
Set the user API key in the
TERRAFORM_USER_KEY env
variable.
-
-
Copy the Gateway API URL and keep it in a safe place.
Note - If you are using the Terraform module , use the
rest_api_url
output.

-
In Terraform Cloud, log in to your organization.
-
From the top menu, click Settings.
-
From the side menu, in the Integrations section, click Run tasks.
-
In the Create a Run Task section:
-
Enter a name for the run task.
-
In the Endpoint URL field, paste the Gateway API URL you copied from AWS.
-
Enter a description for the run task.
-
In the HMAC key field, past the value of the
HMAC_key
from Code Security.
-
-
Click Create run task.

-
Open the relevant Terraform Cloud Workspace.
-
Expand Settings > click Run Tasks.
-
In the Configure Run Task menu, set the Enforcement Level to Mandatory.
-
Click Save.

In Terraform Cloud, trigger a run to test the Run Task.