AWS lambda enrich / modify input object

128 views Asked by At

Usually you need to do something with InputPath, ResultPath and OutputPath to process your data.
But sometimes you need something as simple as modifying a property in your input object, like camel do with Message EIP or Enricher EIP
Is there an option to eliminate the configuration overhead?

1

There are 1 answers

0
Mike On

Consider the approach with Jackson tree model:

public class MyHandler extends JsonHandler<ObjectNode> {

    @Override
    public ObjectNode handleRequest(ObjectNode json, Context context) {
        // enrich or modify the input
        json.putPOJO("enriched", pojo);
        // return (the same) modified input object
        return json;
        // or replace it with something totally different
        // return "Some other POJO";
    }
}

Where JsonHandler parent class is defined as

public abstract class JsonHandler<R> implements RequestStreamHandler, RequestHandler<ObjectNode, R> {
    
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
        ObjectNode json = (ObjectNode) objectMapper.readTree(input);
        R result = handleRequest(json, context);
        output.write(objectMapper.writeValueAsString(result).getBytes());
    }

    @Override
    public abstract R handleRequest(ObjectNode json, Context context);
}