Set up Elasticsearch in the AppHost
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.
Installation
Section titled “Installation”To start building an Aspire app that uses Elasticsearch, install the 📦 Aspire.Hosting.Elasticsearch NuGet package:
aspire add elasticsearchLearn more about aspire add in the command reference.
Or, choose a manual installation approach:
#:package Aspire.Hosting.Elasticsearch@*<PackageReference Include="Aspire.Hosting.Elasticsearch" Version="*" />aspire add elasticsearchLearn more about aspire add in the command reference.
This updates your aspire.config.json with the Elasticsearch hosting integration package:
{ "packages": { "Aspire.Hosting.Elasticsearch": "13.3.0" }}Add Elasticsearch resource
Section titled “Add Elasticsearch resource”Once you’ve installed the hosting integration in your AppHost project, you can add an Elasticsearch resource as shown in the following example:
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...The TypeScript AppHost doesn’t currently expose an addElasticsearch(...) API. To add an Elasticsearch instance to a TypeScript AppHost, use builder.addContainer(...) with the docker.io/library/elasticsearch image, or use builder.addConnectionString(...) to reference an existing instance.
-
When Aspire adds a container image to the AppHost, as shown in the preceding example with the
docker.io/library/elasticsearchimage, it creates a new Elasticsearch instance on your local machine. -
The Elasticsearch resource includes default credentials with a
usernameof"elastic"and a randomly generated password. To set an explicit password, see Add Elasticsearch resource with parameters. -
The AppHost reference call configures a connection in the consuming project named after the referenced Elasticsearch resource, such as
elasticsearchin 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:
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 TypeScript AppHost doesn’t currently expose an addElasticsearch(...) API. Data volume configuration is not available for Elasticsearch in TypeScript AppHosts.
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:
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...The TypeScript AppHost doesn’t currently expose an addElasticsearch(...) API. Data bind mount configuration is not available for Elasticsearch in TypeScript AppHosts.
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:
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...The TypeScript AppHost doesn’t currently expose an addElasticsearch(...) API. Password parameters are not available for Elasticsearch in TypeScript AppHosts.
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:
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...The TypeScript AppHost doesn’t currently expose an addElasticsearch(...) API. To connect to an existing Elasticsearch instance from a TypeScript AppHost, use builder.addConnectionString(...) with a parameter and reference that connection string from your consuming apps instead.
Connection properties
Section titled “Connection properties”For the full reference of Elasticsearch connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to Elasticsearch.
Hosting integration health checks
Section titled “Hosting integration health checks”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.