このトピックでは、Terraformを使用してApsaraDB RDS for PostgreSQLインスタンスのアカウントを作成、変更、クエリ、および削除する方法について説明します。
このトピックのサンプルコードは、数回クリックするだけで実行できます。 詳細については、「Terraform Explorer」をご参照ください。
前提条件
RDSインスタンスが作成されました。 詳細については、「RDSインスタンスの作成」をご参照ください。
RDS インスタンスが実行中の状態である必要があります。 次のいずれかの方法を使用して、RDSインスタンスが [実行中] 状態であるかどうかを確認できます。
クエリインスタンスの詳細に記載されている手順に従って、statusパラメーターの値を確認します。 値が [実行中] の場合、RDSインスタンスは [実行中] 状態です。
ApsaraDB RDSコンソールにログインし、必要なリージョンに切り替えてRDSインスタンスを見つけ、インスタンスのステータスを確認します。
Alibaba Cloudアカウントには、アカウント内のリソースに対するすべての権限があります。 Alibaba Cloudアカウントが漏洩した場合、リソースは大きなリスクにさらされます。 Resource Access Management (RAM) ユーザーを使用し、RAMユーザーのAccessKeyペアを作成することを推奨します。 詳細については、「RAMユーザーの作成」および「AccessKeyペアの作成」をご参照ください。
RAMを使用して、クラウドリソースに対するアクセス許可を効率的に管理する必要があります。 これにより、マルチユーザーコラボレーションの要件を満たすことができ、最小限の権限 (PoLP) の原則に基づいてユーザーに権限を付与して、過度の権限によるセキュリティの脆弱性を防ぐことができます。 詳細については、「RAMユーザーへの権限付与」をご参照ください。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "vpc:DescribeVpcAttribute", "vpc:DescribeRouteTableList", "vpc:DescribeVSwitchAttributes", "vpc:DeleteVpc", "vpc:DeleteVSwitch", "vpc:CreateVpc", "vpc:CreateVSwitch" ], "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": "*" } ] }Terraform環境を準備します。 Terraformを使用するには、次のいずれかの方法を使用できます。
Terraform ExplorerでTerraformを使用する: Alibaba Cloudは、Terraformのオンラインランタイム環境であるTerraform Explorerを提供します。 Terraform Explorerにログインした後、Terraformをインストールする必要なくTerraformを使用できます。 詳細については、「Terraform ExplorerでのTerraformの使用」をご参照ください。 この方法は、Terraformを高速かつ便利な方法で追加費用なしで使用およびデバッグするシナリオに適しています。
Cloud ShellでTerraformを使用する: TerraformはCloud Shellにプリインストールされ、ID認証情報が設定されています。 Cloud ShellでTerraformコマンドを直接実行できます。 詳細については、「Cloud ShellでのTerraformの使用」をご参照ください。 この方法は、Terraformを高速で便利な方法で低コストで使用およびデバッグするシナリオに適しています。
オンプレミスのマシンにTerraformをインストールして構成する: この方法は、ネットワークの状態が悪い、またはカスタム開発環境が使用されているシナリオに適しています。 詳細については、「ローカルPCでのTerraformのインストールと設定」をご参照ください。
Resources
特定のリソースに対して課金されます。 リソースが不要になった場合は、できるだけ早い機会にリソースを解放または購読解除する必要があります。
alicloud_db_account: アカウントを作成します。
alicloud_rds_accounts: アカウントを照会します。
alicloud_vpc: 仮想プライベートクラウド (VPC) を作成します。
alicloud_vswitch: VPC用のvSwitchを作成します。
alicloud_db_instance: RDSインスタンスを作成します。
アカウントの作成
このセクションでは、tf_account_testという名前のアカウントを作成する方法について説明します。
作業ディレクトリとmain.tfという名前の設定ファイルをディレクトリに作成します。 次のコードをmain.tf設定ファイルにコピーします。
必要なリソースを作成します。
variable "region" { default = "cn-hangzhou" } provider "alicloud" { region = var.region } variable "zone_id" { default = "cn-hangzhou-b" } variable "instance_type" { default = "pg.n2.2c.2m" } # Create a VPC. resource "alicloud_vpc" "main" { vpc_name = "alicloud" cidr_block = "172.16.0.0/16" } # Create a vSwitch. resource "alicloud_vswitch" "main" { vpc_id = alicloud_vpc.main.id cidr_block = "172.16.192.0/20" zone_id = var.zone_id } # Create an RDS instance. 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ファイルに、
リソース "alicloud_db_account" "account" {}設定項目を追加します。... resource "alicloud_db_account" "account" { db_instance_id = alicloud_db_instance.instance.id account_name = "tf_account_test" account_password = "!Test@123456" }
次のコマンドを実行して、Terraformのランタイム環境を初期化します。
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.実行計画を作成し、変更をプレビューします。
terraform plan次のコマンドを実行してリソースを作成します。
terraform apply実行中に、プロンプトに従って
yesと入力し、enterキーを押します。 コマンドが正常に実行されるまで待ちます。 次の情報が表示された場合、操作は成功です。alicloud_db_account.account: Creating... alicloud_db_account.account: Creation complete after 6s [id=pgm-****:tf_account_test] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.結果を確認します。
terraform showコマンドを実行します。
次のコマンドを実行してアカウント情報を照会します。
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/Shanghai" 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-hangzhou-j" }ApsaraDB RDSコンソールにログインする
ApsaraDB RDSコンソールにログインし、アカウント情報を表示します。
アカウントのパスワードを変更する
このセクションでは、tf_account_testアカウントのパスワードをTest123 @ rdsに変更する方法について説明します。
main.tfファイルで、次のコードスニペットに基づいて、
リソース "alicloud_db_account" "account"設定項目のaccount_passwordフィールドを変更します。... resource "alicloud_db_account" "account" { ... account_password = "Test123@rds" }を実行します。Run the
terraform applyコマンドを実行します。次の情報が表示される場合は、情報を確認し、yesと入力してパスワードを変更します。
alicloud_vpc.main: Refreshing state... [id=vpc-****] alicloud_vswitch.main: Refreshing state... [id=vsw-****] alicloud_db_instance.instance: Refreshing state... [id=pgm-****] alicloud_db_account.account: Refreshing state... [id=pgm-****:tf_account_test] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: ~ update in-place Terraform will perform the following actions: # alicloud_db_account.account will be updated in-place ~ resource "alicloud_db_account" "account" { ~ account_password = (sensitive value) id = "pgm-****:tf_account_test" name = "tf_account_test" # (6 unchanged attributes hidden) } Plan: 0 to add, 1 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:次のログが表示された場合、操作は成功です。
alicloud_db_account.account: Modifying... [id=pgm-****:tf_account_test] alicloud_db_account.account: Modifications complete after 6s [id=pgm-****:tf_account_test] Apply complete! Resources: 0 added, 1 changed, 0 destroyed.新しいパスワードを使用してデータベースに接続し、新しいパスワードが有効であることを確認します。
アカウントの照会
main.tfファイルに、次のコンテンツを追加します。
... data "alicloud_rds_accounts" "queryaccounts" { db_instance_id = alicloud_db_instance.instance.id }を実行します。Run the
terraform applyコマンドを実行して、RDSインスタンス用に作成されたアカウントを照会します。次のログが表示された場合、クエリは成功です。
alicloud_vpc.main: Refreshing state... [id=vpc-****] alicloud_vswitch.main: Refreshing state... [id=vsw-****] alicloud_db_instance.instance: Refreshing state... [id=pgm-****] data.alicloud_rds_accounts.queryaccounts: Reading... alicloud_db_account.account: Refreshing state... [id=pgm-****:tf_account_test] data.alicloud_rds_accounts.queryaccounts: Read complete after 1s [id=137568****] No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.結果を確認します。
次のコマンドを実行して結果を表示します。
terraform show# data.alicloud_rds_accounts.queryaccounts: data "alicloud_rds_accounts" "queryaccounts" { accounts = [ { account_description = "" account_name = "tf_account_test" account_type = "Normal" database_privileges = [] id = "tf_account_test" priv_exceeded = "" status = "Available" }, ] db_instance_id = "pgm-****" id = "137568****" ids = [ "tf_account_test", ] names = [ "tf_account_test", ] }
アカウントの削除
ここでは、tf_account_testという名前のアカウントを削除する方法について説明します。
main.tfファイルで、
リソース "alicloud_db_account" "account"設定項目を削除します。 この例では、次の情報が削除されます。... resource "alicloud_db_account" "account" { db_instance_id = alicloud_db_instance.instance.id account_name = "tf_account_test" account_password = "Test123@rds" }を実行します。Run the
terraform applyコマンドを実行します。次の情報が表示されたら、情報を確認し、yesと入力してアカウントを削除します。
alicloud_db_account.account: Refreshing state... [id=pgm-****:tf_account_test] alicloud_vpc.main: Refreshing state... [id=vpc-****] alicloud_vswitch.main: Refreshing state... [id=vsw-****] alicloud_db_instance.instance: Refreshing state... [id=pgm-****] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # alicloud_db_account.account will be destroyed # (because alicloud_db_account.account is not in configuration) - resource "alicloud_db_account" "account" { - account_name = "tf_account_test" -> null - account_password = (sensitive value) - account_type = "Normal" -> null - db_instance_id = "pgm-****" -> null - id = "pgm-****:tf_account_test" -> null - instance_id = "pgm-****" -> null - name = "tf_account_test" -> null - status = "Available" -> null - type = "Normal" -> null } Plan: 0 to add, 0 to change, 1 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:次のログが表示された場合、操作は成功です。
alicloud_db_account.account: Destroying... [id=pgm-****:tf_account_test] alicloud_db_account.account: Destruction complete after 6s Apply complete! Resources: 0 added, 0 changed, 1 destroyed.結果を確認します。
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/Shanghai" 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-hangzhou-j" pg_hba_conf { address = "127.0.0.1" database = "all" method = "md5" priority_id = 1 type = "host" user = "all" } }ApsaraDB RDSコンソールにログインする
ApsaraDB RDSコンソールにログインし、アカウントが削除されたかどうかを確認します。
リソースのクリア
Terraformを使用して作成または管理された上記のリソースが不要になった場合は、次のコマンドを実行してリソースを解放します。 terraform destroyコマンドの詳細については、「一般的なコマンド」をご参照ください。
terraform destroy完全なサンプルコード
このトピックのサンプルコードは、数回クリックするだけで実行できます。 詳細については、「Terraform Explorer」をご参照ください。
サンプルコード
さらに多くの例を体験したい場合は、[クイックスタート] ページに移動し、対応するサービスのフォルダーにある例を表示します。