Zum Inhalt springen

Manage MongoDB Flex with Terraform

Diese Seite ist noch nicht in deiner Sprache verfügbar. Englische Seite aufrufen

In this how-to, you will learn how to create a MongoDB Flex instance with Terraform. Thus, you will set up an environment first. After completing the guide, you’ll have knowledge about the basics of Terraform with MongoDB Flex. After completion, you can get further knowledge with the linked resources.

  • You have a service account with sufficient rights.
  • You have a key file for your service account.
  • Your client has Terraform installed.
  1. Verify that Terraform is accessible.

    Terminal window
    terraform --version

    You get something like:

    Terraform v1.12.2
    on darwin_arm64
  2. Create a folder for your Terraform project and change directory.

    Terminal window
    mkdir stackit-terraform-mongodb
    cd stackit-terraform-mongodb
  3. Copy your service account key file into the folder and rename it to service-account-key.json. The guide assumes that your downloaded key account file is in your download folder and has the name sa-key-ee76107b-8923-4661-9ae6-27417d5004f0.json. Please change the next command according to your local environment.

    Terminal window
    mv ~/Downloads/sa-key-ee76107b-8923-4661-9ae6-27417d5004f0.json ./service-account-key.json
  4. Create main.tf.

    Terminal window
    terraform {
    required_providers {
    stackit = {
    source = "stackitcloud/stackit"
    version = "~> 0.30.0" # Use the latest version
    }
    }
    }
    provider "stackit" {
    service_account_key_path = "./service-account-key.json"
    region = "eu01"
    }
    resource "stackit_mongodbflex_instance" "example" {
    project_id = var.project_id
    name = "my-mongodb-instance"
    acl = [
    "79.206.199.114/32" # Attention: Allows access from anywhere. Only for tests!
    ]
    backup_schedule = "0 2 * * *" # Daily at 02:00 AM at night
    flavor = {
    cpu = 1
    ram = 4
    }
    storage = {
    class = "premium-perf2-mongodb"
    size = 10
    }
    version = "8.0"
    replicas = 1
    options = {
    type = "Single"
    snapshot_retention_days = 2 # How long snapshots are stored
    point_in_time_window_hours = 15 # Point-in-time recovery window in hours
    }
    }
    resource "stackit_mongodbflex_user" "example_user" {
    project_id = var.project_id
    instance_id = stackit_mongodbflex_instance.example.instance_id
    username = "myuser"
    database = "default"
    roles = ["readWrite"]
    }
  5. Create variables.tf.

    variable "project_id" {
    description = "STACKIT Project ID"
    type = string
    }
  6. Create terraform.tfvars.

    project_id = "<PROJECT_ID>"
  7. Execute:

    Terminal window
    terraform plan
    terraform apply
  8. Check the output.

    You get something like:

    ...
    Do you want to perform these actions?
    Terraform will perform the actions described above.
    Only 'yes' will be accepted to approve.
    Enter a value: yes
    stackit_mongodbflex_instance.example: Creating...
    stackit_mongodbflex_instance.example: Still creating... [10s elapsed]
    stackit_mongodbflex_instance.example: Still creating... [20s elapsed]
    stackit_mongodbflex_instance.example: Still creating... [30s elapsed]
    stackit_mongodbflex_instance.example: Still creating... [40s elapsed]
    stackit_mongodbflex_instance.example: Still creating... [50s elapsed]
    stackit_mongodbflex_instance.example: Still creating... [1m0s elapsed]
    stackit_mongodbflex_instance.example: Still creating... [1m10s elapsed]
    stackit_mongodbflex_instance.example: Still creating... [1m20s elapsed]
    stackit_mongodbflex_instance.example: Still creating... [1m30s elapsed]
    stackit_mongodbflex_instance.example: Still creating... [1m40s elapsed]
    stackit_mongodbflex_instance.example: Creation complete after 1m42s [id=118e6780-c9d0-48d0-87a3-044fe90c17c4,4cf6f720-1524-40dc-b76f-64e6533b1453]
    stackit_mongodbflex_user.example_user: Creating...
    stackit_mongodbflex_user.example_user: Creation complete after 1s [id=118e6780-c9d0-48d0-87a3-044fe90c17c4,4cf6f720-1524-40dc-b76f-64e6533b1453,8f1b5a44-5e10-4a3b-86ab-5377cfb711df]
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

You successfully created your first MongoDB Flex instance and user with Terraform. You can have a look at the file terraform.tfstate to get an idea what Terraform did. You can also explore STACKIT’s Terraform provider GitHub repository to learn more about its capabilities. For more informations about Terraform in general, please visit the official Terraform website.