Skip to content

Quickstart

Demo

This is a complete walkthrough to see Terraform Backend MongoDB in action.

  1. Make sure the following tools are available from the command line:

  2. Run the application and the database in containers:

    curl -O https://raw.githubusercontent.com/devpro/terraform-backend-mongodb/refs/heads/main/compose.yaml
    curl --create-dirs -o demo/otel-collector-config.yaml https://raw.githubusercontent.com/devpro/terraform-backend-mongodb/refs/heads/main/demo/otel-collector-config.yaml
    docker compose up
    
    curl -O https://raw.githubusercontent.com/devpro/terraform-backend-mongodb/refs/heads/main/compose.yaml
    curl --create-dirs -o demo/otel-collector-config.yaml https://raw.githubusercontent.com/devpro/terraform-backend-mongodb/refs/heads/main/demo/otel-collector-config.yaml
    podman compose up
    
    helm upgrade --install tfbackend terraform-backend-mongodb --repo https://devpro.github.io/helm-charts \
      --set webapi.tag=latest --set mongodb.enabled=true --set mongodb.auth.rootPassword=<rootpassword> --set dotnet.enableScalar=true \
      --create-namespace --namespace tfbackend --wait
    kubectl port-forward svc/tfbackend 9001:80 --namespace tfbackend
    

    The REST API definitions can be viewed and tried through the Scalar web page on localhost:9001

  3. Create the indexes and a user to authenticate calls, whose password is prompted for:

    curl -O https://raw.githubusercontent.com/devpro/terraform-backend-mongodb/refs/heads/main/scripts/tfbeadm
    docker run --rm -it --network tfbackmdb_default -v "$PWD/tfbeadm:/tfbeadm:ro" mongo:9.0 bash -c \
      "apt-get update -qq && apt-get install -qq -y apache2-utils > /dev/null && /tfbeadm create-indexes && /tfbeadm create-user admin dummy"
    
    curl -O https://raw.githubusercontent.com/devpro/terraform-backend-mongodb/refs/heads/main/scripts/tfbeadm
    podman run --rm -it --network tfbackmdb_default -v "$PWD/tfbeadm:/tfbeadm:ro,Z" docker.io/library/mongo:9.0 bash -c \
      "apt-get update -qq && apt-get install -qq -y apache2-utils > /dev/null && /tfbeadm create-indexes && /tfbeadm create-user admin dummy"
    

    htpasswd must be available, from the apache2-utils or httpd-tools package:

    curl -O https://raw.githubusercontent.com/devpro/terraform-backend-mongodb/refs/heads/main/scripts/tfbeadm && chmod +x ./tfbeadm
    export MONGODB_KUBEDEPLOYNAME=deploy/tfbackend-mongodb
    export MONGODB_URI="mongodb://root:<rootpassword>@localhost:27017/tfbackend?authSource=admin"
    ./tfbeadm create-indexes
    ./tfbeadm create-user admin dummy
    
  4. Write Terraform files from a pre-configured sample:

    curl -O https://raw.githubusercontent.com/devpro/terraform-backend-mongodb/refs/heads/main/samples/local-files/main.tf
    
  5. Prepare the working directory for use with Terraform, with the password of the user created above:

    export TF_HTTP_ADDRESS="http://localhost:9001/dummy/state/quickstart"
    export TF_HTTP_LOCK_ADDRESS="$TF_HTTP_ADDRESS/lock"
    export TF_HTTP_UNLOCK_ADDRESS="$TF_HTTP_ADDRESS/lock"
    export TF_HTTP_USERNAME="admin"
    export TF_HTTP_PASSWORD="<password>"
    terraform init
    
  6. Perform the operations indicated in the Terraform project files:

    terraform apply
    
  7. Query the state database to see the Terraform state information:

    docker run --rm --network "tfbackmdb_default" "mongo:9.0" \
      bash -c "mongosh \"mongodb://mongodb:27017/tfbackend_dev\" --eval 'db.tf_state.find().projection({tenant: 1, name: 1, created_at: 1, \"value.version\": 1, \"value.resources.type\": 1, \"value.resources.name\": 1})'"
    
    podman run --rm --network "tfbackmdb_default" "docker.io/library/mongo:9.0" \
      bash -c "mongosh \"mongodb://mongodb:27017/tfbackend_dev\" --eval 'db.tf_state.find().projection({tenant: 1, name: 1, created_at: 1, \"value.version\": 1, \"value.resources.type\": 1, \"value.resources.name\": 1})'"
    
    kubectl exec deploy/tfbackend-mongodb --namespace tfbackend -- \
      bash -c "mongosh \"$MONGODB_URI\" --eval 'db.tf_state.find().projection({tenant: 1, name: 1, created_at: 1, \"value.version\": 1, \"value.resources.type\": 1, \"value.resources.name\": 1})'"
    
  8. Destroy the resources that were created with Terraform:

    terraform destroy