skip to Main Content

variable.tf

variable  "private_subnets" {
    type    = list
    default = ["subnet-abc1,subnet-abc2"]
}

main.tf

resource "aws_db_subnet_group" "rds_subnet_group" {
    name       = var.cluster_name
    subnet_ids = "${var.private_subnets}"

    tags = {
        Name        = var.cluster_name,
        environment = var.environment
    }
}

This the present code i want use varable.tf for subnet this is way i want achive like this subnet-abc1,subnet-abc2

2

Answers


  1. Chosen as BEST ANSWER

    i have sort issue with split this is the answer

    variable.tf
    
    variable  "private_subnets" {
        default = "subnet-abc1,subnet-abc2"
    }
    main.tf
    resource "aws_db_subnet_group" "rds_subnet_group" {
        name       = var.cluster_name
        subnet_ids = "${split(",", var.private_subnets)}"
    
        tags = {
            Name        = var.cluster_name,
            environment = var.environment
        }
    

  2. Considering that your variable has a wrongly defined default value, the first thing to fix is that:

    variable  "private_subnets" {
        type    = list(string)
        default = ["subnet-abc1", "subnet-abc2"]
    }
    

    The way you are currently defining the default value, i.e, ["subnet-abc1,subnet-abc2"], is a list, however, it is a list with one element. Each element in a list of strings needs to start and end with double quotes, i.e. "some value". You can read more about lists in [1].

    Then, you would just need to fix the code in the main.tf to look like this:

    resource "aws_db_subnet_group" "rds_subnet_group" {
        name       = var.cluster_name
        subnet_ids = var.private_subnets
    
        tags = {
            Name        = var.cluster_name,
            environment = var.environment
        }
    }
    

    The syntax for subnets in the main.tf file is the old terraform syntax, so this will work without double quotes and ${}.


    [1] https://developer.hashicorp.com/terraform/language/expressions/types#list

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search