How to get an item from a list

4,517 views
Skip to first unread message

gitted

unread,
Feb 13, 2017, 10:49:30 PM2/13/17
to Terraform
I am using the vpc module, and I am creating 3 private subnets:

private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]


How would I reference the 1st, 2nd or 3rd item in the list?

Lowe Schmidt

unread,
Feb 14, 2017, 4:02:42 AM2/14/17
to terrafo...@googlegroups.com
When you say "the vpc module" do you mean the tf_aws_vpc community module? I'm assuming yes.

To reference the private_subnets from the module you would do "${module.my_vpc.private_subnets[0]}" for the first element in the list. And 1, 2 .. N for each following item. 

and here's a link to the interpolation documentation https://www.terraform.io/docs/configuration/interpolation.html



--
Lowe Schmidt | +46 723 867 157

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/e495bd4b-edd9-4c8c-acf0-25e0ccc6cb58%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gitted

unread,
Feb 14, 2017, 3:57:19 PM2/14/17
to Terraform
Thanks!

BTW, why isn't this allowed?

environment = "development"

db_instance = "app_main-${var.environment}"

I basically want to use another variable when creating a variable.  Not possible?


On Tuesday, February 14, 2017 at 4:02:42 AM UTC-5, Lowe Schmidt wrote:
When you say "the vpc module" do you mean the tf_aws_vpc community module? I'm assuming yes.

To reference the private_subnets from the module you would do "${module.my_vpc.private_subnets[0]}" for the first element in the list. And 1, 2 .. N for each following item. 

and here's a link to the interpolation documentation https://www.terraform.io/docs/configuration/interpolation.html



--
Lowe Schmidt | +46 723 867 157

On 14 February 2017 at 04:49, gitted <sahme...@gmail.com> wrote:
I am using the vpc module, and I am creating 3 private subnets:

private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]


How would I reference the 1st, 2nd or 3rd item in the list?

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

Lowe Schmidt

unread,
Feb 15, 2017, 3:38:46 AM2/15/17
to terrafo...@googlegroups.com
No, right now variables do not allow interpolations in them.

You can do:

variable "environment" { default = "staging" }
variable "app_instance" { default = "app-main" }

resource "aws_elastic_beanstalk" {
...
db_instance = "${var.app_instance}-${var.environment}" 
...
}

but you can't do 

variable "db_instance" { default = "${var.app_instance}-"${var.environment}" }



--
Lowe Schmidt | +46 723 867 157

To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/0d228f6f-343c-4540-a48c-00e6d5b3c282%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages