diff --git a/modules/alloydb/README.md b/modules/alloydb/README.md
index 69e34ce73..c1b5d0d40 100644
--- a/modules/alloydb/README.md
+++ b/modules/alloydb/README.md
@@ -19,6 +19,7 @@ Note that this module assumes that some options are the same for both the primar
- [PSC instance with auto connections](#psc-instance-with-auto-connections)
- [Custom flags and users definition](#custom-flags-and-users-definition)
- [CMEK encryption](#cmek-encryption)
+ - [Managed connection pooling](#managed-connection-pooling)
- [Tag bindings](#tag-bindings)
- [Variables](#variables)
- [Outputs](#outputs)
@@ -406,6 +407,32 @@ module "alloydb" {
# tftest inventory=cmek.yaml e2e
```
+### Managed connection pooling
+
+```hcl
+module "alloydb" {
+ source = "./fabric/modules/alloydb"
+ project_id = var.project_id
+ project_number = var.project_number
+ cluster_name = "db"
+ location = var.region
+ instance_name = "db"
+ network_config = {
+ psa_config = {
+ network = var.vpc.id
+ }
+ }
+
+ connection_pool_flags = {
+ "pool_mode" = "transaction"
+ "max_pool_size" = "240"
+ }
+
+ deletion_protection = false
+}
+# tftest modules=1 resources=2 inventory=connection_pool.yaml e2e
+```
+
## Tag bindings
Refer to the [Creating and managing tags](https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing) documentation for details on usage.
@@ -451,37 +478,38 @@ module "alloydb" {
| name | description | type | required | default |
|---|---|:---:|:---:|:---:|
| [cluster_name](variables.tf#L84) | Name of the primary cluster. | string | ✓ | |
-| [instance_name](variables.tf#L211) | Name of primary instance. | string | ✓ | |
-| [location](variables.tf#L223) | Region or zone of the cluster and instance. | string | ✓ | |
-| [network_config](variables.tf#L268) | Network configuration for cluster and instance. Only one between psa_config and psc_config can be used. | object({…}) | ✓ | |
-| [project_id](variables.tf#L324) | The ID of the project where this instances will be created. | string | ✓ | |
+| [instance_name](variables.tf#L224) | Name of primary instance. | string | ✓ | |
+| [location](variables.tf#L236) | Region or zone of the cluster and instance. | string | ✓ | |
+| [network_config](variables.tf#L281) | Network configuration for cluster and instance. Only one between psa_config and psc_config can be used. | object({…}) | ✓ | |
+| [project_id](variables.tf#L337) | The ID of the project where this instances will be created. | string | ✓ | |
| [annotations](variables.tf#L17) | Map FLAG_NAME=>VALUE for annotations which allow client tools to store small amount of arbitrary data. | map(string) | | null |
| [automated_backup_configuration](variables.tf#L23) | Automated backup settings for cluster. | object({…}) | | {} |
| [availability_type](variables.tf#L61) | Availability type for the primary replica. Either `ZONAL` or `REGIONAL`. | string | | "REGIONAL" |
| [client_connection_config](variables.tf#L67) | Client connection config. | object({…}) | | null |
| [cluster_display_name](variables.tf#L78) | Display name of the primary cluster. | string | | null |
-| [continuous_backup_configuration](variables.tf#L90) | Continuous backup settings for cluster. | object({…}) | | {} |
-| [cross_region_replication](variables.tf#L100) | Cross region replication config. | object({…}) | | {} |
-| [database_version](variables.tf#L157) | Database type and version to create. | string | | "POSTGRES_15" |
-| [deletion_policy](variables.tf#L163) | AlloyDB cluster and instance deletion policy. | string | | null |
-| [deletion_protection](variables.tf#L169) | Whether Terraform will be prevented from destroying the cluster. When the field is set to true or unset in Terraform state, a terraform apply or terraform destroy that would delete the cluster will fail. When the field is set to false, deleting the cluster is allowed. | bool | | null |
-| [display_name](variables.tf#L175) | AlloyDB instance display name. | string | | null |
-| [encryption_config](variables.tf#L181) | Set encryption configuration. KMS name format: 'projects/[PROJECT]/locations/[REGION]/keyRings/[RING]/cryptoKeys/[KEY_NAME]'. | object({…}) | | null |
-| [flags](variables.tf#L190) | Map FLAG_NAME=>VALUE for database-specific tuning. | map(string) | | null |
-| [gce_zone](variables.tf#L196) | The GCE zone that the instance should serve from. This can ONLY be specified for ZONAL instances. If present for a REGIONAL instance, an error will be thrown. | string | | null |
-| [initial_user](variables.tf#L202) | AlloyDB cluster initial user credentials. | object({…}) | | null |
-| [labels](variables.tf#L217) | Labels to be attached to all instances. | map(string) | | null |
-| [machine_config](variables.tf#L229) | AlloyDB machine config. | object({…}) | | {} |
-| [maintenance_config](variables.tf#L243) | Set maintenance window configuration. | object({…}) | | {} |
-| [observability_config](variables.tf#L298) | Advanced query insights config for AlloyDB. Mutually exclusive with query_insights_config. | object({…}) | | null |
-| [prefix](variables.tf#L314) | Optional prefix used to generate instance names. | string | | null |
-| [project_number](variables.tf#L329) | The project number of the project where this instances will be created. Only used for testing purposes. | string | | null |
-| [query_insights_config](variables.tf#L335) | Query insights config. Mutually exclusive with observability_config. It will be ignored if observability_config is enabled. | object({…}) | | {} |
-| [read_pool](variables.tf#L346) | Map of read pool instances to create in the primary cluster. | map(object({…})) | | {} |
-| [skip_await_major_version_upgrade](variables.tf#L402) | Set to true to skip awaiting on the major version upgrade of the cluster. | bool | | true |
-| [subscription_type](variables.tf#L408) | The subscription type of cluster. Possible values are: 'STANDARD' or 'TRIAL'. | string | | "STANDARD" |
-| [tag_bindings](variables.tf#L414) | Tag bindings for this service, in key => tag value id format. | map(string) | | {} |
-| [users](variables.tf#L421) | Map of users to create in the primary instance (and replicated to other replicas). Set PASSWORD to null if you want to get an autogenerated password. The user types available are: 'ALLOYDB_BUILT_IN' or 'ALLOYDB_IAM_USER'. | map(object({…})) | | {} |
+| [connection_pool_flags](variables.tf#L90) | Map FLAG_NAME=>VALUE for managed connection pooling. | map(string) | | null |
+| [continuous_backup_configuration](variables.tf#L103) | Continuous backup settings for cluster. | object({…}) | | {} |
+| [cross_region_replication](variables.tf#L113) | Cross region replication config. | object({…}) | | {} |
+| [database_version](variables.tf#L170) | Database type and version to create. | string | | "POSTGRES_15" |
+| [deletion_policy](variables.tf#L176) | AlloyDB cluster and instance deletion policy. | string | | null |
+| [deletion_protection](variables.tf#L182) | Whether Terraform will be prevented from destroying the cluster. When the field is set to true or unset in Terraform state, a terraform apply or terraform destroy that would delete the cluster will fail. When the field is set to false, deleting the cluster is allowed. | bool | | null |
+| [display_name](variables.tf#L188) | AlloyDB instance display name. | string | | null |
+| [encryption_config](variables.tf#L194) | Set encryption configuration. KMS name format: 'projects/[PROJECT]/locations/[REGION]/keyRings/[RING]/cryptoKeys/[KEY_NAME]'. | object({…}) | | null |
+| [flags](variables.tf#L203) | Map FLAG_NAME=>VALUE for database-specific tuning. | map(string) | | null |
+| [gce_zone](variables.tf#L209) | The GCE zone that the instance should serve from. This can ONLY be specified for ZONAL instances. If present for a REGIONAL instance, an error will be thrown. | string | | null |
+| [initial_user](variables.tf#L215) | AlloyDB cluster initial user credentials. | object({…}) | | null |
+| [labels](variables.tf#L230) | Labels to be attached to all instances. | map(string) | | null |
+| [machine_config](variables.tf#L242) | AlloyDB machine config. | object({…}) | | {} |
+| [maintenance_config](variables.tf#L256) | Set maintenance window configuration. | object({…}) | | {} |
+| [observability_config](variables.tf#L311) | Advanced query insights config for AlloyDB. Mutually exclusive with query_insights_config. | object({…}) | | null |
+| [prefix](variables.tf#L327) | Optional prefix used to generate instance names. | string | | null |
+| [project_number](variables.tf#L342) | The project number of the project where this instances will be created. Only used for testing purposes. | string | | null |
+| [query_insights_config](variables.tf#L348) | Query insights config. Mutually exclusive with observability_config. It will be ignored if observability_config is enabled. | object({…}) | | {} |
+| [read_pool](variables.tf#L359) | Map of read pool instances to create in the primary cluster. | map(object({…})) | | {} |
+| [skip_await_major_version_upgrade](variables.tf#L415) | Set to true to skip awaiting on the major version upgrade of the cluster. | bool | | true |
+| [subscription_type](variables.tf#L421) | The subscription type of cluster. Possible values are: 'STANDARD' or 'TRIAL'. | string | | "STANDARD" |
+| [tag_bindings](variables.tf#L427) | Tag bindings for this service, in key => tag value id format. | map(string) | | {} |
+| [users](variables.tf#L434) | Map of users to create in the primary instance (and replicated to other replicas). Set PASSWORD to null if you want to get an autogenerated password. The user types available are: 'ALLOYDB_BUILT_IN' or 'ALLOYDB_IAM_USER'. | map(object({…})) | | {} |
## Outputs
diff --git a/modules/alloydb/main.tf b/modules/alloydb/main.tf
index 7110897b3..4d7904757 100644
--- a/modules/alloydb/main.tf
+++ b/modules/alloydb/main.tf
@@ -218,6 +218,14 @@ resource "google_alloydb_instance" "primary" {
}
}
+ dynamic "connection_pool_config" {
+ for_each = var.connection_pool_flags != null ? [""] : []
+ content {
+ enabled = true
+ flags = var.connection_pool_flags
+ }
+ }
+
machine_config {
cpu_count = var.machine_config.cpu_count
machine_type = var.machine_config.machine_type
@@ -439,6 +447,14 @@ resource "google_alloydb_instance" "secondary" {
}
}
+ dynamic "connection_pool_config" {
+ for_each = var.connection_pool_flags != null ? [""] : []
+ content {
+ enabled = true
+ flags = var.connection_pool_flags
+ }
+ }
+
machine_config {
cpu_count = coalesce(try(var.cross_region_replication.secondary_machine_config.cpu_count, null), var.machine_config.cpu_count)
machine_type = local.secondary_machine_type
@@ -526,6 +542,14 @@ resource "google_alloydb_instance" "read_pool_primary" {
}
}
+ dynamic "connection_pool_config" {
+ for_each = var.connection_pool_flags != null ? [""] : []
+ content {
+ enabled = true
+ flags = var.connection_pool_flags
+ }
+ }
+
machine_config {
cpu_count = each.value.machine_config.cpu_count
machine_type = each.value.machine_config.machine_type
@@ -623,6 +647,14 @@ resource "google_alloydb_instance" "read_pool_secondary" {
}
}
+ dynamic "connection_pool_config" {
+ for_each = var.connection_pool_flags != null ? [""] : []
+ content {
+ enabled = true
+ flags = var.connection_pool_flags
+ }
+ }
+
machine_config {
cpu_count = each.value.machine_config.cpu_count
machine_type = each.value.machine_config.machine_type
diff --git a/modules/alloydb/variables.tf b/modules/alloydb/variables.tf
index 20bee0837..0aadc190c 100644
--- a/modules/alloydb/variables.tf
+++ b/modules/alloydb/variables.tf
@@ -87,6 +87,19 @@ variable "cluster_name" {
nullable = false
}
+variable "connection_pool_flags" {
+ description = "Map FLAG_NAME=>VALUE for managed connection pooling."
+ type = map(string)
+ default = null
+ validation {
+ condition = var.connection_pool_flags == null || alltrue([
+ for k in keys(var.connection_pool_flags) :
+ (!startswith(k, "connection-pooling-") && length(regexall("-", k)) == 0)
+ ])
+ error_message = "Remove the \"connection-pooling-\" prefix and use underscores instead of dashes in the name. For example, \"connection-pooling-pool-mode\" would be \"pool_mode\"."
+ }
+}
+
variable "continuous_backup_configuration" {
description = "Continuous backup settings for cluster."
type = object({
diff --git a/tests/modules/alloydb/examples/connection_pool.yaml b/tests/modules/alloydb/examples/connection_pool.yaml
new file mode 100644
index 000000000..1585e1008
--- /dev/null
+++ b/tests/modules/alloydb/examples/connection_pool.yaml
@@ -0,0 +1,85 @@
+# Copyright 2024 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+values:
+ module.alloydb.google_alloydb_cluster.primary:
+ annotations: null
+ cluster_id: db
+ cluster_type: PRIMARY
+ continuous_backup_config:
+ - enabled: true
+ encryption_config: []
+ recovery_window_days: 14
+ database_version: POSTGRES_15
+ deletion_policy: DEFAULT
+ deletion_protection: false
+ display_name: db
+ effective_labels:
+ goog-terraform-provisioned: 'true'
+ encryption_config: []
+ etag: null
+ initial_user: []
+ labels: null
+ location: europe-west8
+ maintenance_update_policy: []
+ network_config:
+ - allocated_ip_range: null
+ network: projects/xxx/global/networks/aaa
+ project: project-id
+ psc_config: []
+ restore_backup_source: []
+ restore_backupdr_backup_source: []
+ restore_backupdr_pitr_source: []
+ restore_continuous_backup_source: []
+ secondary_config: []
+ skip_await_major_version_upgrade: true
+ subscription_type: STANDARD
+ terraform_labels:
+ goog-terraform-provisioned: 'true'
+ timeouts: null
+ module.alloydb.google_alloydb_instance.primary:
+ annotations: null
+ availability_type: REGIONAL
+ connection_pool_config:
+ - enabled: true
+ flags:
+ max_pool_size: '240'
+ pool_mode: transaction
+ deletion_policy: DELETE
+ display_name: db
+ effective_labels:
+ goog-terraform-provisioned: 'true'
+ gce_zone: null
+ instance_id: db
+ instance_type: PRIMARY
+ labels: null
+ machine_config:
+ - cpu_count: 2
+ query_insights_config:
+ - query_plans_per_minute: 5
+ query_string_length: 1024
+ record_application_tags: true
+ record_client_address: true
+ read_pool_config: []
+ terraform_labels:
+ goog-terraform-provisioned: 'true'
+ timeouts: null
+
+counts:
+ google_alloydb_cluster: 1
+ google_alloydb_instance: 1
+ modules: 1
+ resources: 2
+
+outputs: {}