|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# ============================================================================= |
| 5 | +# Terraform Backend Configuration Script |
| 6 | +# ============================================================================= |
| 7 | +# This script generates the backend configuration for Terraform state storage. |
| 8 | +# State files are stored in cloud provider buckets for collaboration and durability. |
| 9 | +# |
| 10 | +# IMPORTANT: State files are NEVER deleted by this script or workflows. |
| 11 | +# Only the backend-config.tf file is regenerated to ensure correct configuration. |
| 12 | +# |
| 13 | +# Usage: ./configure-backend.sh <cloud_provider> <component> |
| 14 | +# cloud_provider: gke, eks, aks, or generic |
| 15 | +# component: cert-manager, ingress-controller, lgtm-stack, argocd-agent, etc. |
| 16 | +# |
| 17 | +# Environment Variables Required: |
| 18 | +# GKE: TF_STATE_BUCKET (GCS bucket name) |
| 19 | +# EKS: TF_STATE_BUCKET (S3 bucket name), AWS_REGION |
| 20 | +# AKS: AZURE_STORAGE_ACCOUNT, AZURE_STORAGE_CONTAINER |
| 21 | +# ============================================================================= |
| 22 | + |
| 23 | +CLOUD_PROVIDER="${1:-gke}" |
| 24 | +COMPONENT="${2:-lgtm-stack}" |
| 25 | +BACKEND_FILE="backend-config.tf" |
| 26 | + |
| 27 | +echo "Configuring Terraform backend for: $CLOUD_PROVIDER / $COMPONENT" |
| 28 | +echo "State files will be stored remotely and persist across workflow runs" |
| 29 | + |
| 30 | +case "$CLOUD_PROVIDER" in |
| 31 | + gke) |
| 32 | + if [ -z "${TF_STATE_BUCKET:-}" ]; then |
| 33 | + echo "ERROR: TF_STATE_BUCKET environment variable is required for GKE" |
| 34 | + echo " Set it in GitHub Secrets or export it locally" |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | + cat > "$BACKEND_FILE" <<EOF |
| 38 | +# Auto-generated backend configuration for GCS |
| 39 | +# State files are stored at: gs://${TF_STATE_BUCKET}/terraform/${COMPONENT}/ |
| 40 | +terraform { |
| 41 | + backend "gcs" { |
| 42 | + bucket = "${TF_STATE_BUCKET}" |
| 43 | + prefix = "terraform/${COMPONENT}" |
| 44 | + } |
| 45 | +} |
| 46 | +EOF |
| 47 | + echo "Configured GCS backend: ${TF_STATE_BUCKET}/terraform/${COMPONENT}" |
| 48 | + ;; |
| 49 | + |
| 50 | + eks) |
| 51 | + if [ -z "${TF_STATE_BUCKET:-}" ]; then |
| 52 | + echo "ERROR: TF_STATE_BUCKET environment variable is required for EKS" |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | + if [ -z "${AWS_REGION:-}" ]; then |
| 56 | + echo "ERROR: AWS_REGION environment variable is required for EKS" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + cat > "$BACKEND_FILE" <<EOF |
| 60 | +# Auto-generated backend configuration for S3 |
| 61 | +# State files are stored at: s3://${TF_STATE_BUCKET}/terraform/${COMPONENT}/ |
| 62 | +terraform { |
| 63 | + backend "s3" { |
| 64 | + bucket = "${TF_STATE_BUCKET}" |
| 65 | + key = "terraform/${COMPONENT}/terraform.tfstate" |
| 66 | + region = "${AWS_REGION}" |
| 67 | + encrypt = true |
| 68 | + dynamodb_table = "${TF_STATE_LOCK_TABLE:-terraform-state-lock}" |
| 69 | + } |
| 70 | +} |
| 71 | +EOF |
| 72 | + echo "Configured S3 backend: ${TF_STATE_BUCKET}/terraform/${COMPONENT}" |
| 73 | + echo " Using DynamoDB lock table: ${TF_STATE_LOCK_TABLE:-terraform-state-lock}" |
| 74 | + ;; |
| 75 | + |
| 76 | + aks) |
| 77 | + if [ -z "${AZURE_STORAGE_ACCOUNT:-}" ]; then |
| 78 | + echo "ERROR: AZURE_STORAGE_ACCOUNT environment variable is required for AKS" |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + if [ -z "${AZURE_STORAGE_CONTAINER:-}" ]; then |
| 82 | + echo "ERROR: AZURE_STORAGE_CONTAINER environment variable is required for AKS" |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | + cat > "$BACKEND_FILE" <<EOF |
| 86 | +# Auto-generated backend configuration for Azure Blob Storage |
| 87 | +# State files are stored at: ${AZURE_STORAGE_ACCOUNT}/${AZURE_STORAGE_CONTAINER}/terraform/${COMPONENT}/ |
| 88 | +terraform { |
| 89 | + backend "azurerm" { |
| 90 | + storage_account_name = "${AZURE_STORAGE_ACCOUNT}" |
| 91 | + container_name = "${AZURE_STORAGE_CONTAINER}" |
| 92 | + key = "terraform/${COMPONENT}/terraform.tfstate" |
| 93 | + } |
| 94 | +} |
| 95 | +EOF |
| 96 | + echo "Configured Azure Blob backend: ${AZURE_STORAGE_ACCOUNT}/${AZURE_STORAGE_CONTAINER}/terraform/${COMPONENT}" |
| 97 | + ;; |
| 98 | + |
| 99 | + generic) |
| 100 | + cat > "$BACKEND_FILE" <<EOF |
| 101 | +# Auto-generated backend configuration for Kubernetes |
| 102 | +# State stored as Secret in kube-system namespace |
| 103 | +terraform { |
| 104 | + backend "kubernetes" { |
| 105 | + secret_suffix = "${COMPONENT}" |
| 106 | + namespace = "kube-system" |
| 107 | + labels = { |
| 108 | + "managed-by" = "terraform" |
| 109 | + "component" = "${COMPONENT}" |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +EOF |
| 114 | + echo "Configured Kubernetes backend (secret in kube-system) for ${COMPONENT}" |
| 115 | + echo " WARNING: Kubernetes backend is not recommended for production" |
| 116 | + echo " Consider using cloud storage (GCS/S3/Azure Blob) for better durability" |
| 117 | + ;; |
| 118 | + |
| 119 | + *) |
| 120 | + echo "ERROR: Unknown cloud provider: $CLOUD_PROVIDER" |
| 121 | + exit 1 |
| 122 | + ;; |
| 123 | +esac |
| 124 | + |
| 125 | +echo "Backend configuration written to: $BACKEND_FILE" |
| 126 | +echo "" |
| 127 | +echo "State Management:" |
| 128 | +echo " - State files persist in remote storage across all runs" |
| 129 | +echo " - Only backend-config.tf is regenerated (not state files)" |
| 130 | +echo " - Multiple team members can collaborate using the same bucket" |
| 131 | +echo " - State locking prevents concurrent modifications" |
0 commit comments