Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment