Quickstart
Demo
This is a complete walkthrough to see Terraform Backend MongoDB in action.
-
Make sure the following tools are available from the command line:
-
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 upcurl -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 uphelm 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 tfbackendThe REST API definitions can be viewed and tried through the Scalar web page on localhost:9001
-
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"htpasswdmust be available, from theapache2-utilsorhttpd-toolspackage: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 -
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 -
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 -
Perform the operations indicated in the Terraform project files:
terraform apply -
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})'" -
Destroy the resources that were created with Terraform:
terraform destroy