Skip to content

Type-safety additions for Java AWS Lambda in API Gateway context

License

Notifications You must be signed in to change notification settings

davidmoten/aws-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aws-helper


Maven Central
codecov

Utilities to add type-safety in Java AWS Lambdas with API Gateway.

Getting started

Add this dependency to your pom.xml. It has no transitive dependencies.

<dependency>
    <groupId>com.github.davidmoten</groupId>
    <artifactId>aws-helper</artifactId>
    <version>VERSION_HERE</version>
</dependency>

StandardRequestBodyPassThrough.from

When a request is mapped through from a Gateway API to a Lambda the pass-through json body template looks like this.

When you want to deal with the body in a java Lambda navigating this bit of json is painful because of null checks, nested maps and type coercion. This is where StandardRequestBodyPassThrough helps out:

public class RequestHandler {

    public String getResult(Map<String, Object> input, Context context) {
        LambdaLogger log = context.getLogger();
        
        // expects full request body passthrough from api gateway integration request
        StandardRequestBodyPassThrough request = StandardRequestBodyPassThrough.from(input);
        
        //get info from input in a type-safe way
        log.debug("http-request=" + request.httpRequest().orElse(""));
        log.debug("user=" + request.user().orElse(""));
        log.debug("api-id=" + request.apiId().orElse(""));
        log.debug("source-ip="+ request.sourceIp().orElse(""));
        ...
    }
}       

About

Type-safety additions for Java AWS Lambda in API Gateway context

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages