How can I run terratest via terraform cloud?

523 Views Asked by At

I can run terratest to spin up and destroy an EKS cluster. These are basically the two lines that do it.

defer terraform.Destroy(t, terraformOptions)
terraform.InitAndApply(t, terraformOptions)

And the output shows that it's just running Terraform commands under the hood. It's using a local state.

I can also do just a regular terraform apply/destroy with equivalent results.

And if I specify a remote backend pointing to a Terraform Cloud workspace, same results, just executing via the workspace. The workspace is set to execute with an Agent Pool.

However, I encounter the following issue if I try to run Terratest with the same backend. For some reason, Terratest isn't uploading to right spot, or it's looking in the wrong spot, or something. It is able to initialize ok..

$ go test -timeout 3m -run TestInitAndPlan
TestInitAndPlan 2022-03-07T16:10:48-08:00 test_structure.go:130: Copied terraform folder ../modules/eks to /var/folders/xg/dyv99y_950qdh4yj5qd0891xnxqvd5/T/TestInitAndPlan784035467/project/modules/eks
TestInitAndPlan 2022-03-07T16:10:48-08:00 region.go:92: Using region ap-south-1
TestInitAndPlan 2022-03-07T16:10:48-08:00 retry.go:91: terraform [init -upgrade=false]
TestInitAndPlan 2022-03-07T16:10:48-08:00 logger.go:66: Running command terraform with args [init -upgrade=false]
TestInitAndPlan 2022-03-07T16:10:48-08:00 logger.go:66: Initializing modules...
TestInitAndPlan 2022-03-07T16:10:48-08:00 logger.go:66: Downloading registry.terraform.io/terraform-aws-modules/eks/aws 18.7.2 for eks...
TestInitAndPlan 2022-03-07T16:10:51-08:00 logger.go:66: - eks in .terraform/modules/eks
TestInitAndPlan 2022-03-07T16:10:51-08:00 logger.go:66: - eks.eks_managed_node_group in .terraform/modules/eks/modules/eks-managed-node-group
TestInitAndPlan 2022-03-07T16:10:51-08:00 logger.go:66: - eks.eks_managed_node_group.user_data in .terraform/modules/eks/modules/_user_data
TestInitAndPlan 2022-03-07T16:10:51-08:00 logger.go:66: - eks.fargate_profile in .terraform/modules/eks/modules/fargate-profile
TestInitAndPlan 2022-03-07T16:10:51-08:00 logger.go:66: - eks.self_managed_node_group in .terraform/modules/eks/modules/self-managed-node-group
TestInitAndPlan 2022-03-07T16:10:51-08:00 logger.go:66: - eks.self_managed_node_group.user_data in .terraform/modules/eks/modules/_user_data
TestInitAndPlan 2022-03-07T16:10:51-08:00 logger.go:66: Downloading registry.terraform.io/terraform-aws-modules/key-pair/aws 1.0.0 for key_pair...
TestInitAndPlan 2022-03-07T16:10:52-08:00 logger.go:66: - key_pair in .terraform/modules/key_pair
TestInitAndPlan 2022-03-07T16:10:52-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:10:52-08:00 logger.go:66: Initializing Terraform Cloud...
TestInitAndPlan 2022-03-07T16:10:54-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:10:54-08:00 logger.go:66: Initializing provider plugins...
TestInitAndPlan 2022-03-07T16:10:54-08:00 logger.go:66: - Finding hashicorp/tls versions matching ">= 2.2.0, 3.1.0"...
TestInitAndPlan 2022-03-07T16:10:54-08:00 logger.go:66: - Finding hashicorp/aws versions matching ">= 2.46.0, >= 3.72.0, 3.74.2"...
TestInitAndPlan 2022-03-07T16:10:54-08:00 logger.go:66: - Finding hashicorp/kubernetes versions matching "2.7.1"...
TestInitAndPlan 2022-03-07T16:10:54-08:00 logger.go:66: - Finding terraform-aws-modules/http versions matching "2.4.1"...
TestInitAndPlan 2022-03-07T16:10:54-08:00 logger.go:66: - Finding hashicorp/cloudinit versions matching ">= 2.0.0"...
TestInitAndPlan 2022-03-07T16:10:55-08:00 logger.go:66: - Installing terraform-aws-modules/http v2.4.1...
TestInitAndPlan 2022-03-07T16:10:58-08:00 logger.go:66: - Installed terraform-aws-modules/http v2.4.1 (self-signed, key ID B2C1C0641B6B0EB7)
TestInitAndPlan 2022-03-07T16:10:58-08:00 logger.go:66: - Installing hashicorp/cloudinit v2.2.0...
TestInitAndPlan 2022-03-07T16:11:01-08:00 logger.go:66: - Installed hashicorp/cloudinit v2.2.0 (signed by HashiCorp)
TestInitAndPlan 2022-03-07T16:11:01-08:00 logger.go:66: - Installing hashicorp/tls v3.1.0...
TestInitAndPlan 2022-03-07T16:11:03-08:00 logger.go:66: - Installed hashicorp/tls v3.1.0 (signed by HashiCorp)
TestInitAndPlan 2022-03-07T16:11:04-08:00 logger.go:66: - Installing hashicorp/aws v3.74.2...
TestInitAndPlan 2022-03-07T16:11:31-08:00 logger.go:66: - Installed hashicorp/aws v3.74.2 (signed by HashiCorp)
TestInitAndPlan 2022-03-07T16:11:32-08:00 logger.go:66: - Installing hashicorp/kubernetes v2.7.1...
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: - Installed hashicorp/kubernetes v2.7.1 (signed by HashiCorp)
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: Partner and community providers are signed by their developers.
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: If you'd like to know more about provider signing, you can read about it here:
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: https://www.terraform.io/docs/cli/plugins/signing.html
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: Terraform has created a lock file .terraform.lock.hcl to record the provider
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: selections it made above. Include this file in your version control repository
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: so that Terraform can guarantee to make the same selections by default when
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: you run "terraform init" in the future.
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: Terraform Cloud has been successfully initialized!
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: You may now begin working with Terraform Cloud. Try running "terraform plan" to
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: see any changes that are required for your infrastructure.
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: If you ever set or change modules or Terraform Settings, run "terraform init"
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: again to reinitialize your working directory.
TestInitAndPlan 2022-03-07T16:11:38-08:00 retry.go:91: terraform [plan -input=false -lock=false -lock=false]
TestInitAndPlan 2022-03-07T16:11:38-08:00 logger.go:66: Running command terraform with args [plan -input=false -lock=false -lock=false]
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66: Running plan in Terraform Cloud. Output will stream here. Pressing Ctrl-C
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66: will stop streaming the logs, but will not stop the plan running remotely.
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66: Preparing the remote plan...
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66: The remote workspace is configured to work with configuration at
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66: local_run relative to the target repository.
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66: Terraform will upload the contents of the following directory,
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66: excluding files or directories as defined by a .terraformignore file
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66: at /private/var/folders/xg/dyv99y_950qdh4yj5qd0891xnxqvd5/T/TestInitAndPlan784035467/project/modules/eks/.terraformignore (if it is present),
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66: in order to capture the filesystem context the remote workspace expects:
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66:     /private/var/folders/xg/dyv99y_950qdh4yj5qd0891xnxqvd5/T/TestInitAndPlan784035467/project/modules/eks
TestInitAndPlan 2022-03-07T16:11:41-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:11:49-08:00 logger.go:66: To view this run in a browser, visit:
TestInitAndPlan 2022-03-07T16:11:49-08:00 logger.go:66: https://app.terraform.io/app/cambiahealth/terratest/runs/run-PGy7ABFZxTB9xbbo
TestInitAndPlan 2022-03-07T16:11:49-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:11:51-08:00 logger.go:66: Waiting for the plan to start...
TestInitAndPlan 2022-03-07T16:11:51-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:12:23-08:00 logger.go:66: Terraform v1.1.7
TestInitAndPlan 2022-03-07T16:12:23-08:00 logger.go:66: on linux_amd64
TestInitAndPlan 2022-03-07T16:12:25-08:00 logger.go:66: Operation failed: failed writing remote backend config file: open /root/.tfc-agent/component/terraform/runs/run-PGy7ABFZxTB9xbbo.plan/config/local_run/zzz_backend_override.tf: no such file or directory
TestInitAndPlan 2022-03-07T16:12:26-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:12:26-08:00 logger.go:66: ------------------------------------------------------------------------
TestInitAndPlan 2022-03-07T16:12:26-08:00 logger.go:66:
TestInitAndPlan 2022-03-07T16:12:28-08:00 retry.go:99: Returning due to fatal error: FatalError{Underlying: error while running command: exit status 1; }
--- FAIL: TestInitAndPlan (100.54s)
    plan.go:17:
            Error Trace:    plan.go:17
                                        init_and_plan_test.go:59
            Error:          Received unexpected error:
                            FatalError{Underlying: error while running command: exit status 1; }
            Test:           TestInitAndPlan
FAIL
exit status 1
FAIL    test    101.033s

I can see the output in the remote workspace, indicating it is communicating with the backend, but i'm not sure why this error is happening. Any ideas how to solve this?

0

There are 0 best solutions below