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

Save state-out file locally when pushing to remote fails. #14311

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion state/remote/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package remote

import (
"bytes"
"fmt"

"github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/terraform"
Expand Down Expand Up @@ -59,7 +60,18 @@ func (s *State) PersistState() error {
return err
}

return s.Client.Put(buf.Bytes())
if err := s.Client.Put(buf.Bytes()); err != nil {
// Fallback to write the state to a local file if pushing to remote backend fails.
var l state.State = &state.LocalState{Path: "remote.tfstate"}
if err := l.WriteState(s.state); err == nil {
fmt.Printf("\n" +
"*** Local copy of the state-out file is saved to `remote.tfstate`. ***\n" +
"*** You can upload it manually to the remote backend. ***\n\n")
}
// Return previous (remote) error.
return err
}
return nil
}

// Lock calls the Client's Lock method if it's implemented.
Expand Down