Terraform: `terraform apply <textfile>` gives an unhelpful error: "zip: not a valid zip file"

Created on 8 Aug 2019  ยท  12Comments  ยท  Source: hashicorp/terraform

Terraform Version

Terraform v0.12.4

Terraform Configuration Files

Just need an empty file named, for example, test.

Debug Output

https://gist.github.com/javaJake/88dc0939ccba72a4a091736cb1190fdc

Expected Behavior

There should be an error with a little more usage information sprinkled in, such as:

zip: not a valid zip file
There was an error reading the plan file "test". Plan files are created by running "terraform plan".

Actual Behavior

I got just the cryptic error:

zip: not a valid zip file

Steps to Reproduce

  1. echo > test
  2. terraform apply test
cli enhancement

Most helpful comment

I was also getting the same error zip: not a valid zip file
This is specifically because of mistakenly providing the tf filename in your command.
E.g.
$ terraform plan terra.tf
zip: not a valid zip file

So remove the tf filename from the command and check. Terraform command automatically detect the files with .tf extention, you don't have to explicitly mention that.

$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

All 12 comments

I forgot to include a little background:

I got to this point of confusion because I followed the automation documentation and skipped the "plan" step as recommended because the .tf file is autogenerated in my setup. I misread the command in the documentation as merely an example and included far more arguments than I should have, such as the .tf file.

Hi,
I have the same issue when I run terraform from the command window of Microsoft Visual Code.
Visual code is running on Centos 7.6. Visual Code - Insiders 1.38 version.

However when I run the command from a bash shell outside of VS Code, I get no error.

Any thughts?

Johnson

All,

BTW - if I run the tsch shell in VS code the issue goes away. Just another thought ;-)

Johnson

Hello, I'm currently having this error, please how can it be fixed

You are getting the error because "terraform apply" only works with files made by "terraform plan".

I was also getting the same error zip: not a valid zip file
This is specifically because of mistakenly providing the tf filename in your command.
E.g.
$ terraform plan terra.tf
zip: not a valid zip file

So remove the tf filename from the command and check. Terraform command automatically detect the files with .tf extention, you don't have to explicitly mention that.

$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

Seems like your plugins have become broken.

Try this first:
$ terraform init

I was also getting the same error zip: not a valid zip file
This is specifically because of mistakenly providing the tf filename in your command.
E.g.
$ terraform plan terra.tf
zip: not a valid zip file

So remove the tf filename from the command and check. Terraform command automatically detect the files with .tf extention, you don't have to explicitly mention that.

$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

You need to move your individual tf files to their own directories, and then you can point terraform to that directory. The plan command only accepts directories, and the apply command will only take the entire dir, or plan output file (use -out on plan). I think this limitation is due to the fact that terraform requires a state file for each plan. Here is how I've setup my terraform project, note secrets.tfvars and terraform.tfvars are common between both terraform plans.

$ tree
.
โ”œโ”€โ”€ 1-base
โ”‚ย ย  โ”œโ”€โ”€ provider.tf
โ”‚ย ย  โ”œโ”€โ”€ backend.tf
โ”‚ย ย  โ””โ”€โ”€ core.tf
โ”œโ”€โ”€ 2-k8s
โ”‚ย ย  โ”œโ”€โ”€ 1-k8s.tf
โ”‚ย ย  โ”œโ”€โ”€ 2-helm.tf
โ”‚ย ย  โ”œโ”€โ”€ apps
โ”‚ย ย  โ”œโ”€โ”€ provider.tf
โ”‚ย ย  โ”œโ”€โ”€ backend.tf
โ”‚ย ย  โ”œโ”€โ”€ chart-builds
โ”‚ย ย  โ””โ”€โ”€ charts
โ”œโ”€โ”€ secrets.tfvars
โ”œโ”€โ”€ terraform.tfvars
โ””โ”€โ”€ todo.md

From here I can run:

$ terraform init -var-file=secrets.tfvars ./1-base
$ terraform plan -var-file=secrets.tfvars ./1-base

or

$ terraform apply -var-file=secrets.tfvars ./1-base

I can obviously repeat this process on 2-k8s dir.

I was getting the error
"zip: not a valid zip file" when running the plan
terraform plan -var-file=....environmentlab21a.tfvars
Adding the quotes around the file fixed it.
terraform plan -var-file="....environmentlab21a.tfvars"

I had the same error on this command :
terraform plan -var-file= "./variables/integration.tfvars"
zip: not a valid zip file

For me it is the white space after the "=" sign that causes this error. After removing this white space I have this command terraform plan -var-file="./variables/integration.tfvars" that runs well

I got "zip: not a valid zip file" for using ' instead of of " around my -var argument (i.e. -var='<value>'). Thanks everyone for hints as to why.

I had the same error on this command :
terraform plan -var-file= "./variables/integration.tfvars"
zip: not a valid zip file

For me it is the white space after the "=" sign that causes this error. After removing this white space I have this command terraform plan -var-file="./variables/integration.tfvars" that runs well

Thank you so much for the hint. After spending so many hours I also actually able to resolve by removing white spaces around "="

Was this page helpful?
0 / 5 - 0 ratings