Skip to content

Terraform module to automate creation of AWS tags and Name Attribute

License

Notifications You must be signed in to change notification settings

obytes/terraform-aws-tag

Repository files navigation

Summary

A Terraform open-source module to automate the creation of tags, the Name key and other attributes across resources in AWS, the Name prefix is based on a customizable order passed by the prefix_order variable with a default value of ["enviroemnt", "project_name", "region", "name", "attributes"] and a configurable delimiter variable with value of -.

Features

The module supports the following:

  • Optionally limiting the id variable length to a specific number of chars by modifying the prefix_lenght_limit variable, defaulted to 0 which means no limit.
  • Optionally configure the case letter of tag_key_case and tag_value_case, defaults to title, lower respectively.
  • A random string will be created using the random provider that will be used in case of creating a truncated id, prefix_legnth_limit !=0.
  • Supporting a configurable regex_substitute_chars variable which will replace any non-alphanumeric chars passed to enviroment, project_name, region and, name, defaulted to "/[^(a-z)(A-Z)(0-9)$]/".
  • A validation mechanism to validate some input variables e.g. tag_key_case, tag_value_case, region
  • Optional attributes which will be added automatically to tags variable and id

Usage

Example 1

module "label" {
  source = "github.com/obytes/terraform-aws-tag.git?ref=v1.0.7"
  environment = "dev"
  project_name = "obytes"
  region = "me-south-1"
  delimiter = "+"
  attributes  = ["private"]
  enabled = true
  prefix_length_limit = 10
}

The above example will generate the below output that can be passed to another label module to create a new id that has the base values used on label_1 module

context = {
  "additional_tags" = {}
  "attributes" = [
    "private",
  ]
  "delimiter" = "+"
  "enabled" = "true"
  "environment" = "dev"
  "prefix_length_limit" = 10
  "prefix_order" = [
    "environment",
    "project_name",
    "region",
    "name",
  ]
  "project_name" = "obytes"
  "region" = "me-south-1"
  "tag_key_case" = "title"
  "tag_value_case" = "lower"
  "tags" = {}
}
id = dev+n4rnkc
tags = {
  "Attributes" = "private"
  "Environment" = "dev"
  "Name" = "dev+n4rnkc"
  "Project_name" = "obytes"
  "Region" = "mesouth1"
}

Check the example folder for more details scenarios.

Requirements

Name Version
terraform >=0.13.0
random 3.1.0

Providers

Name Version
random 3.1.0

Modules

No modules.

Resources

Name Type
random_string.this resource

Inputs

Name Description Type Default Required
additional_tags Additional Tags, tags which can be accessed by module..tags_as_list not added to .. map(string) {} no
attributes A list of attributes e.g. private, shared, cost_center list(string) null no
company_name The company name or client name, could be fullName or abbreviation such as ex string null no
context n/a any
{
"additional_tags": {},
"attributes": [],
"company_name": null,
"delimiter": null,
"enabled": true,
"environment": null,
"prefix_length_limit": 0,
"prefix_order": [
"environment",
"project_name",
"company_name",
"region",
"attributes"
],
"project_name": null,
"random_string": null,
"regex_substitute_chars": null,
"region": null,
"tag_key_case": "title",
"tag_value_case": "lower",
"tags": {}
}
no
delimiter Delimiter to be used between project_name, environment, region and, name.
Defaults to - (hyphen). Set to "" to use no delimiter at all.
string null no
enabled A boolean to enable or disable tagging/labeling module string null no
environment Environment, the environment name such as 'stg', 'prd', 'dev' string null no
prefix_length_limit The minimum number of chars required for the id/Name desired (minimum =7)
Set it to 0 for unlimited number of chars, full_id
number null no
prefix_order The order of the Name tag
Defaults to, ["environment", "project_name", "region", "name"]
at least one should be provided
list(string) null no
project_name The project name or organization name, could be fullName or abbreviation such as ex string null no
random_string A Random string, that will be appended to id in case of using prefix_length_limit
Using the default value which is null, the string will be created using the random terraform provider
string null no
regex_substitute_chars a regex to replace empty chars in project_name, environment, region and, name
defaults to "\[a-zA-Z0-9]\", replacing any chars other than chars and digits
string null no
region Environment name such as us-east-1, ap-west-1, eu-central-1 string null no
tag_key_case The letter case of output tag keys
Possible values are lower', upperandtitle<br>defaults to title<br>
string null no
tag_value_case The letter case of output tag values
Possible values are lower', upperandtitle<br>defaults to lower<br>
string null no
tags Tags, Tags to be generated by this module which can be access by module..tags e.g. map('CostCenter', 'Production') map(string) {} no

Outputs

Name Description
additional_tags Additional Tags, tags which can be accessed by module..tags_as_list not added to ..
attributes A list of attributes e.g. private, shared, cost_center
company_name The name of the company or client
context A context to be used as an input for other modules
delimiter Delimiter to be used between project_name, environment, region and, name.
enabled A boolean to enable or disable tagging/labeling module
environment Environment name passed to module such as us-east-1, ap-west-1, eu-central-1
id ID, restricted to prefix_length_limit if set otherwise, full ID
id_full Full ID, not restricted to prefix_length_limit
prefix_order an ordered list of strings that forms the ID attribute
project_name The project name or organization name, could be fullName or abbreviation such as ex
random_string A random string, used in id and id_short in case of setting prefix_lenght_limit
regex_substitute_chars Regex, to be used for id substitution in case of using prefix_length_limit
tags Tags, Tags to be generated by this module which can be access by module..tags e.g. map('CostCenter', 'Production')

About

Terraform module to automate creation of AWS tags and Name Attribute

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages