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

Support internal Lambda extensions #744

Merged
merged 4 commits into from
Dec 3, 2023

Conversation

ramosbugs
Copy link
Contributor

Internal Lambda extensions must be registered during the Lamba lifecycle Init phase, which ends when the runtime calls Next to await the first event. Since the Extension::run function both registers and executes the extension, there was previously no way for the runtime to determine that all internal extensions have been registered and that it's safe to proceed to the Invoke lifecycle phase.

This change introduces an Extension::register method that registers the extension and begins any logs/telemetry handlers. It then returns a new RegisteredExtension abstraction that can be used to invoke the extension's run loop concurrently with the runtime's run loop.

This change maintains backward compatibility by having the existing Extension::run method perform both steps. External Lambda extensions can use either API, and internal extensions should use the new API.

Resolves #743.

By submitting this pull request

  • [X ] I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • [X ] I confirm that I've made a best effort attempt to update all relevant documentation.

Internal Lambda extensions must be registered during the Lamba
lifecycle Init phase, which ends when the runtime calls Next to
await the first event. Since the `Extension::run` function both
registers and executes the extension, there was previously no
way for the runtime to determine that all internal extensions have
been registered and that it's safe to proceed to the Invoke
lifecycle phase.

This change introduces an `Extension::register` method that
registers the extension and begins any logs/telemetry handlers.
It then returns a new `RegisteredExtension` abstraction that can
be used to invoke the extension's run loop concurrently with the
runtime's run loop.

This change maintains backward compatibility by having the existing
`Extension::run` method perform both steps. External Lambda
extensions can use either API, and internal extensions should use
the new API.

Resolves awslabs#743.
}

/// An extension registered by calling [`Extension::register`].
pub struct RegisteredExtension<E> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new type seemed unavoidable since Extension::register has to pass ownership of all the logging and telemetry fields to the spawned background tasks. These are the remaining fields to be used in the invoke phase.

@calavera
Copy link
Contributor

calavera commented Dec 2, 2023

This is great, thanks for the changes! Do you mind creating an example in the examples directory? that way if anyone wants to create internal extensions they can use it as reference.

@ramosbugs
Copy link
Contributor Author

The cargo lambda runtime emulator doesn't seem to support internal extensions, but I tested the example by deploying it to AWS via:

cargo lambda deploy
cargo lambda invoke extension-internal-flush --data-example sqs-event-obj --verbose --remote

This produced the following CloudWatch logs showing both the runtime and extension behaving as expected:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                                 message                                                                                 |
|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1701561431609 | INIT_START Runtime Version: provided:al2023.v9 Runtime Version ARN: arn:aws:lambda:us-west-2::runtime:68313de216b90a01e19138b97177c40478ef50b63a22b29f152b960070428ae8  |
| 1701561431636 | EXTENSION Name: bootstrap State: Ready Events: [INVOKE]                                                                                                                 |
| 1701561431637 | START RequestId: 34bcc601-2f2d-4ad8-af6e-124968936a5a Version: $LATEST                                                                                                  |
| 1701561431637 | [runtime] received event Data { a: "Test", b: 123 }                                                                                                                     |
| 1701561431637 | [extension] waiting for event to be processed                                                                                                                           |
| 1701561431637 | [extension] flushing logs and telemetry                                                                                                                                 |
| 1701561431648 | END RequestId: 34bcc601-2f2d-4ad8-af6e-124968936a5a                                                                                                                     |
| 1701561431648 | REPORT RequestId: 34bcc601-2f2d-4ad8-af6e-124968936a5a Duration: 11.06 ms Billed Duration: 38 ms Memory Size: 128 MB Max Memory Used: 14 MB Init Duration: 26.63 ms     |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Copy link
Contributor

@calavera calavera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@calavera calavera merged commit 6e953eb into awslabs:main Dec 3, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for internal Lambda extensions
2 participants