Skip to main content

hasContext

Useful for checking if a context key exists, either programatically set beforehand or from CLI via

cdk -c KEY=....

Example usage

YourStack.ts
import { CDKContextUtil } from 'aws-cdk-lib-util';
...

export class YourStack extends Stack {
constructor(scope: Construct, id: string, props: IYourStackProps) {
super(scope, id, props);

const {
projectName,
stackEnv,
} = props;
...
const hasTarget = CDKContextUtil.hasContextKey(this, 'SOME_CONTEXT_KEY_TARGET');
if(hasTarget) {
const target = scope.node.tryGetContext('SOME_CONTEXT_KEY_TARGET');
}
...
}
}