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?

1 Answer
1
Accepted Answer

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
answered 6 months ago
profile picture
EXPERT
reviewed 4 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions