Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic creating/reading remote state #7455

Closed
agonzalezro opened this issue Jul 1, 2016 · 11 comments · Fixed by #7464
Closed

Panic creating/reading remote state #7455

agonzalezro opened this issue Jul 1, 2016 · 11 comments · Fixed by #7464

Comments

@agonzalezro
Copy link

agonzalezro commented Jul 1, 2016

Terraform Version

Terraform v0.7.0-dev

Affected Resource(s)

  • terraform_remote_state using s3 bucket.

Terraform Configuration Files

resource "aws_s3_bucket" "state_bucket" {
  bucket = "terraform-state-abcdefg123"
  acl = "private"
}

resource "terraform_remote_state" "on_s3_by_environment_and_prefix" {
  backend = "s3"

  config {
    bucket = "${aws_s3_bucket.state_bucket.bucket}"
    key    = "terraform.tfstate" # "${var.prefix}something.tfstate"
  }
}

Debug Output

I can privately share it or anonymized it if needed.

Panic Output

I can privately share it or anonymized it if needed.

Expected Behavior

  • Create a bucket and upload the sate to it.

Actual Behavior

  • The bucket is created but the state file isn't.

Steps to Reproduce

  1. terraform apply

Important Factoids

We are using roles for the accounts.

@stack72
Copy link
Contributor

stack72 commented Jul 1, 2016

Hi @agonzalezro

My apologies this has happened - please can you tell me post the piece of the debug output that has the panic in it? We don't need anything that has any config in it, just the actual panic message and the few lines after it that with give us an idea

Paul

@agonzalezro
Copy link
Author

Hi @stack72, thanks for the quick response.

Could this help? https://gist.github.com/agonzalezro/69097ba7c3488c8ab7a802c18129ae81#file-error_console

@apparentlymart
Copy link
Contributor

@agonzalezro was the remote state you're reading also generated by Terraform 0.7.0, or was that produced by an earlier version of Terraform?

There were some changes to the state format for 0.7.0 so I'm wondering if you've hit a bug in the state upgrade code.

@jbardin
Copy link
Member

jbardin commented Jul 1, 2016

Hi @agonzalezro,

Thanks for the extra info. I can reproduce this locally, and we will look into it.

@agonzalezro
Copy link
Author

@apparentlymart it doesn't seem like it. I tried in a completely clean state (no previous tfstate).

@jbardin thanks! Good luck & let me know if there is something else I can help with.

@jbardin
Copy link
Member

jbardin commented Jul 1, 2016

The following config will produce a crash on terraform apply with no previous state required (and without the warning message):

resource "aws_s3_bucket" "state_bucket" {
  bucket = "terraform-state-bucket-1234567"
}

data "terraform_remote_state" "remote_state" {
  backend = "s3"

  config {
    bucket = "${aws_s3_bucket.state_bucket.bucket}"
    key    = "terraform.tfstate"
  }
}

@phinze phinze added the core label Jul 1, 2016
@jbardin
Copy link
Member

jbardin commented Jul 1, 2016

Hi @agonzalezro,

After reading more carefully, I think the part of the problem here is that you're not starting with any remote state. This config won't create a remote state, because the terraform_remote_state resource is read-only. This is a bit confusing, but the recent change to a data source makes this more apparent. You first have to run terraform remote config with the required options. https://www.terraform.io/docs/state/remote/s3.html to sync the state.

The crash when the remote state is missing is still a bug, which we can fix for the next release.

@agonzalezro
Copy link
Author

agonzalezro commented Jul 4, 2016

Do you mean that I require a manual step before being able to use the remote state? If I need to run

terraform remote config x y z

knowing the values o x. y & z. I don't really see the advantage of having the remote configuration in a file.

We are trying to create different states per environment based on ${environment} and ${prefix} so we will end up with N different states. I don't see if this is going to be manageable if we can't trust that the state is there os it's going to be created.

Is it possible perhaps to work around this adding an empty file to the s3 bucket with an empty state?

Do you perhaps know how other people ended up managing this?

Thanks!

@rmenn
Copy link
Contributor

rmenn commented Jul 5, 2016

I have build this from source Terraform v0.7.0-dev (bf63dcc172c2edcd09ca17e43ca13148d01b8c33)

I am hitting this as well. Crash on creating the remote state.

aws_s3_bucket.ops-bucket: Creation complete
terraform_remote_state.state-storage: Creating...
  backend:       "" => "s3"
  config.%:      "" => "3"
  config.bucket: "" => "xxxxxxxx"
  config.key:    "" => "ap-south-1/terraform.tfstate"
  config.region: "" => "ap-south-1"
Error applying plan:

1 error(s) occurred:

* terraform_remote_state.state-storage: unexpected EOF

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

Debug message available here : https://gist.github.com/rmenn/88fe8528ab4396138a7539da2bd81deb

jbardin added a commit that referenced this issue Jul 5, 2016
core: Return an error when there's no remote state
@jbardin
Copy link
Member

jbardin commented Jul 5, 2016

Hi @rmenn,

The above PR wasn't merged yet. The latest version on master should no longer crash in this case.

@agonzalezro

The terraform_remote_state resource, which is now referred to as a data source, is read only. You can use the terraform_remote_state to produce outputs for other parts of your config based on a remote state file, but having terraform_remote_state in your config will not change that remote state file whether it exists or not. Syncing with a remote state file is done through a different mechanism setup by the terraform remote config command. (I agree there could be improvement to the documentation around this, and we still need to add more documentation for the new concept of data sources).

@ghost
Copy link

ghost commented Apr 24, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants