Skip to content

Oulumo/lambda-base

Repository files navigation

download

Lambda Base

Lambda Base makes it easier to create Java based Lambda functions on AWS. The main focus is on lambdas used with Amazon API Gateway, but there are base classes for creating authenticator lambdas (also used with API Gateway, in case custom authentication is required) and S3 event lambdas too.

Available from JCenter

The JAR files are available in JCenter repository, by adding lambda-base- prefix to the module names. For example to include the core module in your code in Maven:

<dependency>
  <groupId>fi.oulumo.lambda</groupId>
  <artifactId>lambda-base-core</artifactId>
  <version>0.14.0.0</version>
  <type>pom</type>
</dependency>

To do the same in Gradle:

compile 'fi.oulumo.lambda:lambda-base-core:0.14.0.0'

Lambda components for API Gateway

First we need to create a Handler component. This represents a REST endpoint:

import fi.oulumo.lambda.apigateway.handler.AbstractLambdaRequestHandler;
import fi.oulumo.lambda.apigateway.handler.HandlerContext;
import fi.oulumo.lambda.apigateway.handler.HandlerResponse;
import fi.oulumo.lambda.example.dto.Sentence;
import fi.oulumo.lambda.log.ILogger;
import fi.oulumo.lambda.log.LoggerFactory;

public class EchoHandler extends AbstractLambdaRequestHandler<Sentence, Sentence> {
    private static final ILogger log = LoggerFactory.createLogger(EchoHandler.class);

    @Override
    protected HandlerResponse<Sentence> doHandleLambdaRequest(Sentence input) {
        HandlerContext context = getHandlerContext();

        log.debug("Echo handler called");
        log.debug(context.getAwsContext().getInvokedFunctionArn());

        return new HandlerResponse<>(input);
    }

    @Override
   	public Class<Sentence> getInputClass() {
       	return Sentence.class;
    }
}

Next we create the request manager. This component links together the router to be used, with route definitions. Each route definition consists of a path (possibly with path variables) and a Handler.

import com.google.gson.Gson;
import fi.oulumo.lambda.apigateway.AbstractLambdaRequestManager;
import fi.oulumo.lambda.apigateway.exception.IErrorTranslator;
import fi.oulumo.lambda.apigateway.handler.ILambdaRequestHandler;
import fi.oulumo.lambda.core.context.IHandlerContextDecorator;
import fi.oulumo.lambda.router.Router;
import fi.oulumo.lambda.router.regex.RegexMatchHelper;
import fi.oulumo.lambda.router.regex.RegexMatcher;
import fi.oulumo.lambda.router.route.HttpMethod;

import javax.inject.Inject;

public class ExampleRequestManager extends AbstractLambdaRequestManager<RegexMatchHelper> {
    @Inject
    public ExampleRequestManager(Gson gson, IErrorTranslator errorTranslator, IHandlerContextDecorator handlerContextDecorator) {
   	    super(gson, errorTranslator, handlerContextDecorator);
   	}

   	@Override
   	protected Router<RegexMatchHelper, ILambdaRequestHandler> setupRouter() {
       	Router<RegexMatchHelper, ILambdaRequestHandler> retValue = new Router<>(new RegexMatcher<ILambdaRequestHandler>());

       	retValue.defineRoute(HttpMethod.Get, "/info", new InfoHandler());
       	retValue.defineRoute(HttpMethod.Get, "/hello/:name", new HelloHandler());
       	retValue.defineRoute(HttpMethod.Get, "/echo", new EchoHandler());

       	return retValue;
   	}

}

The route /hello/:name demonstrates how to define path variables. In the handler, the actual value can be queried like this:

HandlerContext context = getHandlerContext();
String nameParameter = context.getParameterValue("name");

For a complete example please check the example directory.

Acknowledgements

  • BCrypt components were adapted from Spring Security project, available using the following licenses:

    • Bcrypt, BCryptTests: Copyright (c) 2006 Damien Miller <djm@mindrot.org>, released under BSD license.

    • BCryptPasswordEncoder, BCryptPasswordEncoderTests: Copyright (c) 2016 Spring Security project, released under the Apache 2.0 license.

  • AuthenticationResponse class was originally published in an Amazon blog post, and was made available under the Apache 2.0 license.

License

Copyright 2016 - 2017 Oulumo Oy

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Releases

No releases published

Packages

No packages published

Languages