Terraform集成示例
Terraform是一個開源工具,用于安全高效地預(yù)配和管理云基礎(chǔ)架構(gòu)和資源。本文為您演示如何通過Terraform創(chuàng)建RDS PostgreSQL實例。
支持資源列表
支持Terraform編排和使用的RDS資源和數(shù)據(jù)源清單,請參見云數(shù)據(jù)庫RDS的資源和數(shù)據(jù)源。如果您還不了解Terraform,請參見Terraform產(chǎn)品介紹。
Resource
alicloud_db_account:管理數(shù)據(jù)庫賬號
alicloud_db_account_privilege:管理數(shù)據(jù)庫賬號訪問指定數(shù)據(jù)庫
alicloud_db_backup_policy:管理數(shù)據(jù)庫備份策略
alicloud_db_connection:管理數(shù)據(jù)庫的連接地址
alicloud_db_database:管理數(shù)據(jù)庫
alicloud_db_instance:管理RDS實例
alicloud_db_read_write_splitting_connection:管理數(shù)據(jù)庫讀寫分離
alicloud_rds_account:管理實例賬號
alicloud_rds_backup:管理實例備份
alicloud_rds_clone_db_instance:恢復(fù)實例
alicloud_rds_db_instance_endpoint:管理集群系列實例的Endpoint
alicloud_rds_db_instance_endpoint_address:管理集群系列實例的外網(wǎng)Endpoint
alicloud_rds_db_node:管理集群系列實例的節(jié)點
alicloud_rds_db_proxy:管理數(shù)據(jù)庫代理
alicloud_rds_ddr_instance:管理跨地域恢復(fù)
alicloud_rds_instance_cross_backup_policy:管理實例跨地域備份策略
alicloud_rds_parameter_group:管理參數(shù)模板
alicloud_rds_service_linked_role:管理服務(wù)關(guān)聯(lián)角色(SLR)
alicloud_rds_upgrade_db_instance:管理實例升級策略
Data Source
alicloud_db_instance_class_infos:查詢實例規(guī)格信息
alicloud_db_instance_classes:查詢實例資源信息
alicloud_db_instance_engines:查詢實例引擎信息
alicloud_db_instances:查詢實例信息
alicloud_db_zones:查詢可用區(qū)信息
alicloud_instance_keywords:查詢預(yù)留關(guān)鍵字信息
alicloud_rds_accounts:查詢賬號信息
alicloud_rds_backups:查詢備份信息
alicloud_rds_character_set_names:查詢支持的字符集
alicloud_rds_class_details:查詢實例規(guī)格的詳細(xì)信息
alicloud_rds_collation_time_zones:查詢可用的字符集排序規(guī)則和時區(qū)信息
alicloud_rds_cross_region_backups:查詢實例跨地域備份信息
alicloud_rds_cross_regions:查詢實例支持跨地域備份的可用區(qū)信息
alicloud_rds_modify_parameter_logs:查詢參數(shù)修改日志
alicloud_rds_parameter_group:查詢參數(shù)模板信息
alicloud_rds_slots:查詢復(fù)制槽信息
配置權(quán)限
使用Terraform,您需要一個阿里云賬號和賬號的訪問密鑰(AccessKey)。為確保您的阿里云賬號及云資源使用安全,如非必要應(yīng)避免直接使用阿里云主賬號來訪問云數(shù)據(jù)庫RDS。建議您創(chuàng)建一個RAM用戶,獲取該用戶的AccessKey,并向其授予相應(yīng)權(quán)限。
創(chuàng)建RAM用戶:
訪問RAM用戶列表,單擊創(chuàng)建用戶。
設(shè)置登錄名稱為rds-test-operator,選擇訪問方式為使用永久 AccessKey 訪問。
單擊確定,創(chuàng)建RAM用戶并保存AccessKey ID與AccessKey Secret信息。
完成授權(quán):
訪問RAM用戶列表,單擊目標(biāo)RAM用戶操作列的添加權(quán)限。
在文本框中搜索
AliyunRDS
,選擇AliyunRDSFullAccess(具有RDS的完全控制權(quán)限)。在文本框中搜索
VPC
,選擇AliyunVPCFullAccess(具有VPC的完全控制權(quán)限)。說明本示例中在創(chuàng)建RDS實例時,會同時創(chuàng)建VPC和交換機。您也可以按需選擇權(quán)限策略或進行自定義策略,更多信息請參見創(chuàng)建自定義權(quán)限策略。
單擊確認(rèn)新增授權(quán),完成授權(quán)操作。
操作步驟
安裝Terraform
使用阿里云Cloud Shell。阿里云Cloud Shell是一款幫助您運維的免費產(chǎn)品,預(yù)裝了Terraform的組件,并配置好身份憑證(credentials)。因此您可直接在Cloud Shell中運行Terraform的命令。詳情請參見Cloud Shell。
在本地安裝和配置Terraform,請參見在本地安裝和配置Terraform。
安裝完成后,您可以打開命令行終端,輸入
terraform version
,若返回版本信息表示已成功安裝。
編寫模板
Terraform通過命令實現(xiàn)對Terraform模板中所定義的資源進行創(chuàng)建、修改、查看和刪除。
創(chuàng)建執(zhí)行目錄并進入。
說明需要為每個Terraform項目創(chuàng)建一個獨立的執(zhí)行目錄。
Linux或macOS:
sudo mkdir /usr/local/terraform cd /usr/local/rds_terraform
重要
如果您使用的非root權(quán)限用戶,則還需要為
rds_terraform
目錄授權(quán),使用sudo chown -R <當(dāng)前用戶名>:<用戶所屬組名> /usr/local/terraform
命令,將rds_terraform
文件夾的owner修改為當(dāng)前用戶。Windows:以D盤下創(chuàng)建
rds_terraform
文件夾為例,進入rds_terraform
文件夾。
在執(zhí)行目錄下,創(chuàng)建Terraform模板(terraform.tf)文件。
Linux或macOS:
touch terraform.tf
Windows:手動創(chuàng)建
terraform.tf
文件。
以查詢RDS PostgreSQL可用區(qū)信息為例,編輯
terraform.tf
文件,補充如下信息。resource "alicloud_vpc" "main" { vpc_name = "alicloud" cidr_block = "172.16.0.0/16" } resource "alicloud_vswitch" "main" { vpc_id = alicloud_vpc.main.id cidr_block = "172.16.192.0/20" zone_id = "cn-hangzhou-j" depends_on = [alicloud_vpc.main] } resource "alicloud_db_instance" "instance" { engine = "PostgreSQL" engine_version = "13.0" instance_type = "pg.n2.2c.2m" instance_storage = "30" instance_charge_type = "Postpaid" vswitch_id = alicloud_vswitch.main.id }
運行模板
本示例以Windows操作系統(tǒng)下使用本地安裝的Terraform為例,在其他操作系統(tǒng)中,運行命令的具體方式可能會有所不同。
進入
D:\rds_terraform
目錄下,初始化加載模塊,包括Provider等模板。terraform init
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/alicloud... - Installing hashicorp/alicloud v1.226.0... - Installed hashicorp/alicloud v1.226.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. ? │ Warning: Additional provider information from registry │ │ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud: │ - For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers. ? Terraform has been successfully initialized! 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.
驗證模板語法是否正確。
terraform validate
返回結(jié)果:
Success! The configuration is valid.
預(yù)覽模板。
terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # alicloud_db_instance.instance will be created + resource "alicloud_db_instance" "instance" { + acl = (known after apply) + auto_upgrade_minor_version = (known after apply) + babelfish_port = (known after apply) + ca_type = (known after apply) + category = (known after apply) + connection_string = (known after apply) + connection_string_prefix = (known after apply) + create_time = (known after apply) + db_instance_storage_type = (known after apply) + db_instance_type = (known after apply) + db_is_ignore_case = (known after apply) + db_time_zone = (known after apply) + deletion_protection = false + engine = "PostgreSQL" + engine_version = "14.0" + force_restart = false + ha_config = (known after apply) + id = (known after apply) + instance_charge_type = "Postpaid" + instance_storage = 30 + instance_type = "pg.n2.2c.2m" + maintain_time = (known after apply) + monitoring_period = (known after apply) + node_id = (known after apply) + port = (known after apply) + private_ip_address = (known after apply) + replication_acl = (known after apply) + resource_group_id = (known after apply) + role_arn = (known after apply) + security_group_id = (known after apply) + security_group_ids = (known after apply) + security_ip_mode = "normal" + security_ips = (known after apply) + server_cert = (known after apply) + server_key = (known after apply) + sql_collector_config_value = 30 + sql_collector_status = (known after apply) + ssl_action = (known after apply) + ssl_connection_string = (known after apply) + ssl_status = (known after apply) + status = (known after apply) + target_minor_version = (known after apply) + tcp_connection_type = (known after apply) + tde_status = (known after apply) + vpc_id = (known after apply) + vswitch_id = (known after apply) + zone_id = (known after apply) + zone_id_slave_a = (known after apply) + zone_id_slave_b = (known after apply) + babelfish_config (known after apply) + parameters (known after apply) + pg_hba_conf (known after apply) } # alicloud_vpc.main will be created + resource "alicloud_vpc" "main" { + cidr_block = "172.16.0.0/16" + create_time = (known after apply) + id = (known after apply) + ipv6_cidr_block = (known after apply) + ipv6_cidr_blocks = (known after apply) + name = (known after apply) + resource_group_id = (known after apply) + route_table_id = (known after apply) + router_id = (known after apply) + router_table_id = (known after apply) + secondary_cidr_blocks = (known after apply) + status = (known after apply) + user_cidrs = (known after apply) + vpc_name = "alicloud" } # alicloud_vswitch.main will be created + resource "alicloud_vswitch" "main" { + availability_zone = (known after apply) + cidr_block = "172.16.192.0/20" + create_time = (known after apply) + id = (known after apply) + ipv6_cidr_block = (known after apply) + ipv6_cidr_block_mask = (known after apply) + name = (known after apply) + status = (known after apply) + vpc_id = (known after apply) + vswitch_name = (known after apply) + zone_id = "cn-hangzhou-j" } Plan: 3 to add, 0 to change, 0 to destroy.
應(yīng)用模板配置。
terraform apply
出現(xiàn)如下配置信息后,確認(rèn)配置信息并輸入
yes
,開始創(chuàng)建。Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # alicloud_db_instance.instance will be created + resource "alicloud_db_instance" "instance" { + acl = (known after apply) + auto_upgrade_minor_version = (known after apply) + babelfish_port = (known after apply) + ca_type = (known after apply) + category = (known after apply) + connection_string = (known after apply) + connection_string_prefix = (known after apply) + create_time = (known after apply) + db_instance_storage_type = (known after apply) + db_instance_type = (known after apply) + db_is_ignore_case = (known after apply) + db_time_zone = (known after apply) + deletion_protection = false + engine = "PostgreSQL" + engine_version = "14.0" + force_restart = false + ha_config = (known after apply) + id = (known after apply) + instance_charge_type = "Postpaid" + instance_storage = 30 + instance_type = "pg.n2.2c.2m" + maintain_time = (known after apply) + monitoring_period = (known after apply) + node_id = (known after apply) + port = (known after apply) + private_ip_address = (known after apply) + replication_acl = (known after apply) + resource_group_id = (known after apply) + role_arn = (known after apply) + security_group_id = (known after apply) + security_group_ids = (known after apply) + security_ip_mode = "normal" + security_ips = (known after apply) + server_cert = (known after apply) + server_key = (known after apply) + sql_collector_config_value = 30 + sql_collector_status = (known after apply) + ssl_action = (known after apply) + ssl_connection_string = (known after apply) + ssl_status = (known after apply) + status = (known after apply) + target_minor_version = (known after apply) + tcp_connection_type = (known after apply) + tde_status = (known after apply) + vpc_id = (known after apply) + vswitch_id = (known after apply) + zone_id = (known after apply) + zone_id_slave_a = (known after apply) + zone_id_slave_b = (known after apply) + babelfish_config (known after apply) + parameters (known after apply) + pg_hba_conf (known after apply) } # alicloud_vpc.main will be created + resource "alicloud_vpc" "main" { + cidr_block = "172.16.0.0/16" + create_time = (known after apply) + id = (known after apply) + ipv6_cidr_block = (known after apply) + ipv6_cidr_blocks = (known after apply) + name = (known after apply) + resource_group_id = (known after apply) + route_table_id = (known after apply) + router_id = (known after apply) + router_table_id = (known after apply) + secondary_cidr_blocks = (known after apply) + status = (known after apply) + user_cidrs = (known after apply) + vpc_name = "alicloud" } # alicloud_vswitch.main will be created + resource "alicloud_vswitch" "main" { + availability_zone = (known after apply) + cidr_block = "172.16.192.0/20" + create_time = (known after apply) + id = (known after apply) + ipv6_cidr_block = (known after apply) + ipv6_cidr_block_mask = (known after apply) + name = (known after apply) + status = (known after apply) + vpc_id = (known after apply) + vswitch_name = (known after apply) + zone_id = "cn-hangzhou-j" } Plan: 3 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:
出現(xiàn)類似如下日志時,表示創(chuàng)建成功。
alicloud_vpc.main: Creating... alicloud_vpc.main: Creation complete after 9s [id=vpc-bp1apzkp9l5gkuq0****] alicloud_vswitch.main: Creating... alicloud_vswitch.main: Creation complete after 4s [id=vsw-bp1lmhzc42h5cc0t8****] alicloud_db_instance.instance: Creating... alicloud_db_instance.instance: Still creating... [10s elapsed] alicloud_db_instance.instance: Still creating... [20s elapsed] ... alicloud_db_instance.instance: Still creating... [6m1s elapsed] alicloud_db_instance.instance: Still creating... [6m11s elapsed] alicloud_db_instance.instance: Creation complete after 6m20s [id=pgm-bp10ckaa2340****] Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
查看結(jié)果。
訪問RDS實例列表,查看已創(chuàng)建的RDS實例。
相關(guān)文檔
通過Terraform調(diào)用RDS OpenAPI的詳細(xì)示例,請參見Terraform。