How to specify a boolean parameter of cloudformation in CDK typescript

0

I want to use a boolean CFN parameter in CDK as below code and get error "An argument for '_context' was not provided."

    const enableRefresh = new cdk.CfnParameter(this, 'EnableRefresh', {
      type: 'String',
      description: 'Enable Refresh rule, disable by default',
      noEcho: false,
      allowedValues: ['true', 'false'],
      default:'true'
    })

    const cfnCondition = new cdk.CfnCondition(this, "Refresh enabled",{
      expression: cdk.Fn.conditionEquals(enableRefresh.valueAsString, 'true')
    })

    const enabledOrNot: boolean | undefined = cfnCondition.resolve() === 'true'
    // const enabledOrNot: IResolvable = cdk.Fn.conditionIf(cfnCondition.logicalId, true, false);
    const refreshEventRule = new events.Rule(this, 'refreshRule', {
      schedule: events.Schedule.rate(cdk.Duration.minutes(100)),
      description: `Refresh every 100 minutes`,
      enabled: enabledOrNot
    })
  }

Can expert give me an example to use the boolean CFN parameter for the enabled (boolean) parameter in events.Ruls's props?

AWS
已提问 6 个月前540 查看次数
1 回答
1
已接受的回答

For conditional resource creation using CfnCondition, you will need to add a condition property to the cfnOptions of the resource.

Check out this sample:

    const enableRefresh = new CfnParameter(this, 'EnableRefresh', {
      type: 'String',
      allowedValues: ['true', 'false'],
      default:'true'
    })

    const isRefreshEnabledCond = new CfnCondition(this, "Refresh enabled",{
      expression: Fn.conditionEquals(enableRefresh.valueAsString , 'true')
    })

    const rule = new events.Rule(this, 'Rule', {
      schedule: events.Schedule.rate(Duration.minutes(100)),
    });
    const cfnRule = rule.node.tryFindChild('Resource') as events.CfnRule;
    cfnRule.cfnOptions.condition = isRefreshEnabledCond

And run cdk synth to verify the template. Make sure you see the Condition attached in the resource definition.

Resources:
  Rule4C995B7F:
    Type: AWS::Events::Rule
    Properties:
      ScheduleExpression: rate(100 minutes)
      State: ENABLED
    Metadata:
      aws:cdk:path: demo-stack9/Rule/Resource
    Condition: Refreshenabled
AWS
已回答 6 个月前
profile picture
专家
已审核 4 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则