Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(aws_cdk.aws_applicationautoscaling): CfnScalableTarget.ScheduledActionProperty does not synthesize vaild cloudformation when using a CfnCondition #19246

Closed
Jengah opened this issue Mar 4, 2022 · 3 comments
Assignees
Labels
@aws-cdk/aws-applicationautoscaling Related to AWS Application Auto Scaling bug This issue is a bug. duplicate This issue is a duplicate. needs-triage This issue or PR still needs to be triaged.

Comments

@Jengah
Copy link

Jengah commented Mar 4, 2022

What is the problem?

When using a CfnCondition to conditionally set scheduled_actions within a CfnScalableTarget, the Scheduled Action properties are not synthesized to valid CloudFormation. Things work as expected without using a CfnCondition.

Reproduction Steps

Conditionally enable Scheduled Actions on a ScalableTarget utilizing a CfnCondition based on the value of a CfnParameter

        scheduled_scaling_param = CfnParameter(
            # .. param details ...
            allowed_values=["true", "false"]
        )

        scheduled_scaling_condition = CfnCondition(
            self,
            "ScheduledScalingCondition",
            expression=Fn.condition_equals(scheduled_scaling_param.value, "true"),
        )

        scalable_target = aa.CfnScalableTarget(
            # ... other req'd properties ...
            scheduled_actions=Fn.condition_if(
                scheduled_scaling_condition.logical_id,
                [
                    aa.CfnScalableTarget.ScheduledActionProperty(
                        schedule="cron(0 0 * * ? *)",
                        scheduled_action_name="OffHoursScaleDown",
                        scalable_target_action=aa.CfnScalableTarget.ScalableTargetActionProperty(
                            max_capacity=6,
                            min_capacity=1,
                        ),
                    ),
                    aa.CfnScalableTarget.ScheduledActionProperty(
                        schedule="cron(0 13 * * ? * )",
                        scheduled_action_name="NormalTrafficScaleUp",
                        scalable_target_action=aa.CfnScalableTarget.ScalableTargetActionProperty(
                            max_capacity=6,
                            min_capacity=4,
                        ),
                    ),
                ],
                Aws.NO_VALUE,
            ),
        )

Synthesize with cdk synth

What did you expect to happen?

Expected valid Cloudformation as a result of cdk synth as such (how we currently handle this in raw yaml CloudFormation):

ScheduledActions:
        Fn::If:
          - ScheduledScalingCondition
          - - Schedule: cron(0 0 * * ? *)
              ScheduledActionName: OffHoursScaleDown
              ScalableTargetAction:
                MaxCapacity: 6
                MinCapacity: 1
            - Schedule: cron(0 13 * * ? * )
              ScheduledActionName: NormalTrafficScaleUp
              ScalableTargetAction:
                MaxCapacity: 6
                MinCapacity: 4
          - Ref: AWS::NoValue

What actually happened?

Invalid properties as a result of cdk synth:

ScheduledActions:
        Fn::If:
          - ScheduledScalingCondition
          - - schedule: cron(0 0 * * ? *)
              scheduledActionName: OffHoursScaleDown
              scalableTargetAction:
                maxCapacity: 6
                minCapacity: 1
            - schedule: cron(0 13 * * ? * )
              scheduledActionName: NormalTrafficScaleUp
              scalableTargetAction:
                maxCapacity: 6
                minCapacity: 4
          - Ref: AWS::NoValue

Which results in a failed cdk deploy due to Encountered unsupported property schedule

CDK CLI Version

2.15.0

Framework Version

2.15.0

Node.js Version

16.13.1

OS

macOS Big Sur v11.6

Language

Python

Language Version

Python (3.9.6)

Other information

I am currently in the process of migrating our maintained CloudFormation yaml templates to CDK. In the spirit of creating as minimal difference between our deployed stack, I am trying to recreate our yaml via cdk synth as closely as possible. We currently use a Condition based on the value of a parameter to enable Scheduled Actions on a ScalableTarget. The idea here being that we have the ability to turn scaling on/off without a code change.

example of the raw yaml we write for our currently deployed CF stacks:

ScheduledActions:
        Fn::If:
        - ScheduledScaling
        -
          - ScalableTargetAction:
              MinCapacity: 1
              MaxCapacity: 6
            Schedule: "cron(0 0 * * ? *)"
            ScheduledActionName: OffHoursScaleDown
          - ScalableTargetAction:
              MinCapacity: 4
              MaxCapacity: 6
            Schedule: "cron(0 13 * * ? *)"
            ScheduledActionName: NormalTrafficScaleUp
        - !Ref "AWS::NoValue"
@Jengah Jengah added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 4, 2022
@github-actions github-actions bot added the @aws-cdk/aws-applicationautoscaling Related to AWS Application Auto Scaling label Mar 4, 2022
@peterwoodworth peterwoodworth added the duplicate This issue is a duplicate. label Mar 7, 2022
@peterwoodworth
Copy link
Contributor

Hey @Jengah, this is a known limitation with ConditionIf.

Generally, we recommend to use if statements in your code, rather than in your template 🙂 There's not a high likelihood we'll be fixing this on ConditionIf soon, so that's what I will recommend as well

@github-actions
Copy link

github-actions bot commented Mar 7, 2022

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@drobbins-ancile
Copy link

Hey @Jengah, this is a known limitation with ConditionIf.

Generally, we recommend to use if statements in your code, rather than in your template 🙂 There's not a high likelihood we'll be fixing this on ConditionIf soon, so that's what I will recommend as well

What would be your suggestion for those of us using CDK to synthesize CloudFormation templates for deployment in other environments? We cannot use if statements in our code as we are not synthesizing in every environment.

Is this not a valid use case for CDK?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-applicationautoscaling Related to AWS Application Auto Scaling bug This issue is a bug. duplicate This issue is a duplicate. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

4 participants