Skip to content
Docs Try Aspire
Docs Try

Set up Elasticsearch in the AppHost

Elasticsearch logo

This article is the reference for the Aspire Elasticsearch Hosting integration. It enumerates the AppHost APIs — with C# examples — that you use to model an Elasticsearch resource in your AppHost project.

If you’re new to the Elasticsearch integration, start with the Get started with Elasticsearch integrations guide. For how consuming apps read the connection information this page exposes, see Connect to Elasticsearch.

To start building an Aspire app that uses Elasticsearch, install the 📦 Aspire.Hosting.Elasticsearch NuGet package:

Terminal
aspire add elasticsearch

Learn more about aspire add in the command reference.

Or, choose a manual installation approach:

C# — AppHost.cs
#:package Aspire.Hosting.Elasticsearch@*
XML — AppHost.csproj
<PackageReference Include="Aspire.Hosting.Elasticsearch" Version="*" />

Once you’ve installed the hosting integration in your AppHost project, you can add an Elasticsearch resource as shown in the following example:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var elasticsearch = builder.AddElasticsearch("elasticsearch");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(elasticsearch);
// After adding all resources, run the app...
  1. When Aspire adds a container image to the AppHost, as shown in the preceding example with the docker.io/library/elasticsearch image, it creates a new Elasticsearch instance on your local machine.

  2. The Elasticsearch resource includes default credentials with a username of "elastic" and a randomly generated password. To set an explicit password, see Add Elasticsearch resource with parameters.

  3. The AppHost reference call configures a connection in the consuming project named after the referenced Elasticsearch resource, such as elasticsearch in the preceding example.

Add Elasticsearch resource with data volume

Section titled “Add Elasticsearch resource with data volume”

Add a data volume to the Elasticsearch resource as shown in the following example:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var elasticsearch = builder.AddElasticsearch("elasticsearch")
.WithDataVolume(isReadOnly: false);
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(elasticsearch);
// After adding all resources, run the app...

The data volume is used to persist Elasticsearch data outside the lifecycle of its container. The data volume is mounted at the /usr/share/elasticsearch/data path in the Elasticsearch container and when a name parameter isn’t provided, the name is generated at random. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.

Add Elasticsearch resource with data bind mount

Section titled “Add Elasticsearch resource with data bind mount”

Add a data bind mount to the Elasticsearch resource as shown in the following example:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var elasticsearch = builder.AddElasticsearch("elasticsearch")
.WithDataBindMount(
source: @"C:\Elasticsearch\Data",
isReadOnly: false);
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(elasticsearch);
// After adding all resources, run the app...

Data bind mounts rely on the host machine’s filesystem to persist Elasticsearch data across container restarts. The data bind mount is mounted at the C:\Elasticsearch\Data on Windows (or /Elasticsearch/Data on Unix) path on the host machine in the Elasticsearch container. For more information on data bind mounts, see Docker docs: Bind mounts.

Add Elasticsearch resource with parameters

Section titled “Add Elasticsearch resource with parameters”

When you want to explicitly provide the password used by the container image, you can pass it as a parameter:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var password = builder.AddParameter("password", secret: true);
var elasticsearch = builder.AddElasticsearch("elasticsearch", password: password);
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(elasticsearch);
// After adding all resources, run the app...

When no password parameter is provided, Aspire generates a strong password automatically. For more information on providing parameters, see External parameters.

Connect to an existing Elasticsearch instance

Section titled “Connect to an existing Elasticsearch instance”

In C#, call AsExisting instead of AddElasticsearch to reference an externally managed Elasticsearch instance:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var elasticsearch = builder.AddElasticsearch("elasticsearch")
.AsExisting(connectionStringParameter: builder.AddParameter("elasticsearch-cs", secret: true));
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(elasticsearch);
// After adding all resources, run the app...

For the full reference of Elasticsearch connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to Elasticsearch.

The Elasticsearch hosting integration automatically adds a health check for the Elasticsearch resource. The health check verifies that the Elasticsearch instance is running and that a connection can be established to it.

The hosting integration relies on the 📦 AspNetCore.HealthChecks.Elasticsearch NuGet package.