Manage MongoDB Flex with Terraform
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.
Prerequisites
Section titled “Prerequisites”- You have a service account with sufficient rights.
- You have a key file for your service account.
- Your client has Terraform installed.
Set up the environment
Section titled “Set up the environment”-
Verify that Terraform is accessible.
Terminal window terraform --versionYou get something like:
Terraform v1.12.2on darwin_arm64 -
Create a folder for your Terraform project and change directory.
Terminal window mkdir stackit-terraform-mongodbcd stackit-terraform-mongodb -
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 namesa-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 -
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_idname = "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 nightflavor = {cpu = 1ram = 4}storage = {class = "premium-perf2-mongodb"size = 10}version = "8.0"replicas = 1options = {type = "Single"snapshot_retention_days = 2 # How long snapshots are storedpoint_in_time_window_hours = 15 # Point-in-time recovery window in hours}}resource "stackit_mongodbflex_user" "example_user" {project_id = var.project_idinstance_id = stackit_mongodbflex_instance.example.instance_idusername = "myuser"database = "default"roles = ["readWrite"]} -
Create
variables.tf.variable "project_id" {description = "STACKIT Project ID"type = string} -
Create
terraform.tfvars.project_id = "<PROJECT_ID>" -
Execute:
Terminal window terraform planterraform apply -
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: yesstackit_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.
Next steps
Section titled “Next steps”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.