Skip to content

Commit 6edc9b2

Browse files
ostermanJamie-BitFlight
authored andcommitted
Add context (#25)
* Add support for context variable * Initial prototype of concept * format code * Better example * Much magic applied * More magic applied * fixed duplication of strings after passing context * fixed duplication of strings after passing context * Got the merge order for the tags correct * Added some comments * Removed the lower() function from the delimiter string normalisation * More comments * Changed to use replace() for removing whitespace in values * Joined the attributes together * Joined the attributes together * Handles empty values from variables * Updated Readme * wrong delimiter in tags, delimiter output added (#31) * Updated README to include the context input and output, added environment as an optional variable. Swapped out null_resource for null_data_source, which reduces some of the output noise but keeps the funtionality the same.
1 parent 85dc644 commit 6edc9b2

File tree

9 files changed

+375
-61
lines changed

9 files changed

+375
-61
lines changed

.gitignore

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
# Compiled files
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
25
*.tfstate
3-
*.tfstate.backup
6+
*.tfstate.*
7+
8+
# .tfvars files
9+
*.tfvars
410

5-
.terraform
6-
.idea
7-
*.iml
11+
**/.idea
12+
**/*.iml
813

9-
.build-harness
10-
build-harness
14+
**/.build-harness
15+
**/build-harness

README.md

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
Terraform module designed to generate consistent label names and tags for resources. Use `terraform-null-label` to implement a strict naming convention.
99

10-
A label follows the following convention: `{namespace}-{stage}-{name}-{attributes}`. The delimiter (e.g. `-`) is interchangeable.
10+
A label follows the following convention: `{namespace}-{environment}-{stage}-{name}-{attributes}`. The delimiter (e.g. `-`) is interchangeable.
11+
The label items are all optional. So if you perfer the term `stage` to `environment` you can exclude environment and the label `id` will look like `{namespace}-{stage}-{name}-{attributes}`.
12+
If attributes are excluded but `stage` and `environment` are included, `id` will look like `{namespace}-{environment}-{stage}-{name}`
1113

1214
It's recommended to use one `terraform-null-label` module for every unique resource of a given resource type.
1315
For example, if you have 10 instances, there should be 10 different labels.
@@ -226,6 +228,97 @@ resource "aws_autoscaling_group" "default" {
226228
tags = ["${module.label.tags_as_list_of_maps}"]
227229
}
228230
```
231+
### Advanced Example 3 as per the [complete example](./examples/complete)
232+
This example shows how you can pass the `context` output of one label module to the next label_module.
233+
Allowing you to create one label that has the base set of values, and then creating every extra label
234+
as a derivative of that.
235+
236+
```hcl
237+
module "label1" {
238+
source = "../../"
239+
namespace = "CloudPosse"
240+
environment = "UAT"
241+
stage = "build"
242+
name = "Winston Churchroom"
243+
attributes = ["fire", "water", "earth", "air"]
244+
245+
tags = {
246+
"City" = "Dublin"
247+
"Environment" = "Private"
248+
}
249+
}
250+
251+
module "label2" {
252+
source = "../../"
253+
context = "${module.label1.context}"
254+
name = "Charlie"
255+
stage = "test"
256+
257+
tags = {
258+
"City" = "London"
259+
"Environment" = "Public"
260+
}
261+
}
262+
263+
module "label3" {
264+
source = "../../"
265+
name = "Starfish"
266+
stage = "release"
267+
268+
tags = {
269+
"Eat" = "Carrot"
270+
"Animal" = "Rabbit"
271+
}
272+
}
273+
```
274+
275+
This creates label outputs like this.
276+
```hcl
277+
Outputs:
278+
label1 = {
279+
attributes = fire-water-earth-air
280+
id = cloudposse-uat-build-winstonchurchroom-fire-water-earth-air
281+
name = winstonchurchroom
282+
namespace = cloudposse
283+
stage = build
284+
}
285+
label1_tags = {
286+
City = Dublin
287+
Environment = Private
288+
Name = cloudposse-uat-build-winstonchurchroom-fire-water-earth-air
289+
Namespace = cloudposse
290+
Stage = build
291+
}
292+
label2 = {
293+
attributes = fire-water-earth-air
294+
id = cloudposse-uat-build-charlie-fire-water-earth-air
295+
name = charlie
296+
namespace = cloudposse
297+
stage = build
298+
}
299+
label2_tags = {
300+
City = London
301+
Environment = Public
302+
Name = cloudposse-uat-build-charlie-fire-water-earth-air
303+
Namespace = cloudposse
304+
Stage = build
305+
}
306+
label3 = {
307+
attributes =
308+
id = release-starfish
309+
name = starfish
310+
namespace =
311+
stage = release
312+
}
313+
label3_tags = {
314+
Animal = Rabbit
315+
Eat = Carrot
316+
Environment =
317+
Name = release-starfish
318+
Namespace =
319+
Stage = release
320+
}
321+
```
229322

230323

231324

@@ -248,18 +341,22 @@ Available targets:
248341
|------|-------------|:----:|:-----:|:-----:|
249342
| additional_tag_map | Additional tags for appending to each tag map. | map | `<map>` | no |
250343
| attributes | Additional attributes (e.g. `policy` or `role`) | list | `<list>` | no |
344+
| context | Default context to use for passing state between label invocations | map | `<map>` | no |
251345
| delimiter | Delimiter to be used between `name`, `namespace`, `stage`, etc. | string | `-` | no |
252346
| enabled | Set to false to prevent the module from creating any resources | string | `true` | no |
253-
| name | Solution name, e.g. 'app' or 'jenkins' | string | - | yes |
254-
| namespace | Namespace, which could be your organization name, e.g. 'cp' or 'cloudposse' | string | - | yes |
255-
| stage | Stage, e.g. 'prod', 'staging', 'dev', or 'test' | string | - | yes |
347+
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | string | `` | no |
348+
| name | Solution name, e.g. 'app' or 'jenkins' | string | `` | no |
349+
| namespace | Namespace, which could be your organization name, e.g. 'cp' or 'cloudposse' | string | `` | no |
350+
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | string | `` | no |
256351
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map | `<map>` | no |
257352

258353
## Outputs
259354

260355
| Name | Description |
261356
|------|-------------|
262357
| attributes | Normalized attributes |
358+
| context | Context of this module to pass between other modules |
359+
| delimiter | Delimiter used in label ID |
263360
| id | Disambiguated ID |
264361
| name | Normalized name |
265362
| namespace | Normalized namespace |
@@ -274,7 +371,7 @@ Available targets:
274371

275372
Check out these related projects.
276373

277-
- [terraform-terraform-label](https://github.com/cloudposse/terraform-terraform-label) - Terraform Module to define a consistent naming convention by (namespace, stage, name, [attributes])
374+
- [terraform-terraform-label](https://github.com/cloudposse/terraform-terraform-label) - Terraform Module to define a consistent naming convention by (namespace, environment, stage, name, [attributes])
278375

279376

280377

README.yaml

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ badges:
3030

3131
related:
3232
- name: "terraform-terraform-label"
33-
description: "Terraform Module to define a consistent naming convention by (namespace, stage, name, [attributes])"
33+
description: "Terraform Module to define a consistent naming convention by (namespace, environment, stage, name, [attributes])"
3434
url: "https://github.com/cloudposse/terraform-terraform-label"
3535

3636
# Short description of this project
3737
description: |-
3838
Terraform module designed to generate consistent label names and tags for resources. Use `terraform-null-label` to implement a strict naming convention.
3939
40-
A label follows the following convention: `{namespace}-{stage}-{name}-{attributes}`. The delimiter (e.g. `-`) is interchangeable.
40+
A label follows the following convention: `{namespace}-{environment}-{stage}-{name}-{attributes}`. The delimiter (e.g. `-`) is interchangeable.
41+
The label items are all optional. So if you perfer the term `stage` to `environment` you can exclude environment and the label `id` will look like `{namespace}-{stage}-{name}-{attributes}`.
42+
If attributes are excluded but `stage` and `environment` are included, `id` will look like `{namespace}-{environment}-{stage}-{name}`
4143
4244
It's recommended to use one `terraform-null-label` module for every unique resource of a given resource type.
4345
For example, if you have 10 instances, there should be 10 different labels.
@@ -239,6 +241,98 @@ usage: |-
239241
tags = ["${module.label.tags_as_list_of_maps}"]
240242
}
241243
```
244+
### Advanced Example 3 as per the [complete example](./examples/complete)
245+
This example shows how you can pass the `context` output of one label module to the next label_module.
246+
Allowing you to create one label that has the base set of values, and then creating every extra label
247+
as a derivative of that.
248+
249+
```hcl
250+
module "label1" {
251+
source = "../../"
252+
namespace = "CloudPosse"
253+
environment = "UAT"
254+
stage = "build"
255+
name = "Winston Churchroom"
256+
attributes = ["fire", "water", "earth", "air"]
257+
258+
tags = {
259+
"City" = "Dublin"
260+
"Environment" = "Private"
261+
}
262+
}
263+
264+
module "label2" {
265+
source = "../../"
266+
context = "${module.label1.context}"
267+
name = "Charlie"
268+
stage = "test"
269+
270+
tags = {
271+
"City" = "London"
272+
"Environment" = "Public"
273+
}
274+
}
275+
276+
module "label3" {
277+
source = "../../"
278+
name = "Starfish"
279+
stage = "release"
280+
281+
tags = {
282+
"Eat" = "Carrot"
283+
"Animal" = "Rabbit"
284+
}
285+
}
286+
```
287+
288+
This creates label outputs like this.
289+
```hcl
290+
Outputs:
291+
label1 = {
292+
attributes = fire-water-earth-air
293+
id = cloudposse-uat-build-winstonchurchroom-fire-water-earth-air
294+
name = winstonchurchroom
295+
namespace = cloudposse
296+
stage = build
297+
}
298+
label1_tags = {
299+
City = Dublin
300+
Environment = Private
301+
Name = cloudposse-uat-build-winstonchurchroom-fire-water-earth-air
302+
Namespace = cloudposse
303+
Stage = build
304+
}
305+
label2 = {
306+
attributes = fire-water-earth-air
307+
id = cloudposse-uat-build-charlie-fire-water-earth-air
308+
name = charlie
309+
namespace = cloudposse
310+
stage = build
311+
}
312+
label2_tags = {
313+
City = London
314+
Environment = Public
315+
Name = cloudposse-uat-build-charlie-fire-water-earth-air
316+
Namespace = cloudposse
317+
Stage = build
318+
}
319+
label3 = {
320+
attributes =
321+
id = release-starfish
322+
name = starfish
323+
namespace =
324+
stage = release
325+
}
326+
label3_tags = {
327+
Animal = Rabbit
328+
Eat = Carrot
329+
Environment =
330+
Name = release-starfish
331+
Namespace =
332+
Stage = release
333+
}
334+
```
335+
242336
243337
include:
244338
- "docs/targets.md"

docs/terraform.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55
|------|-------------|:----:|:-----:|:-----:|
66
| additional_tag_map | Additional tags for appending to each tag map. | map | `<map>` | no |
77
| attributes | Additional attributes (e.g. `policy` or `role`) | list | `<list>` | no |
8+
| context | Default context to use for passing state between label invocations | map | `<map>` | no |
89
| delimiter | Delimiter to be used between `name`, `namespace`, `stage`, etc. | string | `-` | no |
910
| enabled | Set to false to prevent the module from creating any resources | string | `true` | no |
10-
| name | Solution name, e.g. 'app' or 'jenkins' | string | - | yes |
11-
| namespace | Namespace, which could be your organization name, e.g. 'cp' or 'cloudposse' | string | - | yes |
12-
| stage | Stage, e.g. 'prod', 'staging', 'dev', or 'test' | string | - | yes |
11+
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | string | `` | no |
12+
| name | Solution name, e.g. 'app' or 'jenkins' | string | `` | no |
13+
| namespace | Namespace, which could be your organization name, e.g. 'cp' or 'cloudposse' | string | `` | no |
14+
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | string | `` | no |
1315
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map | `<map>` | no |
1416

1517
## Outputs
1618

1719
| Name | Description |
1820
|------|-------------|
1921
| attributes | Normalized attributes |
22+
| context | Context of this module to pass between other modules |
23+
| delimiter | Delimiter used in label ID |
2024
| id | Disambiguated ID |
2125
| name | Normalized name |
2226
| namespace | Normalized namespace |

examples/complete/main.tf

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
1-
module "label" {
2-
source = "../../"
3-
namespace = "Namespace"
4-
stage = "Stage"
5-
name = "Name"
6-
attributes = ["1", "2", "3", ""]
7-
tags = "${map("Key", "Value")}"
1+
module "label1" {
2+
source = "../../"
3+
namespace = "CloudPosse"
4+
environment = "UAT"
5+
stage = "build"
6+
name = "Winston Churchroom"
7+
attributes = ["fire", "water", "earth", "air"]
8+
9+
tags = {
10+
"City" = "Dublin"
11+
"Environment" = "Private"
12+
}
13+
}
14+
15+
module "label2" {
16+
source = "../../"
17+
context = "${module.label1.context}"
18+
name = "Charlie"
19+
stage = "test"
20+
21+
tags = {
22+
"City" = "London"
23+
"Environment" = "Public"
24+
}
25+
}
26+
27+
module "label3" {
28+
source = "../../"
29+
name = "Starfish"
30+
stage = "release"
31+
32+
tags = {
33+
"Eat" = "Carrot"
34+
"Animal" = "Rabbit"
35+
}
836
}

0 commit comments

Comments
 (0)