简体   繁体   English

AWS CDK Python 有条件地创建资源

[英]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?仅当参数为真时,如何将条件添加到正在创建的 VPC 的 VPC 资源中?

I think that I need to get Cloudformation resource, something like that:我认为我需要获取 Cloudformation 资源,例如:

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.使用context数据可以实现有条件的资源创建和许多其他灵活性。 AWS itself recommends context over parameters AWS 本身推荐context不是parameters

In general, we recommend against using AWS CloudFormation parameters with the AWS CDK.通常,我们建议不要将 AWS CloudFormation 参数与 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.与上下文值或环境变量不同,将值传递到 AWS CDK 应用程序而不对其进行硬编码的常用方法,参数值在合成时不可用,因此无法轻松用于 AWS CDK 应用程序的其他部分,特别是对于控制流。

Please read in full at: https://docs.aws.amazon.com/cdk/latest/guide/parameters.html请阅读全文: https : //docs.aws.amazon.com/cdk/latest/guide/parameters.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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