Skip to content

Commit

Permalink
Add feature for contextKey value for nested context
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Kurait <[email protected]>
  • Loading branch information
AndreKurait committed Jun 17, 2024
1 parent 974b6f3 commit c4d427e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ In order to deploy both the stacks the user needs to provide a set of required a

| Name | Requirement | Type | Description |
|-------------------------------|:------------|:----------|:------------|
| contextKey | Optional | string | A top-level key that the rest of the parameters can be nested within for organized configuration. This is used to parse config from a specific key in the `cdk.context.json` or in the `context` block in the `cdk.json` file. |
| distVersion | Required | string | The OpenSearch distribution version (released/un-released) the user wants to deploy |
| securityDisabled | Required | boolean | Enable or disable security plugin |
| adminPassword | Optionally required | string | This value is required when security plugin is enabled and the cluster version is greater or equal to `2.12.0`|
Expand Down Expand Up @@ -109,6 +110,26 @@ cdk synth "*" --context securityDisabled=false \
--context distVersion=2.3.0 --context serverAccessType=ipv4 --context restrictServerAccessTo=10.10.10.10/32
```

Alternatively, you can use the `contextKey` to provide the configuration.

For example, to synthesize the CloudFormation templates using a context key:
```sh
cdk synth "*" --context contextKey=devConfig
```
You would include the configuration in your `cdk.json` file like this:
```js
// cdk.json
{
"context": {
"devConfig": {
"securityDisabled": false,
// ...
}
}
}
```
This approach allows you to manage multiple configurations easily by defining different context keys for each environment.

#### Sample command to set up multi-node cluster with security enabled on x64 AL2 machine

Please note that as of now we only support instances backed by Amazon Linux-2 amis.
Expand Down
13 changes: 13 additions & 0 deletions bin/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ import { InfraStack } from '../lib/infra/infra-stack';
import { NetworkStack } from '../lib/networking/vpc-stack';

const app = new App();

const contextKey = app.node.tryGetContext('contextKey');
if (contextKey) {
const nestedContext = app.node.tryGetContext(contextKey);
if (nestedContext && typeof nestedContext === 'object') {
console.log("Setting context with keys:\n" +

Check warning on line 20 in bin/app.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
Object.entries(nestedContext).map(([key, value]) => ` ${key}: ${value}`).join('\n'));
for (const [nestedKey, nestedValue] of Object.entries(nestedContext)) {

Check failure on line 22 in bin/app.ts

View workflow job for this annotation

GitHub Actions / build

iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations
app.node.setContext(nestedKey, nestedValue);
}
}
}

const region = app.node.tryGetContext('region') ?? process.env.CDK_DEFAULT_REGION;
const account = app.node.tryGetContext('account') ?? process.env.CDK_DEFAULT_ACCOUNT;

Expand Down

0 comments on commit c4d427e

Please sign in to comment.