Skip to content

Commit 42013ec

Browse files
committed
fix: function.latestVersion points to wrong ARN
The `IFunctionRef` implementation of `LatestVersion` returns the underlying function ARN, instead of the version's ARN. This is a similar situation and fix to #35545.
1 parent f95f7ac commit 42013ec

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -866,12 +866,15 @@ class LatestVersion extends FunctionBase implements IVersion {
866866

867867
public get versionRef(): VersionReference {
868868
return {
869-
functionArn: this.functionRef.functionArn,
869+
functionArn: this.functionArn,
870870
};
871871
}
872872

873-
public get functionRef() {
874-
return this.lambda.functionRef;
873+
public get functionRef(): FunctionReference {
874+
return {
875+
functionArn: this.functionArn,
876+
functionName: this.functionName,
877+
};
875878
}
876879

877880
public get functionArn() {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,4 +657,24 @@ describe('alias', () => {
657657
Qualifier: aliasName,
658658
});
659659
});
660+
661+
test('alias\' implementation of IFunctionRef should point to the alias', () => {
662+
// GIVEN
663+
const stack = new Stack();
664+
const fn = new lambda.Function(stack, 'MyLambda', {
665+
code: new lambda.InlineCode('hello()'),
666+
handler: 'index.hello',
667+
runtime: lambda.Runtime.NODEJS_LATEST,
668+
});
669+
const aliasName = 'prod';
670+
671+
// WHEN
672+
const alias = new lambda.Alias(stack, 'Alias', {
673+
aliasName,
674+
version: fn.currentVersion,
675+
});
676+
677+
// THEN
678+
expect(alias.functionRef.functionArn).toEqual(alias.functionArn);
679+
});
660680
});

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,6 +2764,21 @@ describe('function', () => {
27642764
expect(stack.resolve(version2.functionArn)).toEqual(expectedArn);
27652765
});
27662766

2767+
test('latestVersion functionRef ARN is the version ARN, not the plain ARN', () => {
2768+
// GIVEN
2769+
const stack = new cdk.Stack();
2770+
2771+
// WHEN
2772+
const fn = new lambda.Function(stack, 'MyLambda', {
2773+
code: new lambda.InlineCode('hello()'),
2774+
handler: 'index.hello',
2775+
runtime: lambda.Runtime.NODEJS_LATEST,
2776+
});
2777+
2778+
// THEN
2779+
expect(fn.latestVersion.functionRef.functionArn).toEqual(fn.latestVersion.functionArn);
2780+
});
2781+
27672782
test('default function with kmsKeyArn, environmentEncryption passed as props', () => {
27682783
// GIVEN
27692784
const stack = new cdk.Stack();

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,22 @@ describe('lambda version', () => {
229229
version.addFunctionUrl();
230230
}).toThrow(/FunctionUrl cannot be used with a Version/);
231231
});
232+
233+
test('version\'s implementation of IFunctionRef should point to the version', () => {
234+
// GIVEN
235+
const stack = new cdk.Stack();
236+
const fn = new lambda.Function(stack, 'MyLambda', {
237+
code: new lambda.InlineCode('hello()'),
238+
handler: 'index.hello',
239+
runtime: lambda.Runtime.NODEJS_LATEST,
240+
});
241+
242+
// WHEN
243+
const ver = new lambda.Version(stack, 'Version', {
244+
lambda: fn,
245+
});
246+
247+
// THEN
248+
expect(ver.functionRef.functionArn).toEqual(ver.functionArn);
249+
});
232250
});

0 commit comments

Comments
 (0)