How to Scan Terraform State Files for IaC Misconfigurations
Use case: A DevSecOps engineer wants to ensure that deployed cloud infrastructure remains securely configured. By setting up Terraform repositories in Artifactory and enabling Xray indexing, JFrog Advanced Security automatically scans Terraform State files for IaC misconfigurations after every deployment.
Before You Begin
You need the following:
- JFrog Artifactory with Terraform repository support.
- JFrog Xray with Advanced Security enabled.
- Terraform CLI installed and configured on your workstation or CI environment.
- Permissions to create repositories in Artifactory and configure Xray indexing.
Step 1: Set Up Local Terraform Repositories
Create two local Terraform repositories in Artifactory — one for providers and one for modules.
- Navigate to Administration > Repositories > Local.
- Click New Local Repository and select Terraform as the package type.
- Create the first repository for providers (for example,
terraform-providers-local). - Repeat to create a second repository for modules (for example,
terraform-modules-local).
Step 2: Set Up a Remote Terraform Repository
Create a remote Terraform repository to proxy packages from public registries.
- Navigate to Administration > Repositories > Remote.
- Click New Remote Repository and select Terraform as the package type.
- Configure the remote repository URL to point to the public Terraform registry.
- Save the repository (for example,
terraform-remote).
Step 3: Set Up a Virtual Terraform Repository
Create a virtual Terraform repository that aggregates your local and remote repositories.
- Navigate to Administration > Repositories > Virtual.
- Click New Virtual Repository and select Terraform as the package type.
- Add the local provider repository, local modules repository, and remote repository to the virtual repository's resolution order.
- Save the repository (for example,
terraform-virtual).
Step 4: Set Up a Local Terraform Backend Repository and Enable Xray Indexing
Create a local Terraform backend repository where Terraform stores state files after deployment, and enable Xray indexing on it.
- Navigate to Administration > Repositories > Local.
- Click New Local Repository and select Terraform Backend as the package type.
- Save the repository (for example,
terraform-backend-local). - Navigate to Administration > Xray Settings > Indexed Resources.
- Click Add a Repository and select the Terraform backend repository you created.
- Save to enable indexing.
Once indexing is enabled, Xray and Advanced Security scan every state file deployed to this repository.
Step 5: Configure Terraform Client to Use Artifactory
Configure your Terraform CLI to resolve providers and modules through Artifactory.
- In the Artifactory UI, navigate to the virtual Terraform repository you created.
- Click Set Me Up.
- Follow the configuration instructions to point your Terraform client at Artifactory for provider and module resolution.
Step 6: Configure Terraform Backend with Artifactory
Configure Terraform to store state files in the Artifactory backend repository.
- In the Artifactory UI, navigate to the Terraform backend repository you created.
- Click Set Me Up.
- Follow the configuration instructions to configure the Terraform backend block in your Terraform configuration files.
Results
After completing the setup:
- When you run
terraform init, providers and modules are resolved through Artifactory. - When you run
terraform apply, the resulting state file is deployed to the backend Terraform repository in Artifactory. - Xray automatically indexes the state file and Advanced Security scans it for IaC misconfigurations.
- Scan results are available in the JFrog Platform under Xray > Scan Results, showing any cloud misconfigurations detected in your deployed infrastructure.
For more information on the types of misconfigurations detected, see Misconfigurations Scans.
