Skip to content

Commit

Permalink
Add option to limit output id length (cloudposse#98) @tibbing
Browse files Browse the repository at this point in the history
  • Loading branch information
tibbing authored Aug 26, 2020
1 parent 46042f4 commit af6fd1f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ No provider.
| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes` | `string` | `"-"` | no |
| enabled | Set to false to prevent the module from creating any resources | `bool` | `true` | no |
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no |
| id\_max\_length | Specify the max length of the `id` output, or 0 for unrestricted length | `number` | `0` | no |
| label\_order | The naming order of the id output and Name tag | `list(string)` | `[]` | no |
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `""` | no |
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `""` | no |
Expand All @@ -552,7 +553,8 @@ No provider.
| context | Context of this module to pass to other label modules |
| delimiter | Delimiter between `namespace`, `environment`, `stage`, `name` and `attributes` |
| environment | Normalized environment |
| id | Disambiguated ID |
| id | Disambiguated ID restricted to id\_max\_length |
| id\_full | Disambiguated ID not restricted to id\_max\_length |
| label\_order | The naming order of the id output and Name tag |
| name | Normalized name |
| namespace | Normalized namespace |
Expand Down Expand Up @@ -706,8 +708,8 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply

### Contributors

| [![Erik Osterman][osterman_avatar]][osterman_homepage]<br/>[Erik Osterman][osterman_homepage] | [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]<br/>[Andriy Knysh][aknysh_homepage] | [![Igor Rodionov][goruha_avatar]][goruha_homepage]<br/>[Igor Rodionov][goruha_homepage] | [![Sergey Vasilyev][s2504s_avatar]][s2504s_homepage]<br/>[Sergey Vasilyev][s2504s_homepage] | [![Michael Pereira][MichaelPereira_avatar]][MichaelPereira_homepage]<br/>[Michael Pereira][MichaelPereira_homepage] | [![Jamie Nelson][Jamie-BitFlight_avatar]][Jamie-BitFlight_homepage]<br/>[Jamie Nelson][Jamie-BitFlight_homepage] | [![Vladimir][SweetOps_avatar]][SweetOps_homepage]<br/>[Vladimir][SweetOps_homepage] | [![Daren Desjardins][darend_avatar]][darend_homepage]<br/>[Daren Desjardins][darend_homepage] | [![Maarten van der Hoef][maartenvanderhoef_avatar]][maartenvanderhoef_homepage]<br/>[Maarten van der Hoef][maartenvanderhoef_homepage] |
|---|---|---|---|---|---|---|---|---|
| [![Erik Osterman][osterman_avatar]][osterman_homepage]<br/>[Erik Osterman][osterman_homepage] | [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]<br/>[Andriy Knysh][aknysh_homepage] | [![Igor Rodionov][goruha_avatar]][goruha_homepage]<br/>[Igor Rodionov][goruha_homepage] | [![Sergey Vasilyev][s2504s_avatar]][s2504s_homepage]<br/>[Sergey Vasilyev][s2504s_homepage] | [![Michael Pereira][MichaelPereira_avatar]][MichaelPereira_homepage]<br/>[Michael Pereira][MichaelPereira_homepage] | [![Jamie Nelson][Jamie-BitFlight_avatar]][Jamie-BitFlight_homepage]<br/>[Jamie Nelson][Jamie-BitFlight_homepage] | [![Vladimir][SweetOps_avatar]][SweetOps_homepage]<br/>[Vladimir][SweetOps_homepage] | [![Daren Desjardins][darend_avatar]][darend_homepage]<br/>[Daren Desjardins][darend_homepage] | [![Maarten van der Hoef][maartenvanderhoef_avatar]][maartenvanderhoef_homepage]<br/>[Maarten van der Hoef][maartenvanderhoef_homepage] | [![Adam Tibbing][tibbing_avatar]][tibbing_homepage]<br/>[Adam Tibbing][tibbing_homepage] |
|---|---|---|---|---|---|---|---|---|---|

[osterman_homepage]: https://github.com/osterman
[osterman_avatar]: https://img.cloudposse.com/150x150/https://github.com/osterman.png
Expand All @@ -727,6 +729,8 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply
[darend_avatar]: https://img.cloudposse.com/150x150/https://github.com/darend.png
[maartenvanderhoef_homepage]: https://github.com/maartenvanderhoef
[maartenvanderhoef_avatar]: https://img.cloudposse.com/150x150/https://github.com/maartenvanderhoef.png
[tibbing_homepage]: https://github.com/tibbing
[tibbing_avatar]: https://img.cloudposse.com/150x150/https://github.com/tibbing.png

[![README Footer][readme_footer_img]][readme_footer_link]
[![Beacon][beacon]][website]
Expand Down
2 changes: 2 additions & 0 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,5 @@ contributors:
github: darend
- name: Maarten van der Hoef
github: maartenvanderhoef
- name: Adam Tibbing
github: tibbing
4 changes: 3 additions & 1 deletion docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ No provider.
| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes` | `string` | `"-"` | no |
| enabled | Set to false to prevent the module from creating any resources | `bool` | `true` | no |
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no |
| id\_max\_length | Specify the max length of the `id` output, or 0 for unrestricted length | `number` | `0` | no |
| label\_order | The naming order of the id output and Name tag | `list(string)` | `[]` | no |
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `""` | no |
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `""` | no |
Expand All @@ -33,7 +34,8 @@ No provider.
| context | Context of this module to pass to other label modules |
| delimiter | Delimiter between `namespace`, `environment`, `stage`, `name` and `attributes` |
| environment | Normalized environment |
| id | Disambiguated ID |
| id | Disambiguated ID restricted to id\_max\_length |
| id\_full | Disambiguated ID not restricted to id\_max\_length |
| label\_order | The naming order of the id output and Name tag |
| name | Normalized name |
| namespace | Normalized namespace |
Expand Down
2 changes: 1 addition & 1 deletion examples/autoscalinggroup/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ resource "aws_autoscaling_group" "default" {
# Provider #
################################
provider "aws" {
region = "eu-west-1"
region = "eu-west-1"

# Make it faster by skipping unneeded checks here
skip_get_ec2_platforms = true
Expand Down
9 changes: 8 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ locals {

labels = [for l in local.label_order : local.id_context[l] if length(local.id_context[l]) > 0]

id = lower(join(local.delimiter, local.labels))
id_full = lower(join(local.delimiter, local.labels))
id_md5 = md5(local.id_full)
# Truncates ID to given max length, suffixed by 6 character hash of ID for disambiguation
id_short = (var.id_max_length <= 6 ?
substr(local.id_md5, 0, var.id_max_length) :
"${replace(substr(local.id_full, 0, var.id_max_length - 6), "/-$/", "")}-${substr(local.id_md5, 0, 5)}")
id = var.id_max_length != 0 && length(local.id_full) > var.id_max_length ? local.id_short : local.id_full


# Context of this label to pass to other label modules
output_context = {
Expand Down
8 changes: 6 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
output "id" {
value = local.enabled ? local.id : ""
description = "Disambiguated ID"
description = "Disambiguated ID restricted to id_max_length"
}

output "id_full" {
value = local.enabled ? local.id_full : ""
description = "Disambiguated ID not restricted to id_max_length"
}

output "name" {
Expand Down Expand Up @@ -52,4 +57,3 @@ output "label_order" {
value = local.label_order
description = "The naming order of the id output and Name tag"
}

5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@ variable "regex_replace_chars" {
description = "Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`. By default only hyphens, letters and digits are allowed, all other chars are removed"
}

variable "id_max_length" {
type = number
default = 0
description = "Specify the max length of the `id` output, or 0 for unrestricted length"
}

0 comments on commit af6fd1f

Please sign in to comment.