Skip to content

Commit

Permalink
fix(core): prevent the error when the condition is split into groups …
Browse files Browse the repository at this point in the history
…of 10 and 1 in `Fn.conditionOr()` (#25708)

Closes #25696

>The problem I'm running into is for a list of 11 elements. CDK generates two Fn::Or expressions: One with 10 elements and one with 1 element. When deployment this stack, CloudFormation complains that an Fn::Or must contain at least 2 elements.

reproduce code: #25696 (comment)
approach: #25696 (comment)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
wafuwafu13 committed Jun 14, 2023
1 parent d51bd8b commit c135656
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/aws-cdk-lib/core/lib/cfn-fn.ts
Expand Up @@ -334,7 +334,10 @@ export class Fn {
if (conditions.length === 1) {
return conditions[0] as ICfnRuleConditionExpression;
}
return Fn.conditionOr(..._inGroupsOf(conditions, 10).map(group => new FnOr(...group)));
if (conditions.length <= 10) {
return new FnOr(...conditions);
}
return Fn.conditionOr(..._inGroupsOf(conditions, 10).map(group => Fn.conditionOr(...group)));
}

/**
Expand Down
100 changes: 100 additions & 0 deletions packages/aws-cdk-lib/core/test/condition.test.ts
Expand Up @@ -59,4 +59,104 @@ describe('condition', () => {
},
});
});

test('condition length is 10n + 1 in Fn.conditionOr', () => {
// GIVEN
const stack = new cdk.Stack();
const expression = cdk.Fn.conditionOr(
cdk.Fn.conditionEquals('a', '1'),
cdk.Fn.conditionEquals('b', '2'),
cdk.Fn.conditionEquals('c', '3'),
cdk.Fn.conditionEquals('d', '4'),
cdk.Fn.conditionEquals('e', '5'),
cdk.Fn.conditionEquals('f', '6'),
cdk.Fn.conditionEquals('g', '7'),
cdk.Fn.conditionEquals('h', '8'),
cdk.Fn.conditionEquals('i', '9'),
cdk.Fn.conditionEquals('j', '10'),
cdk.Fn.conditionEquals('k', '11'),
);

// WHEN
new cdk.CfnCondition(stack, 'Condition', { expression });

// THEN
expect(toCloudFormation(stack)).toEqual({
Conditions: {
Condition: {
'Fn::Or': [
{
'Fn::Or': [
{ 'Fn::Equals': ['a', '1'] },
{ 'Fn::Equals': ['b', '2'] },
{ 'Fn::Equals': ['c', '3'] },
{ 'Fn::Equals': ['d', '4'] },
{ 'Fn::Equals': ['e', '5'] },
{ 'Fn::Equals': ['f', '6'] },
{ 'Fn::Equals': ['g', '7'] },
{ 'Fn::Equals': ['h', '8'] },
{ 'Fn::Equals': ['i', '9'] },
{ 'Fn::Equals': ['j', '10'] },
],
},
{
'Fn::Equals': ['k', '11'],
},
],
},
},
});
});

test('condition length is more than 10 in Fn.conditionOr', () => {
// GIVEN
const stack = new cdk.Stack();
const expression = cdk.Fn.conditionOr(
cdk.Fn.conditionEquals('a', '1'),
cdk.Fn.conditionEquals('b', '2'),
cdk.Fn.conditionEquals('c', '3'),
cdk.Fn.conditionEquals('d', '4'),
cdk.Fn.conditionEquals('e', '5'),
cdk.Fn.conditionEquals('f', '6'),
cdk.Fn.conditionEquals('g', '7'),
cdk.Fn.conditionEquals('h', '8'),
cdk.Fn.conditionEquals('i', '9'),
cdk.Fn.conditionEquals('j', '10'),
cdk.Fn.conditionEquals('k', '11'),
cdk.Fn.conditionEquals('l', '12'),
);

// WHEN
new cdk.CfnCondition(stack, 'Condition', { expression });

// THEN
expect(toCloudFormation(stack)).toEqual({
Conditions: {
Condition: {
'Fn::Or': [
{
'Fn::Or': [
{ 'Fn::Equals': ['a', '1'] },
{ 'Fn::Equals': ['b', '2'] },
{ 'Fn::Equals': ['c', '3'] },
{ 'Fn::Equals': ['d', '4'] },
{ 'Fn::Equals': ['e', '5'] },
{ 'Fn::Equals': ['f', '6'] },
{ 'Fn::Equals': ['g', '7'] },
{ 'Fn::Equals': ['h', '8'] },
{ 'Fn::Equals': ['i', '9'] },
{ 'Fn::Equals': ['j', '10'] },
],
},
{
'Fn::Or': [
{ 'Fn::Equals': ['k', '11'] },
{ 'Fn::Equals': ['l', '12'] },
],
},
],
},
},
});
});
});

0 comments on commit c135656

Please sign in to comment.