Home > Software design >  Is setting an attribute to null the same as not setting it in Terraform configuration?
Is setting an attribute to null the same as not setting it in Terraform configuration?

Time:04-13

I was looking at the code of terraform VPC module and found

resource "aws_subnet" "public" {
   vpc_id     = aws_vpc_ipv4_cidr_block_association.secondary_cidr.vpc_id
   cidr_block = "172.2.0.0/24"
   availability_zone = null
}

availability_zone attribute is optional based on the docs of AWS provider.

Is setting availability_zone to null is the same as not listing it all? For example, this is the same way to write the following configuration:

resource "aws_subnet" "in_secondary_cidr" {
  vpc_id     = aws_vpc_ipv4_cidr_block_association.secondary_cidr.vpc_id
  cidr_block = "172.2.0.0/24"
}

I've never seen it before. Seems like it's a popular way to declare an optional variable in a module:

variable "private_subnet_assign_ipv6_address_on_creation" {
  description = "Assign IPv6 address on private subnet, must be disabled to change IPv6 CIDRs. This is the IPv6 equivalent of map_public_ip_on_launch"
  type        = bool
  default     = null
}

CodePudding user response:

Yes, if you use null is the same as not listing it all. From docs:

a value that represents absence or omission. If you set an argument of a resource to null, Terraform behaves as though you had completely omitted it — it will use the argument's default value if it has one, or raise an error if the argument is mandatory. null is most useful in conditional expressions, so you can dynamically omit an argument if a condition isn't met.

  • Related