hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.3k stars 9.49k forks source link

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

Open javaJake opened 5 years ago

javaJake commented 5 years ago

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
javaJake commented 5 years ago

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.

johnsonhays commented 5 years ago

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

johnsonhays commented 5 years ago

All,

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

Johnson

joekhese commented 4 years ago

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

javaJake commented 4 years ago

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

indianheron commented 4 years ago

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.

scarybot commented 4 years ago

Seems like your plugins have become broken.

Try this first: $ terraform init

jfortier-haptiq commented 4 years ago

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.

irfanaziz098 commented 4 years ago

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

hakimoun commented 4 years ago

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

erikerikson commented 4 years ago

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.

kailashbisht09 commented 4 years ago

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 "="

flacodirt commented 3 years ago

This is still an issue. Apply only works if you omit the [plan] argument that is supposed to be supported according to:

terraform apply [options] [plan] https://www.terraform.io/docs/cli/commands/apply.html

If you add the pre-generated plan file from a previous plan step: zip: not a valid zip file

Starting: Terraform : apply plan
==============================================================================
Task         : Terraform CLI
Description  : Execute terraform cli commands
Version      : 0.6.15
Author       : Charles Zipp
Help         : 
==============================================================================
/opt/hostedtoolcache/terraform/0.14.9/x64/terraform version
2021/03/26 17:37:33 [INFO] Terraform version: 0.14.9  
2021/03/26 17:37:33 [INFO] Go runtime version: go1.15.6
2021/03/26 17:37:33 [INFO] CLI args: []string{"/opt/hostedtoolcache/terraform/0.14.9/x64/terraform", "version"}
2021/03/26 17:37:33 [DEBUG] Attempting to open CLI config file: /home/AzDevOps/.terraformrc
2021/03/26 17:37:33 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2021/03/26 17:37:33 [INFO] CLI command args: []string{"version"}
Terraform v0.14.9
+ provider registry.terraform.io/hashicorp/azurerm v2.43.0
+ provider registry.terraform.io/hashicorp/random v3.1.0

/opt/hostedtoolcache/terraform/0.14.9/x64/terraform apply -auto-approve -lock=false -input=false -var-file=params/dev.tfvars dev.plan
2021/03/26 17:37:33 [INFO] Terraform version: 0.14.9  
2021/03/26 17:37:33 [INFO] Go runtime version: go1.15.6
2021/03/26 17:37:33 [INFO] CLI args: []string{"/opt/hostedtoolcache/terraform/0.14.9/x64/terraform", "apply", "-auto-approve", "-lock=false", "-input=false", "-var-file=params/dev.tfvars", "dev.plan"}
2021/03/26 17:37:33 [DEBUG] Attempting to open CLI config file: /home/AzDevOps/.terraformrc
2021/03/26 17:37:33 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2021/03/26 17:37:33 [INFO] CLI command args: []string{"apply", "-auto-approve", "-lock=false", "-input=false", "-var-file=params/dev.tfvars", "dev.plan"}
zip: not a valid zip file
sftim commented 3 years ago

There should be an error with a little more usage information sprinkled in

Makes sense to me.

lowflying commented 3 years ago

This is still an issue. Apply only works if you omit the [plan] argument that is supposed to be supported according to:

terraform apply [options] [plan] https://www.terraform.io/docs/cli/commands/apply.html

If you add the pre-generated plan file from a previous plan step: zip: not a valid zip file

Starting: Terraform : apply plan
==============================================================================
Task         : Terraform CLI
Description  : Execute terraform cli commands
Version      : 0.6.15
Author       : Charles Zipp
Help         : 
==============================================================================
/opt/hostedtoolcache/terraform/0.14.9/x64/terraform version
2021/03/26 17:37:33 [INFO] Terraform version: 0.14.9  
2021/03/26 17:37:33 [INFO] Go runtime version: go1.15.6
2021/03/26 17:37:33 [INFO] CLI args: []string{"/opt/hostedtoolcache/terraform/0.14.9/x64/terraform", "version"}
2021/03/26 17:37:33 [DEBUG] Attempting to open CLI config file: /home/AzDevOps/.terraformrc
2021/03/26 17:37:33 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2021/03/26 17:37:33 [INFO] CLI command args: []string{"version"}
Terraform v0.14.9
+ provider registry.terraform.io/hashicorp/azurerm v2.43.0
+ provider registry.terraform.io/hashicorp/random v3.1.0

/opt/hostedtoolcache/terraform/0.14.9/x64/terraform apply -auto-approve -lock=false -input=false -var-file=params/dev.tfvars dev.plan
2021/03/26 17:37:33 [INFO] Terraform version: 0.14.9  
2021/03/26 17:37:33 [INFO] Go runtime version: go1.15.6
2021/03/26 17:37:33 [INFO] CLI args: []string{"/opt/hostedtoolcache/terraform/0.14.9/x64/terraform", "apply", "-auto-approve", "-lock=false", "-input=false", "-var-file=params/dev.tfvars", "dev.plan"}
2021/03/26 17:37:33 [DEBUG] Attempting to open CLI config file: /home/AzDevOps/.terraformrc
2021/03/26 17:37:33 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2021/03/26 17:37:33 [INFO] CLI command args: []string{"apply", "-auto-approve", "-lock=false", "-input=false", "-var-file=params/dev.tfvars", "dev.plan"}
zip: not a valid zip file

Agreed, I am getting similar output running a Plan in step 1 of an Azure yaml pipeline, tar-ing the output and publishing this as an artifact, in step 3 I download, unpack and attempt to apply the plan and get a

│ Error: Failed to load "xyz.tfplan" as a plan file │ │ Error: zip: not a valid zip file

Perhaps this would be better as top level issue? While I agree the error is pretty useless it shouldn't be happening at all judging from the apply docs linked.

3dward commented 3 years ago

This is still an issue. Apply only works if you omit the [plan] argument that is supposed to be supported according to:

terraform apply [options] [plan] https://www.terraform.io/docs/cli/commands/apply.html

If you add the pre-generated plan file from a previous plan step: zip: not a valid zip file

Starting: Terraform : apply plan
==============================================================================
Task         : Terraform CLI
Description  : Execute terraform cli commands
Version      : 0.6.15
Author       : Charles Zipp
Help         : 
==============================================================================
/opt/hostedtoolcache/terraform/0.14.9/x64/terraform version
2021/03/26 17:37:33 [INFO] Terraform version: 0.14.9  
2021/03/26 17:37:33 [INFO] Go runtime version: go1.15.6
2021/03/26 17:37:33 [INFO] CLI args: []string{"/opt/hostedtoolcache/terraform/0.14.9/x64/terraform", "version"}
2021/03/26 17:37:33 [DEBUG] Attempting to open CLI config file: /home/AzDevOps/.terraformrc
2021/03/26 17:37:33 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2021/03/26 17:37:33 [INFO] CLI command args: []string{"version"}
Terraform v0.14.9
+ provider registry.terraform.io/hashicorp/azurerm v2.43.0
+ provider registry.terraform.io/hashicorp/random v3.1.0

/opt/hostedtoolcache/terraform/0.14.9/x64/terraform apply -auto-approve -lock=false -input=false -var-file=params/dev.tfvars dev.plan
2021/03/26 17:37:33 [INFO] Terraform version: 0.14.9  
2021/03/26 17:37:33 [INFO] Go runtime version: go1.15.6
2021/03/26 17:37:33 [INFO] CLI args: []string{"/opt/hostedtoolcache/terraform/0.14.9/x64/terraform", "apply", "-auto-approve", "-lock=false", "-input=false", "-var-file=params/dev.tfvars", "dev.plan"}
2021/03/26 17:37:33 [DEBUG] Attempting to open CLI config file: /home/AzDevOps/.terraformrc
2021/03/26 17:37:33 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.terraform.d/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /home/AzDevOps/.local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2021/03/26 17:37:33 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2021/03/26 17:37:33 [INFO] CLI command args: []string{"apply", "-auto-approve", "-lock=false", "-input=false", "-var-file=params/dev.tfvars", "dev.plan"}
zip: not a valid zip file

I have similar problems with you. Since we are using the same terraform extension, may i inquire if you happen to use in your terraform plan task the publishPlanResults feature ?

On my case, I observed that whenever I used the said feature to view the plan. It causes the terraform apply to unable to process the out file. I think this is due to the exclusive lock of the file. That is why its giving an error of failed to load as a plan file.

In which case, i think its both bug and enhancement.

WORKAROUND: If you still want to use the publishPlanResults feature, what i did was I included in the artifact the files and modules i needed. Afterwards, perform terraform init, plan and apply without using an out file (dont like it but it works) Otherwise, just dont use the publishPlanResults feature in your task for now until this possible bug been remediated.

HTH.

Edward G.

IgorOrmus commented 2 years ago

FYI this works ok in Azure DevOps if instead of the Charles Zipp extension you use the Microsoft provided version of terraform for your plan task https://github.com/microsoft/azure-pipelines-extensions/tree/master/Extensions/Terraform/Src

gunacc commented 1 year ago

Is this bug not yet fixed? I got this and searching for a fix!

Here is the error message which I got in Azure pipelines,

2023-02-16T20:26:17.7546979Z ##[section]Starting: [Dev] Terraform : Apply 2023-02-16T20:26:17.7556719Z ============================================================================== 2023-02-16T20:26:17.7557021Z Task : Terraform CLI 2023-02-16T20:26:17.7557187Z Description : Execute terraform cli commands 2023-02-16T20:26:17.7557397Z Version : 0.7.8 2023-02-16T20:26:17.7557550Z Author : Charles Zipp 2023-02-16T20:26:17.7557726Z Help : 2023-02-16T20:26:17.7557848Z ============================================================================== 2023-02-16T20:26:18.0948185Z [command]/opt/hostedtoolcache/terraform/1.3.9/x64/terraform version 2023-02-16T20:26:18.1277192Z Terraform v1.3.9 2023-02-16T20:26:18.1278022Z on linux_amd64 2023-02-16T20:26:18.1278597Z + provider registry.terraform.io/hashicorp/azurerm v3.43.0 2023-02-16T20:26:18.1279220Z + provider registry.terraform.io/hashicorp/random v3.4.3 2023-02-16T20:26:18.1366245Z [command]/opt/hostedtoolcache/terraform/1.3.9/x64/terraform apply -auto-approve /home/vsts/work/1/a/312-plan.tfplan/312-plan.tfplan 2023-02-16T20:26:18.1622898Z ╷ 2023-02-16T20:26:18.1624065Z │ Error: Failed to load "/home/vsts/work/1/a/312-plan.tfplan/312-plan.tfplan" as a plan file 2023-02-16T20:26:18.1624776Z │  2023-02-16T20:26:18.1625375Z │ Error: zip: not a valid zip file 2023-02-16T20:26:18.1625940Z ╵ 2023-02-16T20:26:18.1664741Z ##[error]Terraform command 'apply' failed with exit code '1'. 2023-02-16T20:26:18.1769231Z ##[error]╷ │ Error: Failed to load "/home/vsts/work/1/a/312-plan.tfplan/312-plan.tfplan" as a plan file │  │ Error: zip: not a valid zip file ╵

2023-02-16T20:26:18.2209568Z ##[section]Finishing: [Dev] Terraform : Apply

flaird commented 1 year ago

I'm getting this error while following the tutorial on: https://developer.hashicorp.com/terraform/tutorials/certification-associate-tutorials-003/plan

I'm on windows_386, 10 Pro, 22H2 using VScode, Terraform v1.5.5

tomas0620 commented 5 months ago

Hi, same error for me in DevOps Server 2020 :(