-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4624 from evanchooly/sam2
Provide Amazon SAM integration for jaxrs based applications
- Loading branch information
Showing
60 changed files
with
1,229 additions
and
1,615 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,4 @@ build | |
docker/distroless/bazel-* | ||
/.apt_generated_tests/ | ||
quarkus.log | ||
replay_*.log | ||
|
||
|
||
|
||
replay_*.logß |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...ent/src/main/java/io/quarkus/amazon/lambda.http/deployment/AmazonLambdaHttpProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
110 changes: 110 additions & 0 deletions
110
...n-lambda-http/maven/src/main/java/io/quarkus/amazon/lambda/http/AmazonLambdaHttpMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
extensions/amazon-lambda-http/maven/src/main/resources/template.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.