terraform workspaces error Incorrect attribute value type

220 Views Asked by At

got an issue with my terraform script

I am using terrafork spaces to enable seperate environments within the same aws account

I configured the following

locals {
  env = {
    marcus = {
      name                    = "marcus"
      vpc_cidr                = ["10.1.0.0/16"]
      subnet1_cidr            = ["10.1.1.0/24"]
      subnet2_cidr            = ["10.1.2.0/24"]
      vpc_name                = "vpc-marcus-csw"
      subnet1_name            = "Subnet1-Marcus"
      subnet2_name            = "Subnet2-Marcus"
      publisher_security_group_name = "publisher-SG-marcus"
      webserver_security_group_name = "webserver-SG-marcus"
      student_security_group_name   = "student-SG-marcus"
      guacamole_security_group_name = "guacamole-SG-marcus"
      master_security_group_name    = "master-SG-marcus"
      route_table1_name             = "route-table-marcus"
      igw_name                      = "igw-marcus"
    }
  }
  workspace = "${local.env[terraform.workspace]}"
}

and reference these within my vpc config with

resource "aws_subnet" "subnet2" {
    vpc_id = aws_vpc.vpc.id
    cidr_block = "${local.workspace["subnet2_cidr"]}"
    map_public_ip_on_launch = var.subnet2_map_public_ip_on_launch
    
    tags = {
        Name = "${local.workspace["subnet2_name"]}"
    }
}

when I do that I am getting

Error: Incorrect attribute value type
│
│   on vpc.tf line 23, in resource "aws_subnet" "subnet2":
│   23:     cidr_block = "${local.workspace["subnet2_cidr"]}"
│     ├────────────────
│     │ local.workspace["subnet2_cidr"] is tuple with 1 element
│
│ Inappropriate value for attribute "cidr_block": string required.
╵

If I remove the brackets from the env variables I am getting

│ Error: Incorrect attribute value type
│
│   on security_group.tf line 63, in resource "aws_security_group" "webserver_security_group":
│   63:     cidr_blocks      = "${local.workspace["subnet2_cidr"]}"
│     ├────────────────
│     │ local.workspace["subnet2_cidr"] is "10.0.2.0/24"
│
│ Inappropriate value for attribute "cidr_blocks": list of string required.

It seems its expecting a list of strings for CIDR, I only want to pass on one CIDR though. If I do this with a variable like var.cidr it works.

Anybody can help?

1

There are 1 best solutions below

0
On

I managed to get it working with:

cidr_block = flatten("${local.workspace["subnet2_cidr"]}")[0]