AWS CDK Python 有条件地创建资源
AWS CDK Python Create Resources conditionally
我想根据参数值创建资源。我怎样才能做到这一点?
示例:
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)
如何将条件添加到 VPC 资源,以便仅当参数为真时才创建 VPC?
我想我需要获取 Cloudformation 资源,类似这样的东西:
vpc.node.default_child # And I think this returns an object from ec2.CfnVPC class, but I'm stuck here.
谢谢
使用 context
数据可以创建条件资源和许多其他灵活性。 AWS 本身推荐 context
而不是 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.
请阅读全文:https://docs.aws.amazon.com/cdk/latest/guide/parameters.html
我想根据参数值创建资源。我怎样才能做到这一点?
示例:
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)
如何将条件添加到 VPC 资源,以便仅当参数为真时才创建 VPC?
我想我需要获取 Cloudformation 资源,类似这样的东西:
vpc.node.default_child # And I think this returns an object from ec2.CfnVPC class, but I'm stuck here.
谢谢
使用 context
数据可以创建条件资源和许多其他灵活性。 AWS 本身推荐 context
而不是 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.
请阅读全文:https://docs.aws.amazon.com/cdk/latest/guide/parameters.html