通過(guò) Terraform 增加 RDS PostgreSQL 實(shí)例的數(shù)據(jù)安全性
本文介紹如何使用Terraform修改安全組、修改IP白名單、修改SSL配置以及切換高安全模式。
本教程所含示例代碼支持一鍵運(yùn)行,您可以直接運(yùn)行代碼。一鍵運(yùn)行
前提條件
已創(chuàng)建RDS PostgreSQL實(shí)例,詳情請(qǐng)參見(jiàn)創(chuàng)建RDS PostgreSQL實(shí)例。
實(shí)例狀態(tài)為運(yùn)行中,您可以通過(guò)如下兩種方式查看:
參見(jiàn)查詢實(shí)例詳情查看參數(shù)status,如果取值為Running則表示實(shí)例狀態(tài)為運(yùn)行中。
前往RDS管理控制臺(tái),切換到目標(biāo)地域,找到指定實(shí)例后,查看實(shí)例狀態(tài)。
由于阿里云賬號(hào)(主賬號(hào))具有資源的所有權(quán)限,一旦發(fā)生泄露將面臨重大風(fēng)險(xiǎn)。建議您使用RAM用戶,并為該RAM用戶創(chuàng)建AccessKey,具體操作方式請(qǐng)參見(jiàn)創(chuàng)建RAM用戶和創(chuàng)建AccessKey。
通過(guò)RAM授權(quán),RAM用戶可以有效地管理其云資源訪問(wèn)權(quán)限,適應(yīng)多用戶協(xié)同工作的需求,并且能夠按需為用戶分配最小權(quán)限,避免權(quán)限過(guò)大導(dǎo)致的安全漏洞。具體操作方式請(qǐng)參見(jiàn)為RAM用戶授權(quán)。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "vpc:DescribeVpcAttribute", "vpc:DescribeRouteTableList", "vpc:DescribeVSwitchAttributes", "vpc:DeleteVpc", "vpc:DeleteVSwitch", "vpc:CreateVpc", "vpc:CreateVSwitch", "vpc:DescribeVSwitches", "ecs:CreateSecurityGroup", "ecs:ModifySecurityGroupPolicy", "ecs:DescribeSecurityGroups", "ecs:ListTagResources", "ecs:DeleteSecurityGroup", "ecs:DescribeSecurityGroupAttribute", "ecs:AuthorizeSecurityGroup", "ecs:RevokeSecurityGroup" ], "Resource": "*" }, { "Action": "rds:*", "Resource": "*", "Effect": "Allow" }, { "Action": "dbs:*", "Resource": "acs:rds:*:*:*", "Effect": "Allow" }, { "Action": "hdm:*", "Resource": "acs:rds:*:*:*", "Effect": "Allow" }, { "Action": "dms:LoginDatabase", "Resource": "acs:rds:*:*:*", "Effect": "Allow" }, { "Effect": "Allow", "Action": "ram:CreateServiceLinkedRole", "Resource": "*", "Condition": { "StringEquals": { "ram:ServiceName": [ "backupencryption.rds.aliyuncs.com" ] } } }, { "Effect": "Allow", "Action": "bss:ModifyAgreementRecord", "Resource": "*" }, { "Effect": "Allow", "Action": [ "bss:DescribeOrderList", "bss:DescribeOrderDetail", "bss:PayOrder", "bss:CancelOrder" ], "Resource": "*" } ] }
準(zhǔn)備Terraform運(yùn)行環(huán)境,您可以選擇以下任一方式來(lái)使用Terraform。
在Terraform Explorer中使用Terraform:阿里云提供了Terraform的在線運(yùn)行環(huán)境,您無(wú)需安裝Terraform,登錄后即可在線使用和體驗(yàn)Terraform。適用于零成本、快速、便捷地體驗(yàn)和調(diào)試Terraform的場(chǎng)景。
Cloud Shell:阿里云Cloud Shell中預(yù)裝了Terraform的組件,并已配置好身份憑證,您可直接在Cloud Shell中運(yùn)行Terraform的命令。適用于低成本、快速、便捷地訪問(wèn)和使用Terraform的場(chǎng)景。
在本地安裝和配置Terraform:適用于網(wǎng)絡(luò)連接較差或需要自定義開(kāi)發(fā)環(huán)境的場(chǎng)景。
使用的資源
本教程示例包含的部分資源會(huì)產(chǎn)生一定費(fèi)用,請(qǐng)?jiān)诓恍枰獣r(shí)及時(shí)進(jìn)行釋放或退訂。
alicloud_vpc:創(chuàng)建專有網(wǎng)絡(luò)VPC。
alicloud_vswitch:創(chuàng)建專有網(wǎng)絡(luò)交換機(jī)。
alicloud_db_instance:創(chuàng)建RDS PostgreSQL實(shí)例。
修改安全組
以修改實(shí)例安全組為sg-****
為例。
創(chuàng)建一個(gè)工作目錄,并在該工作目錄中創(chuàng)建名為main.tf的配置文件,然后將以下代碼復(fù)制到main.tf中。
創(chuàng)建前置資源。
variable "region" { default = "cn-shenzhen" } variable "zone_id" { default = "cn-shenzhen-c" } variable "instance_type" { default = "pg.n2.2c.2m" } provider "alicloud" { region = var.region } # 創(chuàng)建VPC resource "alicloud_vpc" "main" { vpc_name = "alicloud" cidr_block = "172.16.0.0/16" } # 創(chuàng)建交換機(jī) resource "alicloud_vswitch" "main" { vpc_id = alicloud_vpc.main.id cidr_block = "172.16.192.0/20" zone_id = var.zone_id } # 新建安全組 resource "alicloud_security_group" "example" { name = "terraform-example" vpc_id = alicloud_vpc.main.id } # 創(chuàng)建RDS PostgreSQL實(shí)例 resource "alicloud_db_instance" "instance" { engine = "PostgreSQL" engine_version = "13.0" instance_type = var.instance_type instance_storage = "30" instance_charge_type = "Postpaid" vswitch_id = alicloud_vswitch.main.id }
在main.tf文件的
resource "alicloud_db_instance" "instance" {}
中增加security_group_ids
配置項(xiàng),具體配置如下。... resource "alicloud_db_instance" "instance" { ... security_group_ids = [alicloud_security_group.example.id] }
說(shuō)明修改安全組會(huì)覆蓋原有安全組,后續(xù)修改時(shí)請(qǐng)?jiān)?span data-tag="ph" id="codeph-njq-mk0-5bt" class="ph">
[]
中追加即可,使用,
分隔。執(zhí)行以下命令,初始化Terraform運(yùn)行環(huán)境。
terraform init
返回如下信息,表示Terraform初始化成功。
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
創(chuàng)建執(zhí)行計(jì)劃,并預(yù)覽變更。
terraform plan
執(zhí)行以下命令,創(chuàng)建資源。
terraform apply
在執(zhí)行過(guò)程中,根據(jù)提示輸入
yes
并按下Enter鍵,等待命令執(zhí)行完成,若出現(xiàn)以下信息,則表示運(yùn)行成功。alicloud_db_instance.instance: Modifying... [id=pgm-****] alicloud_db_instance.instance: Modifications complete after 4s [id=pgm-****] Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
驗(yàn)證結(jié)果。
執(zhí)行terraform show命令
您可以使用以下命令查看安全組
terraform show
# alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { client_ca_enabled = 0 client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-****" db_instance_storage_type = "cloud_essd" db_time_zone = "Asia/shenzhen" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Manual" id = "pgm-****" instance_charge_type = "Postpaid" instance_name = "terraformtest" instance_storage = 50 instance_type = "pg.n2.2c.2m" maintain_time = "05:00Z-06:00Z" manual_ha_time = "2022-09-30T09:00:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "192.168.XX.XX" resource_group_id = "rg-****" security_group_id = "sg-****" security_group_ids = [ "sg-****", ] security_ip_mode = "normal" security_ips = [ "127.0.0.1", ] sql_collector_config_value = 30 sql_collector_status = "Disabled" storage_auto_scale = "Enable" storage_threshold = 30 storage_upper_bound = 100 target_minor_version = "rds_postgres_1300_20220830" tcp_connection_type = "SHORT" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-hangzhou-j" pg_hba_conf { address = "127.0.0.1" database = "all" method = "md5" priority_id = 1 type = "host" user = "all" } }
登錄RDS管理控制臺(tái)
登錄RDS管理控制臺(tái)查看安全組。
修改IP白名單
以修改白名單為0.0.0.0/0
為例。
在main.tf文件的
resource "alicloud_db_instance" "instance" {}
中增加security_ips
配置項(xiàng),具體配置如下。... resource "alicloud_db_instance" "instance" { ... security_ips = ["0.0.0.0/0"] }
說(shuō)明修改白名單會(huì)覆蓋原有默認(rèn)白名單,后續(xù)修改時(shí)請(qǐng)?jiān)?span data-tag="ph" id="codeph-81u-kqo-r06" class="ph">
[]
中追加即可,使用,
分隔。執(zhí)行以下命令。
terraform apply
在執(zhí)行過(guò)程中,根據(jù)提示輸入
yes
并按下Enter鍵,等待命令執(zhí)行完成,若出現(xiàn)以下信息,則表示運(yùn)行成功。alicloud_db_instance.instance: Modifying... [id=pgm-****] alicloud_db_instance.instance: Modifications complete after 6s [id=pgm-****] Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
驗(yàn)證結(jié)果。
執(zhí)行terraform show命令
您可以使用以下命令查看IP白名單。
terraform show
# alicloud_db_account.account: resource "alicloud_db_account" "account" { account_name = "tf_account_test" account_password = (sensitive value) account_type = "Normal" db_instance_id = "pgm-****" id = "pgm-****:tf_account_test" instance_id = "pgm-****" name = "tf_account_test" status = "Available" type = "Normal" } # alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { client_ca_enabled = 0 client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-****" db_instance_storage_type = "cloud_essd" db_time_zone = "Asia/shenzhen" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Auto" id = "pgm-****" instance_charge_type = "Postpaid" instance_name = "terraformtest" instance_storage = 50 instance_type = "pg.n2.2c.2m" maintain_time = "05:00Z-06:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "172.16.XX.XX" resource_group_id = "rg-****" security_group_ids = [] security_ip_mode = "normal" security_ips = [ "127.0.0.1", ] sql_collector_config_value = 30 sql_collector_status = "Disabled" storage_auto_scale = "Enable" storage_threshold = 30 storage_upper_bound = 100 target_minor_version = "rds_postgres_1300_20220730" tcp_connection_type = "SHORT" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-heyuan-j" pg_hba_conf { address = "127.0.0.1" database = "all" method = "md5" priority_id = 1 type = "host" user = "all" } }
登錄RDS管理控制臺(tái)
登錄RDS管理控制臺(tái)查看IP白名單。
修改SSL設(shè)置
以開(kāi)啟SSL加密為例。
在main.tf文件的
resource "alicloud_db_instance" "instance" {}
中增加ssl_action
配置項(xiàng),具體配置如下:... resource "alicloud_db_instance" "instance" { ... ssl_action = "Open" }
執(zhí)行以下命令。
terraform apply
在執(zhí)行過(guò)程中,根據(jù)提示輸入
yes
并按下Enter鍵,等待命令執(zhí)行完成,若出現(xiàn)以下信息,則表示運(yùn)行成功。alicloud_db_instance.instance: Modifying... [id=pgm-****] alicloud_db_instance.instance: Still modifying... [id=pgm-****, 10s elapsed] ... alicloud_db_instance.instance: Still modifying... [id=pgm-****, 6m31s elapsed] alicloud_db_instance.instance: Modifications complete after 6m35s [id=pgm-****] Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
驗(yàn)證結(jié)果。
執(zhí)行terraform show命令
您可以使用以下命令查看SSL設(shè)置。
terraform show
# alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { acl = "prefer" ca_type = "aliyun" client_ca_enabled = 0 client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-****" db_instance_storage_type = "cloud_essd" db_time_zone = "Asia/shenzhen" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Auto" id = "pgm-****" instance_charge_type = "Postpaid" instance_name = "terraformtest" instance_storage = 50 instance_type = "pg.n2.2c.2m" maintain_time = "05:00Z-06:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "192.168.XX.XX" replication_acl = "prefer" resource_group_id = "rg-****" security_group_id = "sg-****" security_group_ids = [ "sg-****", ] security_ip_mode = "normal" security_ips = [ "0.0.0.0/0", ] server_cert = <<-EOT -----BEGIN CERTIFICATE----- MIIE7jCCA9agAwIBAgICO3****2 N9xwKlPQ65q/kux0yErtwhAD -----END CERTIFICATE----- EOT server_key = <<-EOT -----BEGIN RSA PRIVATE KEY----- MIIJKQIBAAKCAgEAux0yE****e+VAdGp -----END RSA PRIVATE KEY----- EOT sql_collector_config_value = 30 sql_collector_status = "Disabled" ssl_action = "Open" ssl_status = "0" storage_auto_scale = "Enable" storage_threshold = 30 storage_upper_bound = 100 target_minor_version = "rds_postgres_1300_20220830" tcp_connection_type = "SHORT" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-hangzhou-j" pg_hba_conf { address = "127.0.0.1" database = "all" method = "md5" priority_id = 1 type = "host" user = "all" } }
登錄RDS管理控制臺(tái)
登錄RDS管理控制臺(tái)查看SSL設(shè)置。
切換高安全白名單模式(僅本地盤實(shí)例適用)
在main.tf文件的
resource "alicloud_db_instance" "instance" {}
中增加security_ip_mode
配置項(xiàng),具體配置如下。... resource "alicloud_db_instance" "instance" { ... security_ip_mode = "safety" }
說(shuō)明高安全模式無(wú)法切換回通用模式。
執(zhí)行以下命令。
terraform apply
在執(zhí)行過(guò)程中,根據(jù)提示輸入
yes
并按下Enter鍵,等待命令執(zhí)行完成,若出現(xiàn)以下信息,則表示運(yùn)行成功。alicloud_db_instance.instance: Modifying... [id=pgm-****] alicloud_db_instance.instance: Modifications complete after 3s [id=pgm-****] Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
驗(yàn)證結(jié)果。
執(zhí)行terraform show命令
您可以使用以下命令查看白名單模式。
terraform show
# alicloud_db_instance.instance: resource "alicloud_db_instance" "instance" { acl = "prefer" ca_type = "aliyun" client_ca_enabled = 0 client_crl_enabled = 0 connection_string = "pgm-****.pg.rds.aliyuncs.com" connection_string_prefix = "pgm-****" db_instance_storage_type = "cloud_essd" db_time_zone = "Asia/shenzhen" deletion_protection = false engine = "PostgreSQL" engine_version = "13.0" force_restart = false ha_config = "Auto" id = "pgm-****" instance_charge_type = "Postpaid" instance_name = "terraformtest" instance_storage = 50 instance_type = "pg.n2.2c.2m" maintain_time = "05:00Z-06:00Z" monitoring_period = 300 period = 0 port = "5432" private_ip_address = "192.168.XX.XX" replication_acl = "prefer" resource_group_id = "rg-****" security_group_id = "sg-****" security_group_ids = [ "sg-****", ] security_ip_mode = "safety" security_ip_mode = "normal" security_ips = [ "0.0.0.0/0", ] server_cert = <<-EOT -----BEGIN CERTIFICATE----- MIIE7jCCA9agAwIBAgICO3****2 N9xwKlPQ65q/kux0yErtwhAD -----END CERTIFICATE----- EOT server_key = <<-EOT -----BEGIN RSA PRIVATE KEY----- MIIJKQIBAAKCAgEAux0yE****e+VAdGp -----END RSA PRIVATE KEY----- EOT sql_collector_config_value = 30 sql_collector_status = "Disabled" ssl_action = "Open" ssl_status = "0" storage_auto_scale = "Enable" storage_threshold = 30 storage_upper_bound = 100 target_minor_version = "rds_postgres_1300_20220830" tcp_connection_type = "SHORT" vpc_id = "vpc-****" vswitch_id = "vsw-****" zone_id = "cn-hangzhou-j" pg_hba_conf { address = "127.0.0.1" database = "all" method = "md5" priority_id = 1 type = "host" user = "all" } }
登錄RDS管理控制臺(tái)
登錄RDS管理控制臺(tái)查看賬號(hào)信息。
清理資源
當(dāng)您不再需要上述通過(guò)Terraform創(chuàng)建或管理的資源時(shí),請(qǐng)運(yùn)行以下命令以釋放資源。關(guān)于terraform destroy
的更多信息,請(qǐng)參見(jiàn)Terraform常用命令。
terraform destroy
完整示例
當(dāng)前示例代碼支持一鍵運(yùn)行,您可以直接運(yùn)行代碼。一鍵運(yùn)行
示例代碼
variable "region" {
default = "cn-shenzhen"
}
variable "zone_id" {
default = "cn-shenzhen-c"
}
variable "instance_type" {
default = "pg.n2.2c.2m"
}
variable "target_minor_version" {
default = "rds_postgres_1300_20240830"
}
variable "security_ips" {
default = "0.0.0.0/0"
}
provider "alicloud" {
region = var.region
}
# 創(chuàng)建VPC
resource "alicloud_vpc" "main" {
vpc_name = "alicloud"
cidr_block = "172.16.0.0/16"
}
# 創(chuàng)建交換機(jī)
resource "alicloud_vswitch" "main" {
vpc_id = alicloud_vpc.main.id
cidr_block = "172.16.192.0/20"
zone_id = var.zone_id
}
resource "alicloud_security_group" "example" {
name = "terraform-example"
vpc_id = alicloud_vpc.main.id
}
# 創(chuàng)建RDS PostgreSQL實(shí)例
resource "alicloud_db_instance" "instance" {
engine = "PostgreSQL"
engine_version = "13.0"
instance_type = var.instance_type
instance_storage = "30"
instance_charge_type = "Postpaid"
vswitch_id = alicloud_vswitch.main.id
# 修改安全組
# security_group_ids = [alicloud_security_group.example.id]
# 修改IP白名單
# security_ips = [var.security_ips]
# 修改SSL設(shè)置
# ssl_action = "Open"
# 切換高安全白名單模式(僅本地盤實(shí)例適用)
# security_ip_mode = "safety"
}
如果您想體驗(yàn)更多完整示例,請(qǐng)前往更多完整示例中對(duì)應(yīng)產(chǎn)品的文件夾查看。