Skip to content

Commit

Permalink
feat(lambda): Expose $LATEST function version (#2792)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RomainMuller authored and rix0rrr committed Jun 12, 2019
1 parent 192bab7 commit 55d1bc8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/function-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import iam = require('@aws-cdk/aws-iam');
import { IResource, Resource } from '@aws-cdk/cdk';
import { IEventSource } from './event-source';
import { EventSourceMapping, EventSourceMappingOptions } from './event-source-mapping';
import { IVersion } from './lambda-version';
import { CfnPermission } from './lambda.generated';
import { Permission } from './permission';

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

/**
* The `$LATEST` version of this function.
*/
readonly latestVersion: IVersion;

/**
* Adds an event source that maps to this AWS Lambda function.
* @param id construct ID
Expand Down Expand Up @@ -139,6 +145,11 @@ export abstract class FunctionBase extends Resource implements IFunction {
*/
public abstract readonly role?: iam.IRole;

/**
* The $LATEST version of this function.
*/
public readonly latestVersion: IVersion = new LatestVersion(this);

/**
* Whether the addPermission() call adds any permissions
*
Expand Down Expand Up @@ -277,3 +288,16 @@ export abstract class FunctionBase extends Resource implements IFunction {
'Supported: AccountPrincipal, ServicePrincipal');
}
}

/**
* The $LATEST version of a function, useful when attempting to create aliases.
*/
class LatestVersion extends Resource implements IVersion {
public readonly lambda: IFunction;
public readonly version = '$LATEST';

constructor(lambda: FunctionBase) {
super(lambda, '$LATEST');
this.lambda = lambda;
}
}
23 changes: 23 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/test.alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ export = {
test.done();
},

'can create an alias to $LATEST'(test: Test): void {
const stack = new Stack();
const fn = new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('hello()'),
handler: 'index.hello',
runtime: lambda.Runtime.NodeJS810,
});

new lambda.Alias(stack, 'Alias', {
aliasName: 'latest',
version: fn.latestVersion,
});

expect(stack).to(haveResource('AWS::Lambda::Alias', {
FunctionName: { Ref: "MyLambdaCCE802FB" },
FunctionVersion: '$LATEST',
Name: 'latest',
}));
expect(stack).notTo(haveResource('AWS::Lambda::Version'));

test.done();
},

'can use newVersion to create a new Version'(test: Test) {
const stack = new Stack();
const fn = new lambda.Function(stack, 'MyLambda', {
Expand Down

0 comments on commit 55d1bc8

Please sign in to comment.