/terraform-null-context

A simple metadata management context module

Primary LanguageHCL

A metadata module for AWS resources management

This module try to manage the follow items easy, unified and flexible with DRY

  • Resource meta data with same format
  • Tags, auto generated tags
  • Auto generated resource Name and Id if it missed(for some AWS resources)

Input of context

context_values

A map name context to initialize the module, those metadata fields are valid,

  • org - Org or company name
  • dept - Department name
  • team - Team name
  • app - Application name
  • service - Service name
  • component - Component Name
  • env - Environment Name
  • stage - Stage Name
  • alt_env - Alternative env name
  • region - Region Name
  • alt_region - Alternative Region name, e.g. uw2 as short one

To avoid too long label and id name generated, please use shorter name or abbr for this module. Almost all the attributes can be override.

label_attributes and label_delimiter

label.attributes, a attributes list, is used to build the label(name), the order of the list items is applied to the label. The label_attributes items MUST be present in the context map,

Default

label.atrributes = ["team", "service", "component", "env", "region"]

The default label will be team-service-component-env-region as the default label.delimiter is "-".

context_values = {
    team = "myTeam"
    service = "myService"
    component = "myServiceCom"
    env = "dev"
    region = "us-west-2"
}

Then the generated label will be "myTeam-myService-myServiceCom-dev-us-west-2"; label.delimiter can be override too. The generated id will be the label appending random id(8 characters).

tag_attributes and tag_prefix, additional_tags

The attributes in the context to be tagged and prefix for tag Name

In the above sample input context, if the tag_attributes as default(["team", "service", "component", "env"]), then the tags list will be(tag_prefix is iac: as default)

iac:TEAM = myteam
iac:SERVICE = myservice
iac:COMPONENT = myservicecom
iac:ENV = dev

if additional_tags is present, that map will be appended directly

The common situation is that in the context.tf, set the additional_tags = var.tags, then at the AWS Resource tags attribute, assign it as module.context.tags.

Output of the module

  • label and name, now they are both set to auto-generated label mentioned above
  • id - The label value appending an random id as the context.id, used for unique case
  • tags - The auto generated tags map
  • env - Expose the env in context_values if present or dev
  • region - Expose the region in context_values if present or us-west-2

Sample of context.tf file in Terraform module

Please notice the source location might be different according to the module location. It can be,

  • Relative path in the module is in the sampe repos
  • GitHub URL
module "context" {
  source = a3linux/context/null

  context_values = var.context
  additional_tags = try(var.tags, {})
}

variable "context" {
  type = map(any)
  description = "Description of context"
  default = {
    env = "dev"
    region = "us-west-2"
    team = "myTeam"
    service = "myService"
    component = "myServiceCom"
  }
}

Placeholder of label and id in additional_tags

Only CONTEXT:label and CONTEXT:id are supported as placeholder value for additional_tags.

For example,

...
additional_tags = {
    "my:custom" = "CONTEXT:label"
}
...

The my:custom will have the tag value as the label generated by context module. This provide another way to re-use the module attribute for tag.

The context variable can be provided through terragrunt or other variables for flexible and customization.