Create a database account with Terraform

更新时间:
复制 MD 格式

This topic describes how to use Terraform to create a database account for a specific AnalyticDB for PostgreSQL instance.

Note

You can run the sample code in this tutorial with a single click. Run with one click

Prerequisites

  • An Alibaba Cloud account (main account) has full permissions for all your resources. Leaked credentials for this account pose a significant security risk. We recommend using a RAM user and creating an AccessKey for it. For more information, see Create a RAM user and Create an AccessKey.

  • Resource Access Management (RAM) lets you manage permissions for your cloud resources. This helps you enable multi-user collaboration and follow the principle of least privilege, preventing security risks from excessive permissions. For more information, see Manage the permissions of a RAM user.

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "vpc:DescribeVpcAttribute",
                    "vpc:DescribeRouteTableList",
                    "vpc:DescribeVSwitchAttributes",
                    "vpc:DeleteVpc",
                    "vpc:DeleteVSwitch",
                    "vpc:CreateVpc",
                    "vpc:CreateVSwitch",
                    "vpc:ModifyVpcAttribute",
                    "vpc:DescribeVpcs",
                    "vpc:DescribeVSwitches"
                ],
                "Resource": "*"
            },
            {
                "Effect": "Allow",
                "Action": "bss:ModifyAgreementRecord",
                "Resource": "*"
            },
            {
                "Effect": "Allow",
                "Action": [
                    "bss:DescribeOrderList",
                    "bss:DescribeOrderDetail",
                    "bss:PayOrder",
                    "bss:CancelOrder"
                ],
                "Resource": "*"
            },
            {
                "Action": "gpdb:*",
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": "ram:CreateServiceLinkedRole",
                "Resource": "*",
                "Effect": "Allow",
                "Condition": {
                    "StringEquals": {
                        "ram:ServiceName": "adbpg.aliyuncs.com"
                    }
                }
            }
        ]
    }
  • Prepare a Terraform runtime environment. You can use one of the following methods:

Resources used

Note

Some of the resources created in this tutorial incur charges. To avoid unnecessary fees, release the resources when they are no longer needed. For more information, see Billing.

Create a database account

This example shows how to create a high-privilege account for a lakehouse edition cluster of Cloud-Native Data Warehouse AnalyticDB for PostgreSQL.

  1. Create a working directory, create a configuration file named main.tf in it, and then copy the following code into main.tf.

    • Create the prerequisite resources.

      variable "name" {
        default = "terraform-example"
      }
      
      variable "region" {
        default = "cn-shenzhen"
      }
      
      variable "zone_id" {
        default = "cn-shenzhen-e"
      }
      
      variable "db_instance_class" {
        default = "gpdb.group.segsdx1"
      }
      
      variable "instance_spec" {
        default = "2C16G"
      }
      
      provider "alicloud" {
        region = var.region
      }
      
      resource "alicloud_vpc" "default" {
        vpc_name   = "alicloud"
        cidr_block = "172.16.0.0/16"
      }
      
      resource "alicloud_vswitch" "default" {
        vpc_id     = alicloud_vpc.default.id
        cidr_block = "172.16.192.0/20"
        zone_id    = var.zone_id
      }
      
      resource "alicloud_gpdb_instance" "default" {
        db_instance_category  = "HighAvailability"
        db_instance_class     = var.db_instance_class
        db_instance_mode      = "StorageElastic"
        description           = var.name
        engine                = "gpdb"
        engine_version        = "6.0"
        zone_id               = var.zone_id
        instance_network_type = "VPC"
        instance_spec         = var.instance_spec
        payment_type          = "PayAsYouGo"
        seg_storage_type      = "cloud_essd"
        seg_node_num          = 4
        storage_size          = 50
        vpc_id                = alicloud_vpc.default.id
        vswitch_id            = alicloud_vswitch.default.id
        ip_whitelist {
          security_ip_list = "127.0.0.1"
        }
      }
    • Add the resource "alicloud_gpdb_account" "default" configuration block to your main.tf file.

      resource "alicloud_gpdb_account" "default" {
        account_name        = "tf_example"
        db_instance_id      = alicloud_gpdb_instance.default.id
        account_password    = "Example1234"
        account_description = "tf_example"
      }
  2. Run the following command to initialize the Terraform environment.

    terraform init

    The following output indicates a successful initialization.

    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
  3. Run the following command to create an execution plan and preview the changes.

    terraform plan
  4. Run the following command to create the resources.

    terraform apply

    When prompted, enter yes and press Enter. Wait for the command to complete. The following output indicates that the resources were created successfully.

    alicloud_vpc.default: Creating...
    alicloud_vpc.default: Creation complete after 6s [id=vpc-****]
    alicloud_vswitch.default: Creating...
    alicloud_vswitch.default: Creation complete after 4s [id=vsw-****]
    alicloud_gpdb_instance.default: Creating...
    alicloud_gpdb_instance.default: Still creating... [10s elapsed]
    alicloud_gpdb_instance.default: Still creating... [20s elapsed]
    alicloud_gpdb_instance.default: Still creating... [30s elapsed]
    alicloud_gpdb_instance.default: Still creating... [40s elapsed]
    alicloud_gpdb_instance.default: Still creating... [50s elapsed]
    alicloud_gpdb_instance.default: Still creating... [1m0s elapsed]
    alicloud_gpdb_instance.default: Still creating... [1m10s elapsed]
    alicloud_gpdb_instance.default: Still creating... [1m20s elapsed]
    alicloud_gpdb_instance.default: Still creating... [1m30s elapsed]
    alicloud_gpdb_instance.default: Still creating... [1m40s elapsed]
    alicloud_gpdb_instance.default: Still creating... [1m50s elapsed]
    alicloud_gpdb_instance.default: Still creating... [2m0s elapsed]
    alicloud_gpdb_instance.default: Still creating... [2m10s elapsed]
    alicloud_gpdb_instance.default: Still creating... [2m20s elapsed]
    alicloud_gpdb_instance.default: Still creating... [2m30s elapsed]
    alicloud_gpdb_instance.default: Still creating... [2m40s elapsed]
    alicloud_gpdb_instance.default: Still creating... [2m50s elapsed]
    alicloud_gpdb_instance.default: Still creating... [3m0s elapsed]
    alicloud_gpdb_instance.default: Creation complete after 3m5s [id=gp-****]
    alicloud_gpdb_account.default: Creating...
    alicloud_gpdb_account.default: Still creating... [10s elapsed]
    alicloud_gpdb_account.default: Creation complete after 16s [id=gp-****:tf_example]
    
    Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
  5. Verify the result.

    Terraform show

    Run terraform show to view the created account.

    terraform show
    # alicloud_gpdb_account.default:
    resource "alicloud_gpdb_account" "default" {
        account_description = "tf_example"
        account_name        = "tf_example"
        account_password    = (sensitive value)
        account_type        = "Super"
        db_instance_id      = "gp-****"
        id                  = "gp-****:tf_example"
        status              = "1"
    }

    Console

    Log on to the AnalyticDB for PostgreSQL console to view the created account.

Clean up resources

When you no longer need the resources, run the following command to release them. For more information about terraform destroy, see Common commands.

terraform destroy

Complete example

Note

You can run the sample code in this example with a single click. Run with one click

Sample code

variable "name" {
  default = "terraform-example"
}

variable "region" {
  default = "cn-shenzhen"
}

variable "zone_id" {
  default = "cn-shenzhen-e"
}

variable "db_instance_class" {
  default = "gpdb.group.segsdx1"
}

variable "instance_spec" {
  default = "2C16G"
}

provider "alicloud" {
  region = var.region
}

resource "alicloud_vpc" "default" {
  vpc_name   = "alicloud"
  cidr_block = "172.16.0.0/16"
}

resource "alicloud_vswitch" "default" {
  vpc_id     = alicloud_vpc.default.id
  cidr_block = "172.16.192.0/20"
  zone_id    = var.zone_id
}

resource "alicloud_gpdb_instance" "default" {
  db_instance_category  = "HighAvailability"
  db_instance_class     = var.db_instance_class
  db_instance_mode      = "StorageElastic"
  description           = var.name
  engine                = "gpdb"
  engine_version        = "6.0"
  zone_id               = var.zone_id
  instance_network_type = "VPC"
  instance_spec         = var.instance_spec
  payment_type          = "PayAsYouGo"
  seg_storage_type      = "cloud_essd"
  seg_node_num          = 4
  storage_size          = 50
  vpc_id                = alicloud_vpc.default.id
  vswitch_id            = alicloud_vswitch.default.id
  ip_whitelist {
    security_ip_list = "127.0.0.1"
  }
}

resource "alicloud_gpdb_account" "default" {
  account_name        = "tf_example"
  db_instance_id      = alicloud_gpdb_instance.default.id
  account_password    = "Example1234"
  account_description = "tf_example"
}

For more end-to-end examples, go to more complete examples and open the folder for the relevant product.