AWS负载均衡器-更改区域(使用Terraform)

时间:2019-02-12 12:40:35

标签: amazon-web-services terraform terraform-provider-aws aws-load-balancer

我有一个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的预期行为,或者这是一个错误。

1 个答案:

答案 0 :(得分:1)

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

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