OpenSearch
The OpenSearch component lets you add a deployed instance of OpenSearch, or an
OpenSearch domain to your app using Amazon OpenSearch Service.
Create the instance
const search = new sst.aws.OpenSearch("MySearch");Link to a resource
You can link your instance to other resources, like a function or your Next.js app.
new sst.aws.Nextjs("MyWeb", { link: [search]});Once linked, you can connect to it from your function code.
import { Resource } from "sst";import { Client } from "@opensearch-project/opensearch";
const client = new Client({ node: Resource.MySearch.url, auth: { username: Resource.MySearch.username, password: Resource.MySearch.password }});
// Add a documentawait client.index({ index: "my-index", body: { message: "Hello world!" }});
// Search for documentsconst result = await client.search({ index: "my-index", body: { query: { match: { message: "world" } } }});Running locally
By default, your OpenSearch domain is deployed in sst dev. But let’s say you are
running OpenSearch locally.
docker run \ --rm \ -p 9200:9200 \ -v $(pwd)/.sst/storage/opensearch:/usr/share/opensearch/data \ -e discovery.type=single-node \ -e plugins.security.disabled=true \ -e OPENSEARCH_INITIAL_ADMIN_PASSWORD=^Passw0rd^ \ opensearchproject/opensearch:2.17.0You can connect to it in sst dev by configuring the dev prop.
const opensearch = new sst.aws.OpenSearch("MyOpenSearch", { dev: { url: "http://localhost:9200", username: "admin", password: "^Passw0rd^" }});This will skip deploying an OpenSearch domain and link to the locally running OpenSearch process instead.
Cost
By default this component uses a Single-AZ Deployment, On-Demand Instances of a
t3.small.search at $0.036 per hour. And 10GB of General Purpose gp3 Storage
at $0.122 per GB per month.
That works out to $0.036 x 24 x 30 + $0.122 x 10 or $27 per month. Adjust this for
the instance type and the storage you are using.
The above are rough estimates for us-east-1, check out the OpenSearch Service pricing for more details.
Constructor
new OpenSearch(name, args?, opts?)Parameters
-
namestring -
args?OpenSearchArgs -
opts?ComponentResourceOptions
OpenSearchArgs
dev?
Type Object
Configure how this component works in sst dev.
By default, your OpenSearch domain is deployed in sst dev. But if you want to
instead connect to a locally running OpenSearch, you can configure the dev prop.
This will skip deploying an OpenSearch domain and link to the locally running OpenSearch process instead.
Setting the dev prop also means that any linked resources will connect to the right
instance both in sst dev and sst deploy.
{ dev: { username: "admin", password: "Passw0rd!", url: "http://localhost:9200" }}dev.password?
Type Input<string>
Default Inherit from the top-level password.
The password of the local OpenSearch to connect to when running in dev.
dev.url?
Type Input<string>
Default “http://localhost:9200”
The URL of the local OpenSearch to connect to when running in dev.
dev.username?
Type Input<string>
Default Inherit from the top-level username.
The username of the local OpenSearch to connect to when running in dev.
instance?
Type Input<string>
Default “t3.small”
The type of instance to use for the domain. Check out the supported instance types.
{ instance: "m6g.large"}password?
Type Input<string>
Default A random password is generated.
The password of the master user.
{ password: "^Passw0rd^"}Use Secrets to manage the password.
{ password: new sst.Secret("MyDomainPassword").value}storage?
Type Input<“${number} GB” | “${number} TB”>
Default “10 GB”
The storage limit for the domain.
{ storage: "100 GB"}transform?
transform.domain?
Type DomainArgs | (args: DomainArgs, opts: ComponentResourceOptions, name: string) => void
Transform the OpenSearch domain.
transform.policy?
Type PolicyDocument | (args: PolicyDocument, opts: ComponentResourceOptions, name: string) => void
Transform the OpenSearch domain policy.
username?
Type Input<string>
Default “admin”
The username of the master user.
{ username: "admin"}version?
Type Input<string>
Default “OpenSearch_2.17”
The OpenSearch engine version. Check out the available versions.
{ version: "OpenSearch_2.5"}Properties
id
Type Output<string>
The ID of the OpenSearch component.
nodes
Type Object
nodes.domain
Type undefined | Domain
password
Type Output<string>
The password of the master user.
url
Type Output<string>
The endpoint of the domain.
username
Type Output<string>
The username of the master user.
SDK
Use the SDK in your runtime to interact with your infrastructure.
Links
This is accessible through the Resource object in the SDK.
-
passwordstringThe password of the master user.
-
urlstringThe endpoint of the domain.
-
usernamestringThe username of the master user.
Methods
static get
OpenSearch.get(name, id, opts?)Parameters
The name of the component.namestring
The ID of the existing OpenSearch component.idInput<string>-
opts?ComponentResourceOptions
Returns OpenSearch
Reference an existing OpenSearch domain with the given name. This is useful when you create a domain in one stage and want to share it in another. It avoids having to create a new domain in the other stage.
Imagine you create a domain in the dev stage. And in your personal stage frank,
instead of creating a new domain, you want to share the same domain from dev.
const search = $app.stage === "frank" ? sst.aws.OpenSearch.get("MyOpenSearch", "arn:aws:es:us-east-1:123456789012:domain/app-dev-myopensearch-efsmkrbt") : new sst.aws.OpenSearch("MyOpenSearch");Here arn:aws:es:us-east-1:123456789012:domain/app-dev-myopensearch-efsmkrbt is the
ID of the OpenSearch component created in the dev stage.
You can find this by outputting the ID in the dev stage.
return { id: search.id};