Accessing values from a `tuple` for use in terraform as a list

I’m sure I’m being stupid here and that this is a 101 question but…

I have a VPC that has been set up and there is a bunch of information I need to access via the terraform state file. In this case I need to return a list of subnet ids. My problem is that I can’t seem to access them from the tuple.

e.g.
Setting up a Redis ElasticCache service is AWS using the module-cache.git//modules/redis requires subnet_ids to be set as a list(string).

At the moment this looks like this…

module "redis" {
...
subnet_ids = data.terraform_remote_state.vpc.outputs.private_persistence_subnet_ids
...
}

data "terraform_remote_state" "vpc" {
  backend = "s3"

  config = {
    region = var.terraform_state_aws_region
    bucket = var.terraform_state_s3_bucket
    key    = "${var.aws_region}/${var.vpc_name}/vpc/terraform.tfstate"
  }
}

The tuple entry from the terraform state file looks like this…

"private_persistence_subnet_ids": {
  "value": [
	[
	  "subnet-<subnetid 1>",
	  "subnet-<subnetid 2>",
	  "subnet-<subnetid 3>",
	  "subnet-<subnetid 4>",
	  "subnet-<subnetid 5>",
	  "subnet-<subnetid 6>"
	]
  ],
  "type": [
	"tuple",
	[
	  [
		"tuple",
		[
		  "string",
		  "string",
		  "string",
		  "string",
		  "string",
		  "string"
		]
	  ]
	]
  ]
}

I’ve tried many different ways to access this data but always seem to get a “Error: Invalid value for module argument”

Any help would be greatly appreciated

Hi WhiteRabbit,

It looks like the issue is that the vpc module is outputting the private_persistence_subnet_ids as a tuple of tuples. That is, it is a tuple with a single element, which is the actual list of subnet ids. To fix this, you can either:

  • In your module reference add a [0] at the end of the data source reference to extract the nested list.
  • Fix it at the source in the vpc module output to return the list of subnets directly. I expect there is a bug in the infra modules where the output value is value = [module.vpc.private_persistence_subnet_ids], which needs to be replaced with value = module.vpc.private_persistence_subnet_ids.

Best regards,
Yori

Hi Yoriy,

thank you! I did actually try adding a [0] but to no avail. The issue was with the way the output var was defined (with wrapping brackets). With those removed I am able to access the data.

This issue arose while upgrading from terraform 0.11 to 0.12.28.

Thank you for you help.