-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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): support for Lambda SnapStart #23196
Changes from all commits
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 |
---|---|---|
|
@@ -772,6 +772,23 @@ CodeGuru profiling is supported for all Java runtimes and Python3.6+ runtimes. | |
See [the AWS documentation](https://docs.aws.amazon.com/codeguru/latest/profiler-ug/setting-up-lambda.html) | ||
to learn more about AWS Lambda's Profiling support. | ||
|
||
## Lambda with SnapStart | ||
|
||
SnapStart is currently supported only on Java 11 runtime. After you enable Lambda SnapStart for a particular Lambda function, publishing a new version of the function will trigger an optimization process. | ||
|
||
See [the AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html) to learn more about AWS Lambda SnapStart | ||
|
||
```ts | ||
const fn = new lambda.Function(this, 'MyFunction', { | ||
code: lambda.Code.fromAsset('handler.zip'), | ||
runtime: lambda.Runtime.JAVA_11, | ||
handler: 'example.Handler::handleRequest', | ||
snapStart: true, | ||
}); | ||
|
||
const version = fn.currentVersion; | ||
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. Nice! Good to know fn.currentVersion will actually create a new version if no existing version. 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. This is actually another great reason for this to not be a prop. For this to be a prop, you should be able to create a brand new Lambda function with this prop enabled, and the deployment should succeed. This won't work though, because to enable start, a version has to already be published, and that needs an existing function. Normally, CFN can create the lambda function and the version at once since it can figure out the dependency, but this is a circular dependency; the Version depends on the Function, but the Function also depends on the Version because snap start is a property on the function itself. (unless of course CFN figures this out for us, which I will check manually later, but it's not clear right now). So, in summary: we need a method |
||
``` | ||
|
||
## Lambda with Reserved Concurrent Executions | ||
|
||
```ts | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,7 +223,14 @@ export interface FunctionOptions extends EventInvokeConfigOptions { | |
readonly tracing?: Tracing; | ||
|
||
/** | ||
* Enable profiling. | ||
* Enable SnapStart for Lambda Function. | ||
* SnapStart is currently supported only for Java 11 runtime | ||
* | ||
* @default false | ||
*/ | ||
readonly snapStart?: boolean; | ||
|
||
/** Enable profiling. | ||
* @see https://docs.aws.amazon.com/codeguru/latest/profiler-ug/setting-up-lambda.html | ||
* | ||
* @default - No profiling. | ||
|
@@ -769,6 +776,10 @@ export class Function extends FunctionBase { | |
throw new Error(`Ephemeral storage size must be between 512 and 10240 MB, received ${props.ephemeralStorageSize}.`); | ||
} | ||
|
||
if (props.snapStart && props.runtime != Runtime.JAVA_11) { | ||
throw new Error('SnapStart is currently supported only Java 11 runtime'); | ||
} | ||
|
||
const resource: CfnFunction = new CfnFunction(this, 'Resource', { | ||
functionName: this.physicalName, | ||
description: props.description, | ||
|
@@ -805,6 +816,7 @@ export class Function extends FunctionBase { | |
fileSystemConfigs, | ||
codeSigningConfigArn: props.codeSigningConfig?.codeSigningConfigArn, | ||
architectures: this._architecture ? [this._architecture.name] : undefined, | ||
snapStart: props.snapStart ? { applyOn: 'PublishedVersions' } : undefined, | ||
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. I love the clean design but I am also wary if they will support all versions including the $latest one. But I agree we keep the simple design as is. 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. should we check and throws error if the runtime is not JAVA_11 and snapStart is enabled? 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. Yes, we should. Resource creation on AWS throws an error as there is a validation that they do. So it makes sense to add the validation to CDK build. Added validation, and provided unit test for the same. |
||
}); | ||
|
||
resource.node.addDependency(this.role); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":"21.0.0"} | ||
{"version":"22.0.0"} |
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.
I concur with @TheRealAmazonKendra. There's too many restrictions on the lambda to put this as a constructor prop (from https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html):
it's good that you added error checking for the Java11 runtime case, but all of these other restrictions must also be checked at synth time. This bloats the constructor with unnecessary checks that will eventually be removed when Lambda rolls this out to more function types.
Because we need those type checks today, and we will one day have to deprecate them, I agree with @TheRealAmazonKendra's suggestion to make this a new method (maybe
enableSnapStart()
?) on theFunction
class that will do all of this error checking in one place.