Use checks to validate infrastructure
Terraform checks let you define assertions to validate as part of your infrastructure management workflow. Unlike variable validation or custom conditions, check
blocks are decoupled from the lifecycle of a specific resource or data source. Checks let you take advantage of Terraform's abstraction of the differences between different provider APIs. Because Terraform standardizes how you interact with all provider APIs, you can use the familiar Terraform language features and syntax to define conditions to validate in your Terraform runs. Checks also let you verify your assumptions about your infrastructure on an ongoing basis instead of just at the time of provisioning.
In this tutorial, you will define and use checks to check the response code for a service and validate that your TLS certificate is still valid. You will define conditions that reference existing resources and integrate data sources to query your systems. If you complete this tutorial using HCP Terraform, you will also enable health assessments and use continuous validation to verify the results of your checks.
Tip
Health assessments are available in HCP Terraform Plus Edition. Refer to HCP Terraform pricing for details.
Prerequisites
You can complete this tutorial using the same workflow with either Terraform Community Edition or HCP Terraform. HCP Terraform is a platform that you can use to manage and execute your Terraform projects. It includes features like remote state and execution, structured plan output, workspace resource summaries, and more.
Select the HCP Terraform tab to complete this tutorial using HCP Terraform.
This tutorial assumes that you are familiar with the Terraform and HCP Terraform workflows. If you are new to Terraform, complete the Get Started tutorials first. If you are new to HCP Terraform, complete the HCP Terraform Get Started tutorials first.
In order to complete this tutorial, you will need the following:
- Terraform v1.5+ installed locally.
- An AWS account.
- An HCP Terraform account with HCP Terraform locally authenticated.
- An HCP Terraform organization with the Plus edition.
- An HCP Terraform variable set configured with your AWS credentials.
Clone example configuration
Clone the example repository for this tutorial. It contains Terraform configuration for a load balancer, self-signed TLS certificate, target group, security group, and an autoscaling group that runs Teramino, a Terraform-skinned Tetris application.
Change to the repository directory.
Define a check
Checks can validate any condition that you can define with Terraform configuration. A check can validate an attribute of your infrastructure, or the functionality of the resource itself. Rather than writing custom scripts to test assertions about your infrastructure, you can use Terraform language features to validate your infrastructure health.
Open your main.tf
file and add a check block that verifies the status of your TLS certificate to the file. Save the file.
A check
block consists of one or more assert
statements. The assertions contain the condition to verify and the error message to display if the assertion fails. This check verifies the TLS certificate's status. To demonstrate how Terraform handles a failed check, the condition asserts that the status is ERRORED
, which will fail.
Create infrastructure and validate checks
Open your terraform.tf
file and uncomment the cloud
block. Replace the
organization
name with your own HCP Terraform organization.
Initialize your configuration. Terraform will automatically create the learn-terraform-checks
workspace in your HCP Terraform organization.
Note: This tutorial assumes that you are using a tutorial-specific HCP Terraform organization with a global variable set of your AWS credentials. Review the Create a Credential Variable Set for detailed guidance. If you are using a scoped variable set, assign it to your new workspace now.
Now, create your infrastructure. Terraform will evaluate any checks included in your configuration as the last step of the operation, and the configuration will fail the check because your certificate status is actually ISSUED
.
Notice that after Terraform generated the execution plan for the run, it displayed the check, but noted that the result would only be known after the apply. At the end of the apply, Terraform determined the certificate status and showed that the check failed.
Open main.tf
again. Find the aws_launch_configuration.terramino
resource and update the instance_type
to t2.small
. This time, Terraform will know at the time of the plan that your configuration's check fails, but will still let you proceed with the instance resize.
Run terraform apply
. This time, Terraform evaluates the check and notifies that it failed, but still lets you change your infrastructure.
Terraform evaluates checks after generating the plan. Unlike custom conditions or variable validation errors, failed checks do not block applies. Terraform will notify if there are any failures or issues to address, letting you decide whether to proceed with the operation.
Unlike other configuration validation mechanisms, checks are decoupled from other components and resources in your configuration. These differ from variable validation, which lets you ensure the inputs to your configuration satisfy your requirements, and custom conditions, which let you define conditions as part of your resource definitions. These conditions are tied to the specific resource lifecycle, rather than your configuration as a whole.
Enable health assessments
HCP Terraform health assessments run daily to verify the status of your infrastructure. These assessments include drift detection, which verifies that your infrastructure settings still match your intended configuration, and continuous validation, which verifies that any checks defined in your configuration still pass.
You can enable assessments on a specific workspace, or on all workspaces within your organization. Enable assessments on the workspace for this tutorial. First, navigate to your learn-terraform-checks
workspace. In the Health section, select Settings. Then, select Enable and click Save settings.
Health assessments run every 12 hours once enabled for a workspace, but you can also manually trigger an assessment to confirm the status of your workspace. Navigate to your workspace's Health page, then click Start health assessment.
After a few moments, HCP Terraform displays the the failed check result.
You can configure workspace notifications to alert your team with assessment results. Automated health assessments proactively identify issues in your infrastructure configuration, rather than having to wait for a manual operation to validate your checks. By resolving issues promptly, you restore predictability in your infrastructure management workflow, since your team does not have to decide how to reconcile issues as part of other operations. Automated assessments help you identify and remediate issues early, avoiding adding operational complexity to Terraform runs.
Use a data source within a check
You can reference data sources in check block assertions. Terraform queries the data source when it evaluates your configuration's checks, at the end of each Terraform operation. This lets you access the most up-to-date data about your environment when a workspace manages many resources and takes longer to complete Terraform operations.
Add the following check to main.tf
.
This check defines a data source that captures the response of a GET request to your Terramino service and asserts that the status code is 200
.
You can reference any data sources or resource attributes in your configuration to define check conditions, but you cannot access data sources defined within check blocks in the rest of your configuration. The data source namespace is scoped within the check block, and evaluates at the time of the check. If needed, you can specify the depends_on
meta-argument for a data source within the check to enforce an evaluation order.
Next, update the condition on your original check so that it verifies that the certificate status is ISSUED
and your check passes.
Run terraform init
to download the http
provider.
Run terraform apply
. Terraform will query the data source and evaluate your new check.
As expected, both of your checks passed. If the checks in your configuration pass, Terraform will record it in your state file, but will not display output for passing checks as part of your run.
Navigate to your learn-terraform-checks
workspace in HCP Terraform, then go to Health > Continuous validation.
As part of the last run, HCP Terraform detected your new check and that both of your checks now pass.
Then, under your workspace's States, open the latest state version and find the check_results
field. Terraform recorded that both of your checks passed in the state file.
Clean up infrastructure
Destroy the resources you created to avoid incurring additional costs.
If you used HCP Terraform for this tutorial, after destroying your resources, delete the learn-terraform-checks
workspace from your HCP Terraform organization.
Next steps
In this tutorial, you used check blocks to define assertions for Terraform to evaluate as part of your infrastructure management operations. You learned about referencing data sources within check blocks, their limited scoping, and the difference between check blocks and other configuration validation mechanisms.
Checks and configuration-level validation such as custom conditions and variable validation create predictability in your infrastructure operations by helping you verify your assumptions.
Review the following resources to learn more about how to ensure conformity in our infrastructure using Terraform and HCP Terraform.
- Review the checks documentation
- Review check examples for the AWS, Azure, and GCP providers
- Learn how to use custom conditions to validate module inputs and usage
- Learn how to write Sentinel policies to evaluate your infrastructure changes
- Configure OPA policies in HCP Terraform to ensure compliances at an organization level