Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda): Expose $LATEST function version #2792

Merged
merged 3 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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.
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
*
* Lambda.import(this, 'MyImportedFunction', { lambdaArn: new LambdaArn('arn:aws:...') });
*
Expand Down
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