Skip to content

Commit 7cb8ca5

Browse files
committed
ADDED tags module
1 parent 0cceca3 commit 7cb8ca5

File tree

6 files changed

+126
-2
lines changed

6 files changed

+126
-2
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Contributing to Peak's Terraform Modules
44

5-
Despite being primarily maintained by Peak, we welcome pull requests to this repo! We thank you for your time and efforts in making this library better 🙌🏻
5+
Despite being primarily maintained by Peak, we welcome pull requests to this repo! We thank you for your time and efforts in making these modules better 🙌🏻
66

77
## Consider raising an issue first if your proposed PR:
88

@@ -15,6 +15,7 @@ Despite being primarily maintained by Peak, we welcome pull requests to this rep
1515

1616
* Follow best pacticies for Terraform modules like
1717
- Folder Structure ( distribute code in `variables.tf`, `main.tf` and `outputs.tf`)
18+
- Name each resource as `default` for consistency of this repo (this makes outputs consistent).
1819
- Do not hardcode values and keep them configurable
1920
- Mention minimum required provider version
2021
- Provide default values when possible

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This repo list some open to use Terraform modules we use at `Peak AI` because we
88

99
## List of avilable modules
1010

11-
TODO
11+
- [tags (aka labels)](/tags)
1212

1313
## Example usage
1414

tags/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Tags
2+
3+
`Tags` for AWS Resources or `Labels` for Kubernetes objects allows Users to add metadta to Resource/Objects. Each tag is simple key-value pair which makes it easier to manage, search for, and filter resources.
4+
5+
This modules defines base `tags` we here at Peak assign to each of our resources and are just a base Idea. You might might change that as per your organisation's needs. In that case feel free to fork this repo and define base tags fits your organisation needs.
6+
7+
8+
## Gist
9+
10+
All tags and their values are converted to lower case to avoid confusions and duplications
11+
12+
`stage`: Stagging environment the resource belogs to. Must be one of [latest, test, beta, prod]
13+
14+
`tenant`: Tenant the resource belongs to. Defaults to `platform`
15+
16+
`feature`: Feature this resource belogs to say `shopping-cart`
17+
18+
`service`: Service this resource belogs to say `shopiing-cart-database`
19+
20+
## Example usage
21+
22+
Once initialised tags added to as many resources as you want.
23+
24+
### Example 1 > Using latest module
25+
26+
```hcl
27+
# Importing module
28+
module "tags" {
29+
source = "git@github.com:peak-ai/terraform-modules.git//tags"
30+
# Passing values from here will overide the default values
31+
tenant = "new-client"
32+
# It is good practice to make workspaces matching four staging environment rather than hardcoing them
33+
stage = terraform.workspace
34+
# hardcoing example
35+
# stage = "latest"
36+
feature = "example"
37+
service = "example"
38+
}
39+
40+
# Using module to to add tags to a resource say a S3 bucket
41+
provider "aws" {
42+
version = "~> 2.62"
43+
}
44+
45+
resource "aws_s3_bucket" "example" {
46+
bucket = "example"
47+
tags = module.tags.default
48+
}
49+
```
50+
### Example 2 > Minimal using specific version
51+
52+
```hcl
53+
# Importing module
54+
module "tags" {
55+
source = "git@github.com:peak-ai/terraform-modules.git//tags?ref=v0.1.0"
56+
tenant = "new-client"
57+
stage = "latest"
58+
feature = "example"
59+
service = "example"
60+
}
61+
62+
provider "aws" {
63+
version = "~> 2.62"
64+
}
65+
66+
resource "aws_s3_bucket" "example" {
67+
bucket = "example"
68+
tags = module.tags.default
69+
}
70+
```

tags/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
locals {
2+
tags = {
3+
stage = lower(var.stage)
4+
tenant = lower(var.tenant)
5+
feature = lower(var.feature)
6+
service = lower(var.service)
7+
}
8+
}

tags/outputs.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "default" {
2+
value = local.tags
3+
}
4+
5+
output "stage" {
6+
value = var.stage
7+
}
8+
9+
output "tenant" {
10+
value = var.tenant
11+
}
12+
13+
output "feature" {
14+
value = var.feature
15+
}
16+
17+
output "service" {
18+
value = var.service
19+
}

tags/variables.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
terraform {
2+
# This is enabled to use validation in variable
3+
experiments = [variable_validation]
4+
}
5+
6+
variable "stage" {
7+
description = "Stage on which infra should be deployed"
8+
type = string
9+
validation {
10+
condition = can(regex("latest|test|beta|prod", var.stage))
11+
error_message = "Invalid stage! Allowed values are [latest, test, beta, prod]."
12+
}
13+
}
14+
15+
variable "tenant" {
16+
type = string
17+
default = "platform"
18+
}
19+
20+
variable "feature" {
21+
type = string
22+
}
23+
24+
variable "service" {
25+
type = string
26+
}

0 commit comments

Comments
 (0)