How can I automatically delete and create a Lambda function in a different region when updating the provider in a Terraform module?

26 views Asked by At

I'm managing AWS resources with Terraform, including a scenario where Lambda functions and CloudWatch alarms are deployed. My setup is defined with a default AWS provider for us-west-2 and aliases for us-east-1 and us-east-2. Deploying a Lambda module in the default region works as intended. However, when I attempt to redeploy the same Lambda module to a different region by specifying a provider alias, Terraform treats it as a new resource creation instead of an update or move.

Here's a simplified version of my provider configuration in root/providers.tf:

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

provider "aws" {
  alias  = "useast1"
  region = "us-east-1"
}

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

And my module invocation looks like this:

module "mylambdamodulename" {
  source    = "./modules/lambda"
}

For example, changing to

module "mylambdamodulename" {
  source    = "./modules/lambda"
  providers = { aws = aws.useast2 }
}

Creates a new one in useast2 but doesn't destroy the old one in the default region.

Changing the module's name based on the region (e.g., appending _useast2 to the module name) forces Terraform to recognize it as a distinct resource, but this approach is prone to human error and could lead to inconsistencies.

Is there a Terraform-native way to ensure that changing the target deployment region of a module will lead Terraform to treat it as the same resource undergoing an update or relocation, rather than creating a new instance? I'm looking for a solution that would minimize manual naming conventions and potential discrepancies in our infrastructure management process.

This is configured to run a pipeline in github, so manual intervention each time is out of the picture.

0

There are 0 answers