During a deployment, a user runs `terraform apply` but the command fails because the state lock cannot be acquired. They suspect the lock was released after the previous `apply` but is still held. What command can they use to force unlock the state?
Trap 1: `terraform init -force-copy`
The `terraform init` command initializes a working directory, configuring backends and providers. The `-force-copy` flag is specifically used in conjunction with `terraform init -migrate-state` to compel a copy of the state file during a backend migration, even if a move would typically occur. This flag has no function related to releasing or managing state locks, which are handled by distinct commands.
Trap 2: `terraform state unlock`
While `terraform state` is a valid subcommand for various state management operations, `unlock` is not a recognized action directly under it. Terraform's CLI structure requires specific commands for managing state locks, and `terraform state unlock` is syntactically incorrect. Attempting to execute this command would result in an error, indicating an unknown or unsupported subcommand for state manipulation.
Trap 3: `terraform break-lock`
The command `terraform break-lock` is not a valid or recognized command within the Terraform CLI. Terraform's command-line interface follows a predefined structure of commands and subcommands for managing infrastructure. Using an undefined command like `break-lock` will simply result in an 'Unknown command' error, as it does not correspond to any functionality for state lock management or other Terraform operations.
- A
`terraform init -force-copy`
Why wrong: The `terraform init` command initializes a working directory, configuring backends and providers. The `-force-copy` flag is specifically used in conjunction with `terraform init -migrate-state` to compel a copy of the state file during a backend migration, even if a move would typically occur. This flag has no function related to releasing or managing state locks, which are handled by distinct commands.
- B
`terraform force-unlock <lock_id>`
This command is the correct and designated mechanism for manually releasing a stuck or orphaned Terraform state lock. When a Terraform operation is interrupted, the state lock might persist, preventing subsequent operations. By providing the unique `<lock_id>`, this command allows an administrator to force the release of the specific lock, enabling further infrastructure changes to proceed safely. This is an essential recovery tool for maintaining operational continuity.
- C
`terraform state unlock`
Why wrong: While `terraform state` is a valid subcommand for various state management operations, `unlock` is not a recognized action directly under it. Terraform's CLI structure requires specific commands for managing state locks, and `terraform state unlock` is syntactically incorrect. Attempting to execute this command would result in an error, indicating an unknown or unsupported subcommand for state manipulation.
- D
`terraform break-lock`
Why wrong: The command `terraform break-lock` is not a valid or recognized command within the Terraform CLI. Terraform's command-line interface follows a predefined structure of commands and subcommands for managing infrastructure. Using an undefined command like `break-lock` will simply result in an 'Unknown command' error, as it does not correspond to any functionality for state lock management or other Terraform operations.