简体   繁体   English

如何使用自定义的“terraform apply”命令实现 terratest(golang)?

[英]How do I implement terratest(golang) with customized "terraform apply" command?

I use the following lines to run my terraform plan & apply in example/ folder:我使用以下几行来运行我的 terraform 计划并在 example/ 文件夹中应用:

"aws-vault exec sandbox-admin-role --region=us-east-2 -- terraform plan -out=tfplan --var-file=customized.us-east-2.tfvars"
"aws-vault exec sandbox-admin-role --region=us-east-2 -- terraform apply --auto-approve tfplan"

They are running fine & I can destroy it with similar command:它们运行良好,我可以用类似的命令销毁它:

aws-vault exec sandbox-admin-role --region=us-east-2 -- terraform destroy --auto-approve --var-file=customized.us-east-2.tfvars

How do I test it in Golang/terratest with customized terraform command like above?我如何使用自定义的 terraform 命令在 Golang/terratest 中测试它? This is my golang lines testing the the terraform module.这是我的 golang 行测试 terraform 模块。

package test
...
...
func TestTerraformAwsS3Example(t *testing.T) {
    t.Parallel()
...
    terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        TerraformDir: "../examples",
        VarFiles:     []string{"customized.us-east-2.tfvars"},
    })
    
    defer terraform.Destroy(t, terraformOptions)
    
    terraform.InitAndApply(t, terraformOptions)

And I got the following errors when running "go test -v test/s3-bucket_test.go":运行“go test -v test/s3-bucket_test.go”时出现以下错误:

TestTerraformAwsS3Example 2022-03-01T19:14:33-06:00 logger.go:66: Terraform has been successfully initialized!
TestTerraformAwsS3Example 2022-03-01T19:14:33-06:00 logger.go:66: 
TestTerraformAwsS3Example 2022-03-01T19:14:33-06:00 logger.go:66: You may now begin working with Terraform. Try running "terraform plan" to see
TestTerraformAwsS3Example 2022-03-01T19:14:33-06:00 logger.go:66: any changes that are required for your infrastructure. All Terraform commands
TestTerraformAwsS3Example 2022-03-01T19:14:33-06:00 logger.go:66: should now work.
TestTerraformAwsS3Example 2022-03-01T19:14:33-06:00 logger.go:66: 
TestTerraformAwsS3Example 2022-03-01T19:14:33-06:00 logger.go:66: If you ever set or change modules or backend configuration for Terraform,
TestTerraformAwsS3Example 2022-03-01T19:14:33-06:00 logger.go:66: rerun this command to reinitialize your working directory. If you forget, other
TestTerraformAwsS3Example 2022-03-01T19:14:33-06:00 logger.go:66: commands will detect it and remind you to do so if necessary.
TestTerraformAwsS3Example 2022-03-01T19:14:33-06:00 retry.go:91: terraform [apply -input=false -auto-approve -var-file fixtures.us-east-2.tfvars -lock=false]
TestTerraformAwsS3Example 2022-03-01T19:14:33-06:00 logger.go:66: Running command terraform with args [apply -input=false -auto-approve -var-file fixtures.us-east-2.tfvars -lock=false]
TestTerraformAwsS3Example 2022-03-01T19:14:35-06:00 logger.go:66: 
TestTerraformAwsS3Example 2022-03-01T19:14:35-06:00 logger.go:66: Error: Missing required argument
TestTerraformAwsS3Example 2022-03-01T19:14:35-06:00 logger.go:66: 
TestTerraformAwsS3Example 2022-03-01T19:14:35-06:00 logger.go:66: The argument "region" is required, but was not set.
TestTerraformAwsS3Example 2022-03-01T19:14:35-06:00 logger.go:66: 
TestTerraformAwsS3Example 2022-03-01T19:14:35-06:00 retry.go:99: Returning due to fatal error: FatalError{Underlying: error while running command: exit status 1; 
Error: Missing required argument

The argument "region" is required, but was not set.

How do I customize "terraform apply" for the golang test so they could run plan&apply successfully?如何为 golang 测试自定义“terraform apply”,以便他们可以成功运行 plan&apply?

Help appreciated!帮助赞赏!

To get rid of the mysterious "The argument "region" is required, but was not set."摆脱神秘的“需要参数“区域”,但未设置。” error.错误。 I run the test as follows, the region error is gone:我按如下方式运行测试,区域错误消失了:

% aws-vault exec sandbox-admin-role --region=us-east-2 -- go test -v tests/s3-bucket_test.go

    Warning: parent_profile is deprecated, please use include_profile instead in your AWS config
    === RUN   TestTerraformAwsS3Example
    === PAUSE TestTerraformAwsS3Example
    === CONT  TestTerraformAwsS3Example
    TestTerraformAwsS3Example 2022-03-03T10:10:15-06:00 retry.go:91: terraform [init -upgrade=false]
    TestTerraformAwsS3Example 2022-03-03T10:10:15-06:00 logger.go:66: Running command terraform with args [init -upgrade=false]
    TestTerraformAwsS3Example 2022-03-03T10:10:16-06:00 logger.go:66: Initializing modules...
    TestTerraformAwsS3Example 2022-03-03T10:10:16-06:00 logger.go:66: 
    TestTerraformAwsS3Example 2022-03-03T10:10:16-06:00 logger.go:66: Initializing the backend...
    TestTerraformAwsS3Example 2022-03-03T10:10:17-06:00 logger.go:66: 
    TestTerraformAwsS3Example 2022-03-03T10:10:17-06:00 logger.go:66: Initializing provider plugins...
    TestTerraformAwsS3Example 2022-03-03T10:10:17-06:00 logger.go:66: - Using previously-installed hashicorp/null v3.1.0
    TestTerraformAwsS3Example 2022-03-03T10:10:17-06:00 logger.go:66: - Using previously-installed hashicorp/aws v3.74.3
    TestTerraformAwsS3Example 2022-03-03T10:10:17-06:00 logger.go:66: - Using previously-installed hashicorp/random v3.1.0
    TestTerraformAwsS3Example 2022-03-03T10:10:17-06:00 logger.go:66: - Using previously-installed hashicorp/local v2.1.0
    TestTerraformAwsS3Example 2022-03-03T10:10:18-06:00 logger.go:66: 
    TestTerraformAwsS3Example 2022-03-03T10:10:18-06:00 logger.go:66: Terraform has been successfully initialized!
    TestTerraformAwsS3Example 2022-03-03T10:10:18-06:00 logger.go:66: 

I could not find any documentation to alter the terraform.InitAndApply(t, terraformOptions) behavior.我找不到任何文档来改变terraform.InitAndApply(t, terraformOptions)行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将生命周期规则应用于 Terraform 中的现有 s3 存储桶? - How do I apply a lifecycle rule to an EXISTING s3 bucket in Terraform? 您如何使用 terraform 应用程序的 output 作为另一个 terraform 变量的输入? - How do you use the output of a terraform apply as input into another terraform variable? 如何在 VSCode 中更改 Terraform 版本? - How do I Change the Terraform Version in VSCode? 如何在terraform应用中移动terraform资源 - How to move terraform resource in terraform apply Golang如何在Generic中实现接口 - Golang how to implement Interface in Generic 在谷歌云中,使用命令“terraform apply”时出现此错误? - In google cloud, I am getting this error while using the command "terraform apply"? 如何为 Terraform 配置 AWS MFA? - How do I configure AWS MFA for Terraform? 如何强制 terraform 重新创建资源? - How do I force terraform to recreate a resource? 如何在 terraform 中将 rds 与弹性 beantalk 连接 - How do I connect rds with elastic beanstalk in terraform 如何使用 Terraform 清除软删除的 APIM? - How Do I Purge A Soft-Deleted APIM With Terraform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM