Skip to content

Instantly share code, notes, and snippets.

@pahud
Last active December 29, 2020 01:55
Show Gist options
  • Save pahud/78f9567dc7fc0b7afc4791615178988b to your computer and use it in GitHub Desktop.
Save pahud/78f9567dc7fc0b7afc4791615178988b to your computer and use it in GitHub Desktop.
create CDK L2 resource depends on parameter demo
import { App, Construct, Stack, StackProps, CfnParameter, CfnCondition, Fn } from '@aws-cdk/core';
import * as sns from '@aws-cdk/aws-sns';
export interface DemoProps { }
export class Demo extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id)
const stack = Stack.of(this);
const topic = new sns.Topic(this, 'Topic');
const input = new CfnParameter(stack, 'YourName', { allowedValues: ['foo', 'bar'] })
const isFoo = new CfnCondition(stack, 'ISFOO', {
expression: Fn.conditionEquals(input.valueAsString, 'foo')
})
const cfnTopic = topic.node.tryFindChild('Resource') as sns.CfnTopic;
cfnTopic.cfnOptions.condition = isFoo
}
}
export class MyStack extends Stack {
constructor(scope: Construct, id: string, props: StackProps = {}) {
super(scope, id, props);
new Demo(this, 'Demo');
}
}
// for development, use account/region from cdk cli
const devEnv = {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
};
const app = new App();
const stack = new MyStack(app, 'my-stack-dev', { env: devEnv });
// new MyStack(app, 'my-stack-prod', { env: prodEnv });
app.synth();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment