My team and I have been looking at adding testing to our Terraform codebase. Came across the Module Testing Experiment and have been attempting to create a POC based on what has been documented. Created a dummy module that only defines an s3 bucket.
modules/test_module/main.tf
locals {
name = "test_module"
description = "Module used ofr POC of Terraform Tests"
tags = {
project = "test_module"
}
resource "aws_s3_bucket" "this" {
bucket = "test-module-bucket"
acl = "private"
tags = local.tags
cors_rule {
allowed_headers = ["*", ]
allowed_methods = ["POST", ]
allowed_origins = ["*", ]
expose_headers = []
max_age_seconds = 3000
}
}
modules/test_module/tests/defauts/test_defaults.tf
terraform {
required_providers {
test = {
source = "terraform.io/builtin/test"
}
}
}
provider "aws" {
region = "us-east-1"
}
module "main" {
source = "../.."
}
locals{
bucket = module.main.aws_s3_bucket.this
}
resource "test_assertions" "test_bucket" {
component = "test_bucket"
equal "bucket_a_is_private" {
description = "ensure bucket is private"
got = local.bucket.acl
want = "private"
}
check "environment" {
description = "default environment is development"
condition = can(regex("test-module-bucket", local.bucket.bucket))
}
}
But whenever I run terraform test
in modules/test_module
I receive
Error: Unsupported attribute
│
│ on tests/defaults/test_defaults.tf line 17:
│ (source code not available)
│
│ This object does not have an attribute named "aws_s3_bucket".
I’m not sure what I’m missing, according to the official docs and the little 3rd party docs covering the testing experiment this test should be executing.