Skip to content

Commit

Permalink
feat(lambda): Expose $LATEST function version
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 committed Jun 7, 2019
1 parent e4fb811 commit e074e1c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
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;
}
}
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class Function extends FunctionBase {

/**
* Creates a Lambda function object which represents a function not defined
* within this stack.
* within this stwack.
*
* Lambda.import(this, 'MyImportedFunction', { lambdaArn: new LambdaArn('arn:aws:...') });
*
Expand Down
33 changes: 33 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,39 @@ 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(beASupersetOfTemplate({
MyLambdaVersion16CDE3C40: {
Type: "AWS::Lambda::Version",
Properties: {
FunctionName: { Ref: "MyLambdaCCE802FB" }
}
},
Alias325C5727: {
Type: "AWS::Lambda::Alias",
Properties: {
FunctionName: { Ref: "MyLambdaCCE802FB" },
FunctionVersion: '$LATEST',
Name: "latest"
}
}
}));

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 e074e1c

Please sign in to comment.