- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Validate and Test Terraform Configurations
Catch Terraform mistakes before they reach AWS. In this lab, you’ll harden an existing S3 configuration by adding input validation, precondition checks, and postcondition checks. Then you’ll write tests with Terraform’s native testing framework, run them with terraform test, and fix the failing assertions they find.
Lab Info
Table of Contents
-
Challenge
Restrict input values with validation blocks
- Add validation blocks to the environment and bucket name variables.
- Run a plan with invalid values to confirm Terraform rejects them with a clear message.
By the end, your inputs fail early whenever a value is wrong.
-
Challenge
Verify assumptions with precondition checks
- Add a precondition that requires versioning on prod buckets.
- Run plans that break and follow the rule to confirm the precondition fails only when it should.
This gives you a rule that spans more than one input, enforced before anything is created.
-
Challenge
Confirm guarantees with postcondition checks
- Add a postcondition that requires the Environment, Owner, and Project tags on the bucket.
- Deploy the bucket, then remove a tag to confirm the postcondition fails.
Your bucket is now checked by Terraform after it's created, not just before.
-
Challenge
Write and run native tests with terraform test
- Write a test file with a plan-based run block, a run block that expects invalid input to fail, and an apply-based run block.
- Run the tests with
terraform testand review the results.
You'll finish with a test file that checks both your validation and a deployed bucket.
-
Challenge
Interpret test results and fix failing assertions
- Read the failing result and decide whether the configuration or the assertion is wrong.
- Fix the problem and re-run the tests until they all pass, then confirm no resources are left behind.
At the end of this objective, you'll have passing tests and a configuration that meets the requirement.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.