Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Terraform Test Framework Basics
📖 Scenario: You are working on a Terraform module that creates a simple AWS S3 bucket. To ensure your module works correctly, you want to write a basic test using the Terraform test framework introduced in version 1.6.This test will help you verify that the bucket is created with the expected name.
🎯 Goal: Build a Terraform test file that sets up a test environment, applies your module, and checks that the S3 bucket resource is created with the correct bucket name.
📋 What You'll Learn
Create a Terraform test file named bucket_test.go
Use the Terraform test framework syntax introduced in version 1.6
Define a test function named TestS3Bucket
Configure the test to apply the module and check the bucket name output
💡 Why This Matters
🌍 Real World
Terraform tests help ensure your infrastructure code works as expected before deploying to real cloud environments, reducing errors and downtime.
💼 Career
Knowing how to write Terraform tests is valuable for DevOps engineers and cloud architects to maintain reliable infrastructure as code.
Progress0 / 4 steps
1
Create the initial test function and import packages
Write the first lines of the test file bucket_test.go. Import the testing package and the Terraform testing package github.com/hashicorp/terraform-plugin-testing/helper/resource. Then create a test function named TestS3Bucket that takes t *testing.T as a parameter.
Terraform
Hint
Start by declaring the package and importing the required packages. Then define the test function with the exact name TestS3Bucket.
2
Configure the test case with basic settings
Inside the TestS3Bucket function, add a call to resource.Test with a resource.TestCase struct. Set the ProtoV6ProviderFactories field to an empty map map[string]func() (interface{}, error){} to satisfy the provider requirement. Leave other fields empty for now.
Terraform
Hint
Use resource.Test and pass a resource.TestCase struct. Set ProtoV6ProviderFactories to an empty map to avoid provider errors.
3
Add the module configuration and apply step
Inside the resource.TestCase, add a Steps field with a slice containing one resource.TestStep. In that step, set the Config field to a Terraform configuration string that creates an AWS S3 bucket resource named test_bucket with the bucket name my-test-bucket. Use the exact Terraform syntax for the resource block.
Terraform
Hint
Define Steps as a slice with one resource.TestStep. Set the Config field to a Terraform string that creates the S3 bucket with the exact name my-test-bucket.
4
Add a check to verify the bucket name attribute
In the resource.TestStep, add a Check field that uses resource.ComposeTestCheckFunc to verify the S3 bucket resource attribute aws_s3_bucket.test_bucket.bucket equals my-test-bucket. Use resource.TestCheckResourceAttr inside the compose function.
Terraform
Hint
Use Check with resource.ComposeTestCheckFunc and inside it call resource.TestCheckResourceAttr with the resource name, attribute, and expected value.
Practice
(1/5)
1. What is the main purpose of the Terraform test framework introduced in version 1.6+?
easy
A. To monitor cloud resources for performance issues
B. To deploy infrastructure faster without manual approval
C. To write automated tests that check your infrastructure code works as expected
D. To replace Terraform CLI commands with a graphical interface
Solution
Step 1: Understand Terraform test framework purpose
The framework is designed to let you write tests that run your Terraform code and verify resource attributes automatically.
Step 2: Compare options with framework purpose
Only To write automated tests that check your infrastructure code works as expected correctly describes writing automated tests for infrastructure code. Other options describe unrelated features.
Final Answer:
To write automated tests that check your infrastructure code works as expected -> Option C
Quick Check:
Test framework purpose = automated infrastructure tests [OK]
Hint: Focus on testing infrastructure code correctness [OK]
Common Mistakes:
Confusing testing with deployment speed
Thinking it replaces CLI commands
Mixing testing with monitoring
2. Which of the following is the correct syntax to define a Terraform test block in version 1.6+?
easy
A. module "test" { ... }
B. terraform_test "example" { ... }
C. resource "test" "example" { ... }
D. test "example" { ... }
Solution
Step 1: Recall Terraform test block syntax
Terraform 1.6+ uses the test "name" { ... } block to define tests.
Step 2: Eliminate incorrect syntax options
Options B, C, and D use incorrect block types or resource/module keywords not related to tests.
Final Answer:
test "example" { ... } -> Option D
Quick Check:
Test block syntax = test "name" [OK]
Hint: Look for the exact 'test' block keyword [OK]
Common Mistakes:
Using resource or module instead of test block
Adding extra prefixes like terraform_test
Confusing test block with resource block
3. Given this Terraform test snippet, what will the test check?
B. It checks if the instance type of aws_instance.example is 't2.micro'
C. It verifies the instance is running
D. It validates the resource name is 'aws_instance.example'
Solution
Step 1: Analyze the check block
The check block specifies resource "aws_instance.example", attribute "instance_type", and expects it to equal "t2.micro".
Step 2: Match check with options
It checks if the instance type of aws_instance.example is 't2.micro' correctly states the test checks the instance_type attribute equals "t2.micro". Other options misunderstand attribute or resource checks.
Final Answer:
It checks if the instance type of aws_instance.example is 't2.micro' -> Option B
A. The attribute 'name' does not exist for aws_s3_bucket resource
B. The resource block is missing required 'region' argument
C. The test block name cannot contain underscores
D. The equals value must be a number, not a string
Solution
Step 1: Check attribute correctness
The aws_s3_bucket resource uses 'bucket' as the attribute for bucket name, not 'name'. Using 'name' causes the test to fail.
Step 2: Verify other options
Region is usually set in provider, not required here. Test names can have underscores. Equals can be string. So only attribute error is valid.
Final Answer:
The attribute 'name' does not exist for aws_s3_bucket resource -> Option A
Quick Check:
Attribute must match resource schema [OK]
Hint: Check attribute names match resource documentation [OK]
Common Mistakes:
Using wrong attribute names
Assuming region is required in resource block
Thinking test names have naming restrictions
Confusing data types in equals
5. You want to write a Terraform test that verifies multiple attributes of an AWS EC2 instance, including instance_type and tags. Which approach correctly uses the Terraform test framework to check both attributes in one test?
hard
A. Use multiple check blocks inside one test block, each checking one attribute
B. Create separate test blocks for each attribute check
C. Combine attributes in one check block using a list for attribute and equals
D. Use a single check block with a map for attribute and equals
Solution
Step 1: Understand multiple attribute checks in Terraform tests
The Terraform test framework allows multiple check blocks inside one test block to verify different attributes separately.
Step 2: Evaluate options for correctness
Using multiple check blocks inside one test block, each checking one attribute, correctly verifies different attributes separately. Combining attributes into a single check block using a list or map is not supported. Creating separate test blocks for each attribute check works but is less efficient.
Final Answer:
Use multiple check blocks inside one test block, each checking one attribute -> Option A
Quick Check:
Multiple checks = multiple check blocks [OK]
Hint: Use one check block per attribute inside test [OK]
Common Mistakes:
Trying to check multiple attributes in one check block
Splitting checks into many test blocks unnecessarily