I am attempting to pick a random region when deploying my AWS resources.
From the Terraform console I’ve been able to confirm my syntax related to the random_shuffle
resource and element
is correct and will return a single string but Terraform continues to say Error: Invalid AWS Region:
Is the problem coming from me wanting to use a value known only after the apply? If so is there another good way of achieving my intended result? I am deploying Lightsail instances so using the data resource of available zone would not suffice here due to it being tied specifically to EC2.
locals {
region = element(random_shuffle.regions.result, 0)
}
resource "random_shuffle" "regions" {
input = ["us-east-1", "us-east-2", "us-west-2", "ca-central-1",
"eu-west-1", "eu-west-2", "eu-west-3", "eu-central-1", "eu-north-1"]
result_count = 1
}
provider "aws" {
region = local.region
access_key = var.AWS_ACCESS_KEY
secret_key = var.AWS_SECRET_KEY
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.38.0"
}
random = {
source = "hashicorp/random"
version = "3.6.0"
}
}
}