Elasticsearch已接入Terraform,您可以通過Terraform管理Elasticsearch。本文介紹如何通過Terraform建立Elasticsearch。
本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
為了降低資訊安全風險,建議使用最小許可權的RAM使用者完成此教程的操作。請參見建立RAM使用者與為RAM使用者授權,完成此教程所需最小許可權的權限原則如下:
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "vpc:CreateVpc", "vpc:DeleteVpc", "vpc:CreateVSwitch", "vpc:DeleteVSwitch", "ecs:CreateSecurityGroup", "ecs:ModifySecurityGroupPolicy", "ecs:DescribeSecurityGroups", "ecs:ListTagResources", "ecs:DeleteSecurityGroup", "ecs:DescribeSecurityGroupAttribute" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "vpc:DescribeVpcAttribute", "vpc:DescribeRouteTableList", "vpc:DescribeVSwitchAttributes" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "elasticsearch:CreateInstance", "elasticsearch:DescribeInstance", "elasticsearch:ListAckClusters", "elasticsearch:UpdateDescription", "elasticsearch:ListInstance", "elasticsearch:ListAvailableEsInstanceIds" ], "Resource": "*" } ] }準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。
Explorer:阿里雲提供了Terraform的線上運行環境,您無需安裝Terraform,登入後即可線上使用和體驗Terraform。適用於零成本、快速、便捷地體驗和調試Terraform的情境。
Cloud Shell:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。
在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。
本教程樣本包含的部分資源會產生一定費用,請在不需要時及時進行釋放或退訂。
使用的資源
alicloud_vpc:Virtual Private Cloud資源。
alicloud_security_group:安全性群組資源。
alicloud_vswitch:交換器資源。
alicloud_elasticsearch_instance:Elasticsearch執行個體資源。該資源為計費資源,請參見產品計費。
建立Elasticsearch執行個體
建立一個工作目錄,並在該工作目錄中建立名為main.tf的設定檔。以下代碼將建立Elasticsearch執行個體以及相關的Virtual Private Cloud、安全性群組和交換器,將以下代碼複製到main.tf中。
variable "region" { default = "cn-qingdao" } data "alicloud_zones" "default" { available_resource_creation = "VSwitch" available_disk_category = "cloud_ssd" } variable "vpc_cidr_block" { default = "172.16.0.0/16" } variable "vsw_cidr_block" { default = "172.16.0.0/24" } variable "node_spec" { default = "elasticsearch.sn2ne.large" } provider "alicloud" { region = var.region } resource "random_integer" "default" { min = 10000 max = 99999 } resource "alicloud_vpc" "vpc" { vpc_name = "vpc-test_${random_integer.default.result}" cidr_block = var.vpc_cidr_block } resource "alicloud_security_group" "group" { name = "test_${random_integer.default.result}" vpc_id = alicloud_vpc.vpc.id } resource "alicloud_vswitch" "vswitch" { vpc_id = alicloud_vpc.vpc.id cidr_block = var.vsw_cidr_block zone_id = data.alicloud_zones.default.zones[0].id vswitch_name = "vswitch-test-${random_integer.default.result}" } resource "alicloud_elasticsearch_instance" "instance" { description = "test_Instance" instance_charge_type = "PostPaid" data_node_amount = "2" data_node_spec = var.node_spec data_node_disk_size = "20" data_node_disk_type = "cloud_ssd" vswitch_id = alicloud_vswitch.vswitch.id password = "es_password_01" version = "6.7_with_X-Pack" master_node_spec = var.node_spec zone_count = "1" master_node_disk_type = "cloud_ssd" kibana_node_spec = var.node_spec data_node_disk_performance_level = "PL1" tags = { Created = "TF", For = "example", } }執行如下命令,初始化Terraform運行環境。
terraform init返回資訊如下,則Terraform初始化成功。
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.執行如下命令,開始建立Elasticsearch執行個體。
terraform apply在執行過程中,根據提示輸入
yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示建立Elasticsearch執行個體成功。You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes Apply complete! Resources: 5 added, 0 changed, 0 destroyed.驗證結果。
執行terraform show命令
您可以在工作目錄中,使用以下命令查詢Terraform已建立資源的詳細資料:
terraform show
登入Elasticsearch控制台
登入Elasticsearch控制台,查看已建立的Elasticsearch執行個體。

清理資源
當您不再需要上述通過Terraform建立或管理的資源時,請運行以下命令以釋放資源。關於terraform destroy的更多資訊,請參見常用命令。
terraform destroy完整樣本
當前範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
範例程式碼
如果您想體驗更多完整樣本,請前往更多完整樣本中對應產品的檔案夾查看。