Using Terraform Test for testing your terraform code
The wait is over!!!!!!!!!
Using Terraform Test for testing your terraform code
The wait is over!!!!!!!!!

Hashicorp has introduced its testing framework officially (terraform test).
Note: This testing framework is available in Terraform v1.6.0 and later.
Terraform tests let authors validate that module configuration updates do not introduce breaking changes. Tests run against test-specific, short-lived resources, preventing any risk to your existing infrastructure or state.
By default, tests within Terraform create real infrastructure and can run assertions and validations against that infrastructure. This is analogous to integration testing because you are testing Terraform’s core functionality by executing operations and validating the infrastructure Terraform creates
Lets have a simple example to explain how it works.
Root directory:
# Create a single Compute Engine instance
module "simple_vm" {
source = "./modules"
name = "test-vm"
machine_type = "f1-micro"
zone = "us-west1-a"
tags = ["ssh"]
project = "prj-xx-xxx-xxxx"
image = "debian-cloud/debian-11"
network = "my-vpc"
}
Module:
modules/
├── main.tf
├── outputs.tf
└── variables.tf
main.tf content:
# Create a single Compute Engine instance
resource "google_compute_instance" "default" {
name = var.name
machine_type = var.machine_type
zone = var.zone
tags = var.tags
project = var.project
boot_disk {
initialize_params {
image = var.image
}
}
# Install Flask
metadata_startup_script = "sudo apt-get update; sudo apt-get install -yq build-essential python3-pip rsync; pip install flask"
network_interface {
network = var.network
}
}
variables.tf content:
variable "name" {
type = string
default = "test"
}
variable "zone" {
type = string
default = "us-west1-b"
}
variable "tags" {
type = list(string)
default = []
}
variable "project" {
type = string
default = ""
}
variable "network" {
type = string
default = ""
}
variable "image" {
type = string
default = ""
}
variable "machine_type" {
type = string
default = "n1-standard-2"
}
outputs.tf content:
output "name" {
value = google_compute_instance.default.name
}
output "vm_ip" {
value = google_compute_instance.default.network_interface[0].network_ip
}
output "project" {
value = google_compute_instance.default.project
}
Now, create your test file in the root directory with extension .tftest.hcl
├── main.tf
├── modules
│ ├── main.tf
│ ├── outputs.tf
│ └── variables.tf
├── simple-test.tftest.hcl
content of simple-test.tftest.hcl:
run "execute" {
module {
source = "./"
}
command = apply
assert {
condition = module.simple_vm.name == "test-vm"
error_message = "incorrect name"
}
}
Steps to test using terraform test:
1st run terraform init and then terraform test
$ terraform init
Initializing the backend...
Initializing modules...
Initializing provider plugins...
- Reusing previous version of hashicorp/google from the dependency lock file
- Using previously-installed hashicorp/google v5.3.0
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
$ terraform test
simple-test.tftest.hcl... in progress
run "execute"... pass
simple-test.tftest.hcl... tearing down
simple-test.tftest.hcl... pass
Success! 1 passed, 0 failed.
Lets make a change to fail this test:
module "simple_vm" {
source = "./modules"
name = "not-a-test-vm"
machine_type = "f1-micro"
zone = "us-west1-a"
tags = ["ssh"]
project = "prj-xx-xxx-xxxx"
image = "debian-cloud/debian-11"
network = "my-vpc"
}
Again run terraform test:
$ terraform test
simple-test.tftest.hcl... in progress
run "execute"... fail
╷
│ Error: Test assertion failed
│
│ on simple-test.tftest.hcl line 7, in run "execute":
│ 7: condition = module.simple_vm.name == "test-vm"
│ ├────────────────
│ │ module.simple_vm.name is "not-a-test-vm"
│
│ incorrect name
╵
simple-test.tftest.hcl... tearing down 메타데이터
- post_id
- b6fdbe170ba0
- slug
- using-terraform-test-for-testing-your-terraform-code-b6fdbe170ba0
- url
- https://medium.com/@monusraj/using-terraform-test-for-testing-your-terraform-code-b6fdbe170ba0
- canonical_url
- https://medium.com/@monusraj/using-terraform-test-for-testing-your-terraform-code-b6fdbe170ba0
- author_url
- https://medium.com/@monusraj
- status
- ok
- fetched_at
- 2026-06-29 01:02:39