Skip to content

Commit c135656

Browse files
authored
fix(core): prevent the error when the condition is split into groups 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*
1 parent d51bd8b commit c135656

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

packages/aws-cdk-lib/core/lib/cfn-fn.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,10 @@ export class Fn {
334334
if (conditions.length === 1) {
335335
return conditions[0] as ICfnRuleConditionExpression;
336336
}
337-
return Fn.conditionOr(..._inGroupsOf(conditions, 10).map(group => new FnOr(...group)));
337+
if (conditions.length <= 10) {
338+
return new FnOr(...conditions);
339+
}
340+
return Fn.conditionOr(..._inGroupsOf(conditions, 10).map(group => Fn.conditionOr(...group)));
338341
}
339342

340343
/**

packages/aws-cdk-lib/core/test/condition.test.ts

+100
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,104 @@ describe('condition', () => {
5959
},
6060
});
6161
});
62+
63+
test('condition length is 10n + 1 in Fn.conditionOr', () => {
64+
// GIVEN
65+
const stack = new cdk.Stack();
66+
const expression = cdk.Fn.conditionOr(
67+
cdk.Fn.conditionEquals('a', '1'),
68+
cdk.Fn.conditionEquals('b', '2'),
69+
cdk.Fn.conditionEquals('c', '3'),
70+
cdk.Fn.conditionEquals('d', '4'),
71+
cdk.Fn.conditionEquals('e', '5'),
72+
cdk.Fn.conditionEquals('f', '6'),
73+
cdk.Fn.conditionEquals('g', '7'),
74+
cdk.Fn.conditionEquals('h', '8'),
75+
cdk.Fn.conditionEquals('i', '9'),
76+
cdk.Fn.conditionEquals('j', '10'),
77+
cdk.Fn.conditionEquals('k', '11'),
78+
);
79+
80+
// WHEN
81+
new cdk.CfnCondition(stack, 'Condition', { expression });
82+
83+
// THEN
84+
expect(toCloudFormation(stack)).toEqual({
85+
Conditions: {
86+
Condition: {
87+
'Fn::Or': [
88+
{
89+
'Fn::Or': [
90+
{ 'Fn::Equals': ['a', '1'] },
91+
{ 'Fn::Equals': ['b', '2'] },
92+
{ 'Fn::Equals': ['c', '3'] },
93+
{ 'Fn::Equals': ['d', '4'] },
94+
{ 'Fn::Equals': ['e', '5'] },
95+
{ 'Fn::Equals': ['f', '6'] },
96+
{ 'Fn::Equals': ['g', '7'] },
97+
{ 'Fn::Equals': ['h', '8'] },
98+
{ 'Fn::Equals': ['i', '9'] },
99+
{ 'Fn::Equals': ['j', '10'] },
100+
],
101+
},
102+
{
103+
'Fn::Equals': ['k', '11'],
104+
},
105+
],
106+
},
107+
},
108+
});
109+
});
110+
111+
test('condition length is more than 10 in Fn.conditionOr', () => {
112+
// GIVEN
113+
const stack = new cdk.Stack();
114+
const expression = cdk.Fn.conditionOr(
115+
cdk.Fn.conditionEquals('a', '1'),
116+
cdk.Fn.conditionEquals('b', '2'),
117+
cdk.Fn.conditionEquals('c', '3'),
118+
cdk.Fn.conditionEquals('d', '4'),
119+
cdk.Fn.conditionEquals('e', '5'),
120+
cdk.Fn.conditionEquals('f', '6'),
121+
cdk.Fn.conditionEquals('g', '7'),
122+
cdk.Fn.conditionEquals('h', '8'),
123+
cdk.Fn.conditionEquals('i', '9'),
124+
cdk.Fn.conditionEquals('j', '10'),
125+
cdk.Fn.conditionEquals('k', '11'),
126+
cdk.Fn.conditionEquals('l', '12'),
127+
);
128+
129+
// WHEN
130+
new cdk.CfnCondition(stack, 'Condition', { expression });
131+
132+
// THEN
133+
expect(toCloudFormation(stack)).toEqual({
134+
Conditions: {
135+
Condition: {
136+
'Fn::Or': [
137+
{
138+
'Fn::Or': [
139+
{ 'Fn::Equals': ['a', '1'] },
140+
{ 'Fn::Equals': ['b', '2'] },
141+
{ 'Fn::Equals': ['c', '3'] },
142+
{ 'Fn::Equals': ['d', '4'] },
143+
{ 'Fn::Equals': ['e', '5'] },
144+
{ 'Fn::Equals': ['f', '6'] },
145+
{ 'Fn::Equals': ['g', '7'] },
146+
{ 'Fn::Equals': ['h', '8'] },
147+
{ 'Fn::Equals': ['i', '9'] },
148+
{ 'Fn::Equals': ['j', '10'] },
149+
],
150+
},
151+
{
152+
'Fn::Or': [
153+
{ 'Fn::Equals': ['k', '11'] },
154+
{ 'Fn::Equals': ['l', '12'] },
155+
],
156+
},
157+
],
158+
},
159+
},
160+
});
161+
});
62162
});

0 commit comments

Comments
 (0)