Elasticsearch Service

Amazon Elasticsearch Service

The Elasticsearch Service in LocalStack lets you create one or more single-node Elasticsearch/OpenSearch cluster that behaves like the Amazon Elasticsearch Service. This service is, like its AWS counterpart, heavily linked with the OpenSearch Service. Any cluster created with the Elasticsearch Service will show up in the OpenSearch Service and vice versa.

Creating an Elasticsearch cluster

You can go ahead and use awslocal to create a new elasticsearch domain via the aws es create-elasticsearch-domain command.

Note: Unless you use the Elasticsearch default version, the first time you create a cluster with a specific version, the Elasticsearch binary is downloaded, which may take a while to download.

Note: The default Elasticsearch version used is 7.10.0. This is a slight deviation from the default version used in AWS (Elasticsearch 1.5), which is not supported in LocalStack.

  1. $ awslocal es create-elasticsearch-domain --domain-name my-domain
  2. {
  3. "DomainStatus": {
  4. "DomainId": "000000000000/my-domain",
  5. "DomainName": "my-domain",
  6. "ARN": "arn:aws:es:us-east-1:000000000000:domain/my-domain",
  7. "Created": true,
  8. "Deleted": false,
  9. "Endpoint": "my-domain.us-east-1.es.localhost.localstack.cloud:4566",
  10. "Processing": true,
  11. "ElasticsearchVersion": "7.10.0",
  12. "ElasticsearchClusterConfig": {
  13. "InstanceType": "m3.medium.elasticsearch",
  14. "InstanceCount": 1,
  15. "DedicatedMasterEnabled": true,
  16. "ZoneAwarenessEnabled": false,
  17. "DedicatedMasterType": "m3.medium.elasticsearch",
  18. "DedicatedMasterCount": 1
  19. },
  20. "EBSOptions": {
  21. "EBSEnabled": true,
  22. "VolumeType": "gp2",
  23. "VolumeSize": 10,
  24. "Iops": 0
  25. },
  26. "CognitoOptions": {
  27. "Enabled": false
  28. }
  29. }
  30. }

In the LocalStack log you will see something like the following, where you can see the cluster starting up in the background.

  1. 2021-11-08T16:29:28:INFO:localstack.services.es.cluster: starting elasticsearch: /opt/code/localstack/localstack/localstack/infra/elasticsearch/bin/elasticsearch -E http.port=57705 -E http.publish_port=57705 -E transport.port=0 -E network.host=127.0.0.1 -E http.compression=false -E path.data="/var/lib/localstack/lib//elasticsearch/arn:aws:es:us-east-1:000000000000:domain/my-domain/data" -E path.repo="/var/lib/localstack/lib//elasticsearch/arn:aws:es:us-east-1:000000000000:domain/my-domain/backup" -E xpack.ml.enabled=false with env {'ES_JAVA_OPTS': '-Xms200m -Xmx600m', 'ES_TMPDIR': '/var/lib/localstack/lib//elasticsearch/arn:aws:es:us-east-1:000000000000:domain/my-domain/tmp'}
  2. 2021-11-08T16:29:28:INFO:localstack.services.es.cluster: registering an endpoint proxy for http://my-domain.us-east-1.es.localhost.localstack.cloud:4566 => http://127.0.0.1:57705
  3. 2021-11-08T16:29:30:INFO:localstack.services.es.cluster: OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
  4. 2021-11-08T16:29:32:INFO:localstack.services.es.cluster: [2021-11-08T16:29:32,502][INFO ][o.e.n.Node ] [noctua] version[7.10.0], pid[22403], build[default/tar/51e9d6f22758d0374a0f3f5c6e8f3a7997850f96/2020-11-09T21:30:33.964949Z], OS[Linux/5.4.0-89-generic/amd64], JVM[Ubuntu/OpenJDK 64-Bit Server VM/11.0.11/11.0.11+9-Ubuntu-0ubuntu2.20.04]
  5. 2021-11-08T16:29:32:INFO:localstack.services.es.cluster: [2021-11-08T16:29:32,510][INFO ][o.e.n.Node ] [noctua] JVM home [/usr/lib/jvm/java-11-openjdk-amd64], using bundled JDK [false]
  6. 2021-11-08T16:29:32:INFO:localstack.services.es.cluster: [2021-11-08T16:29:32,511][INFO ][o.e.n.Node ] [noctua] JVM arguments [-Xshare:auto, -Des.networkaddress.cache.ttl=60, -Des.networkaddress.cache.negative.ttl=10, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dio.netty.allocator.numDirectArenas=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Djava.locale.providers=SPI,COMPAT, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -Djava.io.tmpdir=/var/lib/localstack/lib//elasticsearch/arn:aws:es:us-east-1:000000000000:domain/my-domain/tmp, -XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=data, -XX:ErrorFile=logs/hs_err_pid%p.log, -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m, -Xms200m, -Xmx600m, -XX:MaxDirectMemorySize=314572800, -Des.path.home=/opt/code/localstack/localstack/localstack/infra/elasticsearch, -Des.path.conf=/opt/code/localstack/localstack/localstack/infra/elasticsearch/config, -Des.distribution.flavor=default, -Des.distribution.type=tar, -Des.bundled_jdk=true]
  7. 2021-11-08T16:29:36:INFO:localstack.services.es.cluster: [2021-11-08T16:29:36,258][INFO ][o.e.p.PluginsService ] [noctua] loaded module [aggs-matrix-stats]
  8. 2021-11-08T16:29:36:INFO:localstack.services.es.cluster: [2021-11-08T16:29:36,259][INFO ][o.e.p.PluginsService ] [noctua] loaded module [analysis-common]
  9. 2021-11-08T16:29:36:INFO:localstack.services.es.cluster: [2021-11-08T16:29:36,260][INFO ][o.e.p.PluginsService ] [noctua] loaded module [constant-keyword]
  10. ...

and after some time, you should see that the Processing state of the domain is set to false:

  1. $ awslocal es describe-elasticsearch-domain --domain-name my-domain | jq ".DomainStatus.Processing"
  2. false

Interact with the cluster

You can now interact with the cluster at the cluster API endpoint for the domain, in this case http://my-domain.us-east-1.es.localhost.localstack.cloud:4566.

For example:

  1. $ curl http://my-domain.us-east-1.es.localhost.localstack.cloud:4566
  2. {
  3. "name" : "localstack",
  4. "cluster_name" : "elasticsearch",
  5. "cluster_uuid" : "IC7E9daNSiepRBB9Ksul7w",
  6. "version" : {
  7. "number" : "7.10.0",
  8. "build_flavor" : "default",
  9. "build_type" : "tar",
  10. "build_hash" : "51e9d6f22758d0374a0f3f5c6e8f3a7997850f96",
  11. "build_date" : "2020-11-09T21:30:33.964949Z",
  12. "build_snapshot" : false,
  13. "lucene_version" : "8.7.0",
  14. "minimum_wire_compatibility_version" : "6.8.0",
  15. "minimum_index_compatibility_version" : "6.0.0-beta1"
  16. },
  17. "tagline" : "You Know, for Search"
  18. }

Or the health endpoint:

  1. $ curl -s http://my-domain.us-east-1.es.localhost.localstack.cloud:4566/_cluster/health | jq .
  2. {
  3. "cluster_name": "elasticsearch",
  4. "status": "green",
  5. "timed_out": false,
  6. "number_of_nodes": 1,
  7. "number_of_data_nodes": 1,
  8. "active_primary_shards": 0,
  9. "active_shards": 0,
  10. "relocating_shards": 0,
  11. "initializing_shards": 0,
  12. "unassigned_shards": 0,
  13. "delayed_unassigned_shards": 0,
  14. "number_of_pending_tasks": 0,
  15. "number_of_in_flight_fetch": 0,
  16. "task_max_waiting_in_queue_millis": 0,
  17. "active_shards_percent_as_number": 100
  18. }

Advanced topics

Endpoints

There are three configurable strategies that govern how domain endpoints are created, and can be configured via the OPENSEARCH_ENDPOINT_STRATEGY (previously ES_ENDPOINT_STRATEGY) environment variable.

ValueFormatDescription
domain<domain-name>.<region>.es.localhost.localstack.cloud:4566This is the default strategy that uses the localhost.localstack.cloud domain to route to your localhost
pathlocalhost:4566/es/<region>/<domain-name>An alternative that can be useful if you cannot resolve LocalStack’s localhost domain
portlocalhost:<port-from-range>Exposes the cluster(s) directly with ports from the external service port range
offDeprecated. This value now reverts to the port setting, using a port from the given range instead of 4571

Regardless of the service from which the clusters were created, the domain of the cluster always corresponds to the engine type (OpenSearch or Elasticsearch) of the cluster. OpenSearch cluster therefore have opensearch in their domain (e.g. my-domain.us-east-1.opensearch.localhost.localstack.cloud:4566) and Elasticsearch clusters have es in their domain (e.g. my-domain.us-east-1.es.localhost.localstack.cloud:4566)

Custom Endpoints

LocalStack allows you to set arbitrary custom endpoints for your clusters in the domain endpoint options. This can be used to overwrite the behavior of the endpoint strategies described above. You can also choose custom domains, however it is important to add the edge port (80/443 or by default 4566).

  1. $ awslocal es create-elasticsearch-domain --domain-name my-domain \
  2. --elasticsearch-version 7.10 \
  3. --domain-endpoint-options '{ "CustomEndpoint": "http://localhost:4566/my-custom-endpoint", "CustomEndpointEnabled": true }'

Once the domain processing is complete, you can access the cluster:

  1. $ curl http://localhost:4566/my-custom-endpoint/_cluster/health

Re-using a single cluster instance

In some cases, you may not want to create a new cluster instance for each domain, for example when you are only interested in testing API interactions instead of actual Elasticsearch functionality. In this case, you can set OPENSEARCH_MULTI_CLUSTER=0 (previously ES_MULTI_CLUSTER). This will multiplex all domains to the same cluster, or return the same port every time when using the port endpoint strategy. This can however lead to unexpected behavior when persisting data into Elasticsearch, or creating clusters with different versions, so we do not recommend it.

Storage Layout

Elasticsearch will be organized in your state directory as follows:

  1. localstack@machine % tree -L 4 volume/state
  2. .
  3. ├── elasticsearch
  4. └── arn:aws:es:us-east-1:000000000000:domain
  5. ├── my-cluster-1
  6. ├── backup
  7. ├── data
  8. └── tmp
  9. ├── my-cluster-2
  10. ├── backup
  11. ├── data
  12. └── tmp

Custom Elasticsearch backends

LocalStack downloads elasticsearch asynchronously the first time you run the aws es create-elasticsearch-domain, so you will get the response from localstack first and then (after download/install) you will have your elasticsearch cluster running locally. You may not want this, and instead use your already running elasticsearch cluster. This can also be useful when you want to run a cluster with a custom configuration that localstack does not support.

To customize the elasticsearch backend, you can your own elasticsearch cluster locally and point localstack to it using the OPENSEARCH_CUSTOM_BACKEND (previously ES_CUSTOM_BACKEND) environment variable. Note that only a single backend can be configured, meaning that you will get a similar behavior as when you re-use a single cluster instance.

Example

The following shows a sample docker-compose file that contains a single-noded elasticsearch cluster and a basic localstack setp.

  1. version: "3.9"
  2. services:
  3. elasticsearch:
  4. container_name: elasticsearch
  5. image: docker.elastic.co/elasticsearch/elasticsearch:7.10.2
  6. environment:
  7. - node.name=elasticsearch
  8. - cluster.name=es-docker-cluster
  9. - discovery.type=single-node
  10. - bootstrap.memory_lock=true
  11. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  12. ports:
  13. - "9200:9200"
  14. ulimits:
  15. memlock:
  16. soft: -1
  17. hard: -1
  18. volumes:
  19. - data01:/usr/share/elasticsearch/data
  20. localstack:
  21. container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
  22. image: localstack/localstack
  23. ports:
  24. - "4566:4566"
  25. depends_on:
  26. - elasticsearch
  27. environment:
  28. - ES_CUSTOM_BACKEND=http://elasticsearch:9200
  29. - DEBUG=${DEBUG- }
  30. - PERSISTENCE=${PERSISTENCE- }
  31. - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
  32. - DOCKER_HOST=unix:///var/run/docker.sock
  33. volumes:
  34. - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
  35. - "/var/run/docker.sock:/var/run/docker.sock"
  36. volumes:
  37. data01:
  38. driver: local
  1. Run docker compose:

    1. $ docker-compose up -d
  2. Create the Elasticsearch domain:

    1. $ awslocal es create-elasticsearch-domain \
    2. --domain-name mylogs-2 \
    3. --elasticsearch-version 7.10 \
    4. --elasticsearch-cluster-config '{ "InstanceType": "m3.xlarge.elasticsearch", "InstanceCount": 4, "DedicatedMasterEnabled": true, "ZoneAwarenessEnabled": true, "DedicatedMasterType": "m3.xlarge.elasticsearch", "DedicatedMasterCount": 3}'
    5. {
    6. "DomainStatus": {
    7. "DomainId": "000000000000/mylogs-2",
    8. "DomainName": "mylogs-2",
    9. "ARN": "arn:aws:es:us-east-1:000000000000:domain/mylogs-2",
    10. "Created": true,
    11. "Deleted": false,
    12. "Endpoint": "mylogs-2.us-east-1.es.localhost.localstack.cloud:4566",
    13. "Processing": true,
    14. "ElasticsearchVersion": "7.10",
    15. "ElasticsearchClusterConfig": {
    16. "InstanceType": "m3.xlarge.elasticsearch",
    17. "InstanceCount": 4,
    18. "DedicatedMasterEnabled": true,
    19. "ZoneAwarenessEnabled": true,
    20. "DedicatedMasterType": "m3.xlarge.elasticsearch",
    21. "DedicatedMasterCount": 3
    22. },
    23. "EBSOptions": {
    24. "EBSEnabled": true,
    25. "VolumeType": "gp2",
    26. "VolumeSize": 10,
    27. "Iops": 0
    28. },
    29. "CognitoOptions": {
    30. "Enabled": false
    31. }
    32. }
    33. }
  3. If the Processing status is true, it means that the cluster is not yet healthy. You can run describe-elasticsearch-domain to receive the status:

    1. $ awslocal es describe-elasticsearch-domain --domain-name mylogs-2
  4. Check the cluster health endpoint and create indices:

    1. $ curl mylogs-2.us-east-1.es.localhost.localstack.cloud:4566/_cluster/health
    2. {"cluster_name":"es-docker-cluster","status":"green","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"active_primary_shards":0,"active_shards":0,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":100.0}[~]
  5. Create an example index:

    1. $ curl -X PUT mylogs-2.us-east-1.es.localhost.localstack.cloud:4566/my-index
    2. {"acknowledged":true,"shards_acknowledged":true,"index":"my-index"}

Differences to AWS

  • By default, AWS only sets the Endpoint attribute of the cluster status once the cluster is up. LocalStack will return the endpoint immediately, but keep Processing = "true" until the cluster has been started.
  • The CustomEndpointOptions allows arbitrary endpoint URLs, which is not allowed in AWS

Last modified July 5, 2022: remove network_mode: bridge and the workarounds it lead to (#192) (4f4a0dc7)