Provides an RDS instance resource. A DB instance is an isolated database environment in the cloud. A DB instance can contain multiple user-created databases.
For information about RDS and how to use it, see What is ApsaraDB for RDS.
data "alicloud_db_zones" "example" {
engine = "MySQL"
engine_version = "8.0"
instance_charge_type = "PostPaid"
category = "Basic"
db_instance_storage_type = "cloud_essd"
}
data "alicloud_db_instance_classes" "example" {
zone_id = data.alicloud_db_zones.example.zones.0.id
engine = "MySQL"
engine_version = "8.0"
category = "Basic"
db_instance_storage_type = "cloud_essd"
instance_charge_type = "PostPaid"
}
resource "alicloud_vpc" "example" {
vpc_name = "terraform-example"
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 = "terraform-example"
}
resource "alicloud_security_group" "example" {
name = "terraform-example"
vpc_id = alicloud_vpc.example.id
}
resource "alicloud_db_instance" "example" {
engine = "MySQL"
engine_version = "8.0"
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"
instance_name = "terraform-example"
vswitch_id = alicloud_vswitch.example.id
monitoring_period = "60"
db_instance_storage_type = "cloud_essd"
security_group_ids = [alicloud_security_group.example.id]
}
data "alicloud_db_zones" "example" {
engine = "MySQL"
engine_version = "8.0"
instance_charge_type = "PostPaid"
category = "Basic"
db_instance_storage_type = "cloud_essd"
}
data "alicloud_db_instance_classes" "example" {
zone_id = data.alicloud_db_zones.example.zones.0.id
engine = "MySQL"
engine_version = "8.0"
category = "Basic"
db_instance_storage_type = "cloud_essd"
instance_charge_type = "PostPaid"
}
resource "alicloud_vpc" "example" {
vpc_name = "terraform-example"
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 = "terraform-example"
}
resource "alicloud_security_group" "example" {
name = "terraform-example"
vpc_id = alicloud_vpc.example.id
}
resource "alicloud_db_instance" "example" {
engine = "MySQL"
engine_version = "8.0"
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"
instance_name = "terraform-example"
vswitch_id = alicloud_vswitch.example.id
monitoring_period = "60"
db_instance_storage_type = "cloud_essd"
security_group_ids = [alicloud_security_group.example.id]
parameters {
name = "delayed_insert_timeout"
value = "600"
}
parameters {
name = "max_length_for_sort_data"
value = "2048"
}
}
data "alicloud_db_zones" "example" {
engine = "MySQL"
engine_version = "8.0"
instance_charge_type = "PostPaid"
category = "Basic"
db_instance_storage_type = "cloud_essd"
}
data "alicloud_db_instance_classes" "example" {
zone_id = data.alicloud_db_zones.example.zones.0.id
engine = "MySQL"
engine_version = "8.0"
category = "Basic"
db_instance_storage_type = "cloud_essd"
instance_charge_type = "PostPaid"
}
resource "alicloud_vpc" "example" {
vpc_name = "terraform-example"
cidr_block = "172.16.0.0/16"
}
resource "alicloud_vswitch" "example" {
count = 2
vpc_id = alicloud_vpc.example.id
cidr_block = format("172.16.%d.0/24", count.index + 1)
zone_id = data.alicloud_db_zones.example.zones[count.index].id
vswitch_name = format("terraform_example_%d", count.index + 1)
}
resource "alicloud_security_group" "example" {
name = "terraform-example"
vpc_id = alicloud_vpc.example.id
}
resource "alicloud_db_instance" "example" {
engine = "MySQL"
engine_version = "8.0"
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"
instance_name = "terraform-example"
vswitch_id = join(",", alicloud_vswitch.example.*.id)
monitoring_period = "60"
db_instance_storage_type = "cloud_essd"
security_group_ids = [alicloud_security_group.example.id]
zone_id = data.alicloud_db_zones.example.zones.0.id
zone_id_slave_a = data.alicloud_db_zones.example.zones.1.id
}
variable "name" {
default = "tf_example"
}
data "alicloud_db_zones" "example" {
engine = "MySQL"
engine_version = "8.0"
instance_charge_type = "PostPaid"
category = "HighAvailability"
db_instance_storage_type = "cloud_essd"
}
data "alicloud_db_instance_classes" "example" {
zone_id = data.alicloud_db_zones.example.zones.0.id
engine = "MySQL"
engine_version = "8.0"
category = "HighAvailability"
instance_charge_type = "PostPaid"
db_instance_storage_type = "cloud_essd"
}
resource "alicloud_vpc" "example" {
vpc_name = var.name
cidr_block = "172.16.0.0/16"
}
resource "alicloud_vswitch" "example" {
count = 2
vpc_id = alicloud_vpc.example.id
cidr_block = format("172.16.%d.0/24", count.index + 1)
zone_id = data.alicloud_db_zones.example.zones[count.index].id
vswitch_name = format("%s_%d", var.name, count.index)
}
resource "alicloud_security_group" "example" {
name = var.name
vpc_id = alicloud_vpc.example.id
}
resource "alicloud_db_instance" "example" {
engine = "MySQL"
engine_version = "8.0"
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"
instance_name = var.name
vswitch_id = join(",", alicloud_vswitch.example.*.id)
monitoring_period = "60"
db_instance_storage_type = "cloud_essd"
zone_id = data.alicloud_db_zones.example.zones.0.id
zone_id_slave_a = data.alicloud_db_zones.example.zones.1.id
}
variable "name" {
default = "tf-example"
}
data "alicloud_db_zones" "example" {
engine = "MySQL"
engine_version = "8.0"
instance_charge_type = "PostPaid"
db_instance_storage_type = "local_ssd"
}
data "alicloud_db_instance_classes" "example" {
zone_id = data.alicloud_db_zones.example.zones.0.id
engine = "MySQL"
engine_version = "8.0"
db_instance_storage_type = "local_ssd"
instance_charge_type = "PostPaid"
}
resource "alicloud_vpc" "example" {
vpc_name = var.name
cidr_block = "172.16.0.0/16"
}
resource "alicloud_vswitch" "example" {
count = 2
vpc_id = alicloud_vpc.example.id
cidr_block = format("172.16.%d.0/24", count.index + 1)
zone_id = data.alicloud_db_zones.example.zones[count.index].id
vswitch_name = format("%s_%d", var.name, count.index)
}
resource "alicloud_security_group" "example" {
name = var.name
vpc_id = alicloud_vpc.example.id
}
resource "alicloud_db_instance" "example" {
engine = "MySQL"
engine_version = "8.0"
category = "Finance"
instance_type = "mysql.n2.xlarge.25"
instance_storage = "20"
instance_charge_type = "Postpaid"
instance_name = var.name
vswitch_id = join(",", alicloud_vswitch.example.*.id)
monitoring_period = "60"
db_instance_storage_type = "local_ssd"
zone_id = data.alicloud_db_zones.example.zones.0.id
zone_id_slave_a = data.alicloud_db_zones.example.zones.1.id
}
variable "name" {
default = "tf-accdbinstance"
}
provider "alicloud" {
region = "cn-hangzhou"
}
data "alicloud_db_zones" "example" {
engine = "MySQL"
engine_version = "8.0"
instance_charge_type = "Serverless"
category = "serverless_basic"
db_instance_storage_type = "cloud_essd"
}
data "alicloud_db_instance_classes" "example" {
zone_id = data.alicloud_db_zones.example.ids.1
engine = "MySQL"
engine_version = "8.0"
category = "serverless_basic"
db_instance_storage_type = "cloud_essd"
instance_charge_type = "Serverless"
commodity_code = "rds_serverless_public_cn"
}
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.ids.1
vswitch_name = var.name
}
resource "alicloud_db_instance" "example" {
engine = "MySQL"
engine_version = "8.0"
instance_storage = data.alicloud_db_instance_classes.example.instance_classes.0.storage_range.min
instance_type = data.alicloud_db_instance_classes.example.instance_classes.0.instance_class
instance_charge_type = "Serverless"
instance_name = var.name
zone_id = data.alicloud_db_zones.example.ids.1
vswitch_id = alicloud_vswitch.example.id
db_instance_storage_type = "cloud_essd"
category = "serverless_basic"
serverless_config {
max_capacity = 8
min_capacity = 0.5
auto_pause = false
switch_force = false
}
}
variable "name" {
default = "tf-accdbinstance"
}
provider "alicloud" {
region = "cn-hangzhou"
}
data "alicloud_db_zones" "example" {
engine = "PostgreSQL"
engine_version = "14.0"
instance_charge_type = "Serverless"
category = "serverless_basic"
db_instance_storage_type = "cloud_essd"
}
data "alicloud_db_instance_classes" "example" {
zone_id = data.alicloud_db_zones.example.ids.1
engine = "PostgreSQL"
engine_version = "14.0"
category = "serverless_basic"
db_instance_storage_type = "cloud_essd"
instance_charge_type = "Serverless"
commodity_code = "rds_serverless_public_cn"
}
data "alicloud_vpcs" "default" {
name_regex = "^default-NODELETING$"
}
data "alicloud_vswitches" "default" {
vpc_id = data.alicloud_vpcs.default.ids.0
zone_id = data.alicloud_db_zones.example.ids.1
}
resource "alicloud_db_instance" "example" {
engine = "PostgreSQL"
engine_version = "14.0"
instance_storage = data.alicloud_db_instance_classes.example.instance_classes.0.storage_range.min
instance_type = data.alicloud_db_instance_classes.example.instance_classes.0.instance_class
instance_charge_type = "Serverless"
instance_name = var.name
zone_id = data.alicloud_db_zones.example.ids.1
vswitch_id = data.alicloud_vswitches.default.ids.0
db_instance_storage_type = "cloud_essd"
category = "serverless_basic"
serverless_config {
max_capacity = 12
min_capacity = 0.5
}
}
variable "name" {
default = "tf-accdbinstance"
}
provider "alicloud" {
region = "cn-hangzhou"
}
data "alicloud_db_zones" "example" {
engine = "SQLServer"
engine_version = "2019_std_sl"
instance_charge_type = "Serverless"
category = "serverless_ha"
db_instance_storage_type = "cloud_essd"
}
data "alicloud_db_instance_classes" "example" {
zone_id = data.alicloud_db_zones.example.ids.1
engine = "SQLServer"
engine_version = "2019_std_sl"
category = "serverless_ha"
db_instance_storage_type = "cloud_essd"
instance_charge_type = "Serverless"
commodity_code = "rds_serverless_public_cn"
}
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.ids.1
vswitch_name = var.name
}
resource "alicloud_db_instance" "example" {
engine = "SQLServer"
engine_version = "2019_std_sl"
instance_storage = data.alicloud_db_instance_classes.example.instance_classes.0.storage_range.min
instance_type = data.alicloud_db_instance_classes.example.instance_classes.0.instance_class
instance_charge_type = "Serverless"
instance_name = var.name
zone_id = data.alicloud_db_zones.example.ids.1
zone_id_slave_a = data.alicloud_db_zones.example.ids.1
vswitch_id = join(",", [alicloud_vswitch.example.id, alicloud_vswitch.example.id])
db_instance_storage_type = "cloud_essd"
category = "serverless_ha"
serverless_config {
max_capacity = 8
min_capacity = 2
}
}
alicloud_db_instance
or removing it from your configurationThe alicloud_db_instance
resource allows you to manage instance_charge_type = "Prepaid"
db instance, but Terraform cannot destroy it.
Deleting the subscription resource or removing it from your configuration will remove it from your state file and management, but will not destroy the DB Instance.
You can resume managing the subscription db instance via the AlibabaCloud Console.
The following arguments are supported:
engine
- (Required, ForceNew) Database type. Value options: MySQL, SQLServer, PostgreSQL, MariaDB.db_instance_storage_type
- (Optional, Available since 1.68.0) The storage type of the instance. Serverless instance, only cloud_essd
can be selected. Valid values:
db_time_zone
- (Optional, ForceNew, Available since 1.136.0) The time zone of the instance. This parameter takes effect only when you set the Engine
parameter to MySQL or PostgreSQL.
Engine
parameter to MySQL.Engine
parameter to PostgreSQL.instance_name
- (Optional) The name of DB instance. It a string of 2 to 256 characters.connection_string_prefix
- (Optional, Available since 1.126.0) The private connection string prefix. If you want to update public connection string prefix, please use resource alicloud_db_connection connection_prefix.
-> NOTE: The prefix must be 8 to 64 characters in length and can contain letters, digits, and hyphens (-). It cannot contain Chinese characters and special characters ~!#%^&*=+\|{};:'",<>/?port
- (Optional, Available since 1.126.0) The private port of the database service. If you want to update public port, please use resource alicloud_db_connection port.instance_charge_type
- (Optional) Valid values are Prepaid
, Postpaid
, Serverless
, Default to Postpaid
. Currently, the resource only supports PostPaid to PrePaid. For more information, see Overview.resource_group_id
(Optional, Computed, Available since 1.86.0, Modifiable in 1.115.0) The ID of resource group which the DB instance belongs.period
- (Optional) The duration that you will buy DB instance (in month). It is valid when instance_charge_type is PrePaid
. Valid values: [1~9], 12, 24, 36.
-> NOTE: The attribute period
is only used to create Subscription instance or modify the PayAsYouGo instance to Subscription. Once effect, it will not be modified that means running terraform apply
will not effect the resource.monitoring_period
- (Optional) The monitoring frequency in seconds. Valid values are 5, 10, 60, 300. Defaults to 300.auto_renew
- (Optional, Available since 1.34.0) Whether to renewal a DB instance automatically or not. It is valid when instance_charge_type is PrePaid
. Default to false
.auto_renew_period
- (Optional, Available since 1.34.0) Auto-renewal period of an instance, in the unit of the month. It is valid when instance_charge_type is PrePaid
. Valid value:[1~12], Default to 1.zone_id
- (ForceNew, Optional) The Zone to launch the DB instance. From version 1.8.1, it supports multiple zone.
If it is a multi-zone and vswitch_id
is specified, the vswitch must in the one of them.
The multiple zone ID can be retrieved by setting multi
to "true" in the data source alicloud_zones
.vswitch_id
- (ForceNew, Optional) The virtual switch ID to launch DB instances in one VPC. If there are multiple vswitches, separate them with commas.private_ip_address
- (Optional, Available since v1.125.0) The private IP address of the instance. The private IP address must be within the Classless Inter-Domain Routing (CIDR) block of the vSwitch that is specified by the VSwitchId parameter.security_ips
- (Optional) List of IP addresses allowed to access all databases of an instance. The list contains up to 1,000 IP addresses, separated by commas. Supported formats include 0.0.0.0/0, 10.23.12.24 (IP), and 10.23.12.24/24 (Classless Inter-Domain Routing (CIDR) mode. /24 represents the length of the prefix in an IP address. The range of the prefix length is [1,32]).db_instance_ip_array_name
- (Optional, Available since 1.125.0) The name of the IP address whitelist. Default value: Default.Note: From 1.63.0, the tag key and value are case sensitive. Before that, they are not case sensitive.
security_group_id
- (Deprecated) It has been deprecated from 1.69.0 and use security_group_ids
instead.security_group_ids
- (Optional, List(string), Available since 1.69.0) The list IDs to join ECS Security Group. At most supports three security groups.maintain_time
- (Optional, Available since 1.56.0) Maintainable time period format of the instance: HH:MMZ-HH:MMZ (UTC time)auto_upgrade_minor_version
- (Optional, Available since 1.62.1) The upgrade method to use. Valid values:
See more details and limitation.
upgrade_db_instance_kernel_version
- (Deprecated from v1.198.0) Whether to upgrade a minor version of the kernel. Valid values:
upgrade_time
- (Optional, Available since 1.126.0) The method to update the minor engine version. Default value: Immediate. It is valid only when target_minor_version
is changed. Valid values:
switch_time
- (Optional, Available since 1.126.0) The specific point in time when you want to perform the update. Specify the time in the ISO 8601 standard in the yyyy-MM-ddTHH:mm:ssZ format. It is valid only when target_minor_version
is changed. The time must be in UTC.category
- (Optional, Available since 1.187.0) The RDS edition of the instance. If you want to create a serverless instance, you must use this value. Valid values:
serverless_config
- (Optional, Available since 1.200.0) The settings of the serverless instance. This parameter is required when you create a serverless instance. This parameter takes effect only when you create an ApsaraDB RDS for Serverless instance. See serverless_config
below.role_arn
- (ForceNew, Optional, Available since 1.208.0) The Alibaba Cloud Resource Name (ARN) of the RAM role.direction
- (Optional, Available since v1.209.1) The instance configuration type. Valid values: ["Up", "Down", "TempUpgrade", "Serverless"]
node_id
- (Optional, Available since v1.213.1) The globally unique identifier (GUID) of the secondary instance. You can call the DescribeDBInstanceHAConfig operation to query the GUID of the secondary instance.
force
- (Optional, ForceNew, Available since v1.213.1) Specifies whether to enable forcible switching. Valid values:
db_param_group_id
- (Optional, Available since v1.222.0) Parameter template ID. Only MySQL and PostgreSQL support this parameter. If this parameter is not specified, the default parameter template is used. You can also customize a parameter template and use it here.
parameters
The parameters mapping supports the following:
name
- (Required) The parameter name.value
- (Required) The parameter value.pg_hba_conf
The pg_hba_conf mapping supports the following:
type
- (Required) The type of connection to the instance. Valid values:
babelfish_config
The babelfish_config mapping supports the following:
babelfish_enabled
- (Required, ForceNew) specifies whether to enable the Babelfish for the instance. If you set this parameter to true, you enable Babelfish for the instance. If you leave this parameter empty, you disable Babelfish for the instance.migration_mode
- (Required, ForceNew) The migration mode of the instance. Valid values: single-db and multi-db.master_username
- (Required, ForceNew) The name of the administrator account. The name can contain lowercase letters, digits, and underscores (_). It must start with a letter and end with a letter or digit. It can be up to 63 characters in length and cannot start with pg.master_user_password
- (Required, ForceNew) The password of the administrator account. The password must contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters. It must be 8 to 32 characters in length. The password can contain any of the following characters:! @ # $ % ^ & * () _ + - =serverless_config
The serverless_config mapping supports the following:
max_capacity
- (Required, Available since 1.200.0) The maximum number of RDS Capacity Units (RCUs). The value of this parameter must be greater than or equal to min_capacity
and only supports passing integers. Valid values:
min_capacity
- (Required, Available since 1.200.0) The minimum number of RCUs. The value of this parameter must be less than or equal to max_capacity
. Valid values:
auto_pause
- (Optional, Available since 1.200.0) Specifies whether to enable the smart startup and stop feature for the serverless instance. Valid values:
switch_force
- (Optional, Available since 1.200.0) Specifies whether to enable the forced scaling feature for the serverless instance. Valid values:
The following attributes are exported:
id
- The RDS instance ID.connection_string
- RDS database connection string.ssl_status
- Status of the SSL feature. Yes
: SSL is turned on; No
: SSL is turned off.db_instance_type
- (Available since 1.197.0) The type of db instance.status
- (Available since 1.204.1) The status of db instance.create_time
- (Available since 1.204.1) The creation time of db instance.The timeouts
block allows you to specify timeouts for certain actions:
create
- (Defaults to 50 mins) Used when creating the db instance (until it reaches the initial Running
status).update
- (Defaults to 30 mins) Used when updating the db instance (until it reaches the initial Running
status).delete
- (Defaults to 20 mins) Used when terminating the db instance.RDS instance can be imported using the id, e.g.
$ terraform import alicloud_db_instance.example rm-abc12345678