全部產品
Search
文件中心

Hologres:通過 Terraform 管理 Hologres 執行個體

更新時間:Feb 04, 2026

使用Terraform管理Hologres資源,無需手動操作,確保了一致性和可重複性,簡化了基礎設施的配置和維護過程,減少了潛在的人為錯誤,提供了更高效、可靠的基礎設施管理體驗。

說明

本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。

前提條件

  • 由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作方式請參見建立RAM使用者建立AccessKey

  • 通過RAM授權,阿里雲使用者可以有效地管理其雲資源存取權限,適應多使用者協同工作的需求,並且能夠按需為使用者指派最小許可權,避免許可權過大導致的安全性漏洞‌。具體操作方式請參見管理RAM使用者的許可權

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "vpc:DescribeVpcAttribute",
                    "vpc:DescribeRouteTableList",
                    "vpc:DescribeVSwitchAttributes",
                    "vpc:DeleteVpc",
                    "vpc:DeleteVSwitch",
                    "vpc:CreateVpc",
                    "vpc:CreateVSwitch"
                ],
                "Resource": "*"
            },
            {
                "Effect": "Allow",
                "Action": "bss:ModifyAgreementRecord",
                "Resource": "*"
            },
            {
                "Effect": "Allow",
                "Action": [
                    "bss:DescribeOrderList",
                    "bss:DescribeOrderDetail",
                    "bss:PayOrder",
                    "bss:CancelOrder"
                ],
                "Resource": "*"
            },
            {
                "Effect": "Allow",
                "Action": [
                    "hologram:CreateInstance",
                    "hologram:GetInstance",
                    "hologram:DeleteInstance",
                    "hologram:UpdateInstanceNetworkType"
                ],
                "Resource": "*"
            }
        ]
    }
  • 準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。

    • 在Terraform Explorer中使用Terraform:阿里雲提供了Terraform的線上運行環境,您無需安裝Terraform,登入後即可線上使用和體驗Terraform。適用於零成本、快速、便捷地體驗和調試Terraform的情境。

    • Cloud Shell:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。

    • 在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。

使用的資源

說明

本教程樣本包含的部分資源會產生一定費用,請在不需要時及時進行釋放或退訂。

建立Hologres執行個體

本文以建立8核32 GB規格的通用型Hologres執行個體為例。

  1. 建立一個工作目錄,並在該工作目錄中建立名為main.tf的設定檔,然後將以下代碼複製到main.tf中。

    variable "region" {
      default = "cn-shenzhen"
    }
    
    provider "alicloud" {
      region = var.region
    }
    
    variable "zone_id" {
      default = "cn-shenzhen-f"
    }
    
    # 建立VPC
    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    = var.zone_id
    }
    
    # 建立hologram執行個體
    resource "alicloud_hologram_instance" "default" {
      instance_type = "Standard"
      pricing_cycle = "Hour"
      cpu           = "8"
      endpoints {
        type = "Intranet"
      }
      endpoints {
        type       = "VPCSingleTunnel"
        vswitch_id = alicloud_vswitch.main.id
        vpc_id     = alicloud_vswitch.main.vpc_id
      }
      zone_id       = alicloud_vswitch.main.zone_id
      instance_name = "terraform-hologram"
      payment_type  = "PayAsYouGo"
    }

    如需建立Serverless型執行個體,可在上述代碼前加入以下代碼內容:

    terraform {   # Serverless型執行個體
      required_version = ">= 0.13"
      required_providers {
        alicloud = {
          source = "hashicorp/alicloud"
          version = "1.259.0"
        }
      }
    }
    說明

    您在建立Serverless型執行個體時,部分參數配置可參見以下內容:

    resource "alicloud_hologram_instance" "default" {
      payment_type  = "PayAsYouGo"
      instance_type = "Serverless"
      pricing_cycle = "Hour" # Serverless型執行個體只支援隨用隨付。
      cpu           = "4"  #Serverless的執行個體的CPU固定為4,且完全免費。
    }
  2. 執行以下命令,初始化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
  3. 建立執行計畫,並預覽變更。

    terraform plan
  4. 執行以下命令,建立資源。

    terraform apply

    在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示運行成功。

    alicloud_vpc.main: Creating...
    alicloud_vpc.main: Creation complete after 7s [id=vpc-****]
    alicloud_vswitch.main: Creating...
    alicloud_vswitch.main: Creation complete after 4s [id=vsw-****]
    alicloud_hologram_instance.default: Creating...
    alicloud_hologram_instance.default: Still creating... [10s elapsed]
    alicloud_hologram_instance.default: Still creating... [20s elapsed]
    alicloud_hologram_instance.default: Still creating... [30s elapsed]
    alicloud_hologram_instance.default: Still creating... [40s elapsed]
    alicloud_hologram_instance.default: Still creating... [50s elapsed]
    alicloud_hologram_instance.default: Still creating... [1m0s elapsed]
    alicloud_hologram_instance.default: Still creating... [1m10s elapsed]
    alicloud_hologram_instance.default: Still creating... [1m20s elapsed]
    alicloud_hologram_instance.default: Still creating... [1m30s elapsed]
    alicloud_hologram_instance.default: Still creating... [1m40s elapsed]
    alicloud_hologram_instance.default: Still creating... [1m50s elapsed]
    alicloud_hologram_instance.default: Still creating... [2m0s elapsed]
    alicloud_hologram_instance.default: Still creating... [2m10s elapsed]
    alicloud_hologram_instance.default: Creation complete after 2m10s [id=hgpostcn-cn-****]
    
    Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
  5. 驗證結果。

    執行terraform show命令

    執行terraform show查看Hologres執行個體資訊。

    terraform show
    # alicloud_hologram_instance.default:
    resource "alicloud_hologram_instance" "default" {
        cold_storage_size  = 0
        cpu                = 8
        create_time        = "2024-11-18T05:24:14.693Z"
        gateway_count      = 0
        id                 = "hgpostcn-cn-****"
        instance_name      = "terraform-hologram"
        instance_type      = "Standard"
        leader_instance_id = null
        payment_type       = "PayAsYouGo"
        pricing_cycle      = "Hour"
        resource_group_id  = "rg-****"
        status             = "Running"
        storage_size       = 0
        zone_id            = "cn-shenzhen-f"
    
        endpoints {
            alternative_endpoints = null
            enabled               = true
            endpoint              = "hgpostcn-cn-****-cn-shenzhen-internal.hologres.aliyuncs.com:80"
            type                  = "Intranet"
            vpc_id                = null
            vpc_instance_id       = null
            vswitch_id            = null
        }
        endpoints {
            alternative_endpoints = null
            enabled               = true
            endpoint              = "hgpostcn-cn-****-cn-shenzhen-vpc-st.hologres.aliyuncs.com:80"
            type                  = "VPCSingleTunnel"
            vpc_id                = "vpc-****"
            vpc_instance_id       = "hgpostcn-cn-****-frontend-st"
            vswitch_id            = "vsw-****"
        }
    }

    登入Hologres管理主控台

    登入Hologres管理主控台,查看Hologres執行個體資訊。image

清理資源

當您不再需要上述通過Terraform建立或管理的資源時,請運行以下命令以釋放資源。關於terraform destroy的更多資訊,請參見常用命令

terraform destroy

完整樣本

說明

當前範例程式碼支援一鍵運行,您可以直接運行代碼。

範例程式碼

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

provider "alicloud" {
  region = var.region
}

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

# 建立VPC
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    = var.zone_id
}

# 建立hologram執行個體
resource "alicloud_hologram_instance" "default" {
  instance_type = "Standard"
  pricing_cycle = "Hour"
  cpu           = "8"
  endpoints {
    type = "Intranet"
  }
  endpoints {
    type       = "VPCSingleTunnel"
    vswitch_id = alicloud_vswitch.main.id
    vpc_id     = alicloud_vswitch.main.vpc_id
  }
  zone_id       = alicloud_vswitch.main.zone_id
  instance_name = "terraform-hologram"
  payment_type  = "PayAsYouGo"
}