Skip to content

Commit 55d1bc8

Browse files
RomainMullerrix0rrr
authored andcommittedJun 12, 2019
feat(lambda): Expose $LATEST function version (#2792)
Sometimes, one wants to create an `Alias` to the `$LATEST` version of a `Function`, but this requires access to an `IVersion` representing that. This features adds a `latestVersion` property to `IFunction` that offers simple access to `$LATEST`. Prior to this, each user needing to access this would have had to roll theiw own implementation of `IVersion`, which is rather cumbersome. Fixes #2776
1 parent 192bab7 commit 55d1bc8

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
 

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

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import iam = require('@aws-cdk/aws-iam');
44
import { IResource, Resource } from '@aws-cdk/cdk';
55
import { IEventSource } from './event-source';
66
import { EventSourceMapping, EventSourceMappingOptions } from './event-source-mapping';
7+
import { IVersion } from './lambda-version';
78
import { CfnPermission } from './lambda.generated';
89
import { Permission } from './permission';
910

@@ -40,6 +41,11 @@ export interface IFunction extends IResource, ec2.IConnectable, iam.IGrantable {
4041
*/
4142
readonly isBoundToVpc: boolean;
4243

44+
/**
45+
* The `$LATEST` version of this function.
46+
*/
47+
readonly latestVersion: IVersion;
48+
4349
/**
4450
* Adds an event source that maps to this AWS Lambda function.
4551
* @param id construct ID
@@ -139,6 +145,11 @@ export abstract class FunctionBase extends Resource implements IFunction {
139145
*/
140146
public abstract readonly role?: iam.IRole;
141147

148+
/**
149+
* The $LATEST version of this function.
150+
*/
151+
public readonly latestVersion: IVersion = new LatestVersion(this);
152+
142153
/**
143154
* Whether the addPermission() call adds any permissions
144155
*
@@ -277,3 +288,16 @@ export abstract class FunctionBase extends Resource implements IFunction {
277288
'Supported: AccountPrincipal, ServicePrincipal');
278289
}
279290
}
291+
292+
/**
293+
* The $LATEST version of a function, useful when attempting to create aliases.
294+
*/
295+
class LatestVersion extends Resource implements IVersion {
296+
public readonly lambda: IFunction;
297+
public readonly version = '$LATEST';
298+
299+
constructor(lambda: FunctionBase) {
300+
super(lambda, '$LATEST');
301+
this.lambda = lambda;
302+
}
303+
}

‎packages/@aws-cdk/aws-lambda/test/test.alias.ts

+23
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ export = {
4040
test.done();
4141
},
4242

43+
'can create an alias to $LATEST'(test: Test): void {
44+
const stack = new Stack();
45+
const fn = new lambda.Function(stack, 'MyLambda', {
46+
code: new lambda.InlineCode('hello()'),
47+
handler: 'index.hello',
48+
runtime: lambda.Runtime.NodeJS810,
49+
});
50+
51+
new lambda.Alias(stack, 'Alias', {
52+
aliasName: 'latest',
53+
version: fn.latestVersion,
54+
});
55+
56+
expect(stack).to(haveResource('AWS::Lambda::Alias', {
57+
FunctionName: { Ref: "MyLambdaCCE802FB" },
58+
FunctionVersion: '$LATEST',
59+
Name: 'latest',
60+
}));
61+
expect(stack).notTo(haveResource('AWS::Lambda::Version'));
62+
63+
test.done();
64+
},
65+
4366
'can use newVersion to create a new Version'(test: Test) {
4467
const stack = new Stack();
4568
const fn = new lambda.Function(stack, 'MyLambda', {

0 commit comments

Comments
 (0)