通过Terraform管理阿里云Elasticsearch

阿里云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:适用于网络连接较差或需要自定义开发环境的场景。

说明

本教程示例包含的部分资源会产生一定费用,请在不需要时及时进行释放或退订。

使用的资源

创建Elasticsearch实例

  1. 创建一个工作目录,并在该工作目录中创建名为main.tf的配置文件。以下代码将创建Elasticsearch实例以及相关的专有网络VPC、安全组和交换机,将以下代码复制到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",
      }
    }
  2. 执行如下命令,初始化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.
  3. 执行如下命令,开始创建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.
  4. 验证结果。

    执行terraform show命令

    您可以在工作目录中,使用以下命令查询Terraform已创建资源的详细信息:

    terraform show

    image

    登录阿里云Elasticsearch控制台

    登录阿里云Elasticsearch控制台,查看已创建的Elasticsearch实例。

    image

清理资源

当您不再需要上述通过Terraform创建或管理的资源时,请运行以下命令以释放资源。关于terraform destroy的更多信息,请参见Terraform常用命令

terraform destroy

完整示例

说明

当前示例代码支持一键运行,您可以直接运行代码。一键运行

示例代码

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",
  }
}

如果您想体验更多完整示例,请前往更多完整示例中对应产品的文件夹查看。

相关文档