Skip to main content

Use OpenTofu with Aiven Provider for Terraform

OpenTofu is an open source infrastructure-as-code tool that you can use to configure your Aiven infrastructure.

Set up your first Aiven project and service using this example to get started with OpenTofu. If you already use the Aiven Provider for Terraform, you can migrate your Terraform resources to OpenTofu.

Prerequisites

Configure your project and services

Set up the OpenTofu project in an empty folder:

  1. Create a file, provider.tf, and add the following code to declare a dependency on the Aiven Provider, specifying the version.

    terraform {
    required_providers {
    aiven = {
    source = "aiven/aiven"
    version = ">=4.0.0, < 5.0.0"
    }
    }
    }

    provider "aiven" {
    api_token = var.aiven_token
    }
  2. Create a file named project.tf and add the following code to create an Aiven project in your organization:

     # Get information about your organization
    data "aiven_organization" "main" {
    name = "ORGANIZATION_NAME"
    }
    # Create a new project in your organization
    resource "aiven_project" "example_project" {
    project = "ORGANIZATION_NAME-example-project"
    parent_id = data.aiven_organization.main.id
    }

    Where ORGANIZATION_NAME is your Aiven organization name.

  3. Create a file named service.tf and add the following code to define the configuration of an Aiven for PostgreSQL® service:

    resource "aiven_pg" "pg" {
    project = aiven_project.example_project.project
    cloud_name = "google-europe-west1"
    plan = "startup-4"
    service_name = "example-pg"
    maintenance_window_dow = "monday"
    maintenance_window_time = "10:00:00"

    pg_user_config {
    pg {
    idle_in_transaction_session_timeout = 900
    log_min_duration_statement = -1
    }
    }
    }
  4. Create a file named variables.tf and add the following code to declare the Aiven token variable:

    variable "aiven_token" {
    description = "Aiven token"
    type = string
    }
  5. Create a file named terraform.tfvars with the following code to store the token value:

    aiven_token = "AIVEN_TOKEN"

    Where AIVEN_TOKEN is your token.

Plan and apply the configuration

  1. The init command prepares the working directly for use with OpenTofu. Use it to automatically find, download, and install the necessary Aiven Provider plugins:

    tofu init
  2. Run the plan command to create an execution plan and preview the changes. This shows you what resources OpenTofu will create or modify:

    tofu plan

    The output is similar to the following:

    OpenTofu used the selected providers to generate the following execution plan.
    ...
    Plan: 2 to add, 0 to change, 0 to destroy.
  3. To create the resources, run:

    tofu apply --auto-approve

    The output is similar to the following:

    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

You can also see the PostgreSQL service in the Aiven Console.

Clean up

To delete the project, service, and data:

  1. Create a destroy plan and preview the changes to your infrastructure with the following command:

    tofu plan -destroy
  2. To delete the resources and all data, run:

    tofu destroy
  3. Enter yes to confirm. The output is similar to the following:

    Do you really want to destroy all resources?
    OpenTofu will destroy all your managed infrastructure, as shown above.
    There is no undo. Only 'yes' will be accepted to confirm.

    Enter a value: yes
    ...
    Destroy complete! Resources: 2 destroyed.