From 74cb66c70028ce1fbfb31845c9a9d027e5c2ace9 Mon Sep 17 00:00:00 2001 From: Ronit Agarwala Date: Wed, 22 Nov 2023 20:54:56 -0500 Subject: [PATCH 1/6] Change import of lodash/chunk --- data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data.ts b/data.ts index 950f13f..3e6bcfb 100644 --- a/data.ts +++ b/data.ts @@ -12,7 +12,7 @@ import { pathToFileURL } from 'url' import { Client } from '@opensearch-project/opensearch' import type { ClientOptions } from '@opensearch-project/opensearch' import { exists } from './paths' -import chunk from 'lodash/chunk' +import chunk from 'lodash/chunk.js' const jsonFilename = 'sandbox-search.json' const jsFilename = 'sandbox-search.js' From b8247843782f133d4ea57bdb15bb476ebbb0f8e6 Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Sun, 26 Nov 2023 09:49:35 -0500 Subject: [PATCH 2/6] Update links in README to point to OpenSearch, not Elastic In #18, we switched from using OpenSearch instead of Elastic for local sandbox mode. Update links in the README accordingly. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 114537c..00edd30 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This is a [plugin](https://arc.codes/docs/en/guides/plugins/overview) for [Architect](https://arc.codes/) that provisions managed [Amazon OpenSearch](https://aws.amazon.com/opensearch-service/) for the application. -When you are using Architect's [sandbox](https://arc.codes/docs/en/reference/cli/sandbox) mode, the plugin [downloads and runs Elasticsearch locally](https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html#elasticsearch-install-packages). +When you are using Architect's [sandbox](https://arc.codes/docs/en/reference/cli/sandbox) mode, the plugin [downloads and runs OpenSearch locally](https://opensearch.org/downloads.html#opensearch). Pair this pacakge with [@nasa-gcn/architect-functions-search](https://github.com/nasa-gcn/architect-functions-search) to connect to the service from your Node.js Lambda function code. @@ -31,7 +31,7 @@ Pair this pacakge with [@nasa-gcn/architect-functions-search](https://github.com dedicatedMasterCount 3 dedicatedMasterType t3.small.search -4. Optionally, create a file called `sandbox-search.json` or `sandbox-search.js` in your project and populate it with sample data to be passed to [`client.bulk()`](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/bulk_examples.html). Here are some examples. +4. Optionally, create a file called `sandbox-search.json` or `sandbox-search.js` in your project and populate it with sample data to be passed to [`client.bulk()`](https://github.com/opensearch-project/opensearch-js/blob/main/guides/bulk.md). Here are some examples. ## sandbox-search.json From 0681aff0053049b83d1b9e6475699e46f845f110 Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Sun, 26 Nov 2023 10:15:18 -0500 Subject: [PATCH 3/6] Document client usage --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index 00edd30..93e6664 100644 --- a/README.md +++ b/README.md @@ -61,3 +61,41 @@ Pair this pacakge with [@nasa-gcn/architect-functions-search](https://github.com {"title": "Star Trek II: The Wrath of Khan"} ] } + +## Connecting to OpenSearch from your application + +In your Architect application, use the [@nasa-gcn/architect-functions-search](https://github.com/nasa-gcn/architect-functions-search) package to connect to your OpenSearch instance. To install the package, run: + +``` +npm i @nasa-gcn/architect-functions-search +``` + +Then add the following JavaScript code to your application: + +```ts +import { search } from '@nasa-gcn/architect-functions-search' + +const client = await search() +``` + +Now `client` is an instance of the [OpenSearch JavaScript client](https://opensearch.org/docs/latest/clients/javascript/index/), and you can call any client method on it — for example, [`client.index.create()`](https://opensearch.org/docs/latest/clients/javascript/index/#creating-an-index), [`client.index()`](https://opensearch.org/docs/latest/clients/javascript/index/#indexing-a-document), or [`client.search()`](https://opensearch.org/docs/latest/clients/javascript/index/#searching-for-documents). + +## Advanced usage from your application + +If you would like to manually connect to OpenSearch from your application, then you will need to sign your requests using [AWS SIG4](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html); the OpenSearch client library provides the [`AwsSigv4Signer`](https://opensearch.org/docs/latest/clients/javascript/index/#authenticating-with-amazon-opensearch-service--aws-sigv4) helper to automate this. + +You can read the API endpoint information using [Architect service discovery](): + +```ts +import * as arc from '@architect/functions' + +const services = await arc.services() +const searchConfig = services.nasa_gcn - architect_plugin_search +``` + +The `searchConfig` object has the following two properties: + +- `searchConfig.node`: the URL of the API endpoint. +- `searchConfig.sig4service`: the value to pass for the `service` property in the ``AwsSigv4Signer` constructor. + +See https://github.com/nasa-gcn/architect-functions-search/blob/main/index.ts for example code. From 8ce2349183f12df85f38762c42caccbe452f9a9d Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Sun, 26 Nov 2023 19:59:54 -0500 Subject: [PATCH 4/6] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 93e6664..e0ec513 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,6 @@ const searchConfig = services.nasa_gcn - architect_plugin_search The `searchConfig` object has the following two properties: - `searchConfig.node`: the URL of the API endpoint. -- `searchConfig.sig4service`: the value to pass for the `service` property in the ``AwsSigv4Signer` constructor. +- `searchConfig.sig4service`: the value to pass for the `service` property in the `AwsSigv4Signer` constructor. See https://github.com/nasa-gcn/architect-functions-search/blob/main/index.ts for example code. From 7c0bf9c86b451304ceb2d714affdd1ce0325ba94 Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Sun, 26 Nov 2023 20:12:41 -0500 Subject: [PATCH 5/6] Rename class LocalOpenSearch to LocalSearch We're going to support either Elasticsearch or OpenSearch in sandbox mode. Rename the class to reflect that. --- index.ts | 4 ++-- run.ts | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/index.ts b/index.ts index 21e6043..ef4e0e9 100644 --- a/index.ts +++ b/index.ts @@ -7,7 +7,7 @@ */ import { launch } from './run.js' -import type { LocalOpenSearch } from './run.js' +import type { LocalSearch } from './run.js' import { populate } from './data.js' import { cloudformationResources as serverlessCloudformationResources, @@ -58,7 +58,7 @@ export const deploy = { }, } -let local: LocalOpenSearch +let local: LocalSearch export const sandbox = { async start({ diff --git a/run.ts b/run.ts index dec69e2..075e233 100644 --- a/run.ts +++ b/run.ts @@ -21,7 +21,7 @@ import { import { pipeline } from 'stream/promises' import { createReadStream } from 'fs' -export class LocalOpenSearch { +export class LocalSearch { private readonly child!: ChildProcess private readonly tempDir!: string readonly port!: number @@ -93,8 +93,6 @@ export class LocalOpenSearch { } } -export async function launch( - ...args: Parameters -) { - return await LocalOpenSearch.launch(...args) +export async function launch(...args: Parameters) { + return await LocalSearch.launch(...args) } From 7803d00c4a0ffbac98945aaa40478645b9f6e47a Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Sun, 26 Nov 2023 20:17:49 -0500 Subject: [PATCH 6/6] Support serverless deployments with `@search` pragma We're going to add some more configs to the `@search` pragma to adjust sandbox settings, so we can no longer use the presence or absence of the pragma itself to determine whether to deploy to service mode or serverless mode. Instead, check whether we are in service or serverless mode by looking for one of the config keys that is required for service mode. --- index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index 21e6043..5471998 100644 --- a/index.ts +++ b/index.ts @@ -32,12 +32,20 @@ function toCollectionName(name: string) { .slice(0, 32) } +function getConfig(arc: { + search?: string[][] +}): Record { + if (arc.search) return Object.fromEntries(arc.search) + else return {} +} + export const deploy = { // @ts-expect-error: The Architect plugins API has no type definitions. start({ cloudformation, inventory, arc, stage }) { let resources - if (arc.search) { - resources = serviceCloudformationResources(Object.fromEntries(arc.search)) + const config = getConfig(arc) + if (config.availabilityZoneCount) { + resources = serviceCloudformationResources(config) } else { const { app } = inventory.inv const collectionName = toCollectionName(`${app}-${stage}`)