Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label order #36

Merged
merged 7 commits into from
Aug 24, 2018
Merged

Label order #36

merged 7 commits into from
Aug 24, 2018

Conversation

Jamie-BitFlight
Copy link
Contributor

A new variable that allows for providing a label_order as a list to specify which variables and in what order they go into the id output and Name tag.

The list can include all or none of the a variable names:
"namespace", "environment", "stage", "name", "attributes"

Resolves #35

Usage:

module "label1" {
  source      = "../../"
  namespace   = "CloudPosse"
  environment = "UAT"
  stage       = "build"
  name        = "Winston Churchroom"
  attributes  = ["fire", "water", "earth", "air"]

  label_order = ["name", "environment", "stage"]

  tags = {
    "City"        = "Dublin"
    "Environment" = "Private"
  }
}
output "label1" {
  value = {
    id         = "${module.label1.id}"
    name       = "${module.label1.name}"
    namespace  = "${module.label1.namespace}"
    stage      = "${module.label1.stage}"
    attributes = "${module.label1.attributes}"
  }
}

Outputs:

label1 = {
  attributes = fire-water-earth-air
  id = winstonchurchroom-uat-build
  name = winstonchurchroom
  namespace = cloudposse
  stage = build
}

Jamie Nelson added 3 commits July 24, 2018 22:15
* 'master' of github.com:cloudposse/terraform-null-label:
  Added missing environment output (#32)
@Jamie-BitFlight Jamie-BitFlight requested a review from osterman July 27, 2018 16:22
@huksley
Copy link

huksley commented Jul 27, 2018

Yes, it works as needed with following setup:

module "label1" {
  source      = "github.com/cloudposse/terraform-null-label?ref=label-order"
  namespace   = "test"
  environment = ""
  stage       = "dev"
  name        = "fe"
  label_order = [ "namespace", "name", "stage"]
}

output "label1" {
  value = {
    id         = "${module.label1.id}"
  }
}

Produces on apply:

Outputs:

label1 = {
  id = test-fe-dev
}

However, I am not using it as is. I am using label through following projects:

https://github.com/cloudposse/terraform-aws-cicd
https://github.com/cloudposse/terraform-aws-codebuild

@osterman osterman self-assigned this Aug 2, 2018
@osterman osterman assigned aknysh and unassigned osterman Aug 23, 2018
@aknysh aknysh requested a review from goruha August 24, 2018 13:59
@aknysh
Copy link
Member

aknysh commented Aug 24, 2018

I tried to use this simpler and more scalable pattern to generate id (which @osterman suggested):

id = "${join(local.delimiter, distinct(compact(null_resource.id.*.triggers.value)))}"

resource "null_resource" "id" {
  count = "${local.enabled ? length(local.label_order_list) : 0}"

  triggers {
    value = "${local.id_context[local.label_order_list[count.index]]}"
  }
}

It worked OK when
data "null_data_source" "tags_as_list_of_maps"
and
tags_as_list_of_maps = ["${data.null_data_source.tags_as_list_of_maps.*.outputs}"]
were commented out.

After uncommenting it, started to throw errors: null_data_source.tags_as_list_of_maps: value of count can't be computed.

And that’s because we introduced a resource (even if it’s null) and other resources depend on it.
So it looks like TF has issues with counts not only for real resources on AWS, but any resource types (even virtual like null), and even inside one module.

So I ended up using this pattern from @Jamie-BitFlight (simplified a little bit):

id_context = {
    name        = "${local.name}"
    namespace   = "${local.namespace}"
    environment = "${local.environment}"
    stage       = "${local.stage}"
    attributes  = "${local.attributes}"
  }

  id = "${lower(join(local.delimiter, compact(list(
    "${local.label_order_length > 0 ? local.id_context[element(local.label_order_final_list, 0)] : ""}",
    "${local.label_order_length > 1 ? local.id_context[element(local.label_order_final_list, 1)] : ""}",
    "${local.label_order_length > 2 ? local.id_context[element(local.label_order_final_list, 2)] : ""}",
    "${local.label_order_length > 3 ? local.id_context[element(local.label_order_final_list, 3)] : ""}",
    "${local.label_order_length > 4 ? local.id_context[element(local.label_order_final_list, 4)] : ""}"))))}"

Works OK for all scenarios including context and label_order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants