Code Monkey home page Code Monkey logo

lammy's Introduction

lammy

Lammy is a microframework for building AWS Lambda functions in Java 8+.

Goals

  • Make it easier to build and maintain Lambda functions using Java 8+
  • Keep JAR size small

Non-Goals

  • Provide implementations of common use cases (e.g., "copy file to S3")
  • Build a framework for microservices in general (i.e., don't rebuild Quarkus or Micronaut)

Design Philosophy

Lammy's design is heavily influenced by JAX-RS. Specifically, the framework implement a straightforward, mechanical control flow that centralizes business logic while exposing hooks that libraries and applications can use for things like customization, serialization, exception handling, and so on. Hopefully, Lammy's design feels clear and familiar.

Examples

Here are some simple examples to illustrate how Lammy works:

Hello World

This function takes a name and gives a gretting provided by an environment variable:

public class HelloWorldFunction extends JacksonBeanLambdaFunctionBase<GreetingRequest,GreetingResponse> {
    public static final String GREETING=getenv("GREETING").orElse("Hello");

    @Override
    public GreetingResponse handleBeanRequest(GreetingRequest request, Context context) {
        return new GreetingResponse(String.format("%s, %s!", GREETING, request.name()));
    }
}

record GreetingRequest(String name) {}

record GreetingResponse(String greeting) {}

The framework provides Jackson serialization out of the box and provides serialization for input and output types automatically. Of course, applications can also specialize or customize serialization as desired using custom serializers, either with or without Jackson.

Additionally, the framework provides some utility methods for configuration using environment variables, such as getenv shown here, which returns an OptionalEnvironmentVariable.

Supported Lambda Function Varieties

Logical Function Types

Lammy supports two logical types of Lambda functions:

  • Function -- A Lambda function with an input and an output
  • Consumer -- A Lambda function with an input and a constant, empty output

Implementation Styles

Similarly, Lammy supports two implementation styles of Lambda functions:

  • Stream -- A Lambda function implementation with business logic implemented using byte stream inputs and outputs
  • Bean -- A Lambda function implementation with business logic implemented using POJO inputs and outputs

Anatomy of a Lambda Function

This section describes the framework's design in terms of three cooperating entities:

  1. Application -- The user's Lambda function
  2. Framework -- The Lammy microframework
  3. Runtime -- The AWS Lambda runtime

Stream Function

A stream-style function has the following steps:

┌─────┐   ┌───────────┐   ┌─────────┐   ┌───────────┐   ┌──────┐
│Input│   │  Request  │   │ Request │   │ Response  │   │Output│
│  1  ├──►│     2     ├──►│    3    ├──►│     4     ├──►│  5   │
│Bytes│   │Interceptor│   │ Handler │   │Interceptor│   │Bytes │
└─────┘   └───────────┘   └─────────┘   └───────────┘   └──────┘
  1. The framework consumes a byte stream from the runtime as input
  2. The application can optionally register one or more "request interceptor" objects to preprocess the input byte stream, which the framework then runs in the order they were registered
  3. The framework calls the application's request handler, which consumes the (optionally preprocessed) input bytes and produces output bytes according to the application's business logic
  4. The application can optionally register one or more "response interceptor" objects to postprocess the output byte stream, which the framework then runs in the order they were registered
  5. The framework produces the byte stream to the runtime as output

Bean Function

A bean-style function has the following steps:

                                 ┌────────────┐   ┌───────────┐   ┌──────────┐   ┌───────────┐   ┌──────────┐
                                 │  Request   │   │  Request  │   │ iii Bean │   │ Response  │   │ Response │
                             ┌──►│     i      ├──►│    ii     ├──►│ Request  ├──►│    iv     ├──►│    v     ├───┐
                             │   │Deserializer│   │  Filter   │   │ Handler  │   │  Filter   │   │Serializer│   │
                             │   └────────────┘   └───────────┘   └──────────┘   └───────────┘   └──────────┘   │
                             │                                                                                  │
                             │                                                                                  │
                             │                                                                                  ▼
┌─────┐   ┌───────────┐   ┌──┴─────────────────────────────────────────────────────────────────────────────────────┐   ┌───────────┐   ┌──────┐
│Input│   │  Request  │   │                                             3                                          │   │ Response  │   │Output│
│  1  ├──►│     2     ├──►│                                   Stream Request Handler                               ├──►│     4     ├──►│  5   │
│Bytes│   │Interceptor│   │                                                                                        │   │Interceptor│   │Bytes │
└─────┘   └───────────┘   └────────────────────────────────────────────────────────────────────────────────────────┘   └───────────┘   └──────┘

The framework implements bean-style functions using stream-style functions, so the easiest way to understand bean-style functions is as a layer "on top of" stream-style functions.

  1. The framework consumes a byte stream from the runtime as input
  2. The application can optionally register one or more "request interceptor" objects to preprocess the input byte stream, which the framework then runs in the order they were registered
  3. The framework calls the application's request handler, which consumes the (optionally preprocessed) input bytes and produces output bytes according to the application's business logic
    1. The application registers a "request deserializer" object, which converts the (optionally preprocessed) input bytes into a bean of the input type
    2. The application can optionally register one or more "request filter" objects to preprocess the input bean, which the framework then runs in the order they were registered
    3. The framework calls the application's request handler, which consumes the (optionally preprocessed) input bean and produces a bean of the output type according to the application's business logic
    4. The application can optionally register one or more "response filter" objects to postprocess the output bean, which the framework then runs in the order they were registered
    5. The application registers a "response serializer" object, which converts the (optionally postprocessed) output bean into output bytes
  4. The application can optionally register one or more "response interceptor" objects to postprocess the output byte stream, which the framework then runs in the order they were registered
  5. The framework produces the byte stream to the runtime as output

Exception Mappers

The application can also optionally register one or more "exception mapper" objects, which are responsible for converting an exception into a byte stream or output bean for stream-style and bean-style functions, respectively.

If the application propagates an exception at any step, then the framework will capture the exception and look for a matching exception mapper. If the framework finds a matching exception mapper, then the exception mapper is used to generate a successful response to the runtime. If the framework cannot find a matching exception mapper, then the framework propagates the exception to the runtime, which then produces a failure response using its default serialization format:

{
    "errorMessage": "Input must be a list that contains 2 numbers.",
    "errorType":"java.lang.InputLengthException",
    "stackTrace": [
        "example.HandlerDivide.handleRequest(HandlerDivide.java:23)",
        "example.HandlerDivide.handleRequest(HandlerDivide.java:14)"
    ]
}

Unfortunately, the runtime does not offer a way for applications to generate an error response with a custom serialization format.

lammy's People

Contributors

dependabot[bot] avatar sigpwned avatar

Watchers

 avatar  avatar

lammy's Issues

New Capability: Message Processing

Users should be able to use Lammy to create message processing capabilities.

  • Input types
    • Message - Message record with attributes, body, etc.
    • Bean - Just the body, deserialized
  • Batch processing style
    • Batch - everything at once, entire batch succeeds or fails
    • Minibatch - one per message, each message succeeds or fails independently
  • Function style
    • Processor - List of inputs to List of outputs
    • Consumer - List of inputs to no output
  • Destination style (processors only)
    • Managed - One destination for all messages, sending managed by AWS
    • Manual - Dynamic destination per message, sending managed by framework
  • Output batching style (processors only)
    • Unbatched - One batch per message
    • Batch - Outputs from all microbatches are combined independent of source
    • Microbatch -- Outputs from each microbatch are combined
  • Message source types
    • SQS
    • SNS
    • Kinesis Firehose
    • Kinesis?
    • EventBridge?
    • CloudWatch Logs?
    • DynamoDB Streams?
  • Message destination types
    • SQS
    • SNS
    • Kinesis Firehose
    • Kinesis?
    • EventBridge?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.