简体   繁体   English

条件 If 和枚举 AWS CDK (Python)

[英]Conditional If and ENUM AWS CDK (Python)

I just use CDK v2 for template generation.我只是使用 CDK v2 来生成模板。 When I try to recreate Conditions with Enums I get an error.当我尝试使用枚举重新创建条件时,出现错误。

Expected enum value, got {"$jsii.byref":"aws-cdk-lib.Intrinsic@10012"}预期的枚举值,得到 {"$jsii.byref":"aws-cdk-lib.Intrinsic@10012"}

Example code:示例代码:

    param_enableDeletionProtection = CfnParameter(self, "EnableDeletionProtection", 
        type="String",
        allowed_values=['True','False'],
        description="Whether critical services (like RDS) get provisioned with deletion protection or not")
condition_enable_deletion_protection = CfnCondition(self, "DeletionProtectionEnabled",
            expression=Fn.condition_equals(param_enableDeletionProtection.value_as_string, 'True')
    )
    cond_removal_policy=Fn.condition_if(condition_enable_deletion_protection.logical_id,
        RemovalPolicy.RETAIN,
        RemovalPolicy.DESTROY
    )
    s3_Bucket = s3.Bucket(self, "MyBucket",
        bucket_name="my-bucket-name",
        removal_policy=cond_removal_policy
    )

Am I doing something wrong?难道我做错了什么? Or is this not supported?还是不支持?

Fn.condition_if is for digging into the guts of the cloudformation template, and will return a statement - not the RETAIN or DESTROY enum that this property is expecting. Fn.condition_if用于挖掘 cloudformation 模板的内容,并将返回一个语句 - 而不是该属性所期望的 RETAIN 或 DESTROY 枚举。 As you are using python, you can litterally just do this:当您使用 python 时,您几乎可以这样做:

   cond_removal_policy=RemovalPolicy.RETAIN if some_condition_true else RemovalPolicy.DESTROY

This does mean that the condition needs to be set during synth which takes place before deployment.这确实意味着需要在部署之前进行的合成期间设置条件。 However, it seems you are already using parameter store for other things, you can continue to make use of that here and either use context variables ( cdk deploy Stack\* -c a_variable_name=someValue ) or other methods to determine what this should be at deployment time.但是,您似乎已经将参数存储用于其他事情,您可以在此处继续使用它,并使用上下文变量( cdk deploy Stack\* -c a_variable_name=someValue )或其他方法来确定这应该是什么部署时间。

remember, CDK doesn't actually do anything with deployment.请记住,CDK 实际上并没有对部署做任何事情。 It ONLY synthesizes a CloudFormation template, then passes template off to cloudformation to deploy.它只合成一个 CloudFormation 模板,然后将模板传递给 cloudformation 进行部署。 So your code is never in effect during the deployment, only in creating the infrastructure.因此,您的代码在部署期间永远不会生效,只会在创建基础架构时生效。 Anything you need to happen during the deployment should be handled either by a custom resource lambda or better yet a Pipeline.在部署期间需要发生的任何事情都应该由自定义资源 lambda 或更好的管道来处理。 Anything after has to be a custom resource.之后的任何内容都必须是自定义资源。 Anything that can occur before beginning deployment can be handled inside CDK stacks (such as discovering information to set up the properties of given resources)在开始部署之前可能发生的任何事情都可以在 CDK 堆栈中处理(例如发现信息以设置给定资源的属性)

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

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