我有一个Terraform模块,它提供了一个自动缩放组和所有必要的基础设施,以在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个错误:
*模块.asg-local.aws_lb.alb:发生1个错误不是有效的负载平衡器ARN状态代码:400,请求id:8b28f0d8-2ec2-11e9-896a-4ffb7ae94bb8
我知道改变区域不是很正常,但无论如何,这都可能发生,对吧?我也想知道这是否是Terraform的预期行为,或者这是否是一个bug。
这是预期行为。实际情况是,当您运行计划/应用程序时,所有资源都会尝试";刷新";其状态。由于您已经更改了提供者区域,因此无法将资源(错误的区域)检索为";刷新";州。
你可以通过基本上通过"-refresh=false";计划和应用跑步。