Skip to content
Latchkey

Terraform AWS provider "BucketAlreadyOwnedByYou" creating an S3 bucket in CI

S3 returned BucketAlreadyOwnedByYou: the bucket name already exists and belongs to your account, but Terraform tried to create it because state has no record of it. Bucket names are globally unique, so you cannot just rename freely.

What this error means

terraform apply fails with "BucketAlreadyOwnedByYou: Your previous request to create the named bucket succeeded and you already own it." The bucket exists; the state does not know it.

Terraform
Error: creating S3 Bucket (my-app-artifacts): operation error S3:
CreateBucket, https response error StatusCode: 409, api error
BucketAlreadyOwnedByYou: Your previous request to create the named bucket
succeeded and you already own it.

Common causes

The bucket exists but state was lost or reset

A backend change, a fresh workspace, or a manual bucket creation left the bucket present while state has no entry for it.

A prior apply created it then failed before saving state

CreateBucket succeeded but the run errored afterward, so the bucket exists yet the state write never recorded it.

How to fix it

Import the existing bucket

  1. Confirm the bucket should be managed by this configuration.
  2. Import it into the resource address.
  3. Re-run plan to confirm Terraform now manages it without recreating.
Terminal
terraform import aws_s3_bucket.artifacts my-app-artifacts

Use a globally unique, derived name

If the bucket should be new, derive a unique name so it does not collide with one you already own.

main.tf
bucket = "my-app-artifacts-${data.aws_caller_identity.current.account_id}"

How to prevent it

  • Import pre-existing buckets before the first apply.
  • Derive bucket names so they are globally unique.
  • Keep one backend so state reflects buckets you already own.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →