Skip to content

Commit 3aef3b8

Browse files
committed
Add Terraform tagging how-to
1 parent cf1c921 commit 3aef3b8

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

docs/technical-standards/How-to guides/tagging.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,59 @@ This guide shows you how to configure tagging to meet the [tagging standard](..
44

55
## Terraform
66

7-
TODO
7+
To add consistent tags to every resource created by a particular provider within a repo, use [the `default_tags` block in your terraform provider block](https://www.hashicorp.com/blog/default-tags-in-the-terraform-aws-provider). This will cascade any tags you specify to all resources created by terraform. For example:
8+
9+
```hcl
10+
provider "aws" {
11+
region = "eu-west-2"
12+
default_tags {
13+
tags = {
14+
Application = "Developer Playbook"
15+
TeamEmail = "[email protected]"
16+
Environment = "production"
17+
Project = var.project
18+
}
19+
}
20+
}
21+
```
22+
23+
See how we use this in [LBHackney-IT/ce-dns](https://github.com/LBHackney-IT/ce-dns/blob/main/providers.tf) for a real-world example, including passing variables into the tag block.
24+
25+
### Dynamic tags in Terraform
26+
27+
Tags such as `AutomationBuildUrl` change every time we run the build in CI. Terraform doesn't support reading environment variables directly in the provider, so we need to pass these in as Terraform variables.
28+
29+
Here's an example for how to add `AutomationBuildUrl`:
30+
31+
1. Create a variable to hold the value in `variables.tf`:
32+
> ℹ️ We use snake_case for variable names to match the Terraform convention
33+
```hcl
34+
variable "automation_build_url" {
35+
description = "The URL of the CI build that deployed or modified this infrastructure"
36+
type = string
37+
nullable = false
38+
}
39+
```
40+
41+
2. Use the environment variable in `providers.tf` where you're defining your tags:
42+
```hcl
43+
provider "aws" {
44+
default_tags {
45+
# ...
46+
AutomationBuildUrl: var.automation_build_url
47+
}
48+
```
49+
50+
3. Then reference the environment variables in `tfvars/production.tfvars`, `tfvars/staging.tfvars`, and any other environments you have.
51+
- For CircleCI this will look like:
52+
```hcl
53+
automation_build_url = $CIRCLE_BUILD_URL
54+
```
55+
- For GitHub Actions this will look like:
56+
57+
```hcl
58+
automation_build_url = ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}
59+
```
860
961
## Serverless Framework
1062

0 commit comments

Comments
 (0)