如何使用自定义的“terraform apply”来实现 terratest(golang)命令?
我使用以下几行来运行我的地形计划和应用在 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"
它们运行良好且良好;我可以使用类似的命令销毁它:
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上述定制的 terraform 命令? 这是我测试 terraform 模块的 golang 行。
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)
运行“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 can run plan&apply success?
帮助表示感谢!
I use the following lines to run my terraform plan & apply in example/ folder:
"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?
This is my golang lines testing the the terraform module.
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":
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?
Help appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
摆脱神秘的“参数“region”是必需的,但未设置。”错误。我按如下方式运行测试,区域错误消失了:
我找不到任何文档来更改 terraform.InitAndApply(t, terraformOptions) 行为。
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:
I could not find any documentation to alter the terraform.InitAndApply(t, terraformOptions) behavior.