Skip to content

Commit

Permalink
Merge pull request #4624 from evanchooly/sam2
Browse files Browse the repository at this point in the history
Provide Amazon SAM integration for jaxrs based applications
  • Loading branch information
patriot1burke authored Oct 22, 2019
2 parents ea828fc + 1bd3111 commit 895c04c
Show file tree
Hide file tree
Showing 60 changed files with 1,229 additions and 1,615 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,4 @@ build
docker/distroless/bazel-*
/.apt_generated_tests/
quarkus.log
replay_*.log



replay_*.logß
5 changes: 5 additions & 0 deletions bom/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@
<artifactId>quarkus-amazon-dynamodb-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda-http-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-azure-functions-http-deployment</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bom/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda-resteasy</artifactId>
<artifactId>quarkus-amazon-lambda-http</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down
11 changes: 11 additions & 0 deletions devtools/maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
38 changes: 38 additions & 0 deletions docs/src/main/asciidoc/amazon-lambda-http-guide.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
////
This guide is maintained in the main Quarkus repository
and pull requests should be submitted there:
https://github.com/quarkusio/quarkus/tree/master/docs/src/main/asciidoc
////
= Quarkus - Amazon Lambda HTTP Applications 

include::./attributes.adoc[]


The `quarkus-amazon-lambda-http` extension allows you to write microservices with Resteasy (JAX-RS),
Undertow (servlet), or Vert.x Web and make these microservices deployable to Amazon AWS using
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html[Amazon's SAM] framework.

== Prerequisites

To complete this guide, you need:

* less than 15 minutes
* JDK 1.8 (Azure requires JDK 1.8)
* Apache Maven 3.5.3+
* https://aws.amazon.com[An Amazon AWS account]
* https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-reference.html#serverless-sam-cli[Amazon SAM CLI]

== Getting Started

This guide can be used on existing projects or new projects. Whether the project is a new green field project or you're
attempting to migrate an existing application, this guide will walk you through the steps necessary to use Quarkus powered
applications on Amazon's infrastructure.

=== New Projects

The simplest scenario is when starting a completely new project. Once you've <<getting-started-guide.adoc,generated>> a new
Quarkus project, you'll need to configure your application for deployment to Amazon. The steps to achieve this are beyond
the scope of this document but you can find the full guide on
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started.html[Amazon's website].

For the purposes of this guide, we'll be using RESTEasy and maven.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
<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>

<parent>
<artifactId>quarkus-amazon-lambda-resteasy-parent</artifactId>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda-http-parent</artifactId>
<version>999-SNAPSHOT</version>
<relativePath>../</relativePath>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-amazon-lambda-resteasy-deployment</artifactId>
<name>Quarkus - Amazon Lambda - RESTEasy - Deployment</name>
<artifactId>quarkus-amazon-lambda-http-deployment</artifactId>
<name>Quarkus - Amazon Lambda HTTP - Deployment</name>

<dependencies>
<dependency>
Expand All @@ -20,23 +21,13 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-server-common-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda-resteasy</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.substratevm</groupId>
<artifactId>svm</artifactId>
<artifactId>quarkus-vertx-web-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
<artifactId>quarkus-amazon-lambda-http</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand All @@ -53,5 +44,4 @@
</plugin>
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.quarkus.amazon.lambda.http.deployment;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.vertx.http.deployment.RequireVirtualHttpBuildItem;

public class AmazonLambdaHttpProcessor {
@BuildStep
public RequireVirtualHttpBuildItem requestVirtualHttp(LaunchModeBuildItem launchMode) {
return launchMode.getLaunchMode() == LaunchMode.NORMAL ? RequireVirtualHttpBuildItem.MARKER : null;
}
}
41 changes: 41 additions & 0 deletions extensions/amazon-lambda-http/maven/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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>

<parent>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda-http-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>

<artifactId>quarkus-amazon-lambda-http-maven</artifactId>
<packaging>maven-plugin</packaging>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-amazon-lambda-http</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package io.quarkus.amazon.lambda.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

@Mojo(name = "configure-aws-lambda", requiresProject = false)
public class AmazonLambdaHttpMojo extends AbstractMojo {
public static final String SAM_HANDLER = HttpHandler.class.getName() + "::handleRequest";
public static final String SAM_RUNTIME = "java8";
public static final String SAM_TEMPLATE = "quarkus.amazon-lambda-http.template";
public static final String SAM_RESOURCE = "quarkus.amazon-lambda-http.resource-name";
public static final String DEFAULT_TEMPLATE = "template.yaml";
public static final String DEFAULT_RESOURCE = "Quarkus";

@Parameter(defaultValue = "${project}")
protected MavenProject project;

@Parameter(property = "sam.template", defaultValue = DEFAULT_TEMPLATE)
private String templateFile;

@Parameter(property = "sam.resource", defaultValue = DEFAULT_RESOURCE)
private String resourceName;

@SuppressWarnings("unchecked")
@Override
public void execute() {
try {
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Properties application = new Properties();
File appProperties = new File("src/main/resources/application.properties");
if (appProperties.exists()) {
try (InputStream stream = new FileInputStream(appProperties)) {
application.load(stream);
templateFile = apply(application, templateFile, SAM_TEMPLATE, DEFAULT_TEMPLATE);

resourceName = apply(application, resourceName, SAM_RESOURCE, DEFAULT_RESOURCE);
}
}
File configFile = new File(templateFile);

Map template;
Map<String, Object> resource;
if (!configFile.exists()) {
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("template.yaml")) {
template = mapper.readValue(inputStream, LinkedHashMap.class);
Map<String, Object> resources = get(template, "Resources");

resource = get(resources, "Quarkus");
resources.remove("Quarkus");

resources.put(resourceName, resource);
}
} else {
try (InputStream inputStream = new FileInputStream(configFile)) {
template = mapper.readValue(inputStream, LinkedHashMap.class);
Map<String, Object> resources = get(template, "Resources");

resource = get(resources, resourceName);

if (resource == null && resources.size() == 1) {
resource = (Map<String, Object>) resources.entrySet().iterator().next().getValue();
}
}
}
if (resource != null) {
Map<String, Object> properties = get(resource, "Properties");
properties.put("Handler", SAM_HANDLER);
properties.put("Runtime", SAM_RUNTIME);
mapper.writer().withDefaultPrettyPrinter().writeValue(configFile, template);
} else {
throw new RuntimeException("Could not find the resource to update");
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

private String apply(Properties properties, String field, String key, String defaultValue) {
String value = properties.getProperty(key);
if (value != null && field.equals(defaultValue)) {
field = value;
}
return field;
}

@SuppressWarnings("unchecked")
public static <T> T get(final Map template, final String... keys) {
Map map = template;
for (int i = 0; i < keys.length - 1; i++) {
map = (Map) map.get(keys[i]);
if (map == null) {
throw new IllegalArgumentException("No object found with the key: " + keys[i]);
}
}
return (T) map.get(keys[keys.length - 1]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Globals:
Function:
Timeout: 5
Resources:
Quarkus:
Type: "AWS::Serverless::Function"
Properties:
Handler: io.quarkus.amazon.lambda.http.HttpHandler::handleRequest+somethingelse
Runtime: "java8"
Policies: "AWSLambdaBasicExecutionRole"
Events:
GetResource:
Type: "Api"
Properties:
Path: "/{proxy+}"
Method: "any"
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
<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>

<parent>
<artifactId>quarkus-build-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
<relativePath>../../build-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-amazon-lambda-resteasy-parent</artifactId>
<name>Quarkus - Amazon Lambda - RESTEasy</name>
<artifactId>quarkus-amazon-lambda-http-parent</artifactId>
<name>Quarkus - Amazon Lambda HTTP</name>
<packaging>pom</packaging>

<modules>
<module>deployment</module>
<module>runtime</module>
<module>deployment</module>
<module>maven</module>
</modules>

</project>
</project>
Loading

0 comments on commit 895c04c

Please sign in to comment.