8

Environment

Terraform v0.12.24 + provider.aws v2.61.0

Running in an alpine container.

Background

I have a basic terraform script running ok, but now I'm extending it and am trying to configure a remote (S3) state.

terraform.tf:

terraform {
  backend "s3" {
    bucket         = "labs"
    key            = "com/company/labs"
    region         = "eu-west-2"
    dynamodb_table = "labs-tf-locks"
    encrypt        = true
  }
}

The bucket exists, and so does the table. I have created them both with terraform and have confirmed through the console.

Problem

When I run terraform init I get:

Error refreshing state: InvalidParameter: 2 validation error(s) found.
- minimum field size of 1, GetObjectInput.Bucket.
- minimum field size of 1, GetObjectInput.Key.

What I've tried

terraform fmt reports no errors and happily reformats my terraform.tf file. I tried moving the stanza into my main.tf too, just in case the terraform.tf file was being ignored for some reason. I got exactly the same results.

I've also tried running this without the alpine container, from an ubuntu ec2 instance in aws, but I get the same results.

I originally had the name of the terraform file in the key. I've removed that (thanks) but it hasn't helped resolve the problem.

Also, I've just tried running this in an older image: hashicorp/terraform:0.12.17 but I get a similar error:

Error: Failed to get existing workspaces: InvalidParameter: 1 validation error(s) found.
- minimum field size of 1, ListObjectsInput.Bucket.

I'm guessing that I've done something trivially stupid here, but I can't see what it is.

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
  • The `key` argument should be the path suffix for the directory where the state will be stored, and not the path suffix to the state itself. Otherwise, the values look fine. – Matthew Schuchard May 21 '20 at 11:14
  • @MattSchuchard - duh! Of course, and thanks, but I've fixed that now and the problem persists. I've updated the question accordingly. – Software Engineer May 21 '20 at 11:27

2 Answers2

19

Solved!!!

I don't understand the problem, but I have a working solution now. I deleted the .terraform directory and reran terraform init. This is ok for me because I don't have an existing state. The insight came from reading the error from the 0.12.17 version of terraform, which complained about not being able to read the workspace.

Error: Failed to get existing workspaces: InvalidParameter: 1 validation error(s) found.
- minimum field size of 1, ListObjectsInput.Bucket.

Which initially led me to believe there was a problem with an earlier version of tf reading a newer version's configuration. So, I blew away the .terraform and it worked with the older tf, so I did it again and it worked with the newer tf too. Obviously, something had gotten itself screwed up in terraform's storage. I don't know how or why. But, it works for me, so...

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
  • I ran into this but it turned out I was giving Terraform an empty value for the state file name on s3. Logged my script variables and the solution was obvious. – jcollum Jan 27 '22 at 17:31
0

In the context of Gitlab Pipelines:

I also had the similar error message and I realised that I had the bucket name in the variables but it was marked as protected and I was running the pipeline on a branch which is not protected. In this case, gitlab does not expose the variables as environment variable and it was failing.

So, you can check if the bucket name is available to the pipeline or not.

agrawalramakant
  • 160
  • 1
  • 5