Terraform S3 Backend Access Denied: Fix "Error Loading State: AccessDenied" (Exact IAM Policy, Hidden Workspaces, errored.tfstate)

Logeshwaran
—

"Error loading state: AccessDenied: Access Denied" means the AWS identity Terraform is using can't read the state file in your S3 backend bucket. Current Terraform versions word it differently, as Error refreshing state: Unable to access object "shop/terraform.tfstate" in S3 bucket "jakes-tf-state": operation error S3: HeadObject, https response error StatusCode: 403, but the cause is the same. The fix is to find out which identity Terraform is really running as, then give it four things: s3:ListBucket on the bucket, s3:GetObject and s3:PutObject on the state file, and Get, Put and Delete on the .tflock lock file. The full policy is below. Here is the surprise that makes this error so slippery: leave out s3:ListBucket and Terraform doesn't fail at all. It quietly decides your other workspaces don't exist. terraform workspace list shows only default, and terraform workspace select staging says staging doesn't exist, while its state sits right there in the bucket. The second surprise costs more: leave out s3:PutObject and terraform apply changes your real infrastructure first, then fails to save what it did.

Jake runs a phone repair shop. His friend Ethan, a developer, moved the shop's AWS setup into Terraform last spring: a website bucket, a small database, the backups. On Ethan's laptop everything worked, because Ethan's AWS user can do anything. Then they set up a build pipeline with its own restricted role, and the first run stopped with "Error loading state: AccessDenied." Ethan fixed that, and the pipeline reported that the staging workspace didn't exist, though Ethan had used it the day before. He gave the role read access, and an apply ran, changed the database settings, and then failed with "Failed to persist state to backend." This page is what Ethan worked out that week: what the error means in both its old and new wording, how to read which S3 call failed, how to find who Terraform really is, the exact IAM policy the S3 backend needs, what each missing permission does (including the two silent ones), how to recover a state that didn't save, and the KMS, bucket policy, credential and configuration errors that look the same.

⚡ Quick Answer

• What it means → Terraform's AWS identity can't read the state object. Plain English.

• Who is Terraform? → check the identity before the policy; it's often not the one you think. Find it.

• The fix → ListBucket on the bucket, Get and Put on the state, Get, Put and Delete on the lock file. Exact policy.

• Workspaces "don't exist" → s3:ListBucket is missing; Terraform hides the error. The silent one.

• "Failed to persist state to backend" → push errored.tfstate back before anything else. Recover it.

Error mentions the lock? The lock file permissions are a separate set.

New to IAM policies? Our plain-English guide to IAM policy JSON explains Effect, Action and Resource in about ten minutes. You can follow this page without it; every step starts from zero.

🧭 NEW HERE? READ THESE FIRST

New to Terraform state and IAM? These five pages make the rest of this one easy:

📌 Bookmark this; the error-to-permission table and the policy are the parts you will come back for.

What "Error loading state: AccessDenied" means

Terraform keeps a record of everything it manages in a file called the state. When a team shares that record, it usually lives in an S3 bucket, configured with a backend "s3" block like this one:

terraform {
  backend "s3" {
    bucket       = "jakes-tf-state"
    key          = "shop/terraform.tfstate"
    region       = "us-east-1"
    use_lockfile = true
  }
}

Every plan and apply starts by reading that file, and every apply ends by writing it back. In between, Terraform takes a lock so two people can't change the same infrastructure at once. With use_lockfile = true the lock is a small object next to the state, named shop/terraform.tfstate.tflock. Each of those steps is a separate request to S3, and each one needs its own permission.

"Access Denied" means S3 received one of those requests, checked it against IAM and the bucket policy, and refused. Nothing is wrong with your state file or your Terraform code. The identity Terraform used simply isn't allowed to do that one thing.

The wording depends on your Terraform version. Older versions printed:

Error: Error loading state: AccessDenied: Access Denied
	status code: 403, request id: ..., host id: ...

Run terraform version to see which one you have. Terraform 1.16, the current release as of October 2026, names the exact S3 operation that failed, which makes the fix much faster:

Error: Error refreshing state: Unable to access object "shop/terraform.tfstate" in S3 bucket "jakes-tf-state": operation error S3: HeadObject, https response error StatusCode: 403, RequestID: ..., HostID: ..., api error Forbidden: Forbidden

Notice the last part. When the failing call is HeadObject, a request that has no response body, S3 can't send its usual error code, so the message says Forbidden instead of AccessDenied. Both mean the same thing: permission refused.

Jake: "So the state file is fine. Terraform just isn't allowed to open it."

Ethan: "Right. Think of the state as the logbook behind your counter. Terraform has to read the logbook before every job, write in it after, and put a 'busy' sign on the counter while it works. The new pipeline is a temp who got the shop keys but not the key to the logbook drawer."

Read the error: which S3 call failed tells you which permission is missing

The single most useful habit with this error is reading the operation name after operation error S3:. It maps straight to the missing permission. Here is what each one looked like with Terraform 1.16, and what it needs:

What you seeThe S3 call that failedPermission it needs
Error refreshing state: Unable to access object ... HeadObject ... api error ForbiddenChecking the state files3:GetObject on the state key
Error loading state: AccessDenied (older Terraform)Reading the state files3:GetObject on the state key
Error saving state: failed to upload state: operation error S3: PutObject ... AccessDeniedWriting the state after applys3:PutObject on the state key
Error acquiring the state lock ... PutObject ... AccessDenied on .tflockCreating the lock files3:PutObject on the lock key
Failed to get existing workspaces: Unable to list objects ... ListObjectsV2 ... AccessDeniedListing workspacess3:ListBucket on the bucket
Only default in terraform workspace list, no errorListing workspaces, error hiddens3:ListBucket on the bucket
S3 bucket "..." does not existAny call; wrong bucket nameNone; fix the name or create the bucket
No valid credential sources foundNone; Terraform found no credentialsNone; give Terraform credentials

If your error doesn't name an operation, you're on an older Terraform, or you want proof. Run the same command with TF_LOG=DEBUG set. Terraform logs every request it sends, and the refused one has http.status_code=403 next to its name, for example rpc.method=S3/HeadObject.

First, find out who Terraform really is

Before you touch a policy, confirm which AWS identity Terraform is using. More AccessDenied errors come from "the wrong identity" than from "the right identity with the wrong policy," and fixing a policy on the wrong role changes nothing.

Terraform's S3 backend finds credentials through the standard AWS chain: environment variables like AWS_ACCESS_KEY_ID and AWS_PROFILE, the shared files in ~/.aws, and the role attached to the machine it runs on, such as an EC2 instance profile. So run this in the same terminal, with the same environment, right before terraform init:

aws sts get-caller-identity

It prints the account and the exact user or role. Three surprises turn up again and again:

  • A leftover environment variable. An old AWS_ACCESS_KEY_ID exported in your shell beats your profile, so Terraform uses keys you forgot about.
  • The backend has its own settings. A profile or assume_role inside the backend "s3" block applies to the state bucket only. Your provider "aws" block can use a completely different identity, so the provider works and the backend doesn't, or the other way round.
  • CI runs as a different role. The pipeline's role is what matters for pipeline errors, not the developer account that works on a laptop.

If the backend assumes a role, the identity S3 sees is that role, so that role's policy is the one to fix:

terraform {
  backend "s3" {
    bucket       = "jakes-tf-state"
    key          = "shop/terraform.tfstate"
    region       = "us-east-1"
    use_lockfile = true
    assume_role = {
      role_arn = "arn:aws:iam::111122223333:role/terraform-state"
    }
  }
}

When there are no credentials at all, the error is different and says so plainly:

Error: No valid credential sources found

That one isn't about permissions. Log in (for IAM Identity Center, aws sso login --profile your-profile), export a profile, or attach a role to the machine. Our guide to an expired AWS SSO token covers the most common reason a working setup suddenly finds no credentials.

Jake: "How can it be the wrong identity? It's my laptop."

Ethan: "Your laptop has three sets of keys in different drawers: the environment, the profile and the backend block. Terraform takes the first one it finds. get-caller-identity tells you which drawer it opened."

The exact IAM policy Terraform's S3 backend needs

Here is the least-privilege policy for one state file with S3 lock files, for the bucket and key above. Attach it to whichever identity get-caller-identity showed:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ListStateBucket",
      "Effect": "Allow",
      "Action": "s3:ListBucket",
      "Resource": "arn:aws:s3:::jakes-tf-state"
    },
    {
      "Sid": "ReadWriteState",
      "Effect": "Allow",
      "Action": ["s3:GetObject", "s3:PutObject"],
      "Resource": "arn:aws:s3:::jakes-tf-state/shop/terraform.tfstate"
    },
    {
      "Sid": "LockFile",
      "Effect": "Allow",
      "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
      "Resource": "arn:aws:s3:::jakes-tf-state/shop/terraform.tfstate.tflock"
    }
  ]
}

Three details in it trip people up:

  • s3:ListBucket goes on the bucket ARN, with no / after the name. Put it on arn:aws:s3:::jakes-tf-state/* and it grants nothing, because listing is an action on the bucket, not on objects.
  • The state file doesn't need s3:DeleteObject. Terraform never deletes the state of the workspace it's using. The lock file does need it, because every unlock deletes the lock.
  • The lock key is the state key plus .tflock, so a policy written for shop/terraform.tfstate alone leaves the lock file out.

If you use workspaces, their state files live under a prefix, env:/ by default, as env:/staging/shop/terraform.tfstate. Add a statement for them. Delete is included here because terraform workspace delete removes a workspace's state:

{
  "Sid": "WorkspaceStates",
  "Effect": "Allow",
  "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
  "Resource": [
    "arn:aws:s3:::jakes-tf-state/env:/*/shop/terraform.tfstate",
    "arn:aws:s3:::jakes-tf-state/env:/*/shop/terraform.tfstate.tflock"
  ]
}

If your backend sets kms_key_id, add kms:Encrypt, kms:Decrypt and kms:GenerateDataKey on that key; the KMS section explains why. And if you still lock with a DynamoDB table, that table needs dynamodb:DescribeTable, dynamodb:GetItem, dynamodb:PutItem and dynamodb:DeleteItem. DynamoDB locking is deprecated in current Terraform, and moving to use_lockfile = true removes that whole set; our state lock guide walks through the migration.

With exactly the three statements above, an identity could init, plan, apply and unlock in the default workspace. Remove any one of them and you get one of the failures in the next four sections.

Check each permission in thirty seconds, without Terraform

You can test the policy with the AWS CLI before you run Terraform at all. Run these as the same identity Terraform uses, in the same shell, and each one exercises the same S3 call Terraform makes. Run them when no Terraform job is in progress, because the last two create and remove the lock file:

  1. Listing (s3:ListBucket): aws s3api list-objects-v2 --bucket jakes-tf-state --max-items 1 --query 'Contents[0].Key'
  2. Reading the state (s3:GetObject): aws s3api head-object --bucket jakes-tf-state --key shop/terraform.tfstate --query ContentLength
  3. Taking the lock (s3:PutObject on the lock key): aws s3api put-object --bucket jakes-tf-state --key shop/terraform.tfstate.tflock
  4. Releasing it (s3:DeleteObject on the lock key): aws s3api delete-object --bucket jakes-tf-state --key shop/terraform.tfstate.tflock

Each one either prints a result or an error that names the refused call. Here is what a read-only role got for steps 3 and 4:

aws: [ERROR]: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
aws: [ERROR]: An error occurred (AccessDenied) when calling the DeleteObject operation: Access Denied

And a role without read access, at step 2, gets the HEAD-style answer:

aws: [ERROR]: An error occurred (403) when calling the HeadObject operation: Forbidden

The only call you can't safely test this way is writing the state itself, because that would replace your real state. Check that one by reading the policy: s3:PutObject must be allowed on the exact state key.

Missing s3:ListBucket: the error Terraform hides

This is the strangest one, and it's deliberate. When Terraform starts, it lists the bucket to find your workspaces. If that listing is refused with AccessDenied and you use the default env: workspace prefix, Terraform writes a warning to its debug log and carries on as if only the default workspace exists. No error reaches your screen.

For a setup with one workspace, that's harmless. For anyone using workspaces, it's baffling. Here the admin identity created a staging workspace, and then an identity without s3:ListBucket looked for it:

terraform workspace list
* default
terraform workspace select staging
Workspace "staging" doesn't exist.

You can create this workspace with the "new" subcommand
or include the "-or-create" flag with the "select" subcommand.

The staging state was in the bucket the whole time, at env:/staging/shop/terraform.tfstate. Running terraform workspace new staging made it more confusing still: Terraform announced "You're now on a new, empty workspace," but the existing staging state wasn't overwritten, and the next plan read it and reported "No changes." The state was safe; the messages were simply wrong about it.

Set a custom workspace_key_prefix in the backend and the same missing permission is no longer hidden. Init stops with a clear error:

Error: Failed to get existing workspaces: Unable to list objects in S3 bucket "jakes-tf-state" with prefix "workspaces/": operation error S3: ListObjectsV2, https response error StatusCode: 403, RequestID: ..., HostID: ..., api error AccessDenied: Access Denied

Either way, the fix is the first statement of the policy: s3:ListBucket on arn:aws:s3:::jakes-tf-state. Missing ListBucket has one more side effect worth knowing: S3 answers "403 Forbidden" instead of "404 Not Found" for an object that doesn't exist, because without list permission you aren't allowed to learn whether it exists.

Jake: "So the pipeline wasn't lying. From where it stood, staging really didn't exist."

Ethan: "From where it stood, yes. It couldn't see the shelf, so it assumed the shelf was empty. That's the one I'll never forget to grant again."

Missing s3:GetObject: "Error refreshing state" at init

Without read access to the state object, Terraform fails at terraform init, right after it configures the backend:

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

Error: Error refreshing state: Unable to access object "shop/terraform.tfstate" in S3 bucket "jakes-tf-state": operation error S3: HeadObject, https response error StatusCode: 403, RequestID: ..., HostID: ..., api error Forbidden: Forbidden

The "Successfully configured" line above the error throws people. It only means Terraform saved your backend settings locally; it hadn't touched S3 yet. Add s3:GetObject on the state key and init completes.

If the identity has GetObject in its IAM policy and still sees this, look in three places, in this order:

  1. The key. A policy for shop/terraform.tfstate doesn't cover shop/prod/terraform.tfstate. Compare the policy's resource with the key in the backend block and, for workspaces, the env:/ prefix.
  2. The bucket policy. An explicit Deny in the bucket policy beats any Allow in IAM. Common ones deny requests that don't come from a particular VPC endpoint, IP range or role.
  3. KMS. If the state is encrypted with a customer managed key, reading it also needs kms:Decrypt, and without it the read fails with an access error even though every S3 permission is right. See the KMS section.

Our guide to S3 AccessDenied on GetObject goes through every remaining cause in order.

Missing s3:PutObject: "Failed to persist state to backend" and errored.tfstate

This is the expensive one. An identity that can read the state, take the lock, but not write the state passes init and plan without a hint of trouble. Then terraform apply does its work, changing your real infrastructure, and only afterwards tries to save the result:

Error saving state: failed to upload state: operation error S3: PutObject,
https response error StatusCode: 403, RequestID: ..., HostID: ...,
api error AccessDenied: Access Denied

Error: Failed to persist state to backend

The error shown above has prevented Terraform from writing the updated state
to the configured backend. To allow for recovery, the state has been written
to the file "errored.tfstate" in the current working directory.

Running "terraform apply" again at this point will create a forked state,
making it harder to recover.

To retry writing this state, use the following command:
    terraform state push errored.tfstate

Your infrastructure has changed, the state in S3 still describes how things were before, and the only accurate record is errored.tfstate in your working directory. In CI, that directory may be deleted the moment the job ends. Here's the recovery, in order:

  1. Save errored.tfstate somewhere safe right now. In a pipeline, make the job upload it as an artifact before it ends; on a laptop, copy it out of the folder.
  2. Don't run terraform apply again. Terraform's own warning above means it: a second apply plans against the old state and tries to create things that already exist.
  3. Add s3:PutObject on the state key to the identity, using the policy above.
  4. Push the saved state with the same identity: terraform state push errored.tfstate.
  5. Run terraform plan. It should show no changes for what the failed apply already did. If it does, the push worked.

Versioning on the state bucket makes this much less scary, because every earlier state is still there if something goes wrong during recovery. Our state lock guide shows how to restore an earlier version of the state file.

Jake: "So the database change went through, but the logbook says it didn't."

Ethan: "Exactly, and the only page that knows the truth is errored.tfstate. Save that page first, fix the drawer key, then tape the page into the logbook. Never do the job again from the old page."

Lock file permissions: "Error acquiring the state lock"

With use_lockfile = true, Terraform creates shop/terraform.tfstate.tflock before it changes anything and deletes it afterwards. If the identity can't create it, nothing runs:

Error: Error acquiring the state lock

Error message: operation error S3: PutObject, https response error
StatusCode: 403, RequestID: ..., HostID: ...,
api error AccessDenied: Access Denied

A read-only identity hits this on terraform plan too, because plan also takes the lock by default. That's the usual "my read-only CI role can't even plan" puzzle. You have two options:

  • Grant Get, Put and Delete on the .tflock key, as in the policy above. The role still can't change the state itself; it can only take the lock.
  • For plan-only jobs that must stay read-only, run terraform plan -lock=false. That skips the lock; fine for a preview, never for an apply.

If you still lock with a DynamoDB table, the permissions sit in a different place. Here's how the two locking styles compare:

use_lockfile = truedynamodb_table (deprecated)
Where the lock livesA .tflock object next to the stateAn item in a DynamoDB table
Permissionss3:GetObject, s3:PutObject, s3:DeleteObject on the lock keydynamodb:DescribeTable, GetItem, PutItem, DeleteItem on the table
Extra resourcesNoneA table you create and pay for
Typical AccessDeniedPutObject on ...tfstate.tflockPutItem on the table
FutureThe current wayWill be removed in a future Terraform version

If the lock is refused because another run is holding it, rather than for permissions, that's a different problem with its own safe fixes; our guide to "Error acquiring the state lock" covers it in depth.

KMS-encrypted state: AccessDenied with every S3 permission granted

If your backend has kms_key_id, or the bucket's default encryption uses a customer managed KMS key, S3 needs KMS to encrypt or decrypt the state on your behalf, and it uses your permissions to do it. Terraform needs kms:Encrypt, kms:Decrypt and kms:GenerateDataKey on that key:

{
  "Sid": "StateKey",
  "Effect": "Allow",
  "Action": ["kms:Encrypt", "kms:Decrypt", "kms:GenerateDataKey"],
  "Resource": "arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
}

Replace the key ARN with yours. Two places can block this even when the IAM policy is right: the key policy, which has to allow your account's IAM policies or the role directly, and cross-account setups, where the key lives in another account. Our guide to S3 KMS.AccessDeniedException walks through both.

Bucket policies, other accounts and organization rules

An IAM policy is only half the decision. S3 also checks the bucket policy, and AWS Organizations can add service control policies on top. An explicit Deny in any of them wins over every Allow. If the identity's IAM policy is correct and the error persists, look for:

  • A bucket policy that requires a condition, such as requests through one VPC endpoint (aws:SourceVpce), from one IP range, or with encryption headers. Pipelines that run outside that network are denied.
  • A state bucket in another account. Your own account's IAM policy can't grant access to someone else's bucket. The bucket's policy must allow your role too, or your backend should assume a role that lives in the bucket's account. Our guide to S3 cross-account access denied shows both halves.
  • A service control policy from your organization that blocks S3 or a Region for your account. Your administrator can check it; the error looks identical from your side.

The simplest cross-account setup puts all access in the state bucket's account: a role there with the policy above, and assume_role in the backend block pointing at it.

Errors that look like AccessDenied but aren't

"S3 bucket ... does not exist"

A typo in the bucket name, or a bucket that was never created, gives this at init:

Error: Failed to get existing workspaces: S3 bucket "jakes-tf-stat" does not exist.

The referenced S3 bucket must have been previously created. If the S3 bucket
was created within the last minute, please wait for a minute or two and try
again.

Terraform never creates its own state bucket. Create it first, usually by hand or from a small separate Terraform setup, then point the backend at it.

"Backend initialization required"

Change anything in the backend "s3" block, such as the key, bucket or region, and the next plan refuses to run:

Error: Backend initialization required, please run "terraform init"

Reason: Backend configuration block has changed

Run terraform init -migrate-state if you want Terraform to copy your existing state to the new location, or terraform init -reconfigure if the new location already has the right state and you just want to point at it. Picking the wrong one leaves you with an empty state in the new place, so read the prompt before you answer yes.

Wrong Region

The backend's region must be the Region the bucket lives in, which can differ from the Region your infrastructure lives in. If you're not sure where the bucket is, our guide to S3 PermanentRedirect shows how to find a bucket's Region.

Set up the state bucket so these errors never start

Terraform never creates the bucket that holds its own state, so it has to exist before the first terraform init. Three commands set it up the way the S3 backend expects, with versioning so any earlier state can be restored, and with every kind of public access blocked:

aws s3api create-bucket --bucket jakes-tf-state --region us-east-1
aws s3api put-bucket-versioning --bucket jakes-tf-state --versioning-configuration Status=Enabled
aws s3api put-public-access-block --bucket jakes-tf-state --public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true

For any Region other than us-east-1, the first command also needs --create-bucket-configuration LocationConstraint=eu-west-1 (with your Region), and the backend's region must match it. You can confirm both settings afterwards:

aws s3api get-bucket-versioning --bucket jakes-tf-state
{
    "Status": "Enabled"
}

Versioning is the setting that turns most of this page's bad days into minor ones. A state overwritten by a failed recovery, a fork from a second apply, or a wrong -reconfigure can all be undone by restoring an earlier version of the state object. Bucket names are global across all AWS accounts, so pick one nobody else has, typically your company or account name plus -tf-state.

Keep the state bucket separate from everything else, too. A bucket that holds only state, with a policy that only the Terraform roles can use, is easy to reason about, and no website deploy or log job can ever touch it.

The S3 backend block: every setting that matters

Most S3 backends need only four lines, but the rest of the settings explain a lot of the errors on this page. Here is what each one does:

SettingWhat it doesWorth knowing
bucketThe bucket that holds the stateMust already exist
keyThe path of the state file inside the bucketYour IAM policy must name this exact path
regionThe bucket's RegionNot necessarily your infrastructure's Region
use_lockfileLocks with a .tflock object next to the stateDefaults to false; needs Get, Put and Delete on the lock key
encryptServer-side encryption of the state and lock filesSet it to true
kms_key_idUses your own KMS key for that encryptionAdds kms:Encrypt, kms:Decrypt and kms:GenerateDataKey
workspace_key_prefixWhere workspace states liveDefaults to env:; a custom value makes missing ListBucket a visible error
profileA named profile for the backend onlyCan also come from AWS_PROFILE
assume_roleA role the backend assumes for state accessThat role is the identity S3 sees
dynamodb_tableThe old DynamoDB lock tableDeprecated; move to use_lockfile

"Variables not allowed" in the backend block

A very common next step after fixing permissions is trying to make the bucket name a variable, so the same code can use a different bucket per environment. Terraform refuses:

Error: Variables not allowed

  on main.tf line 8, in terraform:
   8:     bucket = var.state_bucket

Variables may not be used here.

The backend is set up before Terraform reads variables, so it can't use them. The supported way is a partial configuration: leave the varying settings out of the block and pass them to terraform init. Keep an empty block in your code:

terraform {
  backend "s3" {}
}

Put the settings for each environment in its own file, such as backend.hcl:

bucket       = "jakes-tf-state"
key          = "shop/terraform.tfstate"
region       = "us-east-1"
use_lockfile = true

And point init at it:

terraform init -backend-config=backend.hcl

You can also pass single values, as in -backend-config="key=shop/prod.tfstate". Whichever way you choose, remember the IAM policy names the bucket and key, so each environment's key needs to be in the policy of the role that uses it.

Terraform S3 backend access in CI/CD pipelines

Most of these errors appear the day a team moves Terraform from laptops to a pipeline. A pipeline role built for least privilege usually gets the S3 permissions roughly right and misses one of the details: the lock key, ListBucket on the bucket ARN, or the workspace prefix. Three habits keep pipelines out of trouble:

  • Print the identity first. Make aws sts get-caller-identity the first step of the job. It costs nothing and answers "who is Terraform" in the log of every run.
  • Keep plan and apply roles separate. A plan role with read access plus the lock file, and an apply role with write access, means a plan job can never change state.
  • Keep errored.tfstate if it appears. Configure the job to save it as an artifact whenever it exists, so a failed save never disappears with the runner.

For pipelines that sign in to AWS with OpenID Connect, as GitHub Actions and GitLab can, the role in get-caller-identity is the one in your workflow's role setting, and its trust policy decides which repositories and branches can use it. If the role can't be assumed at all, the error comes from that step, before Terraform starts.

Still stuck? Troubleshooting by symptom

Works on my laptop, AccessDenied in CI

The two run as different identities. Compare aws sts get-caller-identity in both places and give the CI role the policy above, including the lock key.

AccessDenied right after init said "Successfully configured"

That line only means the settings were saved locally. The refresh that follows is the first real S3 call, and it needs s3:GetObject on the state key.

Plan works, apply fails at the end

The identity can read but not write the state. Follow the errored.tfstate recovery before running anything else.

My workspaces disappeared

s3:ListBucket is missing on the bucket ARN. Terraform hides that error with the default env: prefix and shows only default. Add ListBucket and run terraform workspace list again.

Read-only role can't run plan

Plan takes the lock. Grant Get, Put and Delete on the .tflock key, or run terraform plan -lock=false for preview-only jobs.

Everything is granted and it still fails

Look for an explicit Deny in the bucket policy, a KMS key policy that doesn't allow the role, a service control policy, or a policy written for a different key or workspace prefix than the one in your backend block.

Terraform S3 backend access denied: frequently asked questions

What does "Error loading state: AccessDenied: Access Denied" mean in Terraform?

The AWS identity Terraform is using can't read the state file in your S3 backend bucket. Current versions word it as "Error refreshing state: Unable to access object ... HeadObject ... 403." Give that identity s3:GetObject on the state key, plus the rest of the backend policy.

What permissions does the Terraform S3 backend need?

s3:ListBucket on the bucket, s3:GetObject and s3:PutObject on the state key, and s3:GetObject, s3:PutObject and s3:DeleteObject on the .tflock lock key. Workspaces need the same on their env:/ prefix, and a kms_key_id needs kms:Encrypt, kms:Decrypt and kms:GenerateDataKey.

Does Terraform need s3:DeleteObject on the state file?

No. Terraform doesn't delete the state of the workspace it's using. The lock file does need s3:DeleteObject, because every unlock deletes it, and terraform workspace delete needs it on that workspace's state.

Why does terraform workspace list only show default?

The identity is missing s3:ListBucket on the state bucket. With the default env: prefix, Terraform treats the AccessDenied on listing as "only default exists" and shows no error. Add s3:ListBucket on the bucket ARN, without a trailing slash.

Why does Terraform say a workspace doesn't exist when it does?

Same cause: without s3:ListBucket, Terraform can't see the workspace states under env:/, so terraform workspace select fails. The state is still in the bucket; granting ListBucket makes the workspace appear again.

What is errored.tfstate in Terraform?

It's the state Terraform couldn't save to the backend after an apply, written to your working directory so nothing is lost. Keep it, fix the permission, then run terraform state push errored.tfstate before any other apply.

What does "Failed to persist state to backend" mean?

The apply changed your infrastructure but Terraform couldn't write the new state, usually because s3:PutObject is missing on the state key. The real state is in errored.tfstate; push it back after fixing the permission.

Why does my Terraform plan fail with Error acquiring the state lock?

Plan takes the lock by default, so it needs s3:PutObject, s3:GetObject and s3:DeleteObject on the .tflock key. Grant those, or use terraform plan -lock=false for read-only preview jobs. If another run holds the lock, see our state lock guide.

How do I see which AWS identity Terraform is using?

Run aws sts get-caller-identity in the same shell and environment right before terraform init. Remember that a profile or assume_role inside the backend block changes the identity for the state bucket only.

What does "No valid credential sources found" mean in Terraform?

Terraform found no AWS credentials at all: no environment variables, no usable profile, no instance role. Log in with aws sso login, set AWS_PROFILE, or attach a role to the machine running Terraform.

Why does HeadObject return Forbidden instead of AccessDenied?

A HEAD request has no response body, so S3 can't include its usual error code. The SDK reports the HTTP status instead, Forbidden. It means the same as AccessDenied: the identity lacks s3:GetObject on that object.

Does the Terraform S3 backend need DynamoDB?

Not anymore. With use_lockfile = true, locking uses an object in the same bucket. DynamoDB-based locking is deprecated; if you still use it, the table needs DescribeTable, GetItem, PutItem and DeleteItem.

What KMS permissions does the Terraform S3 backend need?

kms:Encrypt, kms:Decrypt and kms:GenerateDataKey on the key in kms_key_id. The key policy must also allow the role, either directly or by allowing your account's IAM policies.

Can a bucket policy cause Terraform AccessDenied?

Yes. An explicit Deny in the bucket policy, for example one that requires a VPC endpoint or IP range, overrides any Allow in IAM. Check the bucket policy whenever the IAM policy looks correct.

How do I use a state bucket in another AWS account?

Either allow your role in the bucket's policy as well as in your IAM policy, or create a role in the bucket's account and set assume_role with its role_arn in the backend block. The second keeps all access in one account.

What is the difference between terraform init -reconfigure and -migrate-state?

-migrate-state copies your existing state to the new backend location; -reconfigure just points Terraform at the new location without copying. Use -migrate-state when moving state, -reconfigure when the new location already holds the right state.

Can I use variables in a Terraform backend block?

No. Terraform configures the backend before it reads variables, so it reports "Variables not allowed." Use a partial configuration instead: an empty backend "s3" {} block and terraform init -backend-config=backend.hcl, or -backend-config="key=value" pairs.

What is workspace_key_prefix in the Terraform S3 backend?

It's the prefix under which non-default workspace states are stored, env: by default, giving keys like env:/staging/shop/terraform.tfstate. Your IAM policy must cover that prefix, and with a custom value a missing s3:ListBucket shows up as a clear error instead of hidden workspaces.

Does Terraform create the S3 bucket for its state?

No. The bucket must exist before terraform init; otherwise init fails with "S3 bucket ... does not exist." Create it separately, with versioning on so earlier states can be restored.

For readers preparing for an AWS certification

We have been covering AWS certifications alongside our fixes, so here is a short extra for readers studying for one. If you only came for the fix, you already have it; feel free to skip this part.

Exams that test this topic: DevOps Engineer – Professional (DOP-C02) • Security – Specialty (SCS-C03).

Here are the exam-style questions this topic tends to raise, with the reasoning you need to answer them:

1. Which permissions does the S3 backend need?

s3:ListBucket on the bucket and GetObject and PutObject on the state key, plus DeleteObject for lock files, and DynamoDB permissions if a lock table is used.

2. The state bucket uses a KMS key. What else is needed?

kms:Decrypt and kms:GenerateDataKey on that key.

3. How do you use a backend in another account?

Assume a role in that account from the backend configuration, or grant access through the bucket policy.

4. How do you keep secrets out of state files?

Avoid storing secrets as resource attributes, restrict state access, and encrypt the bucket.

5. How do you see which identity Terraform uses?

aws sts get-caller-identity with the same profile or environment.

6. How do you separate state for environments?

Use different keys or buckets per environment and account.

Exam codes and dates change often. Our AWS certification changes page keeps track of what is current.

πŸ“š ALSO READ

Where to go next, from the state lock to the KMS and cross-account errors that look exactly like this one:

📌 Bookmark this; the policy and the error-to-permission table apply to every Terraform pipeline you will ever set up.

By Friday, Jake's pipeline had a role with exactly the three statements from the policy section, plus the workspace prefix. Its first step prints the identity, and the job keeps errored.tfstate as an artifact if one ever appears. The database change from Tuesday's failed apply went back into the state with one terraform state push, and the next plan came back with "No changes." Staging reappeared the moment ListBucket was added. Ethan pinned the line below in the team chat.

📌 If you keep one line from this page

Check who Terraform is before you change any policy, then grant ListBucket on the bucket, Get and Put on the state, and Get, Put and Delete on the lock.

Missing ListBucket hides your workspaces; missing PutObject leaves the truth in errored.tfstate.

Revision note. Written October 11, 2026, for every pipeline that worked on a laptop and nowhere else. "Trust, but verify," goes the old Russian proverb; with Terraform, the verifying starts with get-caller-identity.

Related