Provides an RDS read write splitting connection resource to allocate an Intranet connection string for RDS instance, see What is DB Read Write Splitting Connection.
variable "name" {
default = "tf-example"
}
data "alicloud_db_zones" "example" {
engine = "MySQL"
engine_version = "5.7"
category = "HighAvailability"
db_instance_storage_type = "local_ssd"
}
data "alicloud_db_instance_classes" "example" {
zone_id = data.alicloud_db_zones.example.ids.0
engine = "MySQL"
engine_version = "5.7"
category = "HighAvailability"
db_instance_storage_type = "local_ssd"
}
resource "alicloud_vpc" "example" {
vpc_name = var.name
cidr_block = "172.16.0.0/16"
}
resource "alicloud_vswitch" "example" {
vpc_id = alicloud_vpc.example.id
cidr_block = "172.16.0.0/24"
zone_id = data.alicloud_db_zones.example.zones.0.id
vswitch_name = var.name
}
resource "alicloud_security_group" "example" {
name = var.name
vpc_id = alicloud_vpc.example.id
}
resource "alicloud_db_instance" "example" {
engine = "MySQL"
engine_version = "5.7"
category = "HighAvailability"
instance_type = data.alicloud_db_instance_classes.example.instance_classes.0.instance_class
instance_storage = data.alicloud_db_instance_classes.example.instance_classes.0.storage_range.min
instance_charge_type = "Postpaid"
db_instance_storage_type = "local_ssd"
instance_name = var.name
vswitch_id = alicloud_vswitch.example.id
security_ips = ["10.168.1.12", "100.69.7.112"]
}
resource "alicloud_db_readonly_instance" "example" {
zone_id = alicloud_db_instance.example.zone_id
master_db_instance_id = alicloud_db_instance.example.id
engine_version = alicloud_db_instance.example.engine_version
instance_storage = alicloud_db_instance.example.instance_storage
instance_type = alicloud_db_instance.example.instance_type
instance_name = "${var.name}readonly"
vswitch_id = alicloud_vswitch.example.id
}
resource "alicloud_db_read_write_splitting_connection" "example" {
instance_id = alicloud_db_readonly_instance.example.master_db_instance_id
connection_prefix = "example-con-123"
distribution_type = "Standard"
}
The following arguments are supported:
instance_id
- (Required, ForceNew) The Id of instance that can run database.distribution_type
- (Required) Read weight distribution mode. Values are as follows: Standard
indicates automatic weight distribution based on types, Custom
indicates custom weight distribution. connection_prefix
- (Optional, ForceNew) Prefix of an Internet connection string. It must be checked for uniqueness. It may consist of lowercase letters, numbers, and underlines, and must start with a letter and have no more than 30 characters. Default to port
- (Optional) Intranet connection port. Valid value: [3001-3999]. Default to 3306.max_delay_time
- (Optional) Delay threshold, in seconds. The value range is 0 to 7200. Default to 30. Read requests are not routed to the read-only instances with a delay greater than the threshold. weight
- (Optional) Read weight distribution. Read weights increase at a step of 100 up to 10,000. Enter weights in the following format: {"Instanceid":"Weight","Instanceid":"Weight"}. This parameter must be set when distribution_type is set to Custom. The following attributes are exported:
id
- The Id of DB instance.connection_string
- Connection instance string.RDS read write splitting connection can be imported using the id, e.g.
$ terraform import alicloud_db_read_write_splitting_connection.example abc12345678