Skip to content

Commit

Permalink
Merge pull request #33 from Space48/make-gcp-bucket-configurable
Browse files Browse the repository at this point in the history
Make GCP backend state bucket configurable, If bucket not provided then use local state
  • Loading branch information
S48-Mo authored Aug 27, 2021
2 parents 08585ca + bd4123d commit 5365d05
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
2 changes: 2 additions & 0 deletions build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type BuildOpts = {
region: string;
debug: boolean;
environment: string;
backend?: string;
};

export default (options: BuildOpts) => {
Expand All @@ -30,6 +31,7 @@ export default (options: BuildOpts) => {
functionsDir: functionsOutDir,
environment: options.environment,
region: options.region,
backendBucket: options.backend,
});
app.synth();

Expand Down
4 changes: 4 additions & 0 deletions cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const cmdBuild: cliCommand = (argv) => {
"--region": String,
"--env": String,
"--out-dir": String,
"--backend": String,

// Aliases
"-h": "--help",
Expand All @@ -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
Expand All @@ -61,12 +63,14 @@ 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,
project: (args["--project"] as unknown) as string,
region,
debug: !!args["--debug"],
environment,
backend,
});
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
19 changes: 12 additions & 7 deletions stacks/gcp/GcpStack.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand All @@ -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", {
Expand Down
2 changes: 1 addition & 1 deletion stacks/gcp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type StackOptions = {
functionsDir: string;
environment: string;
region: string;
backendBucket: string;
backendBucket?: string;
backendPrefix?: string;
};

Expand Down

0 comments on commit 5365d05

Please sign in to comment.