Terraform-provider-aws: Error creating S3 bucket: BucketAlreadyOwnedByYou: Your previous request to create the named bucket succeeded and you already own it.

Created on 13 Jun 2017  ·  25Comments  ·  Source: hashicorp/terraform-provider-aws

_This issue was originally opened by @fubarbaz as hashicorp/terraform#10692. It was migrated here as part of the provider split. The original body of the issue is below._


Terraform 0.7.13 generates:

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

after I imported the resource, and specified it in the main.tf Terraform file. I would like to know how I can use a bucket named "foo" which has been created manually and essentially import it (obviously, I already imported it per documentation). So, perhaps it's the import feature which doesn't work.

An alternative way to get into this state is deleting the terraform state files, importing the S3 resource after that, adding it to the configuration and running terraform apply.

bug servics3

Most helpful comment

Happening for me as well with the latest release

All 25 comments

Same here, any known workaround?

This issue is still happening in 10.0, is there any word on it?

Happening for me as well with the latest release

Still happening with 0.10.6 btw...

Any workaround on this?

My workaround (if the Bucket is not too big) is to copy the bucket content over another one and then put the bucket under TF, so manual delete and TF to recreate.

Fortunately, for now, this happened to me only on tiny buckets...

We've got the same issue, we want to share the same bucket between dev and prod environments, the bucket is defined in a module which is loaded by both dev and prod main.tf files .

I wonder if we are structuring the code in the right way, but like this is actually impossible to use TF for handling S3 buckets.

As of AWS provider version 1.1.0, there is a data source for aws_s3_bucket: https://www.terraform.io/docs/providers/aws/d/s3_bucket.html

thanks @bflad that works well

Hopefully the above mentioned aws_s3_bucket data source works well for folks -- I'm going to close this old issue to help clean up the repository, but don't hesitate to write back or create a new ticket if there are still use case troubles here.

still happening here,
when i try to just run the terraform apply second time without any changes I got this above error you have mentioned,

my tf file

resource "aws_s3_bucket" "sample_bucket_xxxx" {
bucket = "a_bucket_xxxxxxxxxxx"
acl = "private"
region = "${var.AWS_REGION}"
}
,

When I try to to destroy it:
$ terraform destroy
Do you really want to destroy?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
Destroy complete! Resources: 0 destroyed.

I have a running scenario in another folder and could not figure out what was wrong with this one

I think the issue started to appear again in Terraform v0.11.4 and disappear in Terraform v0.11.5 (fixed)
I have just installed version 11.5 and got rid off the issue.

Seeing it in v0.11.7

Seeing it in v0.11.8

Seeing in Terraform v0.11.10

Seeing in Terraform v0.11.11 too

Seeing in Terraform v0.11.13 too aws provider version 2.4.0

Can anyone clarify the solution?

Why is this issue closed? Can reproduce with terraform v0.11.13.

@kinowarrior see @bflad answer, https://www.terraform.io/docs/providers/aws/d/s3_bucket.html that would do it

I don't see how using a "data" block in place of a "resource" block solves the issue.
The problem here seems to be that the "resource" block doesn't recognise the bucket already exists and tries to create it again, triggering the error.

@aterreno - thanks. I actually fixed my own issue, but as a total noob to terraform shall try to share what I know.

So I was getting weird situation whereby if I didn't create the s3 bucket explicitly, i got an error about the s3 bucket does not exist yet. And then when i manually added the bucket, i got an error as described here (about already owned bla bla bla).

It turns out I think I misconfigured my remote backend. I inadvertently gave it the same s3 bucket as the s3 resource I was creating with terraform...

Simplified extract:

resource "aws_s3_bucket" "main" {
  bucket = "${var.s3_bucket_prefix}-${var.environment}-${var.s3_region}"
  acl    = "private"
  tags   = "${local.s3_tags}"
  region = "${var.s3_region}"
 ....

terraform {
  backend "s3" {
    bucket = "${var.s3_bucket_prefix}-${var.environment}-${var.s3_region}"


    key = "test/backbone"

    encrypt = "true"

    ....

By commenting out the terraform backend declaration (effectively getting rid of the remote state management), doing a rm -rf .terraform, and then the usual terraform init, terraform plan, terraform apply sequence it sorted itself out.

Long story short, user error in my case I think. Re-introduced the backend configuration, giving the bucket name a unique -statemgmt suffix, and everything worked as expected.

We have this issue when terraform is trying to create an S3 bucket that already exists.
Using data instead of resource - as proposed by @kinowarrior - is the way we solve it.

@kinowarrior see @bflad answer, https://www.terraform.io/docs/providers/aws/d/s3_bucket.html that would do it

Hi folks 👋

If you are encountering a BucketAlreadyOwnedByYou error with the aws_s3_bucket resource, this means that the S3 Bucket was previously created by some other means (potentially another part of your Terraform configuration) and that you will either need to import the existing S3 Bucket configuration into Terraform or use the aws_s3_bucket data source instead of the aws_s3_bucket resource. Which fix is correct for your environment will be specific to what you are trying to accomplish in your Terraform configuration.

Terraform expects only one source of truth for managing a single piece of infrastructure. While you can technically import a Terraform resource in multiple places (at the moment), their configurations will perpetually conflict with each other if there are differences.

If you are attempting to read-only reference the existing S3 Bucket, replace the resource configuration with a data source configuration. e.g.

data "aws_s3_bucket" "mybucket" {
  bucket = "existing-bucket-name"
}

output "my_bucket_name" {
  value = "${data.aws_s3_bucket.mybucket.bucket}"
}

If you are instead attempting to manage the existing S3 Bucket, the following command can be used to import this resource into Terraform (as documented in the Import section of the aws_s3_bucket resource documentation):

$ terraform import aws_s3_bucket.mybucket existing-bucket-name

We have a current proposal out to start catching errors like these and provide better guidance on what to do in these situations: #9223

Hopefully this helps clear up any confusion. If you're looking for general assistance, please note that we use GitHub issues in this repository for tracking bugs and enhancements with the Terraform AWS Provider codebase rather than for questions. While we may be able to help with certain simple problems here it's generally better to use one of the community forums where there are far more people ready to help, whereas the GitHub issues here are generally monitored only by a few maintainers and dedicated community members interested in code development of the Terraform AWS Provider itself.

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings