Terraform Error refreshing state: BucketRegionError: incorrect region

Terraform Error refreshing state: BucketRegionError: incorrect region

我有用于创建 AWS 资源的 terraform 文件 main.tf

provider "aws" {
    region = "us-east-2"
}

resource "aws_instance" "example" {

    ami = "ami-0c55b159cbfafe1f0"
    instance_type = "t2.micro"
    vpc_security_group_ids = [
        aws_security_group.instance.id]

    user_data = <<-EOF
              #!/bin/bash
              echo "Hello, World" > index.html
              nohup busybox httpd -f -p "${var.server_port}" &
              EOF

    tags = {
        Name = "terraform-example"
    }
}

resource "aws_security_group" "instance" {

    name = "terraform-example-instance"

    ingress {

        from_port = var.server_port
        to_port = var.server_port
        protocol = "tcp"
        cidr_blocks = [
            "0.0.0.0/0"]
    }
}

resource "aws_security_group" "elb" {
    name = "terraform-example-elb"
    # Allow all outbound
    egress {
        from_port = 0
        to_port = 0
        protocol = "-1"
        cidr_blocks = [
            "0.0.0.0/0"]
    }
    # Inbound HTTP from anywhere
    ingress {
        from_port = 80
        to_port = 80
        protocol = "tcp"
        cidr_blocks = [
            "0.0.0.0/0"]
    }
}

variable "server_port" {

    description = "The port the server will use for HTTP requests"
    type = number
    default = 8080
}


variable "elb_port" {

    description = "The port the server will use for HTTP requests"

    type = number
    default = 80
}


resource "aws_launch_configuration" "example" {

    image_id = "ami-0c55b159cbfafe1f0"
    instance_type = "t2.micro"
    security_groups = [
        aws_security_group.instance.id]

    user_data = <<-EOF
              #!/bin/bash
              echo "Hello, World" > index.html
              nohup busybox httpd -f -p "${var.server_port}" &
              EOF
    lifecycle {
        create_before_destroy = true
    }
}


resource "aws_elb" "example" {

    name = "terraform-asg-example"
    security_groups = [
        aws_security_group.elb.id]

    availability_zones = data.aws_availability_zones.all.names

    health_check {
        target = "HTTP:${var.server_port}/"
        interval = 30
        timeout = 3
        healthy_threshold = 2
        unhealthy_threshold = 2
    }

    # This adds a listener for incoming HTTP requests.
    listener {
        lb_port = var.elb_port
        lb_protocol = "http"
        instance_port = var.server_port
        instance_protocol = "http"
    }
}


resource "aws_autoscaling_group" "example" {

    launch_configuration = aws_launch_configuration.example.id
    availability_zones = data.aws_availability_zones.all.names

    min_size = 2
    max_size = 10

    load_balancers = [
        aws_elb.example.name]
    health_check_type = "ELB"

    tag {
        key = "Name"
        value = "terraform-asg-example"
        propagate_at_launch = true
    }
}

data "aws_availability_zones" "all" {}


output "public_ip" {

    value = aws_instance.example.public_ip
    description = "The public IP of the web server"
}

我成功创建了资源,然后销毁了它们。现在,我想为该项目创建一个 AWS S3 远程后端,并将额外资源附加到同一文件中 -

resource "aws_s3_bucket" "terraform_state" {

    bucket = "terraform-up-and-running-state12345"
    # Enable versioning so we can see the full revision history of our
    # state files
    versioning {
        enabled = true
    }
    # Enable server-side encryption by default
    server_side_encryption_configuration {
        rule {
            apply_server_side_encryption_by_default {
                sse_algorithm = "AES256"
            }
        }
    }
}


resource "aws_dynamodb_table" "terraform_locks" {

    name = "terraform-up-and-running-locks"
    billing_mode = "PAY_PER_REQUEST"
    hash_key = "LockID"

    attribute {
        name = "LockID"
        type = "S"
    }
}



output "s3_bucket_arn" {

    value = aws_s3_bucket.terraform_state.arn
    description = "The ARN of the S3 bucket"
}

output "dynamodb_table_name" {

    value = aws_dynamodb_table.terraform_locks.name
    description = "The name of the DynamoDB table"
}

然后,我创建了一个名为 backend.tf 的新文件并在其中添加代码:

terraform {

    
    backend "s3" {

        # Replace this with your bucket name!
        bucket = "terraform-up-and-running-state12345"
        key = "global/s3/terraform.tfstate"
        region = "us-east-2"


        # Replace this with your DynamoDB table name!
        dynamodb_table = "terraform-up-and-running-locks"
        encrypt = true
    }
}

当我 运行 $ terraform init 时,出现以下错误:

Initializing the backend...
Backend configuration changed!

Terraform has detected that the configuration specified for the backend
has changed. Terraform will now check for existing state in the backends.


╷
│ Error: Error loading state:
│     BucketRegionError: incorrect region, the bucket is not in 'us-east-2' region at endpoint ''
│   status code: 301, request id: , host id: 
│ 
│ Terraform failed to load the default state from the "s3" backend.
│ State migration cannot occur unless the state can be loaded. Backend
│ modification and state migration has been aborted. The state in both the
│ source and the destination remain unmodified. Please resolve the
│ above error and try again.

我从终端创建了 S3 存储桶:

$ aws s3api create-bucket --bucket terraform-up-and-running-state12345 --region us-east-2 --create-bucket-configuration LocationConstraint=us-east-2

然后,我再次尝试,再次收到同样的错误。但是,桶已经在那里:

我也不能 运行 destroy 命令:

$ terraform destroy               

Acquiring state lock. This may take a few moments...
╷
│ Error: Error acquiring the state lock
│ 
│ Error message: 2 errors occurred:
│   * ResourceNotFoundException: Requested resource not found
│   * ResourceNotFoundException: Requested resource not found
│ 
│ 
│ 
│ Terraform acquires a state lock to protect the state from being written
│ by multiple users at the same time. Please resolve the issue above and try
│ again. For most commands, you can disable locking with the "-lock=false"
│ flag, but this is not recommended.

有人可以向我解释为什么会这样以及如何解决吗?

删除 .terraform 文件夹并尝试 terraform init 再次

错误是因为没有创建与之同步的 S3 存储桶。

  • 在 .terraform/terraform.tfstate
  • 中删除 s3 的 json 对象
  • 删除生成远程后端的对象运行
  • 地形初始化