0

我有一个 Terraform 模块,它预置了一个 Auto-Scaling 组和所有必要的基础设施以在 AWS 上支持它。通常,Terraform 非常擅长检测基础设施代码的变化。但是,今天我注意到,如果 Terraform 正在管理负载均衡器,则区域的更改将导致错误。

我构建了一个最小示例来复制错误(此示例需要有效的 AWS 配置文件)

# =========================================================================================
#                 PROVIDER

provider "aws" {
  region  = "${var.aws-region}"
  profile = "${var.aws-profile}"
}

# =========================================================================================
#                 VARIABLES

variable "aws-region" {
  description = "The AWS region"
  type        = "string"
  default = "eu-west-3"
}

variable "aws-profile" {
  description = "The name of the AWS shared credentials account."
  type        = "string"
}

# =========================================================================================
#                 LOAD BALANCER

resource "aws_lb" "alb" {
  name                       = "load-balancer"
  internal                   = false
  load_balancer_type         = "application"
  enable_deletion_protection = false
  subnets                    = ["${aws_subnet.subnet-1.id}", "${aws_subnet.subnet-2.id}"]

}

# =========================================================================================
#                 NETWORKING

resource "aws_vpc" "vpc" {
  cidr_block           = "10.0.0.0/16"
}

resource "aws_subnet" "subnet-1" {
  vpc_id            = "${aws_vpc.vpc.id}"
  cidr_block        = "10.0.0.0/24"
  availability_zone = "${var.aws-region}a"
}

resource "aws_subnet" "subnet-2" {
  vpc_id            = "${aws_vpc.vpc.id}"
  cidr_block        = "10.0.1.0/24"
  availability_zone = "${var.aws-region}b"
}


resource "aws_internet_gateway" "ig" {
  vpc_id = "${aws_vpc.vpc.id}"
}

要复制错误:

  • 1) 运行terraform init; terraform apply
  • 2)改变地区
  • 3) 重复步骤 1) 会导致错误

错误如下:

错误:刷新状态错误:发生 1 个错误:
* module.asg-local.aws_lb.alb:发生 1 个错误:
* module.asg-local.aws_lb.alb:aws_lb.alb:检索 ALB 时出错:
ValidationError:
“arn:aws:elasticloadbalancing:us-east-1:199344973012:loadbalancer/app/rafa-lizzie-alb/ccbf16e255c2f904”不是有效的负载均衡器 ARN 状态代码:400,请求 ID:8b28f0d8-2ec2-11e9- 896a-4ffb7ae94bb8

我知道改变地区不是很正常,但无论如何,它可能会发生,对吧?我还想知道这是否是 Terraform 的预期行为,或者这是否是一个错误。

4

1 回答 1

1

这是预期的行为。发生的情况是,当您运行计划/应用时,所有资源都将尝试“刷新”其状态。由于您更改了提供者区域,因此无法检索资源(错误区域)以“刷新”状态。

您可以通过基本上传递“-refresh=false”来计划和应用运行来绕过此行为。

于 2021-06-24T19:42:10.060 回答