mohit
mohit

Reputation: 2469

Terraform: Error while importing s3 bucket with difference in storage class region and provider region

I am trying to import a bucket which is in us-east-1 region to a parent module which is in ap-south-1 region. I don't want to migrate the bucket to ap-south-1 region as of now but I want its state to be in the parent module which has provider region ap-south-1. I am getting below error while doing it.

Command:

terraform import aws_s3_bucket.cdn staging.domain.com

Error:

terraform import aws_s3_bucket.cdn staging.domain.com                    [15:02:27]
aws_s3_bucket.cdn: Importing from ID "staging.domain.com"...

Error: aws_s3_bucket.cdn (import id: staging.domain.com): import aws_s3_bucket.cdn (id: staging.domain.com): Error importing AWS S3 bucket policy: BucketRegionError: incorrect region, the bucket is not in 'ap-south-1' region at endpoint ''
    status code: 301, request id: , host id:

Is it even possible as of now?

Upvotes: 0

Views: 3123

Answers (2)

turiyag
turiyag

Reputation: 2897

This was a bit of a slog for me, the final answer was "use the right region". So I had a bucket in us-west-2, but my other stuff was in us-east-1. I needed to use:


provider "aws" {
  alias  = "oregon"
  region = "us-west-2"
}


resource "aws_s3_bucket" "tf_logs" {
  provider = aws.oregon
  bucket = "my-bucket-name"
}

And then it worked.


The way I found this out was by running terraform with more logging:

TF_LOG=DEBUG terraform import aws_s3_bucket.tf_logs my-bucket-name

Where it then gave me a cryptic "Bad Request", which I put into Postman, which gave me this helpful message:

HTTP 400 Bad Request
<Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'us-west-2'</Message>

Upvotes: 0

RyanKim
RyanKim

Reputation: 1805

I think that region of the bucket(us-east-1) is not matched with region of parent module(ap-south-1).

So I suggest to use multiple provider. https://www.terraform.io/docs/configuration/providers.html#alias-multiple-provider-instances


1. create a aws provider for us-east-1 region

// default aws provider for parent module
provider "aws" {
  ...
  region = "ap-south-1"
  ...
}

// add for us-east-1 
provider "aws" {
  ...
  region  = "us-east-1"
  alias   = "us-east-1"
  ...
}
  1. set new aws provider to aws_s3_bucket.cdn
resource "aws_s3_bucket" "cdn" {
  ...
  provider    = "aws.us-east-1"
  ...
}
  1. import with provider option https://www.terraform.io/docs/commands/import.html#provider-provider
terraform import -provider=aws.us-east-1 aws_s3_bucket.cdn staging.domain.com 

Upvotes: 3

Related Questions