Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Terraform variables for setting AlloyDB database flags

Hi, I want to use `database_flags` in terraform resource of `google_alloydb_instance`.

According to the document, you can use `database_flags` in `google_alloydb_instance`, isn't it?

If so, could you tell me some examples of using `database_flags` ?

Solved Solved
0 7 1,802
1 ACCEPTED SOLUTION

Hi, @ms4446 .

I tried to `terraform apply`, and I got an error saying that the type of `database_flags` is map of string, and when I wrote the following code, it worked out.

 

 

resource "google_alloydb_instance" "main" {
cluster = google_alloydb_cluster.main.name
instance_id = "${var.project_id}-alloydb-instance"
instance_type = "PRIMARY"

database_flags = {
"alloydb.logical_decoding" = "on"
}

depends_on = [google_service_networking_connection.vpc_connection]
}

 

 
thanks for your response.

View solution in original post

7 REPLIES 7

Yes,  AlloyDB is a managed database service from that is fully compatible Postgres.

For example, for Postgres, some common flags include:

  • max_connections: The maximum number of concurrent connections to the database.
  • shared_buffers: The amount of memory the database uses for shared memory buffers.
  • work_mem: The amount of memory used by internal sort operations and hash tables.

Here is an example of using database_flags with AlloyDB instance in Terraform:

resource "google_alloydb_instance" "default" {
name = "test-alloydb-instance"
project = "my-gcp-project"
region = "us-central1"
database_version = "POSTGRES_13"

settings {
tier = "db-f1-micro"

database_flags {
name = "max_connections"
value = "100"
}

database_flags {
name = "shared_buffers"
value = "256MB"
}
}
}

 

Hi, @ms4446. Thank you for your response.
Based on the example code you provided, should database_flags be written similarly to google_sql_database_instance?
Am I correct in understanding that database_flags is nested within the settings?

thanks.

Yes, that's correct. The database_flags block should be nested within the settings block, similar to the google_sql_database_instance resource. This is a typical pattern for Terraform resources where certain settings are grouped under a common block.

Thanks a lot!

Hi, @ms4446 

I tried the following code, but encountered an error.
Code:
terraform
resource "google_alloydb_instance" "default" {
  name = "test-alloydb-instance"

  settings {
    tier = "db-f1-micro"

    database_flags {
      name  = "max_connections"
      value = "100"
    }

    database_flags {
      name  = "shared_buffers"
      value = "256MB"
    }
  }
}
Error message:
╷
│ Error: Unsupported block type
│
│   on ./alloydb/main.tf line 108, in resource "google_alloydb_instance" "default":
│  108:   settings {
│
│ Blocks of type "settings" are not expected here.
Also, I encountered an error with the following code.
Code:
terraform
resource "google_alloydb_instance" "default" {
  name = "test-alloydb-instance"

  database_flags {
    name  = "max_connections"
    value = "100"
  }
}
Error message:
╷
│ Error: Unsupported block type
│
│   on ./alloydb/main.tf line 108, in resource "google_alloydb_instance" "default":
│  108:   database_flags {
│
│ Blocks of type "database_flags" are not expected here. Did you mean to define argument "database_flags"? If so, use the equals sign to assign it a value.
Therefore, is it correct to use database_flags as an argument, like this? :
terraform
resource "google_alloydb_instance" "default" {
  name = "test-alloydb-instance"

  database_flags = {
    max_connections = "100"
  }
}

 thanks for replying.

Sorry for the confusion. Upon further research the google_alloydb_instance resource does indeed support the database_flags argument. However, it is directly nested under the instance resource and not under any subblock like settings​.

Here is an example of the proper syntax:

resource "google_alloydb_instance" "primary" {
cluster = google_alloydb_cluster.default.name
instance_id = "your-instance-id"
instance_type = "PRIMARY"
display_name = "your-instance-display-name"

database_flags = [
{
name = "max_connections"
value = "100"
},
{
name = "shared_buffers"
value = "256MB"
}
]

# other arguments...
}

Please note that the values of database_flags are provided as a list of maps, where each map contains a name and a value. Ensure that the flags you use are compatible with the database engine in use (PostgreSQL in your case).

In the example code from the official repository, the database_flags argument and its value are provided via variables. If you're still encountering errors, you may want to ensure that you have correctly defined and provided these variables.

Lastly, in the AlloyDB module, the creation of an AlloyDB instance is split into two separate resource blocks: one for the AlloyDB cluster (google_alloydb_cluster) and one for the AlloyDB instance (google_alloydb_instance). The instance is associated with the cluster via the cluster argument, which is set to the name of the google_alloydb_cluster resource. Make sure your Terraform configuration reflects this structure

For more more details please refer to the official GoogleCloudPlatform repository for AlloyDB

Hi, @ms4446 .

I tried to `terraform apply`, and I got an error saying that the type of `database_flags` is map of string, and when I wrote the following code, it worked out.

 

 

resource "google_alloydb_instance" "main" {
cluster = google_alloydb_cluster.main.name
instance_id = "${var.project_id}-alloydb-instance"
instance_type = "PRIMARY"

database_flags = {
"alloydb.logical_decoding" = "on"
}

depends_on = [google_service_networking_connection.vpc_connection]
}

 

 
thanks for your response.