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.

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:
- Linting: Static analysis to ensure your code follows best practices and security standards (e.g., "no open SSH ports").
- 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

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:
YAMLjobs: 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 -checkensures your code is readable and follows standard formatting rules.terraform validatechecks 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.
- Open your workflow YAML file.
- Add a new job called
infrastructure-testthat runs before yourdeployjob. - Include the
terraform fmtandterraform validatesteps shown above. - Commit and push the change to a new branch.
- Observe the pipeline: if you have a formatting error in your
.tffile, 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
tfsecto 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 validatewill 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.
Work with me

CI/CD Pipeline & Docker Containerization
Ship with confidence: automated CI/CD pipelines and Docker setups so every push is tested and deployed — no more manual, error-prone releases.

AI Automation & Agentic Workflow Development
Automate the repetitive work eating your time — content pipelines, data workflows, and agentic AI tasks that run themselves.


