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:
-
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
} -
Create a file named
project.tfand 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_NAMEis your Aiven organization name. -
Create a file named
service.tfand 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
}
}
} -
Create a file named
variables.tfand add the following code to declare the Aiven token variable:variable "aiven_token" {
description = "Aiven token"
type = string
} -
Create a file named
terraform.tfvarswith the following code to store the token value:aiven_token = "AIVEN_TOKEN"Where
AIVEN_TOKENis your token.
Plan and apply the configuration
-
The
initcommand prepares the working directly for use with OpenTofu. Use it to automatically find, download, and install the necessary Aiven Provider plugins:tofu init -
Run the
plancommand to create an execution plan and preview the changes. This shows you what resources OpenTofu will create or modify:tofu planThe 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. -
To create the resources, run:
tofu apply --auto-approveThe 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:
-
Create a destroy plan and preview the changes to your infrastructure with the following command:
tofu plan -destroy -
To delete the resources and all data, run:
tofu destroy -
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.
Related pages
- Try OpenTofu with another sample project.