Skip to content

A Terraform module designed to generate consistent names and tags for cloud resources

Notifications You must be signed in to change notification settings

Callumccr/tf-mod-label

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

README Header

tf-mod-label

Module description

Use the tf-mod-label Terraform module to implement a strict, and consistent naming convention, tagging blocks, and outputs for infrastructure resources.

Project: %!s() : [%!s()] | [%!s()]

Introduction

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

It's recommended to use one terraform-null-label module for every unique resource of a given resource type. For example, if you have 10 instances, there should be 10 different labels. However, if you have multiple different kinds of resources (e.g. instances, security groups, file systems, and elastic ips), then they can all share the same label assuming they are logically related.

NOTE: The null refers to the primary Terraform provider used in this module.

Releases of this module from 0.12.0 onward support HCL2 and only work with Terraform 0.12 or newer.

Usage

IMPORTANT: The master branch is used in source just as an example. In your code, do not pin to master because there may be breaking changes between releases. Instead pin to the release tag (e.g. ?ref=tags/x.y.z) of one of our latest releases.

The below values shown in the usage of this module are purely representative, please replace desired values as required.

Simple Usage:

module "label" {
  source             = "git::ssh://git@github.com:Callumccr/tf-mod-label.git?ref=master
  namespace          = "callum"
  environment        = "dev"
  name               = "label"
  attributes         = ["1"]
  delimiter          = "-"
  additional_tag_map = {} /* Additional attributes (e.g. 1) */
  label_order        = ["environment", "namespace", "name", "attributes"]
}

This will create an id with the value of dev-callum-label-1 because when generating id, the default order is environment, namespace, name, attributes. (you can override it by using the label_order variable)

Now reference the label when creating an instance:

resource "aws_instance" "default" {
  instance_type = "t1.micro"
  tags          = module.label.tags
}

Or define a security group:

resource "aws_security_group" "default" {
  vpc_id = var.vpc_id
  name   = module.label.id
  tags   = module.label.tags
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

Quick Start

Examples

Advanced Examples:

  • Full advanced examples can be found on the unmodified upstream provider here

Providers

No provider.

Inputs

Name Description Type Default Required
additional_tag_map Additional tags for appending to each tag map map(string) {} no
attributes Additional attributes (e.g. 1) list(string) [] no
context Default context to use for passing state between label invocations
object({
namespace = string
environment = string
stage = string
name = string
enabled = bool
delimiter = string
attributes = list(string)
label_order = list(string)
tags = map(string)
additional_tag_map = map(string)
regex_replace_chars = string
})
{
"additional_tag_map": {},
"attributes": [],
"delimiter": "",
"enabled": true,
"environment": "",
"label_order": [],
"name": "",
"namespace": "",
"regex_replace_chars": "",
"stage": "",
"tags": {}
}
no
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
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
regex_replace_chars 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 string "/[^a-zA-Z0-9-]/" no
stage Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' string "" no
tags Additional tags (e.g. map('BusinessUnit','XYZ') map(string) {} no

Outputs

Name Description
attributes List of attributes
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
label_order The naming order of the id output and Name tag
name Normalized name
namespace Normalized namespace
stage Normalized stage
tags Normalized Tag map
tags_as_list_of_maps Additional tags as a list of maps, which can be used in several AWS resources

Related Projects

You can find more Terraform Modules by vising the link.

Additionally, check out these other related, and maintained projects.

References

For additional context, refer to some of these links.

Help

Got a question? We got answers.

File a Github issue, or message us on Slack

Contributors

Callum Robertson
Callum Robertson


README Footer

About

A Terraform module designed to generate consistent names and tags for cloud resources

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published