Skip to content

Commit 40e6f44

Browse files
lsy1968shanye997
authored andcommitted
201-use-case-create-postgresql-db-for-rds-instance
1 parent 3eafed5 commit 40e6f44

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## Introduction
2+
3+
<!-- DOCS_DESCRIPTION_CN -->
4+
本示例用于在阿里云上创建一个PostgreSQL类型的云数据库实例。
5+
本示例来自[创建一个云数据库实例](https://help.aliyun.com/document_detail/111635.html)
6+
<!-- DOCS_DESCRIPTION_CN -->
7+
8+
<!-- DOCS_DESCRIPTION_EN -->
9+
This example is used to create a PostgreSQL database for RDS instance on Alibaba Cloud.
10+
This example is from [Create an ApsaraDB for RDS instance](https://help.aliyun.com/document_detail/111635.html).
11+
<!-- DOCS_DESCRIPTION_EN -->
12+
13+
<!-- BEGIN_TF_DOCS -->
14+
## Providers
15+
16+
| Name | Version |
17+
|------|---------|
18+
| <a name="provider_alicloud"></a> [alicloud](#provider\_alicloud) | n/a |
19+
20+
## Modules
21+
22+
No modules.
23+
24+
## Resources
25+
26+
| Name | Type |
27+
|------|------|
28+
| [alicloud_db_account_privilege.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/db_account_privilege) | resource |
29+
| [alicloud_db_connection.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/db_connection) | resource |
30+
| [alicloud_db_database.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/db_database) | resource |
31+
| [alicloud_db_instance.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/db_instance) | resource |
32+
| [alicloud_rds_account.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/rds_account) | resource |
33+
| [alicloud_vpc.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vpc) | resource |
34+
| [alicloud_vswitch.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
35+
| [alicloud_db_zones.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/db_zones) | data source |
36+
37+
## Inputs
38+
39+
| Name | Description | Type | Default | Required |
40+
|------|-------------|------|---------|:--------:|
41+
| <a name="input_account_name"></a> [account\_name](#input\_account\_name) | 设置RDS账号名称 | `string` | `"tf_example"` | no |
42+
| <a name="input_account_password"></a> [account\_password](#input\_account\_password) | 设置RDS账号的密码 | `string` | `"!Qaz1234"` | no |
43+
| <a name="input_connection_prefix"></a> [connection\_prefix](#input\_connection\_prefix) | 设置外网连接地址的前缀 | `string` | `"test1234"` | no |
44+
| <a name="input_db_category"></a> [db\_category](#input\_db\_category) | 设置产品系列,按量付费类型实例只支持Basic、HighAvailability、cluster。 | `string` | `"Basic"` | no |
45+
| <a name="input_db_instance_storage_type"></a> [db\_instance\_storage\_type](#input\_db\_instance\_storage\_type) | 设置实例存储类型 | `string` | `"cloud_essd"` | no |
46+
| <a name="input_engine_version"></a> [engine\_version](#input\_engine\_version) | 设置数据库版本 | `string` | `"14.0"` | no |
47+
| <a name="input_instance_storage"></a> [instance\_storage](#input\_instance\_storage) | 设置存储空间 | `string` | `"20"` | no |
48+
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | 设置实例规格 | `string` | `"pg.n2.2c.1m"` | no |
49+
| <a name="input_name"></a> [name](#input\_name) | 设置资源名称 | `string` | `"tf_test"` | no |
50+
<!-- END_TF_DOCS -->
51+
52+
## Documentation
53+
<!-- docs-link -->
54+
55+
The template is based on Aliyun document: [Create an ApsaraDB for RDS instance](https://help.aliyun.com/document_detail/111635.html)
56+
57+
<!-- docs-link -->
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
provider "alicloud" {
2+
region = "cn-hangzhou"
3+
}
4+
5+
# 设置资源名称
6+
variable "name" {
7+
default = "tf_test"
8+
}
9+
10+
# 设置产品系列,按量付费类型实例只支持Basic、HighAvailability、cluster。
11+
variable "db_category" {
12+
default = "Basic"
13+
}
14+
15+
# 设置实例规格
16+
variable "instance_type" {
17+
default = "pg.n2.2c.1m"
18+
}
19+
20+
# 设置实例存储类型
21+
variable "db_instance_storage_type" {
22+
default = "cloud_essd"
23+
}
24+
25+
# 设置数据库版本
26+
variable "engine_version" {
27+
default = "14.0"
28+
}
29+
30+
# 设置存储空间
31+
variable "instance_storage" {
32+
default = "20"
33+
}
34+
35+
# 设置RDS账号名称
36+
variable "account_name" {
37+
default = "tf_example"
38+
}
39+
40+
# 设置RDS账号的密码
41+
variable "account_password" {
42+
default = "!Qaz1234"
43+
}
44+
45+
#设置外网连接地址的前缀
46+
variable "connection_prefix" {
47+
default = "test1234"
48+
}
49+
50+
# 查询符合以下要求的可用区
51+
data "alicloud_db_zones" "default" {
52+
engine = "PostgreSQL"
53+
engine_version = var.engine_version
54+
instance_charge_type = "PostPaid"
55+
category = var.db_category
56+
db_instance_storage_type = var.db_instance_storage_type
57+
}
58+
59+
# 专有网络
60+
resource "alicloud_vpc" "default" {
61+
vpc_name = var.name
62+
cidr_block = "10.1.0.0/21"
63+
}
64+
65+
# 交换机
66+
resource "alicloud_vswitch" "default" {
67+
vswitch_name = var.name
68+
vpc_id = alicloud_vpc.default.id
69+
cidr_block = "10.1.1.0/24"
70+
zone_id = data.alicloud_db_zones.default.zones[0].id
71+
}
72+
73+
# RDS数据库实例
74+
resource "alicloud_db_instance" "default" {
75+
engine = "PostgreSQL"
76+
engine_version = var.engine_version
77+
instance_type = var.instance_type
78+
category = var.db_category
79+
db_instance_storage_type = var.db_instance_storage_type
80+
instance_storage = var.instance_storage
81+
instance_charge_type = "Postpaid"
82+
instance_name = var.name
83+
vswitch_id = alicloud_vswitch.default.id
84+
}
85+
86+
# ProgresSQL数据库
87+
resource "alicloud_db_database" "default" {
88+
instance_id = alicloud_db_instance.default.id
89+
name = var.name
90+
}
91+
92+
# RDS账号
93+
resource "alicloud_rds_account" "default" {
94+
db_instance_id = alicloud_db_instance.default.id
95+
account_name = var.account_name
96+
account_password = var.account_password
97+
}
98+
99+
# 授权账号访问数据库
100+
resource "alicloud_db_account_privilege" "default" {
101+
instance_id = alicloud_db_instance.default.id
102+
account_name = alicloud_rds_account.default.account_name
103+
privilege = "DBOwner"
104+
db_names = [alicloud_db_database.default.name]
105+
}
106+
107+
# 申请外网连接地址
108+
resource "alicloud_db_connection" "default" {
109+
instance_id = alicloud_db_instance.default.id
110+
connection_prefix = var.connection_prefix
111+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
required_providers {
3+
alicloud = {
4+
source = "aliyun/alicloud"
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)