简体   繁体   中英

Terraform init fails for remote backend S3 when creating the state bucket

I was trying to create a remote backend for my S3 bucket.

provider "aws" {
  version = "1.36.0"
  profile = "tasdik"
  region  = "ap-south-1"
}

terraform {
  backend "s3" {
    bucket = "ops-bucket"
    key    = "aws/ap-south-1/homelab/s3/terraform.tfstate"
    region = "ap-south-1"
  }
}

resource "aws_s3_bucket" "ops-bucket" {
  bucket = "ops-bucket"
  acl    = "private"

  versioning {
    enabled = true
  }

  lifecycle {
    prevent_destroy = true
  }

  tags {
    Name       = "ops-bucket"
    Environmet = "devel"
  }
}

I haven't applied anything yet, the bucket is not present as of now. So, terraform asks me to do an init . But when I try to do so, I get a

$ terraform init       

Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.
Error loading state: BucketRegionError: incorrect region, the bucket is not in 'ap-south-1' region
    status code: 301, request id: , host id:

Terraform will initialise any state configuration before any other actions such as a plan or apply. Thus you can't have the creation of the S3 bucket for your state to be stored in be defined at the same time as you defining the state backend.

Terraform also won't create an S3 bucket for you to put your state in, you must create this ahead of time.

You can either do this outside of Terraform such as with the AWS CLI:

aws s3api create-bucket --bucket "${BUCKET_NAME}" --region "${BUCKET_REGION}" \
          --create-bucket-configuration LocationConstraint="${BUCKET_REGION}"

or you could create it via Terraform as you are trying to do so but use local state for creating the bucket on the first apply and then add the state configuration and re-init to get Terraform to migrate the state to your new S3 bucket.

As for the error message, S3 bucket names are globally unique across all regions and all AWS accounts. The error message is telling you that it ran the GetBucketLocation call but couldn't find a bucket in ap-south-1 . When creating your buckets I recommend making sure they are likely to be unique by doing something such as concatenating the account ID and possibly the region name into the bucket name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM