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
preguntada hace 6 meses540 visualizaciones
1 Respuesta
1
Respuesta aceptada

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
respondido hace 6 meses
profile picture
EXPERTO
revisado hace 4 meses

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas