Terraform Tutorial — Resource Already Exists

1 minute read

A common error: Terraform tries to create a resource that already exists in the cloud.


The Error

Error: error creating S3 Bucket: BucketAlreadyOwnedByYou:
Your previous request to create the named bucket succeeded and you already own it.

  on main.tf line 1, in resource "aws_s3_bucket" "main":
   1: resource "aws_s3_bucket" "main" {

Cause: The resource exists in AWS but not in Terraform state. This happens when:

  • Someone created the resource manually
  • A previous apply crashed after creating but before updating state
  • You’re working with a new Terraform config against existing infrastructure

Fix: Import the Resource

# 1. Write the resource block
resource "aws_s3_bucket" "main" {
  bucket = "my-existing-bucket"
}

# 2. Import into state
$ terraform import aws_s3_bucket.main my-existing-bucket

# 3. Plan to see what's different
$ terraform plan

Fix: Use import Block (Terraform 1.5+)

import {
  to = aws_s3_bucket.main
  id = "my-existing-bucket"
}

resource "aws_s3_bucket" "main" {
  bucket = "my-existing-bucket"
}

Then run:

$ terraform plan -generate-config-out=generated.tf
$ terraform apply

Fix: Remove and Recreate

If you don’t need the existing resource:

# Delete the existing resource manually (AWS console or CLI)
$ aws s3 rb s3://my-existing-bucket

# Then let Terraform create it
$ terraform apply

Common Resources That Conflict

Resource Unique Identifier
S3 Bucket Bucket name (globally unique)
IAM Role Role name
Security Group Name + VPC ID
Route53 Zone Domain name
DynamoDB Table Table name

What’s Next?

Let’s learn about debug logging — the ultimate troubleshooting tool.


← State Drift and Conflicts Debug Logging →