From 9f4f0542bfea629869d74bf6d42a7fa45e729389 Mon Sep 17 00:00:00 2001 From: Yohan Belval Date: Wed, 30 Oct 2019 14:12:52 -0400 Subject: [PATCH] added nb_instances to produce multiple names with index. --- main.tf | 11 +++++++++++ outputs.tf | 7 ++++++- variables.tf | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 0f9fa75..55c8677 100644 --- a/main.tf +++ b/main.tf @@ -3,3 +3,14 @@ locals { suffix = "${join(var.separator, var.suffixes)}" separated_name = "${var.separator}${var.name}${var.separator}" } + +data "null_data_source" "names" { + count = var.nb_instances + inputs = { + result = var.nb_instances > 1 ? substr("${local.prefix}${local.separated_name}${count.index}${var.separator}${local.suffix}", 0, var.max_length) : substr("${local.prefix}${var.separator}${var.name}${var.separator}${local.suffix}", 0, var.max_length) + } +} + +locals { + results = data.null_data_source.names.*.outputs.result +} diff --git a/outputs.tf b/outputs.tf index 6779956..f5b34f7 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,4 +1,9 @@ output "result" { description = "The generated resource name." - value = substr("${local.prefix}${local.separated_name}${local.suffix}", 0, var.max_length) + value = local.results[0] +} + +output "results" { + description = "The generated resource names." + value = local.results } diff --git a/variables.tf b/variables.tf index 0cbb6d9..b69b742 100644 --- a/variables.tf +++ b/variables.tf @@ -3,6 +3,10 @@ variable "name" { type = string } +variable "nb_instances" { + default = 1 +} + variable "prefixes" { description = "List of prefixes to append in front of the resource name." type = list(string)