Skip to content

Commit

Permalink
Merge branch 'master' into lambda-nodejs-local-cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Aug 26, 2020
2 parents 98f9513 + e6db2a0 commit 04cbc6c
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 31 deletions.
8 changes: 4 additions & 4 deletions .mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pull_request_rules:
- "#changes-requested-reviews-by=0"
- status-success~=AWS CodeBuild us-east-1
#- status-success=Semantic Pull Request
- status-success=mandatory-changes
- status-success=validate-pr
- name: automatic merge (2+ approvers)
actions:
comment:
Expand All @@ -56,7 +56,7 @@ pull_request_rules:
- "#changes-requested-reviews-by=0"
- status-success~=AWS CodeBuild us-east-1
#- status-success=Semantic Pull Request
- status-success=mandatory-changes
- status-success=validate-pr
- name: automatic merge (no-squash)
actions:
comment:
Expand All @@ -82,7 +82,7 @@ pull_request_rules:
- "#changes-requested-reviews-by=0"
- status-success~=AWS CodeBuild us-east-1
#- status-success=Semantic Pull Request
- status-success=mandatory-changes
- status-success=validate-pr
- name: remove stale reviews
actions:
dismiss_reviews:
Expand Down Expand Up @@ -126,4 +126,4 @@ pull_request_rules:
- "#changes-requested-reviews-by=0"
- status-success~=AWS CodeBuild us-east-1
#- status-success=Semantic Pull Request
- status-success=mandatory-changes
- status-success=validate-pr
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/test/distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,42 @@ describe('with Lambda@Edge functions', () => {

expect(() => app.synth()).toThrow(/KEY/);
});

test('with singleton function', () => {
const singleton = new lambda.SingletonFunction(stack, 'Singleton', {
uuid: 'singleton-for-cloudfront',
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromInline('code'),
handler: 'index.handler',
});

new Distribution(stack, 'MyDist', {
defaultBehavior: {
origin,
edgeLambdas: [
{
functionVersion: singleton.currentVersion,
eventType: LambdaEdgeEventType.ORIGIN_REQUEST,
},
],
},
});

expect(stack).toHaveResourceLike('AWS::CloudFront::Distribution', {
DistributionConfig: {
DefaultCacheBehavior: {
LambdaFunctionAssociations: [
{
EventType: 'origin-request',
LambdaFunctionARN: {
Ref: 'SingletonLambdasingletonforcloudfrontCurrentVersion0078406348a0962a52448a200cd0dbc0e22edb2a',
},
},
],
},
},
});
});
});

test('price class is included if provided', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ nodeunitShim({
isDefaultBehavior: true,
lambdaFunctionAssociations: [{
eventType: LambdaEdgeEventType.ORIGIN_REQUEST,
lambdaFunction: lambdaFunction.addVersion('1'),
lambdaFunction: lambdaFunction.currentVersion,
}],
},
],
Expand All @@ -458,7 +458,7 @@ nodeunitShim({
{
'EventType': 'origin-request',
'LambdaFunctionARN': {
'Ref': 'LambdaVersion1BB7548E1',
'Ref': 'LambdaCurrentVersionDF706F6A97fb843e9bd06fcd2bb15eeace80e13e',
},
},
],
Expand Down Expand Up @@ -492,7 +492,7 @@ nodeunitShim({
isDefaultBehavior: true,
lambdaFunctionAssociations: [{
eventType: LambdaEdgeEventType.ORIGIN_REQUEST,
lambdaFunction: lambdaFunction.addVersion('1'),
lambdaFunction: lambdaFunction.currentVersion,
}],
},
],
Expand Down Expand Up @@ -532,7 +532,7 @@ nodeunitShim({
isDefaultBehavior: true,
lambdaFunctionAssociations: [{
eventType: LambdaEdgeEventType.ORIGIN_REQUEST,
lambdaFunction: lambdaFunction.addVersion('1'),
lambdaFunction: lambdaFunction.currentVersion,
}],
},
],
Expand Down
17 changes: 6 additions & 11 deletions packages/@aws-cdk/aws-lambda/lib/function-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ export abstract class FunctionBase extends Resource implements IFunction {
*/
protected _connections?: ec2.Connections;

private _latestVersion?: LatestVersion;

/**
* Adds a permission to the Lambda resource policy.
* @param id The id ƒor the permission construct
Expand Down Expand Up @@ -244,8 +246,10 @@ export abstract class FunctionBase extends Resource implements IFunction {
}

public get latestVersion(): IVersion {
// Dynamic to avoid infinite recursion when creating the LatestVersion instance...
return new LatestVersion(this);
if (!this._latestVersion) {
this._latestVersion = new LatestVersion(this);
}
return this._latestVersion;
}

/**
Expand Down Expand Up @@ -320,15 +324,6 @@ export abstract class FunctionBase extends Resource implements IFunction {
});
}

/**
* Checks whether this function is compatible for Lambda@Edge.
*
* @internal
*/
public _checkEdgeCompatibility(): void {
return;
}

/**
* Returns the construct tree node that corresponds to the lambda function.
* For use internally for constructs, when the tree is set up in non-standard ways. Ex: SingletonFunction.
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-lambda/lib/lambda-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Construct, Fn, Lazy, RemovalPolicy } from '@aws-cdk/core';
import { Alias, AliasOptions } from './alias';
import { EventInvokeConfigOptions } from './event-invoke-config';
import { Function } from './function';
import { FunctionBase, IFunction, QualifiedFunctionBase } from './function-base';
import { IFunction, QualifiedFunctionBase } from './function-base';
import { CfnVersion } from './lambda.generated';
import { addAlias } from './util';

Expand Down Expand Up @@ -253,7 +253,7 @@ export class Version extends QualifiedFunctionBase implements IVersion {
return Lazy.stringValue({
produce: () => {
// Validate that the underlying function can be used for Lambda@Edge
if (this.lambda instanceof FunctionBase) {
if (this.lambda instanceof Function) {
this.lambda._checkEdgeCompatibility();
}

Expand Down
25 changes: 18 additions & 7 deletions packages/@aws-cdk/aws-lambda/lib/singleton-lambda.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import { Function as LambdaFunction, FunctionProps } from './function';
import { FunctionBase, IFunction } from './function-base';
import { FunctionBase } from './function-base';
import { Version } from './lambda-version';
import { Permission } from './permission';

/**
Expand Down Expand Up @@ -45,7 +46,7 @@ export class SingletonFunction extends FunctionBase {
public readonly role?: iam.IRole;
public readonly permissionsNode: cdk.ConstructNode;
protected readonly canCreatePermissions: boolean;
private lambdaFunction: IFunction;
private lambdaFunction: LambdaFunction;

constructor(scope: cdk.Construct, id: string, props: SingletonFunctionProps) {
super(scope, id);
Expand All @@ -61,6 +62,18 @@ export class SingletonFunction extends FunctionBase {
this.canCreatePermissions = true; // Doesn't matter, addPermission is overriden anyway
}

/**
* Returns a `lambda.Version` which represents the current version of this
* singleton Lambda function. A new version will be created every time the
* function's configuration changes.
*
* You can specify options for this version using the `currentVersionOptions`
* prop when initializing the `lambda.SingletonFunction`.
*/
public get currentVersion(): Version {
return this.lambdaFunction.currentVersion;
}

public addPermission(name: string, permission: Permission) {
return this.lambdaFunction.addPermission(name, permission);
}
Expand All @@ -83,9 +96,7 @@ export class SingletonFunction extends FunctionBase {

/** @internal */
public _checkEdgeCompatibility() {
if (this.lambdaFunction instanceof FunctionBase) {
return this.lambdaFunction._checkEdgeCompatibility();
}
return this.lambdaFunction._checkEdgeCompatibility();
}

/**
Expand All @@ -96,12 +107,12 @@ export class SingletonFunction extends FunctionBase {
return this.lambdaFunction.node;
}

private ensureLambda(props: SingletonFunctionProps): IFunction {
private ensureLambda(props: SingletonFunctionProps): LambdaFunction {
const constructName = (props.lambdaPurpose || 'SingletonLambda') + slugify(props.uuid);
const existing = cdk.Stack.of(this).node.tryFindChild(constructName);
if (existing) {
// Just assume this is true
return existing as FunctionBase;
return existing as LambdaFunction;
}

return new LambdaFunction(cdk.Stack.of(this), constructName, props);
Expand Down
25 changes: 25 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/test.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,31 @@ export = testCase({
test.done();
},

'multiple calls to latestVersion returns the same version'(test: Test) {
const stack = new cdk.Stack();

const fn = new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('hello()'),
handler: 'index.hello',
runtime: lambda.Runtime.NODEJS_10_X,
});

const version1 = fn.latestVersion;
const version2 = fn.latestVersion;

const expectedArn = {
'Fn::Join': ['', [
{ 'Fn::GetAtt': ['MyLambdaCCE802FB', 'Arn'] },
':$LATEST',
]],
};
test.equal(version1, version2);
test.deepEqual(stack.resolve(version1.functionArn), expectedArn);
test.deepEqual(stack.resolve(version2.functionArn), expectedArn);

test.done();
},

'currentVersion': {
// see test.function-hash.ts for more coverage for this
'logical id of version is based on the function hash'(test: Test) {
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-lambda/test/test.lambda-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ export = {
handler: 'index.handler',
code: lambda.Code.fromInline('foo'),
});
const version = fn.addVersion('1');
const version = fn.currentVersion;

// THEN
test.deepEqual(stack.resolve(version.edgeArn), { Ref: 'FnVersion1C3F5F93D' });
test.deepEqual(stack.resolve(version.edgeArn), { Ref: 'FnCurrentVersion17A89ABB19ed45993ff69fd011ae9fd4ab6e2005' });

test.done();
},
Expand All @@ -179,7 +179,7 @@ export = {
handler: 'index.handler',
code: lambda.Code.fromInline('foo'),
});
const version = fn.addVersion('1');
const version = fn.currentVersion;

// WHEN
new lambda.Function(stack, 'OtherFn', {
Expand Down
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/test.singleton-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,28 @@ export = {

test.done();
},

'current version of a singleton function'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const singleton = new lambda.SingletonFunction(stack, 'Singleton', {
uuid: '84c0de93-353f-4217-9b0b-45b6c993251a',
code: new lambda.InlineCode('foo'),
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'index.handler',
});

// WHEN
const version = singleton.currentVersion;
version.addAlias('foo');

// THEN
expect(stack).to(haveResource('AWS::Lambda::Version', {
FunctionName: {
Ref: 'SingletonLambda84c0de93353f42179b0b45b6c993251a840BCC38',
},
}));

test.done();
},
};

0 comments on commit 04cbc6c

Please sign in to comment.