## TL;DR

Your `.terraform.lock.hcl` does not cover a provider your configuration now requires. That happens when you add a provider (or a module that needs one) after the last `terraform init`. Run `terraform init -upgrade` to record the new selection, commit the lock file, and move on.

## The error

```text
Error: Inconsistent dependency lock file

- provider registry.terraform.io/hashicorp/random: required by this
  configuration but no version is selected
```

Another common shape:

```text
Error: Inconsistent dependency lock file. The following dependency selections recorded in the lock file are inconsistent...
```

## Steps to fix

1. Confirm the cause: you (or a merged PR) added a provider to `required_providers` or added a module that pulls one in, without re-running init.
   - Expected: `git log --oneline -3 -- .terraform.lock.hcl` shows the lock file is older than the config change.
2. Run `terraform init -upgrade`.
   - Expected: Terraform selects versions for the new providers and rewrites `.terraform.lock.hcl`. `init` succeeds.
3. Commit the updated `.terraform.lock.hcl`.
   - Expected: `git status` shows the lock file modified; CI will now use the same selections.
4. Run `terraform plan` to confirm everything resolves.
   - Expected: plan starts without lock errors.

## When to use this

- `terraform init` fails with `Inconsistent dependency lock file` after adding a provider, adding a module, or pulling a branch that did either.

## When NOT to use this

- `Failed to query available provider packages` means the registry cannot satisfy your version constraints (a constraint problem, not a lock problem). If the lock file is committed and CI still fails, the committed file is stale: regenerate it locally and commit.

## Compatibility

- Terraform 0.14+ (lock file introduced). `-upgrade` behavior is stable across 1.x.

## Root cause

The lock file pins the exact provider versions and hashes your configuration resolved to. Adding a new provider requirement makes the recorded selections incomplete, and Terraform refuses to guess: it errors rather than silently picking a version that might differ from what a teammate gets.

## Edge cases

- If your repo gitignores `.terraform.lock.hcl`, every fresh clone re-resolves and you lose reproducibility; commit the lock file instead.
- `terraform init -upgrade` upgrades everything; to add one provider without moving others, temporarily narrow with `-upgrade=false` is not possible, so review the plan diff after upgrading.
- With `dev_overrides` in the CLI config, Terraform skips lock entries for those providers (1.16+ warns about it).