-
Notifications
You must be signed in to change notification settings - Fork 4k
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
fix(lambda-nodejs): NodejsFunction construct incompatible with lambda@edge #9562
Changes from 6 commits
625d49b
0bde106
5dcc8d5
3f55a28
7a05847
1ab542b
b538779
9465e60
2ff9840
c1dc747
5455085
006206b
1ce04e1
efd2327
6c9fdc7
b878a9b
deed339
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert'; | ||
import { ABSENT, expect, haveResource, haveResourceLike } from '@aws-cdk/assert'; | ||
import * as certificatemanager from '@aws-cdk/aws-certificatemanager'; | ||
import * as lambda from '@aws-cdk/aws-lambda'; | ||
import * as s3 from '@aws-cdk/aws-s3'; | ||
|
@@ -426,8 +426,7 @@ nodeunitShim({ | |
const stack = new cdk.Stack(); | ||
const sourceBucket = new s3.Bucket(stack, 'Bucket'); | ||
|
||
const lambdaFunction = new lambda.SingletonFunction(stack, 'Lambda', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to move from a
|
||
uuid: 'xxxx-xxxx-xxxx-xxxx', | ||
const lambdaFunction = new lambda.Function(stack, 'Lambda', { | ||
code: lambda.Code.inline('foo'), | ||
handler: 'index.handler', | ||
runtime: lambda.Runtime.NODEJS_10_X, | ||
|
@@ -444,7 +443,7 @@ nodeunitShim({ | |
isDefaultBehavior: true, | ||
lambdaFunctionAssociations: [{ | ||
eventType: LambdaEdgeEventType.ORIGIN_REQUEST, | ||
lambdaFunction: lambdaFunction.latestVersion, | ||
lambdaFunction: lambdaFunction.addVersion('1'), | ||
}], | ||
}, | ||
], | ||
|
@@ -459,13 +458,7 @@ nodeunitShim({ | |
{ | ||
'EventType': 'origin-request', | ||
'LambdaFunctionARN': { | ||
'Fn::Join': [ | ||
'', | ||
[ | ||
{ 'Fn::GetAtt': [ 'SingletonLambdaxxxxxxxxxxxxxxxx69D4268A', 'Arn' ] }, | ||
':$LATEST', | ||
], | ||
], | ||
'Ref': 'LambdaVersion1BB7548E1', | ||
}, | ||
}, | ||
], | ||
|
@@ -476,6 +469,82 @@ nodeunitShim({ | |
test.done(); | ||
}, | ||
|
||
'associate a lambda with removable env vars'(test: Test) { | ||
const app = new cdk.App(); | ||
const stack = new cdk.Stack(app, 'Stack'); | ||
const sourceBucket = new s3.Bucket(stack, 'Bucket'); | ||
|
||
const lambdaFunction = new lambda.Function(stack, 'Lambda', { | ||
code: lambda.Code.inline('foo'), | ||
handler: 'index.handler', | ||
runtime: lambda.Runtime.NODEJS_10_X, | ||
}); | ||
lambdaFunction.addEnvironment('KEY', 'value', { removeInEdge: true }); | ||
|
||
new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', { | ||
originConfigs: [ | ||
{ | ||
s3OriginSource: { | ||
s3BucketSource: sourceBucket, | ||
}, | ||
behaviors: [ | ||
{ | ||
isDefaultBehavior: true, | ||
lambdaFunctionAssociations: [{ | ||
eventType: LambdaEdgeEventType.ORIGIN_REQUEST, | ||
lambdaFunction: lambdaFunction.addVersion('1'), | ||
}], | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
expect(stack).to(haveResource('AWS::Lambda::Function', { | ||
Environment: ABSENT, | ||
})); | ||
|
||
test.done(); | ||
}, | ||
|
||
'throws when associating a lambda with incompatible env vars'(test: Test) { | ||
const app = new cdk.App(); | ||
const stack = new cdk.Stack(app, 'Stack'); | ||
const sourceBucket = new s3.Bucket(stack, 'Bucket'); | ||
|
||
const lambdaFunction = new lambda.Function(stack, 'Lambda', { | ||
code: lambda.Code.inline('foo'), | ||
handler: 'index.handler', | ||
runtime: lambda.Runtime.NODEJS_10_X, | ||
environment: { | ||
KEY: 'value', | ||
}, | ||
}); | ||
|
||
new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', { | ||
originConfigs: [ | ||
{ | ||
s3OriginSource: { | ||
s3BucketSource: sourceBucket, | ||
}, | ||
behaviors: [ | ||
{ | ||
isDefaultBehavior: true, | ||
lambdaFunctionAssociations: [{ | ||
eventType: LambdaEdgeEventType.ORIGIN_REQUEST, | ||
lambdaFunction: lambdaFunction.addVersion('1'), | ||
}], | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
test.throws(() => app.synth(), /KEY/); | ||
|
||
test.done(); | ||
}, | ||
|
||
'distribution has a defaultChild'(test: Test) { | ||
const stack = new cdk.Stack(); | ||
const sourceBucket = new s3.Bucket(stack, 'Bucket'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,11 @@ export interface IFunction extends IResource, ec2.IConnectable, iam.IGrantable { | |
* Configures options for asynchronous invocation. | ||
*/ | ||
configureAsyncInvoke(options: EventInvokeConfigOptions): void; | ||
|
||
/** | ||
* Checks whether this function is compatible for Lambda@Edge. | ||
*/ | ||
checkEdgeCompatibility(): void; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why have you added this API to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the underlying Lambda of a version is an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I was not able to follow your response. Can you expand a bit more? If we wanted it to be on a base class so that it's available on all implementations, we could make it a protected abstract method on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK with We don't need this method on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's moved to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also do you prefer the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's my preference but not strongly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Went for the
|
||
} | ||
|
||
/** | ||
|
@@ -318,6 +323,10 @@ export abstract class FunctionBase extends Resource implements IFunction { | |
}); | ||
} | ||
|
||
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. | ||
|
@@ -417,4 +426,8 @@ class LatestVersion extends FunctionBase implements IVersion { | |
public addAlias(aliasName: string, options: AliasOptions = {}) { | ||
return addAlias(this, this, aliasName, options); | ||
} | ||
|
||
public get edgeArn(): never { | ||
throw new Error('$LATEST function version cannot be used for Lambda@Edge'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
edgeArn
must always be lazily processed. Would be better to move theLazy.stringValue()
block into the getter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed better, 👍