Skip to content

Change Lambda event in layer

0

Hello,

Is it possible that from a Lambda internal extension, that runs as a layer using JAVA_TOOL_OPTIONS environment parameter like here, to call the Lambda handler method (handleRequest), and cancel the call of the Lambda environment ?
Is it possible, in this kind of invocation, to change the event type in the layer (e.g. from SNS event to included S3 event - which is the main purpose of this architecture) ?
Which is the preferred architecture to change the input type of a Lambda, from the wrapper event sent by source into the inner event expected by destination ?

Thank you,
Mihai ADAM

asked 2 years ago196 views

1 Answer
0
Accepted Answer

While it's possible to modify certain aspects of Lambda function behavior using layers and extensions, directly canceling the Lambda environment's call to the handler or changing the event type from within a layer is not a standard or recommended practice. Lambda's execution model is designed to maintain a clear separation between the function code and its runtime environment for security and consistency reasons.

However, there are alternative approaches to achieve what you're aiming for:

  1. Custom Runtime: Instead of using a layer, you could create a custom runtime that intercepts the event, modifies it as needed, and then invokes your handler function. This gives you more control over the execution flow.

  2. Wrapper Function: You can create a wrapper function that receives the initial event (e.g., SNS), transforms it into the desired format (e.g., S3 event), and then calls your main handler function with the transformed event.

  3. Event Mapping: Use AWS services like EventBridge to map and transform events before they reach your Lambda function. This allows you to modify the event structure without changing your Lambda code.

  4. Lambda Layers for Shared Code: While layers can't directly modify the event, you can use them to include shared code that helps with event transformation within your main Lambda function.

The preferred architecture for changing the input type of a Lambda function depends on your specific use case, but generally, it's recommended to handle event transformation either:

a) Before the event reaches Lambda (using services like EventBridge) b) Within the Lambda function itself (using a wrapper function or shared code in layers)

These approaches maintain the integrity of Lambda's execution model while giving you the flexibility to work with different event types.

Remember, while it's tempting to try to modify Lambda's core behavior, it's usually better to work within the designed patterns to ensure maintainability and compatibility with future AWS updates.

Sources
How Lambda fits into the event-driven paradigm - AWS Lambda
Community | Step-by-Step Guide to Creating an AWS Lambda Function Layer
AWS re:Post Live | Diving Deep into Lambda Layers on AWS - Live on October 21st! | AWS re:Post

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Thank you for your answer, Could you please provide a link to some documentation about EventBridge that transforms events before the event reaches Lambda (e.g. SNS into S3 event ) ?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.