|
| 1 | +# Reusable Terraform Patterns |
| 2 | + |
| 3 | +This document describes common patterns used across the Terraform modules in this repository. |
| 4 | + |
| 5 | +## 1Password Integration Pattern |
| 6 | + |
| 7 | +All modules follow a consistent pattern for retrieving secrets from 1Password: |
| 8 | + |
| 9 | +```hcl |
| 10 | +provider "onepassword" { |
| 11 | + connect_url = var.OP_CONNECT_HOST |
| 12 | + connect_token = var.OP_CONNECT_TOKEN |
| 13 | +} |
| 14 | +
|
| 15 | +data "onepassword_vault" "kubernetes" { |
| 16 | + name = "Kubernetes" |
| 17 | +} |
| 18 | +
|
| 19 | +module "onepassword_<service>" { |
| 20 | + source = "github.com/joryirving/terraform-1password-item" |
| 21 | + vault = data.onepassword_vault.kubernetes.name |
| 22 | + item = "<service_name>" |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +## Backend Configuration Pattern |
| 27 | + |
| 28 | +All modules use a consistent S3-compatible backend configuration: |
| 29 | + |
| 30 | +```hcl |
| 31 | +terraform { |
| 32 | + backend "s3" { |
| 33 | + bucket = "terraform-state" |
| 34 | + key = "<module_name>/<module_name>.tfstate" |
| 35 | + region = "ca-west-1" |
| 36 | +
|
| 37 | + endpoints = { |
| 38 | + s3 = "https://s3.jory.dev" |
| 39 | + } |
| 40 | +
|
| 41 | + skip_credentials_validation = true |
| 42 | + skip_requesting_account_id = true |
| 43 | + skip_metadata_api_check = true |
| 44 | + skip_region_validation = true |
| 45 | + use_path_style = true |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Provider Declaration Pattern |
| 51 | + |
| 52 | +All modules declare providers with version constraints and configure them to use 1Password for secrets: |
| 53 | + |
| 54 | +```hcl |
| 55 | +terraform { |
| 56 | + required_providers { |
| 57 | + <provider_name> = { |
| 58 | + source = "<provider_source>" |
| 59 | + version = "<version_constraint>" |
| 60 | + } |
| 61 | +
|
| 62 | + onepassword = { |
| 63 | + source = "1password/onepassword" |
| 64 | + version = "3.1.2" |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## Module Structure |
| 71 | + |
| 72 | +Each module follows a consistent file structure: |
| 73 | + |
| 74 | +- `main.tofu` - Provider declarations and required providers |
| 75 | +- `variables.tofu` - Input variables with descriptions |
| 76 | +- `outputs.tofu` - Output values (if applicable) |
| 77 | +- `backend.tofu` - Backend configuration |
| 78 | +- Other files grouped by resource type or functionality |
| 79 | + |
| 80 | +## Secret Management Pattern |
| 81 | + |
| 82 | +All sensitive values are retrieved from 1Password and never hardcoded in the configuration files. The pattern ensures secrets are handled securely while maintaining configuration flexibility. |
0 commit comments