Rafael Marques
Rafael Marques

Reputation: 1445

AWS Load Balancer - change region (with Terraform)

I have a Terraform module that provisions an Auto-Scaling group and all the necessary infrastructure to support it on AWS. Usually, Terraform is quite good at detecting changes in the infrastructure code. However, today I noticed that, if Terraform is managing a load-balancer, a change in the region will lead to an error.

I constructed a minimal example to replicate the error (this example requires a valid AWS profile)

# =========================================================================================
#                 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}"
}

To replicate the error:

The error is as follows:

Error: Error refreshing state: 1 error(s) occurred:
* module.asg-local.aws_lb.alb: 1 error(s) occurred:
* module.asg-local.aws_lb.alb: aws_lb.alb: Error retrieving ALB:
ValidationError:
'arn:aws:elasticloadbalancing:us-east-1:199344973012:loadbalancer/app/rafa-lizzie-alb/ccbf16e255c2f904' is not a valid load balancer ARN status code: 400, request id: 8b28f0d8-2ec2-11e9-896a-4ffb7ae94bb8

I know that it is not very normal to change regions, but in any case, it might happen, right? I would also like to know if this is the expected behaviour from Terraform, or if this is a bug.

Upvotes: 0

Views: 1395

Answers (1)

user385157
user385157

Reputation: 11

It is expected behavior. What's happening is that when you run plan/apply, all the resources will try to "refresh" its state. Since you've changed provider region, it is not able to retrieve the resource (wrong region) to "refresh" the state.

You can bypass this behavior by basically passing "-refresh=false" to plan and apply runs.

Upvotes: 1

Related Questions