简体   繁体   中英

AWS CDK Python Create Resources conditionally

I would like to create resources depending on the parameters value. How can I achieve that?

Example:

vpc_create = core.CfnParameter(stack, "createVPC")
condition = core.CfnCondition(stack, 
                             "testeCondition",
                              expression=core.Fn.condition_equals(vpc_create, True)
)
vpc = ec2.Vpc(stack, "MyVpc", max_azs=3)

How add the condition to VPC resource for VPC being created only if the parameter it's true?

I think that I need to get Cloudformation resource, something like that:

vpc.node.default_child # And I think this returns an object from ec2.CfnVPC class, but I'm stuck here.

Thanks

Conditional resource creation and a lot of other flexibility is possible using context data. AWS itself recommends context over parameters

In general, we recommend against using AWS CloudFormation parameters with the AWS CDK. Unlike context values or environment variables, the usual way to pass values into your AWS CDK apps without hard-coding them, parameter values are not available at synthesis time, and thus cannot be easily used in other parts of your AWS CDK app, particularly for control flow.

Please read in full at: https://docs.aws.amazon.com/cdk/latest/guide/parameters.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM