Last active
December 29, 2020 01:55
-
-
Save pahud/78f9567dc7fc0b7afc4791615178988b to your computer and use it in GitHub Desktop.
create CDK L2 resource depends on parameter demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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