Back to Blog
Lesson 55 of the CI/CD: Continuous Integration from Scratch course
DevOpsAugust 30, 20264 min read

Testing Infrastructure: IaC Validation in CI/CD Pipelines

Stop deploying broken cloud resources. Learn to run linting and plan verification on your IaC scripts to catch infrastructure errors before they hit production.

DevOpsCI/CDInfrastructure as CodeTerraformGitHub ActionsAutomation
View of large industrial pipelines running through a lush forest landscape.

Previously in this course, we explored Infrastructure as Code Basics to provision cloud resources. While automating infrastructure is powerful, it carries a unique risk: a single typo in a configuration file can accidentally delete databases or expose network ports to the public.

Testing infrastructure—or IaC validation—adds a critical safety layer by ensuring your code is syntactically correct and logically sound before you ever execute a deployment.

Why Validate Infrastructure Code?

In software development, we rely on unit tests to catch logic bugs. In infrastructure, we use two primary methods to catch "deployment bugs" before they cause an outage:

  1. Linting: Static analysis to ensure your code follows best practices and security standards (e.g., "no open SSH ports").
  2. Plan Verification: Running a "dry run" of your infrastructure changes to inspect exactly what will be created, modified, or destroyed.

Think of it this way: linting checks the syntax and style of your blueprint, while plan verification checks the impact of the construction.

Linting Your Infrastructure

Detailed image of a cotton boll against an orange backdrop, showcasing texture.

Most IaC tools come with built-in linting commands. For this example, we will use Terraform, which is the industry standard for infrastructure automation. The terraform validate command checks for syntax errors, while tflint (an external tool) catches common configuration mistakes.

Add this step to your existing GitHub Actions workflow:

YAML
jobs:
  validate-infra:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v2
      - name: Terraform Format Check
        run: terraform fmt -check
      - name: Terraform Validate
        run: terraform validate
  • terraform fmt -check ensures your code is readable and follows standard formatting rules.
  • terraform validate checks if your configuration is internally consistent (e.g., verifying that all required variables are defined).

Verifying Infrastructure Plans

The "Plan" phase is where you simulate the deployment. By generating a plan file, you can output a summary of changes and, more importantly, check if those changes align with your expectations.

In your pipeline, we generate a plan and save it as a file:

YAML
      - name: Terraform Plan
        run: terraform plan -out=tfplan

If you are working in a team, you should add a step to convert this plan into a human-readable format or use a security scanner to check the plan for policy violations—a concept we touched upon in Dependency Scanning.

Hands-on Exercise: The "Dry Run" Gate

Your task is to update your current project's pipeline to include a validation gate.

  1. Open your workflow YAML file.
  2. Add a new job called infrastructure-test that runs before your deploy job.
  3. Include the terraform fmt and terraform validate steps shown above.
  4. Commit and push the change to a new branch.
  5. Observe the pipeline: if you have a formatting error in your .tf file, the pipeline should fail at the linting stage, preventing the deployment.

Common Pitfalls

  • Ignoring Warnings: Many linters provide warnings that aren't strictly "errors." Treat these as technical debt; if you ignore them now, they will cause issues as your infrastructure grows.
  • Hardcoded Secrets: Never include secrets in your infrastructure code. Use tools like tfsec to scan for hardcoded credentials in your IaC files during the linting phase.
  • State Drift: Remember that validation only checks the code, not the real-world state. If someone manually changes a resource in the AWS Console, terraform validate will pass, but your deployment might fail or behave unexpectedly.

FAQ

Q: Is testing infrastructure as important as testing application code? A: It is arguably more important. A bug in your app might crash a service; a bug in your infrastructure (like a misconfigured firewall) can expose your entire company’s data to the internet.

Q: Should I use a dedicated testing framework? A: For beginners, start with validate and plan. As you advance, tools like Terratest allow you to write Go code to spin up real infrastructure, verify it, and tear it down automatically.

Recap

We have moved from manual deployment to a robust CI/CD process. By adding IaC validation to your pipeline, you ensure that only clean, verified, and safe infrastructure configurations reach your cloud environment. You are now treating your infrastructure with the same discipline as your application source code.

Up next: We will look at Integrating External Security Tools to add a final layer of automated protection to your pipeline.

Similar Posts