aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.env.template2
-rw-r--r--b2-backup/main.tf10
-rw-r--r--b2-backup/outputs.tf11
-rw-r--r--b2-backup/provider.tf22
-rwxr-xr-xbootstrap-state-bucket.sh1
-rwxr-xr-xsetup-env.sh25
6 files changed, 67 insertions, 4 deletions
diff --git a/.env.template b/.env.template
index 91e9082..628de4b 100644
--- a/.env.template
+++ b/.env.template
@@ -1,3 +1,5 @@
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
+export B2_APPLICATION_KEY=
+export B2_APPLICATION_KEY_ID=
export HCLOUD_TOKEN=
diff --git a/b2-backup/main.tf b/b2-backup/main.tf
new file mode 100644
index 0000000..ccabfd7
--- /dev/null
+++ b/b2-backup/main.tf
@@ -0,0 +1,10 @@
+resource "b2_bucket" "host_backups" {
+ bucket_name = "eugenetriguba-com-host-backups"
+ bucket_type = "allPrivate"
+}
+
+resource "b2_application_key" "eugenetriguba_com_key" {
+ key_name = "eugenetriguba-com-host-key"
+ bucket_ids = [b2_bucket.host_backups.bucket_id]
+ capabilities = ["listBuckets", "listFiles", "readFiles", "writeFiles", "deleteFiles"]
+}
diff --git a/b2-backup/outputs.tf b/b2-backup/outputs.tf
new file mode 100644
index 0000000..942b0ab
--- /dev/null
+++ b/b2-backup/outputs.tf
@@ -0,0 +1,11 @@
+output "eugenetriguba_com_key_id" {
+ description = "Application Key ID"
+ value = b2_application_key.eugenetriguba_com_key.application_key_id
+ sensitive = true
+}
+
+output "eugenetriguba_com_key" {
+ description = "Application Key"
+ value = b2_application_key.eugenetriguba_com_key.application_key
+ sensitive = true
+}
diff --git a/b2-backup/provider.tf b/b2-backup/provider.tf
new file mode 100644
index 0000000..cc7ab60
--- /dev/null
+++ b/b2-backup/provider.tf
@@ -0,0 +1,22 @@
+terraform {
+ required_version = ">= 1.0.0"
+ required_providers {
+ b2 = {
+ source = "Backblaze/b2"
+ version = "~> 0.14.0"
+ }
+ }
+
+ backend "s3" {
+ bucket = "eugenetriguba-terraform-state"
+ key = "eugenetriguba.com/b2-backup/terraform.tfstate"
+ region = "us-west-004"
+ endpoint = "https://s3.us-west-004.backblazeb2.com"
+
+ skip_credentials_validation = true
+ skip_region_validation = true
+ skip_metadata_api_check = true
+ skip_requesting_account_id = true
+ skip_s3_checksum = true
+ }
+}
diff --git a/bootstrap-state-bucket.sh b/bootstrap-state-bucket.sh
index 02cf099..a72df4d 100755
--- a/bootstrap-state-bucket.sh
+++ b/bootstrap-state-bucket.sh
@@ -19,6 +19,7 @@
# USAGE:
# ./bootstrap-state-bucket.sh
set -o errexit
+set -o nounset
KEY=terraform-state-key
BUCKET=eugenetriguba-terraform-state
diff --git a/setup-env.sh b/setup-env.sh
index 2bb57f3..6659d39 100755
--- a/setup-env.sh
+++ b/setup-env.sh
@@ -13,16 +13,33 @@
#
# USAGE:
# ./bootstrap-state-bucket.sh
+# # Next, manually: Setup a "Backblaze B2 Master Key" key entry in 1Password
+# # before you run setup-env.sh. That is needed if you want to apply the
+# # b2-backup/ workspace.
# ./setup-env.sh
# source .env
set -o errexit
+set -o nounset
-OP_ITEM="Backblaze B2 Terraform State Key"
+OP_TF_STATE_ITEM="Backblaze B2 Terraform State Key"
+OP_MASTER_KEY_ITEM="Backblaze B2 Master Key"
main() {
- KEY_ID=$(get_op_label_value "$OP_ITEM" "key_id")
- KEY_VALUE=$(get_op_label_value "$OP_ITEM" "key_value")
- printf "export AWS_ACCESS_KEY_ID=%s\nexport AWS_SECRET_ACCESS_KEY=%s\n" "$KEY_ID" "$KEY_VALUE" >>.env
+ TF_STATE_KEY_ID=$(get_op_label_value "$OP_TF_STATE_ITEM" "key_id")
+ TF_STATE_KEY_VALUE=$(get_op_label_value "$OP_TF_STATE_ITEM" "key_value")
+ # NOTE: Terraform uses the S3 compatible B2 API, so we need the Backblaze
+ # credentials setup with 'AWS' environment variables.
+ printf \
+ "export AWS_ACCESS_KEY_ID=%s\nexport AWS_SECRET_ACCESS_KEY=%s\n" \
+ "$TF_STATE_KEY_ID" \
+ "$TF_STATE_KEY_VALUE" \
+ >>.env
+
+ OP_MASTER_KEY_ID=$(get_op_label_value "$OP_MASTER_KEY_ITEM" "key_id")
+ OP_MASTER_KEY_VALUE=$(get_op_label_value "$OP_MASTER_KEY_ITEM" "key_value")
+ # However, when communicating with B2 directly e.g. applying the b2-backup/
+ # workspace, we need the B2 specific environment variables.
+ printf "export B2_APPLICATION_KEY_ID=%s\nexport B2_APPLICATION_KEY=%s\n" "$OP_MASTER_KEY_ID" "$OP_MASTER_KEY_VALUE" >>.env
}
get_op_label_value() {