Manage PostgreSQL Flex with Terraform
Diese Seite ist noch nicht in deiner Sprache verfügbar. Englische Seite aufrufen
In this guide, you learn how to create a PostgreSQL Flex instance with Terraform. You will set up an environment first. After completing the guide, you’ll have basic knowledge of Terraform with PostgreSQL Flex. After completion, you can get further knowledge from 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-postgresqlcd stackit-terraform-postgresql -
Copy your service account key file into the folder and rename it to
service-account-key.json. This 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" # Verwende die neueste Version}}}provider "stackit" {service_account_key_path = "./service-account-key.json"region = "eu01"}resource "stackit_postgresqlflex_instance" "example" {project_id = var.project_idname = "my-postgresql-instance"acl = ["79.206.199.114/32"]backup_schedule = "0 2 * * *" # Täglich um 2 Uhr nachtsflavor = {cpu = 1ram = 4}storage = {class = "premium-perf6-postgresql"size = 5}version = "17.0"replicas = 1options = {type = "Single"}}resource "stackit_postgresql_user" "example_user" {project_id = var.project_idinstance_id = stackit_postgresqlflex_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_postgresqlflex_instance.example: Creating...stackit_postgresqlflex_instance.example: Still creating... [10s elapsed]stackit_postgresqlflex_instance.example: Still creating... [20s elapsed]stackit_postgresqlflex_instance.example: Still creating... [30s elapsed]stackit_postgresqlflex_instance.example: Still creating... [40s elapsed]stackit_postgresqlflex_instance.example: Still creating... [50s elapsed]stackit_postgresqlflex_instance.example: Still creating... [1m0s elapsed]stackit_postgresqlflex_instance.example: Still creating... [1m10s elapsed]stackit_postgresqlflex_instance.example: Still creating... [1m20s elapsed]stackit_postgresqlflex_instance.example: Still creating... [1m30s elapsed]stackit_postgresqlflex_instance.example: Still creating... [1m40s elapsed]stackit_postgresqlflex_instance.example: Creation complete after 1m42s [id=118e6780-c9d0-48d0-87a3-044fe90c17c4,4cf6f720-1524-40dc-b76f-64e6533b1453]stackit_postgresqlflex_user.example_user: Creating...stackit_postgresqlflex_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 succesfully created your first PostgreSQL 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 information about Terraform in general, please visit
the official Terraform website.