Skip to content

Instantly share code, notes, and snippets.

@sapessi
Created December 14, 2016 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sapessi/7b8c465b8e79492dad0518d38acda446 to your computer and use it in GitHub Desktop.
Save sapessi/7b8c465b8e79492dad0518d38acda446 to your computer and use it in GitHub Desktop.
aws-serverless-java-container-jersey sample
package com.sapessi.sample.jersey;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.jersey.JerseyLambdaContainerHandler;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
public class LambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse> {
// Both these instructions will only be executed on the first invocation of the function and reused for all
// following calls
// load the Jersey resources from my package. This initializes the Jax RS application (ResourceConfig) by default
// with Jersey
private ResourceConfig jerseyApplication = new ResourceConfig()
.packages("com.sapessi.sample.jersey.resources")
.register(JacksonFeature.class); // this should autowire, struggled to make it work so using the old way of loading it
// Once I have the app I can initialize the handler
private JerseyLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler
= JerseyLambdaContainerHandler.getAwsProxyHandler(jerseyApplication);
@Override
public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context) {
// proxy events
return handler.proxy(awsProxyRequest, context);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sapessi.sample</groupId>
<artifactId>serverless-jersey-example</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-jersey</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.24</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Example Pet Store API written in jersey with the aws-serverless-java-container library
Resources:
PetStoreFunction:
Type: AWS::Serverless::Function
Properties:
Handler: com.sapessi.sample.jersey.LambdaHandler::handleRequest
Runtime: java8
CodeUri: target/serverless-jersey-example-1.0-SNAPSHOT.jar
MemorySize: 512
Policies: AWSLambdaBasicExecutionRole
Timeout: 20
Events:
GetResource:
Type: Api
Properties:
Path: /{proxy+}
Method: any
Outputs:
JerseyPetStoreApi:
Description: URL for application
Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/pets'
Export:
Name: JerseyPetStoreApi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment