@groton/terraform 0.0.0-stage → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,59 @@
1
- # Temporary Holding Version
1
+ # @groton/terraform
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Common Terraform modules and scripts
4
+
5
+ ## Requires
6
+
7
+ The macOS-flavored set of requirements (your mileage may vary):
8
+
9
+ ```sh
10
+ brew install terraform
11
+ brew install gcloud-cli
12
+ ```
13
+
14
+ ## Install
15
+
16
+ ```sh
17
+ pnpm install -D @groton/terraform
18
+ pnpm setup-terraform
19
+ ```
20
+
21
+ ## Use
22
+
23
+ This repo provides a number of re-usable Terraform modules in the [`modules`](./modules/) directory. By convention, these can be added to a Terraform declaration by URL:
24
+
25
+ ```hcl
26
+ module "apis" {
27
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/apis?ref=v2.0.1"
28
+ project_id = var.project_id
29
+ required_apis = [
30
+ module.database.required_apis,
31
+ ]
32
+ }
33
+
34
+ module "database" {
35
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/database?ref=v2.0.1"
36
+ project_id = var.project_id
37
+ database_id = var.database_id
38
+ required_apis = module.apis.dependency_ids
39
+ }
40
+ ```
41
+
42
+ In addition there are some convenience scripts that the `setup-terraform` script references when configuring package scripts:
43
+
44
+ | Script | Use |
45
+ | --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
46
+ | `setup-terraform` | Sets up the local project deploy to Google Cloud using a Terraform `infra` directory and `turbo` monorepo pruning. Invoke with `--help` or `-h` for all options. |
47
+ | `swap-gitignore` | Swaps between a [default `.gitignore`](./bin/gitignore/commit.gitignore) value for managing a GitHub repository and a [`.gitignore` that is tailored more specifically to `turbo prune`](./bin/gitignore/prune.gitignore). |
48
+ | `require-gcloud-auth` | `gcloud` is required for Terraform artifact management using [`modules/artifacts`](./modules/artifacts/). This script ensures that `gcloud` is authorized. |
49
+ | `op-terraform` | Requires the 1Password CLI tool `op`. Wraps `terraform init` and `terraform apply` with some useful flags and loads environment variables from the local `.env` file and 1Password. |
50
+
51
+ ## Templates
52
+
53
+ ### `default`
54
+
55
+ A basic Google Cloud Run service with a runner service account, deployed from the local monorepo in a project in `/app`.
56
+
57
+ ### `veracross-delegate`
58
+
59
+ A [Next.js](https://nextjs.org/) app in `/app` with Veracross SSO using [Auth.js](https://authjs.dev/) and a delegated Veracross service account user whose credentials and refresh token are stored in Google Cloud Secret Manager and managed by a service in `/services/tokens` using [@delegated-token/veracross](https://www.npmjs.com/package/@delegated-token/veracross)
@@ -0,0 +1,11 @@
1
+ .env
2
+ !.env.example
3
+ **/.terraform/
4
+ **/.turbo/
5
+ **/build/
6
+ **/dist/
7
+ **/node_modules/
8
+ *.log
9
+ *.tfstate
10
+ *.tfstate.*
11
+ /out/
@@ -0,0 +1,12 @@
1
+ .*
2
+ !.gcloudignore
3
+ *.log
4
+ *.md
5
+ *.tfstate
6
+ *.tfstate.*
7
+ **/build/
8
+ **/dist/
9
+ **/eslint.config.mjs
10
+ **/node_modules/
11
+ /out/
12
+ /scripts/
@@ -0,0 +1,24 @@
1
+ #!/bin/sh
2
+
3
+ source "$PWD/.env"
4
+ OP_TOKEN=$(op item get --account="$OP_ACCOUNT" --field=credential --reveal $OP_ITEM)
5
+
6
+ COMMAND=""
7
+ case $1 in
8
+ init)
9
+ COMMAND="init -upgrade"
10
+ ;;
11
+ apply)
12
+ COMMAND="apply -input=false -auto-approve"
13
+ ;;
14
+ *)
15
+ exit
16
+ ;;
17
+ esac
18
+
19
+ op run \
20
+ --env-file=.env \
21
+ --account="$OP_ACCOUNT" \
22
+ --no-masking \
23
+ -- \
24
+ terraform -chdir=./infra $COMMAND
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+
3
+ gcloud auth print-access-token >/dev/null 2>&1 || \
4
+ gcloud auth login
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env node
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { Colors } from '@qui-cli/colors';
5
+ import { Core, Positionals } from '@qui-cli/core';
6
+ import { Init } from '@qui-cli/init';
7
+ import { register } from '@qui-cli/plugin';
8
+
9
+ Init.configure({
10
+ enclosingDirectory: false,
11
+ template: path.resolve(import.meta.dirname, '../template')
12
+ });
13
+
14
+ Positionals.requireAtLeast(0);
15
+
16
+ const templates = fs.readdirSync(
17
+ path.join(import.meta.dirname, '../templates')
18
+ );
19
+
20
+ await register({
21
+ name: 'setup-terraform',
22
+ options() {
23
+ return {
24
+ opt: {
25
+ template: {
26
+ description: `Select a particular template`,
27
+ hint: templates.map((t) => Colors.quotedValue(`"${t}"`)).join('|'),
28
+ short: 't',
29
+ default: 'default'
30
+ }
31
+ }
32
+ };
33
+ },
34
+ init({ values: { template } }) {
35
+ const dir = path.resolve(import.meta.dirname, '../templates', template);
36
+ if (!fs.existsSync(dir)) {
37
+ throw new Error(`Invalid template ${Colors.value(template)}`);
38
+ }
39
+ Init.configure({
40
+ template: dir
41
+ });
42
+ }
43
+ });
44
+
45
+ await Core.run();
@@ -0,0 +1,3 @@
1
+ #!/bin/sh
2
+
3
+ cp "$(dirname $0)/gitignore/$1.gitignore" "$PWD/.gitignore"
package/package.json CHANGED
@@ -1,6 +1,32 @@
1
1
  {
2
2
  "name": "@groton/terraform",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "2.0.1",
4
+ "type": "module",
5
+ "directories": {
6
+ "bin": "./bin"
7
+ },
8
+ "dependencies": {
9
+ "@qui-cli/colors": "^3.2.5",
10
+ "@qui-cli/core": "^7.1.0",
11
+ "@qui-cli/init": "^2.0.1",
12
+ "@qui-cli/plugin": "^4.2.0"
13
+ },
14
+ "devDependencies": {
15
+ "@battis/commit": "^0.0.12",
16
+ "@battis/eslint": "^3.0.9",
17
+ "@battis/monorepo": "^1.1.0",
18
+ "@battis/prettier": "^2.0.11",
19
+ "@battis/workspace": "^0.0.12",
20
+ "npm-run-all": "^4.1.5"
21
+ },
22
+ "peerDependencies": {
23
+ "turbo": "^2.11.13"
24
+ },
25
+ "scripts": {
26
+ "release": "run-s release:*",
27
+ "release:changelog": "commit-and-tag-version --noBumpWhenEmptyChanges",
28
+ "release:git": "git push --follow-tags",
29
+ "release:require-pnpm-auth": "require-pnpm-auth groton-it",
30
+ "release:publish": "pnpm publish --access public"
31
+ }
6
32
  }
@@ -0,0 +1,81 @@
1
+ terraform {
2
+ required_providers {
3
+ google = {
4
+ source = "hashicorp/google"
5
+ version = "8.12"
6
+ }
7
+ }
8
+ }
9
+
10
+ provider "google" {
11
+ project = var.project_id
12
+ region = var.region
13
+ }
14
+
15
+ locals {
16
+ source_path = "${path.root}/../"
17
+ }
18
+
19
+
20
+ data "google_project" "project" {
21
+ project_id = var.project_id
22
+ }
23
+
24
+ module "apis" {
25
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/apis?ref=v2.0.1"
26
+
27
+ project_id = var.project_id
28
+ required_apis = [
29
+ module.artifacts.required_apis,
30
+ module.runners.required_apis,
31
+ module.app.required_apis
32
+ ]
33
+ }
34
+
35
+ module "artifacts" {
36
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/artifacts?ref=v2.0.1"
37
+
38
+ project_id = data.google_project.project.project_id
39
+ region = var.region
40
+ source_path = local.source_path
41
+ services = { app = "." }
42
+ enabled_apis = module.apis.enabled_apis
43
+ }
44
+
45
+ locals {
46
+ service_names = {
47
+ app = "app"
48
+ }
49
+ }
50
+
51
+ module "runners" {
52
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/runners?ref=v2.0.1"
53
+
54
+ project_id = data.google_project.project.project_id
55
+ services = [local.service_names.app]
56
+ enabled_apis = module.apis.enabled_apis
57
+ }
58
+
59
+ locals {
60
+ service_uris = {
61
+ app = coalesce(
62
+ var.app_url,
63
+ "https://${local.service_names.app}-${data.google_project.project.number}.${var.region}.run.app"
64
+ )
65
+ }
66
+ }
67
+
68
+ module "app" {
69
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/service?ref=v2.0.1"
70
+
71
+ project_id = data.google_project.project.project_id
72
+ region = var.region
73
+ name = local.app
74
+ image = module.artifacts.uris.app
75
+ service_account = module.runners.emails.app
76
+ env_vars = {
77
+ "DEBUG" = var.debug
78
+ }
79
+ public = true
80
+ enabled_apis = module.apis.enabled_apis
81
+ }
@@ -0,0 +1,6 @@
1
+ output "service_uris" {
2
+ description = "URIs for the individual services"
3
+ value = {
4
+ app = module.app.uri
5
+ }
6
+ }
@@ -0,0 +1,18 @@
1
+ variable "project_id" {
2
+ type = string
3
+ }
4
+
5
+ variable "region" {
6
+ type = string
7
+ default = "us-east4"
8
+ }
9
+
10
+ variable "app_url" {
11
+ type = string
12
+ default = null
13
+ }
14
+
15
+ variable "debug" {
16
+ type = string
17
+ default = "false"
18
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "scripts": {
3
+ "build": "turbo build",
4
+ "deploy": "run-s deploy:*",
5
+ "deploy:build": "run-s build",
6
+ "deploy:clean": "del ./out",
7
+ "deploy:prepare-prune": "swap-gitignore prune",
8
+ "deploy:prune": "turbo prune app --out-dir ./out/app",
9
+ "deploy:gcloudignore": "cp ./.gcloudignore ./out/app/",
10
+ "deploy:cleanup-prune": "swap-gitignore commit",
11
+ "deploy:require-gcloud-auth": "require-gcloud-auth",
12
+ "deploy:terraform": "op-terraform apply",
13
+ "gcp-build": "turbo build",
14
+ "start": "turbo start --env-mode=loose",
15
+ "release": "turbo --concurrency 1 release"
16
+ },
17
+ "devDependencies": {
18
+ "npm-run-all": "^4.1.5"
19
+ }
20
+ }
@@ -0,0 +1,2 @@
1
+ publicHoistPattern:
2
+ - turbo
@@ -0,0 +1,23 @@
1
+ {
2
+ "$schema": "https://turborepo.dev/schema.json",
3
+ "tasks": {
4
+ "build": {
5
+ "dependsOn": ["^build"],
6
+ "outputs": [
7
+ ".next/**",
8
+ "!.next/cache/**",
9
+ "!.next/dev/**",
10
+ "dist",
11
+ "build"
12
+ ]
13
+ },
14
+ "start": {
15
+ "cache": false,
16
+ "persistent": true
17
+ },
18
+ "release": {
19
+ "dependsOn": ["^release"],
20
+ "outputs": ["package.json", "CHANGELOG.md"]
21
+ }
22
+ }
23
+ }
@@ -0,0 +1,147 @@
1
+ terraform {
2
+ required_providers {
3
+ google = {
4
+ source = "hashicorp/google"
5
+ version = "8.12"
6
+ }
7
+ }
8
+ }
9
+
10
+ provider "google" {
11
+ project = var.project_id
12
+ region = var.region
13
+ }
14
+
15
+ locals {
16
+ services = {
17
+ app = "app"
18
+ tokens = "tokens"
19
+ }
20
+ source_path = "${path.root}/../${var.source_path}"
21
+ }
22
+
23
+ data "google_project" "project" {
24
+ project_id = var.project_id
25
+ }
26
+
27
+ module "apis" {
28
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/apis?ref=v2.0.1"
29
+
30
+ project_id = var.project_id
31
+ required_apis = [
32
+ module.artifacts.required_apis,
33
+ module.runners.required_apis,
34
+ module.database.required_apis,
35
+ module.tokens.required_apis,
36
+ module.app.required_apis
37
+ ]
38
+ }
39
+
40
+ module "artifacts" {
41
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/artifacts?ref=v2.0.1"
42
+
43
+ project_id = data.google_project.project.project_id
44
+ region = module.database.region
45
+ source_path = local.source_path
46
+ services = local.services
47
+ enabled_apis = module.apis.enabled_apis
48
+ }
49
+
50
+ module "runners" {
51
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/runners?ref=v2.0.1"
52
+
53
+ project_id = data.google_project.project.project_id
54
+ services = keys(local.services)
55
+ enabled_apis = module.apis.enabled_apis
56
+ }
57
+
58
+ module "database" {
59
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/database?ref=v2.0.1"
60
+
61
+ project_id = data.google_project.project.project_id
62
+ region = var.region
63
+ database_id = var.database_id
64
+ accessors = {
65
+ app = module.runners.emails.app
66
+ }
67
+ ttl = {
68
+ "sessions" = "expires"
69
+ "accessTokens" = "expiresAt"
70
+ "idTokens" = "exp"
71
+ }
72
+ enabled_apis = module.apis.enabled_apis
73
+ }
74
+
75
+ locals {
76
+ service_uris = {
77
+ app = coalesce(
78
+ var.app_url,
79
+ "https://${local.services.app}-${data.google_project.project.number}.${module.database.region}.run.app"
80
+ )
81
+ tokens = coalesce(
82
+ var.tokens_url,
83
+ "https://${local.services.tokens}-${data.google_project.project.number}.${module.database.region}.run.app"
84
+ )
85
+ }
86
+ }
87
+
88
+ module "tokens" {
89
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/service?ref=v2.0.1"
90
+
91
+ project_id = data.google_project.project.project_id
92
+ region = module.database.region
93
+ name = "tokens"
94
+ image = module.artifacts.uris.tokens
95
+ service_account = module.runners.emails.tokens
96
+ secrets_read_only = {
97
+ "VC_SVC_CREDENTIALS" = <<-EOT
98
+ {
99
+ "school_route": "groton",
100
+ "client_id": ""
101
+ "client_secret": ""
102
+ "redirect_uri": "${local.service_uris.app}/api/auth/delegate/redirect"
103
+ }
104
+ EOT
105
+ }
106
+ secrets_read_write = {
107
+ "VC_SVC_TOKEN" = "Will be replaced when app is authorized"
108
+ }
109
+ env_vars = {
110
+ "GOOGLE_CLOUD_PROJECT" = data.google_project.project.project_id
111
+ "PUBLIC_URL" = local.service_uris.tokens
112
+ "DEBUG" = var.debug
113
+ }
114
+ min_instance_count = 1
115
+ max_instance_count = 1
116
+ invokers = { app = module.runners.emails.app }
117
+ public = false
118
+ enabled_apis = module.apis.enabled_apis
119
+ }
120
+
121
+ module "app" {
122
+ source = "git::ssh://git@github.com/groton-school/terraform.git//modules/service?ref=v2.0.1"
123
+
124
+ project_id = data.google_project.project.project_id
125
+ region = module.database.region
126
+ name = local.services.app
127
+ image = module.artifacts.uris.app
128
+ min_instance_count = 1
129
+ service_account = module.runners.emails.app
130
+ secrets_read_only = {
131
+ "AUTH_SECRET" = "pnpx auth secret"
132
+ "AUTH_VERACROSS_ID" = "From Veracross OAuth App"
133
+ "AUTH_VERACROSS_SECRET" = "From Veracross OAuth App"
134
+ }
135
+ env_vars = {
136
+ "DATABASE_ID" = module.database.database_id
137
+ "VC_SVC_CONSUMER_URL" = local.service_uris.app
138
+ "VC_SVC_PROVIDER_URL" = module.tokens.uri
139
+ "VC_SVC_ROUTE" = "groton"
140
+ "AUTH_TRUST_HOST" = "true"
141
+ "AUTH_URL" = local.service_uris.app
142
+ "AUTH_VERACROSS_ROUTE" = "groton"
143
+ "DEBUG" = var.debug
144
+ }
145
+ public = true
146
+ enabled_apis = module.apis.enabled_apis
147
+ }
@@ -0,0 +1,27 @@
1
+ output "service_uris" {
2
+ description = "URIs for the individual services"
3
+ value = {
4
+ app = module.app.uri
5
+ tokens = module.tokens.uri
6
+
7
+ }
8
+ }
9
+
10
+ output "secret_configuration" {
11
+ description = "Google Cloud Console URL to configure secret values"
12
+ value = "https://console.cloud.google.com/security/secret-manager/secrets?project=${data.google_project.project.project_id}"
13
+ }
14
+
15
+
16
+ output "oauth_redirect_uri" {
17
+ description = "OAuth 2.0 redirect_uri values to register with Veracross"
18
+ value = [
19
+ "${module.app.uri}/api/auth/callback/veracross",
20
+ "${module.app.uri}/api/auth/delegate/redirect"
21
+ ]
22
+ }
23
+
24
+ output "authorize_url" {
25
+ description = "URL to authorize delegated service account Data API access"
26
+ value = "${module.app.uri}/api/auth/delegate/authorize"
27
+ }
@@ -0,0 +1,39 @@
1
+ variable "project_id" {
2
+ type = string
3
+ }
4
+
5
+ variable "source_path" {
6
+ type = string
7
+ default = "out"
8
+ }
9
+
10
+ variable "region" {
11
+ type = string
12
+ default = "us-east4"
13
+ }
14
+
15
+ variable "database_id" {
16
+ # does not default to "(default)" to avoid unnecessary appengine depenency
17
+ type = string
18
+ default = "app-db"
19
+ }
20
+
21
+ variable "school_route" {
22
+ type = string
23
+ default = "groton"
24
+ }
25
+
26
+ variable "app_url" {
27
+ type = string
28
+ default = null
29
+ }
30
+
31
+ variable "tokens_url" {
32
+ type = string
33
+ default = null
34
+ }
35
+
36
+ variable "debug" {
37
+ type = string
38
+ default = "false"
39
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "scripts": {
3
+ "build": "turbo build",
4
+ "deploy": "run-s deploy:*",
5
+ "deploy:build": "run-s build",
6
+ "deploy:clean": "del ./out",
7
+ "deploy:prepare-prune": "swap-gitignore prune",
8
+ "deploy:prune": "run-p services:*",
9
+ "services:tokens": "SERVICE=\"tokens\" run-s prune:*",
10
+ "services:app": "SERVICE=\"app\" run-s prune:*",
11
+ "prune:turbo": "turbo prune $SERVICE --out-dir ./out/$SERVICE",
12
+ "prune:gcloudignore": "cp ./.gcloudignore ./out/$SERVICE/",
13
+ "deploy:cleanup-prune": "swap-gitignore commit",
14
+ "deploy:require-gcloud-auth": "require-gcloud-auth",
15
+ "deploy:terraform": "op-terraform apply",
16
+ "gcp-build": "turbo build",
17
+ "start": "turbo start --env-mode=loose",
18
+ "release": "turbo --concurrency 1 release"
19
+ },
20
+ "devDependencies": {
21
+ "npm-run-all": "^4.1.5"
22
+ }
23
+ }
@@ -0,0 +1,2 @@
1
+ publicHoistPattern:
2
+ - turbo
@@ -0,0 +1,23 @@
1
+ {
2
+ "$schema": "https://turborepo.dev/schema.json",
3
+ "tasks": {
4
+ "build": {
5
+ "dependsOn": ["^build"],
6
+ "outputs": [
7
+ ".next/**",
8
+ "!.next/cache/**",
9
+ "!.next/dev/**",
10
+ "dist",
11
+ "build"
12
+ ]
13
+ },
14
+ "start": {
15
+ "cache": false,
16
+ "persistent": true
17
+ },
18
+ "release": {
19
+ "dependsOn": ["^release"],
20
+ "outputs": ["package.json", "CHANGELOG.md"]
21
+ }
22
+ }
23
+ }