You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform Terratest报错:Destroy函数未定义问题求助

Terraform.Destory未定义错误解决方案

我尝试使用Terratest测试用于部署AWS实例并托管静态网站的Terraform代码,但运行测试模块后,出现了Destroy函数未定义的错误。

问题代码

package test

import (
    "fmt"
    http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
    "github.com/gruntwork-io/terratest/modules/terraform"

    "testing"
    "time"
)

func TestTerraformHelloWorldExample(t *testing.T) {
    t.Parallel()

    // Construct the terraform options with default retryable errors to handle the most common
    // retryable errors in terraform testing.
    terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        // Set the path to the Terraform code that will be tested.
        TerraformDir: "../project-3/webserver",
    })
    terraform.InitAndApply(t, terraformOptions)

    defer terraform.Destory(t, terraformOptions)
    publicIp := terraform.Output(t, terraformOptions, "public_ip")
    url := fmt.Sprintf("http://%s:8080", publicIp)
    http_helper.HttpGetWithRetry(t, url, nil, 200, "I have made  a Terraform module", 30, 5*time.Second)
}

错误输出

> go test webserver_test.go
# command-line-arguments [command-line-arguments.test]
./webserver_test.go:23:18: undefined: terraform.Destory
FAIL    command-line-arguments [build failed]
FAIL 

解决方案

问题源于拼写错误:你调用的terraform.Destory是错误写法,Terratest中正确的销毁函数是terraform.Destroy(单词正确拼写为Destroy,而非Destory)。

修正后的代码行应为:

defer terraform.Destroy(t, terraformOptions)

将该行拼写修正后,重新运行测试即可解决该错误。

内容的提问来源于stack exchange,提问作者NIRAJ BAIS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 17:01:15