AnRedd
AnRedd

Reputation: 186

terraform module argument with null value

I have created route_table module in terraform with internet_gateway_id as argument, but i want to create another route table for private subnet NAT_Instance, but route_table module does not have instance_id argument.
I want to use same route_table module which can have internet_gateway_id if it is for internet_gateway or can have instance_id if it is for NAT_Instance.
Can i give value "null" for any argument, if it is not required? means, i want provide 2 arguments, one is "gateway_id" and another one is for "instance_id" and want to send the value as "null" if either one is required receptively. I have not executed the below code , but planning write a code like below. Could you please suggest me the right way to handle this scenario?

resource "aws_route_table" "rtable" {
    vpc_id = "${var.vpc_id}"

    route {
    cidr_block = "${var.igw_cidr_block}"
    gateway_id = "${var.igw_id}"
    instance_id = null
    }

    tags = {
       Name = "${var.name}"
    }
}

Upvotes: 1

Views: 1263

Answers (2)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16805

Can i give value "null" for any argument, if it is not required? means, i want provide 2 arguments, one is "gateway_id" and another one is for "instance_id" and want to send the value as "null" if either one is required receptively.

You can, but you don't have to. The argument is optional, the value of it it will be null by default. Just remove the argument, and don't associate anything to it.

Upvotes: 1

AnRedd
AnRedd

Reputation: 186

yes, the below code worked to handle the scenario like creating one route table module which can be used to attach either internet_gateway_id or instance_id by providing either one value as null respectively.

resource "aws_route_table" "rtable" {
    vpc_id = "${var.vpc_id}"

    route {
    cidr_block = "${var.igw_cidr_block}"
    gateway_id = "${var.igw_id}"
    instance_id = "${var.instance_id}"
    }

    tags = {
       Name = "${var.name}"
    }
}

# variables declaration file vars.tf
variable "vpc_id" {}
variable "igw_cidr_block" {}
variable "igw_id" {
   default = null
}
variable "instance_id" {
   default = null
}
variable "name" {}

Upvotes: 1

Related Questions