Skip to content

Commit 0573f71

Browse files
authored
feat: renovate, aqua, tf-docs, TF workspaces (#10)
* Adds Renovate configuration (along with documentation) * Adds `terraform-docs` along with corresponding tools * Adds example of using TF workspaces Also let's come up with the new name for this template repo, and I'll adjust accordingly in the other dependent repos. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added configuration files for automated dependency updates and documentation generation tools. * Introduced environment-specific variable files for development and production. * Added a new input variable and output for the template root module to enhance customization and visibility. * **Improvements** * Updated documentation for improved clarity, structure, and actionable guidance. * Enhanced module documentation with clearer requirements, usage instructions, and example commands. * **Updates** * Upgraded versions of required tools, linters, and providers for better compatibility and security. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 48f3773 commit 0573f71

File tree

12 files changed

+115
-51
lines changed

12 files changed

+115
-51
lines changed

.github/renovate.json5

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": [
3+
"config:best-practices",
4+
"github>aquaproj/aqua-renovate-config#2.7.5"
5+
],
6+
"schedule": ["before 5am on Monday"],
7+
"baseBranches": ["main", "master"],
8+
"labels": ["auto-upgrade"],
9+
"dependencyDashboardAutoclose": true,
10+
"enabledManagers": ["terraform"],
11+
"terraform": {
12+
"ignorePaths": [
13+
"**/context.tf",
14+
"components/**", // We should upgrade the majority of components manually
15+
]
16+
},
17+
}

.terraform-docs.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: 0.20.0
2+
formatter: markdown table
3+
4+
settings:
5+
anchor: false
6+
html: false
7+
lockfile: false
8+
9+
recursive:
10+
enabled: true
11+
path: root-modules
12+
include-main: false
13+
14+
output:
15+
file: README.md
16+
mode: inject
17+
template: |-
18+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
19+
{{ .Content }}
20+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

.trunk/trunk.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ lint:
2020
# Incompatible with some Terraform features: https://github.com/tenable/terrascan/issues/1331
2121
- terrascan
2222
enabled:
23-
- tofu@1.10.1
23+
- tofu@1.10.3
2424
- actionlint@1.7.7
25-
- checkov@3.2.447
25+
- checkov@3.2.451
2626
- git-diff-check
2727
- markdownlint@0.45.0
2828
- prettier@3.6.2
29-
- tflint@0.58.0
30-
- trivy@0.63.0
31-
- trufflehog@3.89.2
29+
- tflint@0.58.1
30+
- trivy@0.64.1
31+
- trufflehog@3.90.0
3232
- yamllint@1.37.1
3333
ignore:
3434
- linters: [tofu]

README.md

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
1-
# example-tf
1+
# infra-monorepo-template
22

33
This repository serves as an example and template for how Masterpoint thinks about organizing a vanilla Terraform or OpenTofu (from now on referred to as "TF") monorepo with root modules, child modules, and accompanying tooling.
44

55
This includes example configurations and recommendations around the following topics:
66

7-
<!-- TODO: Link to what Multi-instance root modules are once https://github.com/masterpointio/masterpoint.io/pull/49 ships -->
8-
9-
1. Example organizational structure for an IaC Monorepo (Multi-instance Root Modules + Child Modules)
7+
1. Example organizational structure for an IaC Monorepo ([Multi-instance Root Modules](https://masterpoint.io/blog/terraform-opentofu-terminology-breakdown/#multi-instance-root-modules) + Child Modules)
108
1. Root module example is provided in the [root-modules/template-root-module](./root-modules/template-root-module/) directory.
119
2. Child module example is provided in the [child-modules/random-pet](./child-modules/random-pet/) directory
1210
2. [Recommendations for module file structure](#structure) with [file-by-file guidance](#file-by-file-guidance)
1311
3. [Recommendations for version pinning TF + Providers](#versioning-tf-and-providers)
14-
4. [Managing which TF binary is used per project](#managing-which-tf-binary-is-used-per-project)
12+
4. [Managing which TF binary is used per project using Aqua](#managing-which-tf-binary-is-used-per-project-using-aqua)
1513
5. [Guidance on linting + CI for TF](#tf-linting--ci)
16-
6. [Frequently Asked Questions](#frequently-asked-questions)
17-
18-
<!-- TODO
19-
1. Example Native TF Tests with accompanying GitHub Action workflow for running tests
20-
-->
21-
22-
## Recommendations (TODO: Discuss)
23-
24-
We recommend to include:
25-
26-
- Module Description: Provide a concise explanation of what the module does and its intended use cases.
27-
- Usage Instructions: Include code snippets demonstrating how to call the module from a [root module](https://developer.hashicorp.com/terraform/language/modules#the-root-module).
28-
- Inputs & Outputs Summary: List the module’s input variables (with defaults and required ones highlighted) and outputs. We recommend using [terraform-docs](https://github.com/terraform-docs/terraform-docs) to keep the summary up-to-date.
29-
- Prerequisites and Dependencies: Mention any dependencies, required providers, or external resources.
30-
- Example Configurations: If applicable, include or link to example code snippets or a separate examples/ directory.
14+
6. [Renovate to Automate Dependency Updates](#renovate-to-automate-dependency-updates)
15+
7. [Frequently Asked Questions](#frequently-asked-questions)
3116

3217
## Structure
3318

@@ -132,11 +117,9 @@ The principles below apply to both root and child modules, unless otherwise spec
132117
133118
We’re particular about how we version providers and Terraform/OpenTofu in child and root modules. We recommend the following:
134119
135-
### Child Modules
120+
### [Child Modules](https://masterpoint.io/blog/terraform-opentofu-terminology-breakdown/#child-modules)
136121
137-
<!-- TODO: Update to link Child Modules to Terms blog post once live -->
138-
139-
Since child-modules are intended to be used many times throughout your or others code, it’s important to make it so that they create as little restrictions on the consuming root module as possible.
122+
Since [child-modules](https://masterpoint.io/blog/terraform-opentofu-terminology-breakdown/#child-modules) are intended to be used many times throughout your or others code, it’s important to make it so that they create as little restrictions on the consuming root module as possible.
140123
141124
This means you should:
142125
@@ -162,11 +145,9 @@ terraform {
162145

163146
In this example, the child module only demands a minimum version (Terraform or OpenTofu 1.3, Random provider 3.0), letting the root module run newer versions as they become available.
164147

165-
### Root Modules
166-
167-
<!-- TODO: Update to link Root Modules to Terms blog post once live -->
148+
### [Root Modules](https://masterpoint.io/blog/terraform-opentofu-terminology-breakdown/#root-modules)
168149

169-
Root modules are intended to be planned and applied and therefore they should be more prescriptive so that they’re called consistently in each case that you instantiate a new root module instance (i.e. create a new state file).
150+
[Root modules](https://masterpoint.io/blog/terraform-opentofu-terminology-breakdown/#root-modules) are intended to be planned and applied and therefore they should be more prescriptive so that they’re called consistently in each case that you instantiate a new root module instance (i.e. create a new state file).
170151

171152
To accomplish that, you should do the following:
172153

@@ -195,7 +176,7 @@ In this example two things are happening:
195176

196177
If you're more willing to use the bleeding edge of providers, you can always use the `~>` operator on the minor version like so `version = "~> 5.81"`. This will enable any new minor version updates and is essentially a shorthand for `>= 5.81.0 && < 6.0`. Be aware that providers do break and this has the possibility to frustrating bugs from providers to affect your project.
197178

198-
## Managing which TF binary is used per project
179+
## Managing which TF binary is used per project using Aqua
199180

200181
At Masterpoint, we're big fans of [Aqua](https://aquasecurity.github.io/aqua/) for managing which TF binary is used per project. This allows us to have a single TF binary that is used across our entire project, but still enables us to use root module specific TF versions if needed.
201182

@@ -229,6 +210,12 @@ As you can see, this is a LOT of checks that trunk is supporting for us and this
229210

230211
Check out our [.trunk/trunk.yaml](.trunk/trunk.yaml) file to see how we configure this and [check the trunk Code Quality getting started documentation](https://docs.trunk.io/code-quality) on how you can use this tool for your own project.
231212

213+
## Renovate to Automate Dependency Updates
214+
215+
We use [Renovate](https://github.com/apps/renovate) to automatically keep our Terraform and OpenTofu dependencies up-to-date through automated pull requests. This includes providers, modules, and even the Terraform/OpenTofu binaries themselves through Aqua.
216+
217+
This repository comes with configurations available at [.github/renovate.json5](.github/renovate.json5) that are configured to run on a weekly basis. Visit the [Renovate GitHub App page](https://github.com/apps/renovate) to install the app and configure it to run on your repository. There's also an [example tutorial that Renovate](https://github.com/renovatebot/tutorial) has to guide setting up Renovate.
218+
232219
## Frequently Asked Questions
233220

234221
### Why do you prefer DRY (Don't Repeat Yourself) Root Modules vs WET (Write Every Time) Root Modules?

aqua.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ registries:
1111
ref: v4.331.0 # renovate: depName=aquaproj/aqua-registry
1212
packages:
1313
- name: opentofu/opentofu@v1.9.0
14+
- name: terraform-docs/terraform-docs@v0.20.0

root-modules/template-root-module/README.md

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22

33
This is a template root module that creates a random pet resource. It can be easily copied and updated for other use-cases.
44

5+
## Using `tfvars` with [TF Workspaces](https://masterpoint.io/blog/terraform-opentofu-terminology-breakdown/#tf-cli-workspaces)
6+
7+
Terraform/OpenTofu workspaces are used with environment-specific tfvars files for separate TF state files to keep your configuration DRY. This allows you to maintain the same infrastructure code while using different variable values for different environments.
8+
9+
```bash
10+
# Set up a new workspace for `dev` -- only needs to be done once
11+
tofu workspace new dev
12+
# Select the workspace, this is how TF knows where the state file is stored
13+
tofu workspace select dev
14+
# Apply the configuration with the corresponding tfvars file
15+
tofu apply --var-file tfvars/dev.tfvars
16+
```
17+
18+
This approach ensures that:
19+
20+
- Your infrastructure code remains consistent across environments
21+
- Environment-specific configurations are isolated in tfvars files
22+
- State files are automatically separated by workspace
23+
- You can easily switch between environments
24+
525
<!-- README TEMPLATE: AFTER READING THE BELOW SECTION, DELETE THE BELOW SECTION AND REPLACE WITH YOUR OWN CONTENT -->
626
727
## Documentation Recommendations (DO NOT INCLUDE THIS INTO THE REAL README)
@@ -16,33 +36,36 @@ This is a template root module that creates a random pet resource. It can be eas
1636
1737
## Requirements
1838
19-
| Name | Version |
20-
| ------------------------------------------------------------------------ | ------- |
21-
| <a name="requirement_terraform"></a> [terraform](#requirement_terraform) | ~> 1.0 |
22-
| <a name="requirement_random"></a> [random](#requirement_random) | ~> 3.0 |
39+
| Name | Version |
40+
| --------- | -------- |
41+
| terraform | 1.10.3 |
42+
| random | ~> 3.7.2 |
2343
2444
## Providers
2545
2646
No providers.
2747
2848
## Modules
2949
30-
| Name | Source | Version |
31-
| ----------------------------------------------------------------- | ------------------------ | ------- |
32-
| <a name="module_random_pet"></a> [random_pet](#module_random_pet) | masterpointio/random/pet | 0.1.0 |
50+
| Name | Source | Version |
51+
| ---------- | ------------------------------ | ------- |
52+
| random_pet | ../../child-modules/random-pet | n/a |
3353
3454
## Resources
3555
3656
No resources.
3757
3858
## Inputs
3959
40-
| Name | Description | Type | Default | Required |
41-
| --------------------------------------------------- | ----------------------------- | -------- | ------- | :------: |
42-
| <a name="input_length"></a> [length](#input_length) | The length of the random name | `number` | `2` | no |
60+
| Name | Description | Type | Default | Required |
61+
| ------ | ------------------------------------------- | -------- | ---------- | :------: |
62+
| length | The length of the random name | `number` | `2` | no |
63+
| prefix | The prefix to prepend to the generated name | `string` | `"random"` | no |
4364
4465
## Outputs
4566
46-
No outputs.
67+
| Name | Description |
68+
| --------------- | ----------------------------- |
69+
| random_pet_name | The generated random pet name |
4770
4871
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

root-modules/template-root-module/main.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
locals {
2-
# Get the current timestamp and format it as YYYYMMDD
3-
prefix = formatdate("YYYYMMDD", timestamp())
2+
# Get the current timestamp and format it as YYYYMMDD, then concatenate with user-provided prefix
3+
prefix = "${var.prefix}-${formatdate("YYYYMMDD", timestamp())}"
44
}
55

66
module "random_pet" {
7-
source = "masterpointio/random/pet"
8-
version = "0.1.0"
7+
# If using a module from Terraform/OpenTofu Registry:
8+
# source = "masterpointio/random/pet"
9+
# version = "0.1.0"
10+
11+
# If using a module from a local directory:
12+
source = "../../child-modules/random-pet"
913

1014
length = var.length
1115
prefix = local.prefix
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "random_pet_name" {
2+
description = "The generated random pet name"
3+
value = module.random_pet.random_pet_name
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
prefix = "dev"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
prefix = "prod"

0 commit comments

Comments
 (0)