1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

TerraformでElastic CloudにElasticsearchを構築する方法

Last updated at Posted at 2023-09-04

Terraformを使ってElastic CloudにElasticsearchを構築する手順を説明していきます.本記事ではElastic Cloudを使ったことがない人でもElasticsearchを構築できるように説明します.

検証環境

Elastic Cloudにサインアップ

まず初めにElastic Cloudのサインアップカラ説明していきます.もうすでにElastic Cloudについて知っていたらTerraformで新たにdeploymentを作成するまで進んで大丈夫です.
Elastic CloudはGoogleアカウントやMicrosoftアカウントを持っていれば,すぐにサインアップできます.またElastic Cloudの14日間の無料トライアルがあるのでそちらを使っていきましょう.

無料トライアルをクリックしたら,サインアップを求められます.Googleアカウントを持っていれば,すぐにElastic Cloudのアカウントが作成されます.
次にElastic Cloudのdeploymentを作ることができます.Cloud providerはAWSやGCP,Azureを選ぶことができますが,今回はAWSを選択します.
deployment名も記入したらCrate deploymentをクリックします.!
first-deployment.png

そしたらElastic CloudのHome画面になります.そしたら画面中央に作成されたdeploymentがあるのがわかります.
first-deployment_in-elastic-cloud.png
14日間の無料トライアルならお金がかからずに遊ぶことができます.

Terraformで新たにdeploymentを作成する.

次はElastic Cloudの画面からではなく,Terraformを使ってdeploymentを作成していきます.

API Keyを作成する

まず初めにAPI Keyを作成します.Elastic Cloudのホーム画面にあるDedicated deploymentsをクリックします.

Dedicated_deployments.png
左サイドバーにあるFeaturesをクリックします.
features.png
左下にAPI KeysがあるのでAdd API Keysをクリックします.
add-api-key.png

そうするとCreate API keyが表示されると思うのでクリックします.
API keyの名前入力とAPI keyのいつまでAPI keyを有効にするかが求められるので,ここはご自身でお好きに設定してください.
(下記の画像は別に黒く塗りつぶす意味もなかったのですが,なぜかやってしまいました...)
create_api-key.png

入力を終えたらAPI keyが表示されると思います.こちらをコピーしてどこかに保管をしてください!もう二度と同じAPI Keyは表示されないので,忘れると一度作り直すことになります.
API Keyの保存を終えたら以下のような画面になると思います.これでAPI keyの作成は終えました.
5_created.png

API Keyのセット

API keyは環境変数としてセットするか,Terraformのコード内に記述するの2択があります.

export EC_API_KEY="<apikey>"
provider "ec" {
  apikey = "<apikey>"
}

今回はTerraformのコード内にAPI keyを記述します.

コードを記述する

では次にTerraformのコードを書いていきます.main.tfを作成して以下のコードを記述します

main.tf
provider "ec" {
  apikey = "xxx"
}

terraform {
  required_providers {
    ec = {
      source = "elastic/ec"
      version = "0.4.1"
    }
  }
}

resource "ec_deployment" "test_cluster" {
  region = "ap-southeast-1"
  version = "8.9.1"
  deployment_template_id = "aws-cpu-optimized-arm"
  name = "test cluster"

  elasticsearch  {
    autoscale = "false"
    topology {
      id="hot_content"
      size="1g"
      size_resource="memory"
      zone_count=1
    }
  }

  kibana {
    topology {
      size="1g"
      size_resource="memory"
      zone_count=1
    }
  }
}

上記を記入したらterraform initを行います.以降はterraformtfと略します.

$ tf init                                                                                                                                                                                                                               ─╯

Initializing the backend...

Initializing provider plugins...
- Finding elastic/ec versions matching "0.4.1"...
- Installing elastic/ec v0.4.1...
- Installed elastic/ec v0.4.1 (signed by a HashiCorp partner, key ID 7FE579EDEC6DAA7B)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

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.

次にtf planでElasti Cloudで作成されるはずのリソースを確認します.

$ tf plan                                                                                                                                                                                                                               ─╯

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # ec_deployment.test_cluster will be created
  + resource "ec_deployment" "test_cluster" {
      + alias                  = (known after apply)
      + apm_secret_token       = (sensitive value)
      + deployment_template_id = "aws-cpu-optimized-arm"
      + elasticsearch_password = (sensitive value)
      + elasticsearch_username = (known after apply)
      + id                     = (known after apply)
      + name                   = "test cluster"
      + region                 = "ap-southeast-1"
      + version                = "8.9.1"

      + elasticsearch {
          + autoscale      = "false"
          + cloud_id       = (known after apply)
          + http_endpoint  = (known after apply)
          + https_endpoint = (known after apply)
          + ref_id         = "main-elasticsearch"
          + region         = (known after apply)
          + resource_id    = (known after apply)

          + topology {
              + config                    = (known after apply)
              + id                        = "hot_content"
              + instance_configuration_id = (known after apply)
              + node_roles                = (known after apply)
              + node_type_data            = (known after apply)
              + node_type_ingest          = (known after apply)
              + node_type_master          = (known after apply)
              + node_type_ml              = (known after apply)
              + size                      = "1g"
              + size_resource             = "memory"
              + zone_count                = 1

              + autoscaling {
                  + max_size             = (known after apply)
                  + max_size_resource    = (known after apply)
                  + min_size             = (known after apply)
                  + min_size_resource    = (known after apply)
                  + policy_override_json = (known after apply)
                }
            }

          + trust_account {
              + account_id      = (known after apply)
              + trust_all       = (known after apply)
              + trust_allowlist = (known after apply)
            }

          + trust_external {
              + relationship_id = (known after apply)
              + trust_all       = (known after apply)
              + trust_allowlist = (known after apply)
            }
        }

      + kibana {
          + elasticsearch_cluster_ref_id = "main-elasticsearch"
          + http_endpoint                = (known after apply)
          + https_endpoint               = (known after apply)
          + ref_id                       = "main-kibana"
          + region                       = (known after apply)
          + resource_id                  = (known after apply)

          + topology {
              + instance_configuration_id = (known after apply)
              + size                      = "1g"
              + size_resource             = "memory"
              + zone_count                = 1
            }
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

出力結果に特に問題がなかったらtf applyでdeploymentを作成します.

$ tf apply
...(terraform plan実行時の差分が出る)
Plan: 1 to add, 0 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:

Enter a valueが求められるのでyesと入力します.

Plan: 1 to add, 0 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: yes

ec_deployment.test_cluster: Creating...
ec_deployment.test_cluster: Still creating... [10s elapsed]
ec_deployment.test_cluster: Still creating... [20s elapsed]
ec_deployment.test_cluster: Still creating... [30s elapsed]
ec_deployment.test_cluster: Still creating... [40s elapsed]
ec_deployment.test_cluster: Still creating... [50s elapsed]

そしたらdeploymentの作成が開始されます.
構築の様子はElastic CloudのdeploymentのActivityから確認できます.
6_activity.png
上記の画像ではElasticsearchの構築は終わり,Kibanaの構築を進めています.
作成が終わるとターミナルではApply complete! Resources: 1 added, 0 changed, 0 destroyed.と表示されます.
Elastic Cloudのdeployment画面に戻るとリソースが作成されたことがわかります.
7_finish.png
Terraformで作成したtest clusterで警告が出ていますが,こちらは1つのAvailability Zoneでしか作成していないためです.基本的には2つ以上のAvailability ZoneでNodeを作成することが推奨されています.

おわりに

本記事ではTerraformを使ってElasti CloudにElasticsearchを構築する方法を紹介しました.まだ構築の段階なので,もっとElasticsearchを触れて記事にできれば良いと思いました.

参考文献

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?