Skip to content

Commit 208ef18

Browse files
author
Leon Michalski
committed
pass arrays as is
1 parent d8ebe73 commit 208ef18

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

packages/aws-cdk-lib/aws-lambda/test/function.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5219,6 +5219,61 @@ describe('L1 Relationships', () => {
52195219
});
52205220
});
52215221

5222+
it('array reference should be valid', () => {
5223+
const stack = new cdk.Stack();
5224+
const role = new iam.Role(stack, 'SomeRole', {
5225+
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
5226+
});
5227+
const layer1 = new lambda.LayerVersion(stack, 'LayerVersion1', {
5228+
code: lambda.Code.fromAsset(path.join(__dirname, 'my-lambda-handler')),
5229+
compatibleRuntimes: [lambda.Runtime.PYTHON_3_13],
5230+
});
5231+
const layerArray = [layer1, 'layer2Arn'];
5232+
new lambda.CfnFunction(stack, 'MyLambda', {
5233+
code: { zipFile: 'foo' },
5234+
role: role,
5235+
layers: layerArray,
5236+
});
5237+
5238+
layerArray.push('layer3Arn');
5239+
5240+
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
5241+
Properties: {
5242+
Role: { 'Fn::GetAtt': ['SomeRole6DDC54DD', 'Arn'] },
5243+
Layers: [{ Ref: 'LayerVersion139D4D7A8' }, 'layer2Arn', 'layer3Arn'],
5244+
},
5245+
});
5246+
});
5247+
5248+
it('nested array references should still be valid', () => {
5249+
const stack = new cdk.Stack();
5250+
const role = new iam.Role(stack, 'SomeRole', {
5251+
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
5252+
});
5253+
const securityGroup = new ec2.SecurityGroup(stack, 'SG', {
5254+
vpc: new ec2.Vpc(stack, 'VPC'),
5255+
});
5256+
const vpcConfig = {
5257+
securityGroupIds: [securityGroup, 'securityGroupArn2'],
5258+
};
5259+
new lambda.CfnFunction(stack, 'MyLambda', {
5260+
code: { zipFile: 'foo' },
5261+
role: role,
5262+
vpcConfig,
5263+
});
5264+
5265+
vpcConfig.securityGroupIds.push('securityGroupArn3');
5266+
5267+
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
5268+
Properties: {
5269+
Role: { 'Fn::GetAtt': ['SomeRole6DDC54DD', 'Arn'] },
5270+
VpcConfig: {
5271+
SecurityGroupIds: [{ 'Fn::GetAtt': ['SGADB53937', 'GroupId'] }, 'securityGroupArn2', 'securityGroupArn3'],
5272+
},
5273+
},
5274+
});
5275+
});
5276+
52225277
it('tokens should be passed as is', () => {
52235278
const stack = new cdk.Stack();
52245279
const role = new iam.Role(stack, 'SomeRole', {

tools/@aws-cdk/spec2cdk/lib/cdk/resolver-builder.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ export class ResolverBuilder {
9393
].join(' ?? ');
9494
const resolver = (_: Expression) => {
9595
if (resolvableType.arrayOfType) {
96-
return expr.directCode(`props.${name}?.map((item: any) => ${ buildChain('item') })`);
96+
return expr.directCode(
97+
`(props.${name}?.forEach((item: ${propType.arrayOfType!.toString()}, i: number, arr: ${propType.toString()}) => { arr[i] = ${buildChain('item')}; }), props.${name} as ${resolvableType.toString()})`,
98+
);
9799
} else {
98100
return expr.directCode(buildChain(`props.${name}`));
99101
}
@@ -118,11 +120,11 @@ export class ResolverBuilder {
118120
const isArray = baseType.arrayOfType !== undefined;
119121

120122
const flattenCall = isArray
121-
? propValue.callMethod('map', expr.ident(functionName))
123+
? expr.directCode(`props.${name}.forEach((item: any, i: number, arr: any[]) => { arr[i] = ${functionName}(item) }), props.${name}`)
122124
: expr.ident(functionName).call(propValue);
123125

124126
const condition = optional
125-
? expr.cond(propValue).then(flattenCall).else(expr.UNDEFINED)
127+
? expr.cond(expr.not(propValue)).then(expr.UNDEFINED).else(flattenCall)
126128
: flattenCall;
127129

128130
return isArray

0 commit comments

Comments
 (0)