MATIH Platform is in active MVP development. Documentation reflects current implementation status.
18. CI/CD & Build System
Stage 00: Terraform

Stage 00: Terraform Infrastructure

Stage 00 provisions all cloud infrastructure using Terraform. It is the first stage in the CD pipeline and has no dependencies. All subsequent stages depend on the resources created here.

Source file: scripts/stages/00-terraform.sh


Execution Flow

1. validate_prerequisites    Check terraform, az, jq are installed
2. register_providers        Register required Azure resource providers
3. setup_backend             Ensure TF state storage account exists
4. initialize                terraform init with backend config
5. import_existing           Import pre-existing resources into state
6. sync_state                terraform refresh to detect drift
7. plan_changes              terraform plan to generate execution plan
8. apply_changes             terraform apply if changes detected
9. export_outputs            Cache outputs for downstream stages

Prerequisites

The stage validates that required tools are installed:

require_command terraform "Install Terraform from https://terraform.io"
require_command az "Install Azure CLI from https://aka.ms/installazurecli"
require_command jq "Install jq: brew install jq"

And authenticates with Azure:

if [[ -n "${ARM_CLIENT_ID:-}" ]]; then
    az login --service-principal \
        --username "$ARM_CLIENT_ID" \
        --password "$ARM_CLIENT_SECRET" \
        --tenant "$ARM_TENANT_ID"
fi

Configuration

TERRAFORM_DIR="${MATIH_ROOT}/infrastructure/terraform/environments/azure-matihlabs"
RESOURCE_GROUP="matihplatformrg"
AKS_CLUSTER="matihlabsaks"
TF_STATE_STORAGE_ACCOUNT="matihlabstfstate"
TF_STATE_CONTAINER="tfstate"
TF_STATE_KEY="matih-platform.tfstate"
AZURE_LOCATION="centralindia"

Output Caching

Terraform outputs are cached as JSON for faster access by downstream stages:

terraform -chdir="$TERRAFORM_DIR" output -json > "$TF_OUTPUTS_FILE"

Other stages read outputs via tf_get_output() from lib/core/config.sh:

ACR_NAME=$(tf_get_output "acr_name" "matihlabsacr")
AKS_CLUSTER_NAME=$(tf_get_output "aks_cluster_name" "matihlabsaks")

Error Codes

CodeNameDescription
E001MISSING_PREREQUISITEterraform, az, or jq not installed
E100TERRAFORM_INIT_FAILEDBackend initialization failed
E101TERRAFORM_PLAN_FAILEDPlan generation failed
E102TERRAFORM_APPLY_FAILEDApply execution failed
E105TERRAFORM_IMPORT_FAILEDResource import failed
E106TERRAFORM_REFRESH_FAILEDState refresh failed
E200AZURE_LOGIN_FAILEDAzure authentication failed
E203PROVIDER_REGISTRATION_FAILEDAzure provider registration failed
E208STORAGE_FAILEDState storage account setup failed

Resources Provisioned

This stage creates all cloud infrastructure for the MATIH platform:

ResourcePurpose
AKS ClusterKubernetes cluster with multiple node pools
Azure Container RegistryDocker image registry
Azure Key VaultSecret management
Azure PostgreSQL Flexible ServerManaged database
Azure Redis CacheCaching layer
Virtual NetworkNetwork isolation
Storage AccountsBlob storage, state storage
Azure OpenAILLM inference endpoints
DNS ZonePlatform domain management