This is an automated email from the ASF dual-hosted git repository. aldettinger pushed a commit to branch camel-quarkus-main in repository https://gitbox.apache.org/repos/asf/camel-quarkus-examples.git
commit 6839e730e6c37b30506d314fc6d28bbfb9d8f893 Author: ravishankarhassain <[email protected]> AuthorDate: Tue Aug 10 00:34:23 2021 +0100 Added the module Deploying a Camel Route in AWS Lambda to the examples --- aws-lambda/README.md | 197 ++++++++++++++ aws-lambda/payload.json | 3 + aws-lambda/pom.xml | 287 +++++++++++++++++++++ aws-lambda/results.png | Bin 0 -> 27634 bytes aws-lambda/src/main/docker/Dockerfile.jvm | 55 ++++ aws-lambda/src/main/docker/Dockerfile.native | 27 ++ .../src/main/docker/Dockerfile.native-distroless | 23 ++ .../camel/quarkus/examples/AWSLambdaHandler.java | 39 +++ .../apache/camel/quarkus/examples/CamelRoute.java | 37 +++ .../camel/quarkus/examples/GreetService.java | 27 ++ .../org/apache/camel/quarkus/examples/Person.java | 36 +++ .../quarkus/examples/AWSLambdaHandlerTest.java | 45 ++++ .../quarkus/examples/AWSLambdaHandlerTestIT.java | 25 ++ .../src/test/resources/application.properties | 17 ++ docs/modules/ROOT/attachments/examples.json | 5 + 15 files changed, 823 insertions(+) diff --git a/aws-lambda/README.md b/aws-lambda/README.md new file mode 100644 index 0000000..49673f7 --- /dev/null +++ b/aws-lambda/README.md @@ -0,0 +1,197 @@ +# Deploying a Camel Route in AWS Lambda : A Camel Quarkus example + +This example uses the following framework + + * **Quarkus** - *The Supersonic Subatomic Java Framework for building Cloud Native Applications* + * **Apache Camel** - *The Swiss Army Knife of Enterprise Application Integration for integrating heterogeneous systems* + * **AWS Lambda** - *Event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services* + +If you want to learn more about + +* *Quarkus - please visit its website: https://quarkus.io/* +* *Apache Camel - please visit its website: https://camel.apache.org/* +* *AWS Lambda - please visit its website: https://aws.amazon.com/lambda/* + +## Provided Code + +### Quarkus Camel Amazon Lambda Integration example + +This example contains a sample Greeter service build using Quarkus & Camel framework implementing Dependency Injection design principle which can be deployed as functions into the Amazon Lambda. + +> :warning: **INCOMPATIBLE WITH DEV MODE**: Amazon Lambda Binding is not compatible with dev mode yet! + + +#### Quarkus Extensions / Dependencies Used + +1. **[Apache Camel](https://quarkus.io/guides/camel)** + * [camel-quarkus-core](https://camel.apache.org/camel-quarkus/latest/reference/extensions/core.html) + * [camel-quarkus-direct](https://camel.apache.org/camel-quarkus/latest/reference/extensions/direct.html) + * [camel-quarkus-log](https://camel.apache.org/camel-quarkus/latest/reference/extensions/log.html) + +2. **Amazon Lambda** + * [quarkus-amazon-lambda](https://quarkus.io/guides/amazon-lambda) + +3. **[Context Dependency Injection](https://quarkus.io/guides/cdi)** + * [quarkus-arc](https://quarkus.io/guides/cdi-reference) + +4. **[Testing](https://quarkus.io/guides/getting-started-testing)** + * quarkus-junit5 + * [quarkus-junit5-mockito](https://quarkus.io/blog/mocking/) + * quarkus-test-amazon-lambda + +## Running the application in dev mode + +You can run your application in dev mode that enables live coding using: +```shell script +mvn compile quarkus:dev +``` + +> **_NOTE:_** Quarkus now ships with a Dev UI, which is available in dev mode only at http://localhost:8080/q/dev/. + +## Building and Packaging the Java code as Quarkus JVM application + +The application can be packaged using: +```shell script +mvn clean package +``` +This will compile and package your code. + +It produces the `quarkus-run.jar` file in the `target/quarkus-app/` directory. +Be aware that it’s not an _uber-jar_ as the dependencies are copied into the `target/quarkus-app/lib/` directory. + +If you want to build an _uber-jar_, execute the following command: +```shell script +mvn package -Dquarkus.package.type=uber-jar +``` + +The application is now runnable using `java -jar target/quarkus-app/quarkus-run.jar`. + +It also generates a zip file `target/function.zip`. This zip file contains your java code along with the dependencies. + +<a name="native"> </a> + +## Building and Packaging the Java code as Quarkus Native executable + +If you want a lower memory footprint and faster initialization times for your lambda, you can compile your Java code to a native executable. Just make sure to rebuild your project with the -Pnative switch. +> :warning: **Building Native Executables will take much longer time and depends on the underlying system** +```shell script +mvn package -Pnative +``` +> :information_source: If you are building on a non-Linux system Or, if you don't have GraalVM installed, you can run the native executable build using docker container. You need to pass in a property instructing quarkus to use a docker build as Amazon Lambda requires linux binaries. +>You can do this by passing this property to your Maven build: `-Dquarkus.native.container-build=true`. However, This requires to have docker installed locally. + +```shell script +mvn package -Pnative -Dquarkus.native.container-build=true +``` + +Either of these commands will compile and create a native executable image. +You can then execute your native executable with: `./target/code-with-quarkus-1.0.0-SNAPSHOT-runner` + +It also generates a zip file `target/function.zip`. This zip file contains your native executable image renamed to bootstrap. This is a requirement of the AWS Lambda Custom (Provided) Runtime. + +If you want to learn more about building native executables, please consult https://quarkus.io/guides/maven-tooling.html. + +## Extra Build Generated Files + **_NOTE:_** After you run the build, there are a few extra files generated by the quarkus-amazon-lambda extension. These files are in the build directory: `target/` + +* function.zip - lambda deployment file + +* manage.sh - wrapper around aws lambda cli calls + +* bootstrap-example.sh - example bootstrap script for native deployments + +* sam.jvm.yaml - (optional) for use with sam cli and local testing + +* sam.native.yaml - (optional) for use with sam cli and native local testing + +> :information_source: [Please click here to know more on how to use these scripts for automated deployment](https://quarkus.io/guides/amazon-lambda#extra-build-generated-files) + +## Deploying the Quarkus JVM application to AWS Lambda via AWS Web Console + +1. Go to AWS Web console and search for Lambda Service + +2. Click Create Function and select Author From Scratch + +3. Give the name for your function which should be unique + +4. Select Java 11 (Corretto) as Runtime + +5. Under Permission feel free to create / use existing role to give the required permission for your lambda function + +6. Once the function is created click the function name to upload the generated function.zip file and configure it. + +7. Scroll down and select the Code tab. Click the upload from dropdown on right-hand side of the screen and select .zip or .jar file + +8. Click upload and browse to the path where the generated zip file `target/function.zip` was created and select the function.zip file and click save + +9. Under the Code tab scroll down to the Runtime settings and click edit + +10. For the Handler details please provide the Quarkus Handler + +```shell script +io.quarkus.amazon.lambda.runtime.QuarkusStreamHandler::handleRequest +``` + +### Testing the AWS Lamda Function + +1. Now select the Test tab for executing a quick test. Copy paste the below json payload and hit Test + + ```shell script + { + "name": "Ravishankar" + } + ``` +2. If everything goes fine you should get the below response along with the lambda execution logs & stats + + ```shell script + Hello Ravishankar ! How are you? from GreetService + ``` +## Deploying the Quarkus Native executable to AWS Lambda via AWS Web Console + +**_NOTE:_** [Please ensure that you have built your Java code as Quarkus Native executable](#native) + +1. Go to AWS Web console and search for Lambda Service + +2. Click Create Function and select Author From Scratch + +3. Give the name for your function which should be unique + +4. For Runtime please scroll down and Select Provide your own bootstrap on Amazon Linux 2 under Custom Runtime + +5. Under Permission feel free to create / use existing role to give the required permission for your lambda function + +6. Once the function is created click the function name to upload the generated function.zip file and configure it. + +7. Scroll down and select the Code tab. Click the upload from dropdown on right-hand side of the screen and select .zip or .jar file + +8. Click upload and browse to the path where the generated zip file `target/function.zip` was created and select the function.zip file and click save + +9. Under the Code tab scroll down to the Runtime settings and click edit + +10. For the Handler details please provide the below Handler + + ```shell script + not.used.in.provided.runtime + ``` + +11. Then Select the Configuration tab and click Environment Variables + +12. For Key enter `DISABLE_SIGNAL_HANDLERS` & for Value enter `true` + +### Testing the AWS Lamda Function + +1. Now select the Test tab for executing a quick test. Copy paste the below json payload and hit Test + + ```shell script + { + "name": "Ravishankar" + } + ``` +2. If everything goes fine you should get the below response along with the lambda execution logs & stats + + ```shell script + Hello Ravishankar ! How are you? from GreetService + ``` + +## JVM vs Native : Results based on lambda execution logs & stats + \ No newline at end of file diff --git a/aws-lambda/payload.json b/aws-lambda/payload.json new file mode 100644 index 0000000..192c26b --- /dev/null +++ b/aws-lambda/payload.json @@ -0,0 +1,3 @@ +{ + "name": "Ravishankar" +} \ No newline at end of file diff --git a/aws-lambda/pom.xml b/aws-lambda/pom.xml new file mode 100644 index 0000000..25e7072 --- /dev/null +++ b/aws-lambda/pom.xml @@ -0,0 +1,287 @@ +<?xml version="1.0"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--> + +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.camel.quarkus.examples</groupId> + <artifactId>camel-quarkus-examples-aws-lambda</artifactId> + <version>2.2.0-SNAPSHOT</version> + + <name>Camel Quarkus :: Examples :: AWS Lambda</name> + <description>Camel Quarkus Example :: Deploying a Camel Route in AWS Lambda</description> + + <properties> + <camel-quarkus.version>2.1.0</camel-quarkus.version> + <quarkus.version>2.1.0.Final</quarkus.version> + + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> + <maven.compiler.target>11</maven.compiler.target> + <maven.compiler.source>11</maven.compiler.source> + <maven.compiler.testTarget>${maven.compiler.target}</maven.compiler.testTarget> + <maven.compiler.testSource>${maven.compiler.source}</maven.compiler.testSource> + + <formatter-maven-plugin.version>2.11.0</formatter-maven-plugin.version> + <impsort-maven-plugin.version>1.3.2</impsort-maven-plugin.version> + <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version> + <maven-jar-plugin.version>3.2.0</maven-jar-plugin.version> + <maven-resources-plugin.version>3.1.0</maven-resources-plugin.version> + <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version> + <mycila-license.version>3.0</mycila-license.version> + </properties> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-bom</artifactId> + <version>${camel-quarkus.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-direct</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-log</artifactId> + </dependency> + <dependency> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-amazon-lambda</artifactId> + </dependency> + <dependency> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-arc</artifactId> + </dependency> + <dependency> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-junit5</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-junit5-mockito</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-test-amazon-lambda</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>net.revelc.code.formatter</groupId> + <artifactId>formatter-maven-plugin</artifactId> + <version>${formatter-maven-plugin.version}</version> + <configuration> + <configFile>${maven.multiModuleProjectDirectory}/eclipse-formatter-config.xml</configFile> + </configuration> + </plugin> + + <plugin> + <groupId>net.revelc.code</groupId> + <artifactId>impsort-maven-plugin</artifactId> + <version>${impsort-maven-plugin.version}</version> + <configuration> + <groups>java.,javax.,org.w3c.,org.xml.,junit.</groups> + <removeUnused>true</removeUnused> + <staticAfter>true</staticAfter> + <staticGroups>java.,javax.,org.w3c.,org.xml.,junit.</staticGroups> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>${maven-compiler-plugin.version}</version> + <configuration> + <parameters>true</parameters> + <showDeprecation>true</showDeprecation> + <showWarnings>true</showWarnings> + <compilerArgs> + <arg>-Xlint:unchecked</arg> + </compilerArgs> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>${maven-surefire-plugin.version}</version> + <configuration> + <failIfNoTests>false</failIfNoTests> + <systemProperties> + <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> + <maven.home>${maven.home}</maven.home> + </systemProperties> + </configuration> + </plugin> + + <plugin> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-maven-plugin</artifactId> + <version>${quarkus.version}</version> + <extensions>true</extensions> + <executions> + <execution> + <goals> + <goal>build</goal> + <goal>generate-code</goal> + <goal>generate-code-tests</goal> + </goals> + </execution> + </executions> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-failsafe-plugin</artifactId> + <version>${maven-surefire-plugin.version}</version> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <version>${maven-jar-plugin.version}</version> + </plugin> + + <plugin> + <groupId>com.mycila</groupId> + <artifactId>license-maven-plugin</artifactId> + <version>${mycila-license.version}</version> + <configuration> + <failIfUnknown>true</failIfUnknown> + <header>${maven.multiModuleProjectDirectory}/header.txt</header> + <excludes> + <exclude>**/*.adoc</exclude> + <exclude>**/*.txt</exclude> + <exclude>**/LICENSE.txt</exclude> + <exclude>**/LICENSE</exclude> + <exclude>**/NOTICE.txt</exclude> + <exclude>**/NOTICE</exclude> + <exclude>**/README</exclude> + <exclude>**/pom.xml.versionsBackup</exclude> + </excludes> + <mapping> + <java>SLASHSTAR_STYLE</java> + <properties>CAMEL_PROPERTIES_STYLE</properties> + <kt>SLASHSTAR_STYLE</kt> + </mapping> + <headerDefinitions> + <headerDefinition>${maven.multiModuleProjectDirectory}/license-properties-headerdefinition.xml</headerDefinition> + </headerDefinitions> + </configuration> + </plugin> + </plugins> + </pluginManagement> + + <plugins> + <plugin> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-maven-plugin</artifactId> + <version>${quarkus.version}</version> + <extensions>true</extensions> + <executions> + <execution> + <goals> + <goal>build</goal> + <goal>generate-code</goal> + <goal>generate-code-tests</goal> + </goals> + </execution> + </executions> + </plugin> + + <plugin> + <groupId>net.revelc.code.formatter</groupId> + <artifactId>formatter-maven-plugin</artifactId> + <executions> + <execution> + <id>format</id> + <goals> + <goal>format</goal> + </goals> + <phase>process-sources</phase> + </execution> + </executions> + </plugin> + + <plugin> + <groupId>net.revelc.code</groupId> + <artifactId>impsort-maven-plugin</artifactId> + <executions> + <execution> + <id>sort-imports</id> + <goals> + <goal>sort</goal> + </goals> + <phase>process-sources</phase> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <profiles> + <profile> + <id>native</id> + <activation> + <property> + <name>native</name> + </property> + </activation> + <properties> + <quarkus.package.type>native</quarkus.package.type> + </properties> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-failsafe-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>integration-test</goal> + <goal>verify</goal> + </goals> + <configuration> + <systemPropertyVariables> + <native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path> + <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> + <maven.home>${maven.home}</maven.home> + </systemPropertyVariables> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + </profiles> +</project> \ No newline at end of file diff --git a/aws-lambda/results.png b/aws-lambda/results.png new file mode 100644 index 0000000..a806e20 Binary files /dev/null and b/aws-lambda/results.png differ diff --git a/aws-lambda/src/main/docker/Dockerfile.jvm b/aws-lambda/src/main/docker/Dockerfile.jvm new file mode 100644 index 0000000..90bc63f --- /dev/null +++ b/aws-lambda/src/main/docker/Dockerfile.jvm @@ -0,0 +1,55 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode +# +# Before building the container image run: +# +# mvn package +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.jvm -t camel-quarkus-examples/aws-lambda-jvm . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 qcamel-quarkus-examples/aws-lambda-jvm +# +# If you want to include the debug port into your docker image +# you will have to expose the debug port (default 5005) like this : EXPOSE 8080 5005 +# +# Then run the container using : +# +# docker run -i --rm -p 8080:8080 -p 5005:5005 -e JAVA_ENABLE_DEBUG="true" camel-quarkus-examples/aws-lambda-jvm +# +### +FROM registry.access.redhat.com/ubi8/ubi-minimal:8.4 + +ARG JAVA_PACKAGE=java-11-openjdk-headless +ARG RUN_JAVA_VERSION=1.3.8 +ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' +# Install java and the run-java script +# Also set up permissions for user `1001` +RUN microdnf install curl ca-certificates ${JAVA_PACKAGE} \ + && microdnf update \ + && microdnf clean all \ + && mkdir /deployments \ + && chown 1001 /deployments \ + && chmod "g+rwX" /deployments \ + && chown 1001:root /deployments \ + && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \ + && chown 1001 /deployments/run-java.sh \ + && chmod 540 /deployments/run-java.sh \ + && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/conf/security/java.security + +# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size. +ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" +# We make four distinct layers so if there are application changes the library layers can be re-used +COPY --chown=1001 target/quarkus-app/lib/ /deployments/lib/ +COPY --chown=1001 target/quarkus-app/*.jar /deployments/ +COPY --chown=1001 target/quarkus-app/app/ /deployments/app/ +COPY --chown=1001 target/quarkus-app/quarkus/ /deployments/quarkus/ + +EXPOSE 8080 +USER 1001 + +ENTRYPOINT [ "/deployments/run-java.sh" ] + diff --git a/aws-lambda/src/main/docker/Dockerfile.native b/aws-lambda/src/main/docker/Dockerfile.native new file mode 100644 index 0000000..ab14aa2 --- /dev/null +++ b/aws-lambda/src/main/docker/Dockerfile.native @@ -0,0 +1,27 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode +# +# Before building the container image run: +# +# mvn package -Pnative +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.native -t camel-quarkus-examples/aws-lambda . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 camel-quarkus-examples/aws-lambda +# +### +FROM registry.access.redhat.com/ubi8/ubi-minimal:8.4 +WORKDIR /work/ +RUN chown 1001 /work \ + && chmod "g+rwX" /work \ + && chown 1001:root /work +COPY --chown=1001:root target/*-runner /work/application + +EXPOSE 8080 +USER 1001 + +CMD ["./application", "-Dquarkus.http.host=0.0.0.0"] diff --git a/aws-lambda/src/main/docker/Dockerfile.native-distroless b/aws-lambda/src/main/docker/Dockerfile.native-distroless new file mode 100644 index 0000000..08f3de7 --- /dev/null +++ b/aws-lambda/src/main/docker/Dockerfile.native-distroless @@ -0,0 +1,23 @@ +#### +# This Dockerfile is used in order to build a distroless container that runs the Quarkus application in native (no JVM) mode +# +# Before building the container image run: +# +# mvn package -Pnative +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.native-distroless -t camel-quarkus-examples/aws-lambda . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 camel-quarkus-examples/aws-lambda +# +### +FROM quay.io/quarkus/quarkus-distroless-image:1.0 +COPY target/*-runner /application + +EXPOSE 8080 +USER nonroot + +CMD ["./application", "-Dquarkus.http.host=0.0.0.0"] diff --git a/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/AWSLambdaHandler.java b/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/AWSLambdaHandler.java new file mode 100644 index 0000000..ce45e3a --- /dev/null +++ b/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/AWSLambdaHandler.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.camel.quarkus.examples; + +import javax.inject.Inject; +import javax.inject.Named; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.LambdaLogger; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import org.apache.camel.ProducerTemplate; + +@Named("awsLambdaHandler") +public class AWSLambdaHandler implements RequestHandler<Person, String> { + + @Inject + ProducerTemplate template; + + @Override + public String handleRequest(Person input, Context context) { + LambdaLogger logger = context.getLogger(); + logger.log("Calling Camel Route :)"); + return template.requestBody("direct:input", input, String.class); + } +} diff --git a/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/CamelRoute.java b/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/CamelRoute.java new file mode 100644 index 0000000..b8c569f --- /dev/null +++ b/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/CamelRoute.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.camel.quarkus.examples; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; + +import org.apache.camel.builder.RouteBuilder; + +@ApplicationScoped +public class CamelRoute extends RouteBuilder { + + @Inject + GreetService greetService; + + @Override + public void configure() throws Exception { + from("direct:input").routeId("Test") + .log("Inside Camel Route Received Payload ==> ${body}") + .setBody().body(Person.class, p -> greetService.greet(p.getName())) + .end(); + } +} diff --git a/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/GreetService.java b/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/GreetService.java new file mode 100644 index 0000000..0d46f67 --- /dev/null +++ b/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/GreetService.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.camel.quarkus.examples; + +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class GreetService { + + public String greet(String name) { + return String.format("Hello %s ! How are you? from GreetService", name); + } +} diff --git a/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/Person.java b/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/Person.java new file mode 100644 index 0000000..cd6553e --- /dev/null +++ b/aws-lambda/src/main/java/org/apache/camel/quarkus/examples/Person.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.camel.quarkus.examples; + +public class Person { + + private String name; + + public String getName() { + return name; + } + + public Person setName(String name) { + this.name = name; + return this; + } + + @Override + public String toString() { + return String.format("Person[name= %s]", this.name); + } +} diff --git a/aws-lambda/src/test/java/org/apache/camel/quarkus/examples/AWSLambdaHandlerTest.java b/aws-lambda/src/test/java/org/apache/camel/quarkus/examples/AWSLambdaHandlerTest.java new file mode 100644 index 0000000..4f3364f --- /dev/null +++ b/aws-lambda/src/test/java/org/apache/camel/quarkus/examples/AWSLambdaHandlerTest.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.camel.quarkus.examples; + +import io.quarkus.amazon.lambda.test.LambdaClient; +import io.quarkus.test.junit.QuarkusMock; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +@QuarkusTest +public class AWSLambdaHandlerTest { + + @BeforeAll + public static void setup() { + + GreetService mock = Mockito.mock(GreetService.class); + Mockito.when(mock.greet("Stu")).thenReturn("Hello Stu ! How are you? from GreetService"); + QuarkusMock.installMockForType(mock, GreetService.class); + } + + @Test + public void testSimpleLambdaSuccess() throws Exception { + Person in = new Person(); + in.setName("Stu"); + String out = LambdaClient.invoke(String.class, in); + Assertions.assertEquals("Hello Stu ! How are you? from GreetService", out); + } +} diff --git a/aws-lambda/src/test/java/org/apache/camel/quarkus/examples/AWSLambdaHandlerTestIT.java b/aws-lambda/src/test/java/org/apache/camel/quarkus/examples/AWSLambdaHandlerTestIT.java new file mode 100644 index 0000000..6debf16 --- /dev/null +++ b/aws-lambda/src/test/java/org/apache/camel/quarkus/examples/AWSLambdaHandlerTestIT.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +package org.apache.camel.quarkus.examples; + +import io.quarkus.test.junit.NativeImageTest; + +@NativeImageTest +public class AWSLambdaHandlerTestIT extends AWSLambdaHandlerTest { + + // Execute the same tests but in native mode. +} diff --git a/aws-lambda/src/test/resources/application.properties b/aws-lambda/src/test/resources/application.properties new file mode 100644 index 0000000..3db48e4 --- /dev/null +++ b/aws-lambda/src/test/resources/application.properties @@ -0,0 +1,17 @@ +## --------------------------------------------------------------------------- +## Licensed to the Apache Software Foundation (ASF) under one or more +## contributor license agreements. See the NOTICE file distributed with +## this work for additional information regarding copyright ownership. +## The ASF licenses this file to You 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. +## --------------------------------------------------------------------------- +quarkus.lambda.enable-polling-jvm-mode=true diff --git a/docs/modules/ROOT/attachments/examples.json b/docs/modules/ROOT/attachments/examples.json index 6875aa8..56bb1ae 100644 --- a/docs/modules/ROOT/attachments/examples.json +++ b/docs/modules/ROOT/attachments/examples.json @@ -68,5 +68,10 @@ "title": "XML Hello World", "description": "Shows how to define Camel routes using XML.", "link": "https://github.com/apache/camel-quarkus-examples/tree/main/timer-log-xml" + }, + { + "title": "Deploying a Camel Route in AWS Lambda", + "description": "Demonstrates how to define Camel routes using CDI and deployed as functions into the Amazon Lambda", + "link": "https://github.com/apache/camel-quarkus-examples/tree/main/aws-lambda" } ] \ No newline at end of file
