diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2026-10-12 18:11:21 -0700 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2026-10-12 18:11:21 -0700 |
| commit | f6a43b7479fad02d3163da52f55f5feb3f78d9c9 (patch) | |
| tree | de25a7ee7b774da12fd79024caf586e3d9746c7a /bootstrap-state-bucket.sh | |
| parent | Add license (diff) | |
| download | site-infra-f6a43b7479fad02d3163da52f55f5feb3f78d9c9.zip | |
Migrate terraform state into backblaze b2 bucket
Diffstat (limited to 'bootstrap-state-bucket.sh')
| -rwxr-xr-x | bootstrap-state-bucket.sh | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/bootstrap-state-bucket.sh b/bootstrap-state-bucket.sh new file mode 100755 index 0000000..02cf099 --- /dev/null +++ b/bootstrap-state-bucket.sh @@ -0,0 +1,56 @@ +#!/bin/sh +# +# DESCRIPTION: +# bootstrap-state-bucket sets up a terraform state bucket that is used by the +# rest of the terraform modules, and creates a secure note in 1Password for +# the credentials of the state key. This state is kept in backblaze's b2 +# service, which provides object storage 'buckets' similar to Amazon's S3. +# +# PREREQUISITES: +# - `b2` (backblaze CLI) +# - `op` (1password CLI) +# - `jq` +# - `awk` +# +# The `b2` and `op` CLI must both already be authenticated. +# - `b2 account auth` +# - `op signin` +# +# USAGE: +# ./bootstrap-state-bucket.sh +set -o errexit + +KEY=terraform-state-key +BUCKET=eugenetriguba-terraform-state +CAPS=listBuckets,listFiles,readFiles,writeFiles,deleteFiles +OP_VAULT=Private + +main() { + if ! b2 bucket get $BUCKET >/dev/null 2>&1; then + echo "$BUCKET does not exist, creating.." + b2 bucket create $BUCKET allPrivate >/dev/null + else + echo "$BUCKET exists." + fi + + if ! b2 key list | grep --quiet $KEY; then + echo "$KEY does not exist, creating.." + + OUT=$(b2 key create $KEY $CAPS --bucket "$BUCKET") + KEY_ID=$(echo "$OUT" | awk '{ print $1 }') + KEY_VAL=$(echo "$OUT" | awk '{ print $2 }') + + OP_ITEM_TITLE="Backblaze B2 Terraform State Key" + op item template get 'Secure Note' | op item create \ + --title "$OP_ITEM_TITLE" \ + --vault $OP_VAULT \ + key_id="$KEY_ID" \ + key_value="$KEY_VAL" >/dev/null + + echo "Created $KEY in B2 and $OP_ITEM_TITLE in 1Password" + else + echo "$KEY exists." + fi +} + +main |