|
| 1 | +--- |
| 2 | +page_title: "Manage indices using rollover and templates" |
| 3 | +--- |
| 4 | + |
| 5 | +When indices are created using `elasticsearch_index_template` and configured to rollover with Index Lifecycle Management (ILM, part of Xpack) or Index State Management (ISM, part of OpenDistro), this provider can follow the most recent index so that the underlying index is still managed correctly. |
| 6 | + |
| 7 | +This works by: |
| 8 | +1. Create an index template to link the ISM or ILM policy with the indices. Then when creating the first index with `elasticsearch_index`, the provider inspects the response for an ILM/ISM lifecycle policy. |
| 9 | +1. If the ILM/ISM lifecycle policy contains the setting `rollover_alias`, the provider sets this field in the Terraform state. |
| 10 | +1. On the next Terraform run: if the `rollover_alias` is present then it uses this value to get the current `is_write_index`. |
| 11 | + |
| 12 | +For example, using ILM: |
| 13 | + |
| 14 | +1. Create the following `main.tf` file: |
| 15 | + |
| 16 | +``` |
| 17 | +provider "elasticsearch" { |
| 18 | + url = "http://localhost:9200" |
| 19 | +} |
| 20 | +
|
| 21 | +resource "elasticsearch_xpack_index_lifecycle_policy" "test" { |
| 22 | + name = "test" |
| 23 | + body = <<EOF |
| 24 | +{ |
| 25 | + "policy": { |
| 26 | + "phases": { |
| 27 | + "hot": { |
| 28 | + "min_age": "0ms", |
| 29 | + "actions": { |
| 30 | + "rollover": { |
| 31 | + "max_size": "50gb" |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | +EOF |
| 39 | +} |
| 40 | +
|
| 41 | +resource "elasticsearch_index_template" "test" { |
| 42 | + name = "test" |
| 43 | + body = <<EOF |
| 44 | +{ |
| 45 | + "index_patterns": [ |
| 46 | + "test-*" |
| 47 | + ], |
| 48 | + "settings": { |
| 49 | + "index": { |
| 50 | + "lifecycle": { |
| 51 | + "name": "${elasticsearch_xpack_index_lifecycle_policy.test.name}", |
| 52 | + "rollover_alias": "test" |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | +EOF |
| 58 | +} |
| 59 | +
|
| 60 | +resource "elasticsearch_index" "test" { |
| 61 | + name = "test-000001" |
| 62 | + number_of_shards = 1 |
| 63 | + number_of_replicas = 1 |
| 64 | + aliases = jsonencode({ |
| 65 | + "test" = { |
| 66 | + "is_write_index" = true |
| 67 | + } |
| 68 | + }) |
| 69 | +
|
| 70 | + depends_on = [elasticsearch_index_template.test] |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +2. Start a new Elasticsearch Container: |
| 75 | + |
| 76 | +``` |
| 77 | +$ export ES_OSS_IMAGE=elasticsearch:7.9.2 |
| 78 | +$ docker-compose up -d elasticsearch |
| 79 | +Creating network "terraform-provider-elasticsearch_default" with the default driver |
| 80 | +Creating terraform-provider-elasticsearch_elasticsearch_1 ... done |
| 81 | +``` |
| 82 | + |
| 83 | +3. Create the example Index Lifecycle Policy, Index Template and Index: |
| 84 | + |
| 85 | +``` |
| 86 | +$ terraform apply -auto-approve |
| 87 | +elasticsearch_xpack_index_lifecycle_policy.test: Creating... |
| 88 | +elasticsearch_xpack_index_lifecycle_policy.test: Creation complete after 0s [id=test] |
| 89 | +elasticsearch_index_template.test: Creating... |
| 90 | +elasticsearch_index_template.test: Creation complete after 0s [id=test] |
| 91 | +elasticsearch_index.test: Creating... |
| 92 | +elasticsearch_index.test: Creation complete after 0s [id=test-000001] |
| 93 | +
|
| 94 | +Apply complete! Resources: 3 added, 0 changed, 0 destroyed. |
| 95 | +``` |
| 96 | + |
| 97 | +4. Manually rollover the example index: |
| 98 | + |
| 99 | +``` |
| 100 | +$ curl -X POST http://localhost:9200/test/_rollover |
| 101 | +{"acknowledged":true,"shards_acknowledged":true,"old_index":"test-000001","new_index":"test-000002","rolled_over":true,"dry_run":false,"conditions":{}} |
| 102 | +``` |
| 103 | + |
| 104 | +5. Delete the initial created index: |
| 105 | + |
| 106 | +``` |
| 107 | +$ curl -X DELETE http://localhost:9200/test-000001 |
| 108 | +{"acknowledged":true} |
| 109 | +``` |
| 110 | + |
| 111 | +6. Now all changes will be made to the index `test-000002`. |
0 commit comments