Terraform:如何根据条件在模块中设置变量?

时间:2020-04-03 19:28:46

标签: terraform terraform-provider-aws

我想传递一个变量,该变量将允许我指定一个AWS实例的VPC和子网设置列表。有固定的VPC和子网设置很有意义,所以我只想允许用户使用单个变量(即使用A或B)选择一个。

例如,假设我有两个可用的VPC,它们在模块variables.tf的{​​{1}}文件中指定:

my_instance

variable "a_vpc_cidr_block" { default = "105.191.44.0/22" } variable "a_vpc_id" { default = "id_a"} variable "a_vpc_name" { default = "vpc_a" } variable "a_subnet_availability_zone" { default = "us-east-1a" } variable "a_subnet_cidr_block" { default = "105.191.25.0/25" } variable "a_subnet_name" { default = "instance_A" } variable "b_vpc_cidr_block" { default = "105.191.45.0/22" } variable "b_vpc_id" { default = "id_b"} variable "b_vpc_name" { default = "vpc_b" } variable "b_subnet_availability_zone" { default = "us-east-1a" } variable "b_subnet_cidr_block" { default = "105.191.35.0/25" } variable "b_subnet_name" { default = "instance_B" } 模块将采用环境将指定的单个输入变量,其值为'A'或'B'(是否可以将变量的选项限制为值列表例如my_instance?),并且将在options=['A', 'B']中被调用,以用于具有单个实例的Terraform配置:

terraform.tf

我现在想在模块的主文件(module "my_instance" { source = "../../modules/my_instance" option = "A" } )中实现一些逻辑,在该逻辑中,从modules/my_instance/my_instance.tf中的VPC和子网设置的两个集合中决定使用哪个。我想要这样的东西(伪代码):

modules/my_instance/variables.tf

这是某种使用计数的问题吗?

if var.option == 'A'
    vpc_cidr_block           = var.a_vpc_cidr_block
    vpc_id                   = var.a_vpc_id
    vpc_name                 = var.a_vpc_name
    subnet_availability_zone = var.a_subnet_availability_zone
    subnet_cidr_block        = var.a_subnet_cidr_block
    subnet_name              = var.a_subnet_name
else if var.option == 'B'
    vpc_cidr_block           = var.b_vpc_cidr_block
    vpc_id                   = var.b_vpc_id
    vpc_name                 = var.b_vpc_name
    subnet_availability_zone = var.b_subnet_availability_zone
    subnet_cidr_block        = var.b_subnet_cidr_block
    subnet_name              = var.b_subnet_name
else
    raise an error

# get a data resource identified by the VPC variables
data "aws_vpc" "instance_vpc" {
  cidr_block = var.vpc_cidr_block

  tags = {
    Name = var.vpc_name
  }
}

# get a data resource identified by the VPC variables
data "aws_subnet" "instance_subnet" {
  vpc_id             = var.vpc_id
  cidr_block         = var.subnet_cidr_block
  availability_zone  = var.subnet_availability_zone

  tags = {
    Name = var.subnet_name
  }
}

# create an AWS key pair resource
resource "aws_key_pair" "instance_aws_key_pair" {
  key_name    = "component_key_${terraform.workspace}"
  public_key  = file("~/.ssh/terraform.pub")
}

# create the AWS EC2 instance
resource "aws_instance" "my_aws_instance" {
  key_name        = aws_key_pair.instance_aws_key_pair.key_name
  ami             = "ami-b12345"
  instance_type   = "t2.micro"
  subnet_id       = data.aws_subnet.instance_subnet.id

  connection {
    type        = "ssh"
    user        = "terraform"
    private_key = file("~/.ssh/terraform")
    host        = self.public_ip
  }

  tags = {
    "Name"      : "my_instance_name"
    "Terraform" : "true"
  }
}

有没有办法做到这一点,还是有更好的办法?我是Terraform的新手,所以我可能会缺少明显的东西。

1 个答案:

答案 0 :(得分:3)

您在这里有几个问题。

首先,您应该能够使用较新的实验性custom validation rules来断言某个值在特定的值列表中。

第二,为了确定要使用的变量集,我建议使用本地值好的旧map

例如,

locals {
  vpc_info = {
    "A" = {
      vpc_cidr_block           = var.a_vpc_cidr_block
      vpc_id                   = var.a_vpc_id
      vpc_name                 = var.a_vpc_name
      subnet_availability_zone = var.a_subnet_availability_zone
      subnet_cidr_block        = var.a_subnet_cidr_block
      subnet_name              = var.a_subnet_name
    }
    "B" = {
      vpc_cidr_block           = var.b_vpc_cidr_block
      vpc_id                   = var.b_vpc_id
      vpc_name                 = var.b_vpc_name
      subnet_availability_zone = var.b_subnet_availability_zone
      subnet_cidr_block        = var.b_subnet_cidr_block
      subnet_name              = var.b_subnet_name
    }
  }
}

然后,您应该能够在如下所示的selected选项中引用特定字段

  local.vpc_info[var.option].vpc_name

让我知道这是否触及了您所有的问题。