diff --git a/build/index.ts b/build/index.ts index 6fc0418..9bce0ef 100644 --- a/build/index.ts +++ b/build/index.ts @@ -11,6 +11,7 @@ export type BuildOpts = { region: string; debug: boolean; environment: string; + backend?: string; }; export default (options: BuildOpts) => { @@ -30,6 +31,7 @@ export default (options: BuildOpts) => { functionsDir: functionsOutDir, environment: options.environment, region: options.region, + backendBucket: options.backend, }); app.synth(); diff --git a/cli/build.ts b/cli/build.ts index ffed0fe..92e666a 100644 --- a/cli/build.ts +++ b/cli/build.ts @@ -14,6 +14,7 @@ export const cmdBuild: cliCommand = (argv) => { "--region": String, "--env": String, "--out-dir": String, + "--backend": String, // Aliases "-h": "--help", @@ -40,6 +41,7 @@ export const cmdBuild: cliCommand = (argv) => { --region=[region] Set a valid region (defaults to europe-west2 for GCP) --env=[env] Set an environment eg: production, staging, dev, uat --out-dir=[dir] Set the dir for the build files + --backend=[path] Path to a remote backend Terraform state --help, -h Displays this message --debug, -d Outputs debug logging For more information run a command with the --help flag @@ -61,6 +63,7 @@ export const cmdBuild: cliCommand = (argv) => { const outDir = args["--out-dir"] ?? "./.build"; const region = args["--region"] ?? "europe-west2"; const environment = args["--env"] ?? "dev"; + const backend = args["--backend"]; return build({ dir, outDir, @@ -68,5 +71,6 @@ export const cmdBuild: cliCommand = (argv) => { region, debug: !!args["--debug"], environment, + backend, }); }; diff --git a/package-lock.json b/package-lock.json index cfca11f..42b5bd7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@space48/cloud-seed", - "version": "0.2.13", + "version": "0.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f67008f..9384d90 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@space48/cloud-seed", - "version": "0.2.13", + "version": "0.3.0", "description": "Cloud infrastructure automation tool that uses Terraform CDK.", "main": "dist/index.js", "bin": { diff --git a/stacks/gcp/GcpStack.ts b/stacks/gcp/GcpStack.ts index db90634..8673a20 100644 --- a/stacks/gcp/GcpStack.ts +++ b/stacks/gcp/GcpStack.ts @@ -1,6 +1,6 @@ import fs from "fs"; import { Construct } from "constructs"; -import { GcsBackend, TerraformStack } from "cdktf"; +import { GcsBackend, LocalBackend, TerraformStack } from "cdktf"; import { CloudfunctionsFunction, CloudfunctionsFunctionIamMember, @@ -25,7 +25,6 @@ const defaultStackOptions: StackOptions = { functionsDir: ".build/functions", environment: "dev", region: "europe-west2", - backendBucket: "s48-terraform-state", }; export default class GcpStack extends TerraformStack { @@ -41,11 +40,17 @@ export default class GcpStack extends TerraformStack { ...options, }; - // Configure the remote backend where state will be stored. - new GcsBackend(this, { - bucket: this.options.backendBucket, - prefix: this.options.backendPrefix, - }); + // If bucket is defined, + // then configure the remote backend where state will be stored, + // else use a local backend. + this.options.backendBucket?.length + ? new GcsBackend(this, { + bucket: this.options.backendBucket, + prefix: this.options.backendPrefix, + }) + : new LocalBackend(this, { + path: path.join(this.options.functionsDir + "../"), + }); // Configure the Google Provider. new GoogleProvider(this, "GoogleAuth", { diff --git a/stacks/gcp/types.ts b/stacks/gcp/types.ts index bf0a186..2141acc 100644 --- a/stacks/gcp/types.ts +++ b/stacks/gcp/types.ts @@ -4,7 +4,7 @@ export type StackOptions = { functionsDir: string; environment: string; region: string; - backendBucket: string; + backendBucket?: string; backendPrefix?: string; };