diff --git a/docs/src/main/asciidoc/adapters/aws.adoc b/docs/src/main/asciidoc/adapters/aws.adoc index 6ab00adc4..39d4bc9f2 100644 --- a/docs/src/main/asciidoc/adapters/aws.adoc +++ b/docs/src/main/asciidoc/adapters/aws.adoc @@ -57,10 +57,32 @@ For example, to deploy behind an API Gateway, use `--handler org.springframework == Custom Runtime -An https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html[AWS Lambda custom runtime] can be created really easily using the HTTP export features in Spring Cloud Function Web. To make this work just add Spring Cloud Function AWS and Spring Cloud Function Web as dependencies in your project and set the following in your `application.properties`: +You can also benefit from https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html[AWS Lambda custom runtime] feature of AWS Lambda +and Spring Cloud Function provides all the necessary components to make it easy. -``` -spring.cloud.function.web.export.enabled=true -``` +From the code perspective the application should look no different then any other Spring Cloud Function application. +The only thing you need to do is to provide a `bootstrap` script in the root of your zip/jar that runs the Spring Boot application. +and select "Custom Runtime" when creating a function in AWS. +Here is an example 'bootstrap' file: +```text +#!/bin/sh -Set the handler name in AWS to the name of your function. Then provide a `bootstrap` script in the root of your zip/jar that runs the Spring Boot application. The functional bean definition style works for custom runtimes too, and is faster than the `@Bean` style, so the example `FuncApplication` above would work. A custom runtime can start up much quicker even than a functional bean implementation of a Java lambda - it depends mostly on the number of classes you need to load at runtime. Spring doesn't do very much here, so you can reduce the cold start time by only using primitive types in your function, for instance, and not doing any work in custom `@PostConstruct` initializers. +cd ${LAMBDA_TASK_ROOT:-.} + +java -Dspring.main.web-application-type=none -Dspring.jmx.enabled=false \ + -noverify -XX:TieredStopAtLevel=1 -Xss256K -XX:MaxMetaspaceSize=128M \ + -Djava.security.egd=file:/dev/./urandom \ + -cp .:`echo lib/*.jar | tr ' ' :` com.example.LambdaApplication +``` +The `com.example.LambdaApplication` represents your application which contains function beans. + +Set the handler name in AWS to the name of your function. You can use function composition here as well (e.g., `uppecrase|reverse`). +That is pretty much all. Once you upload your zip/jar to AWS your function will run in custom runtime. +We provide a https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-aws-custom-new[sample project] +where you can also see how to configure yoru POM to properly generate the zip file. + +The functional bean definition style works for custom runtimes as well, and is +faster than the `@Bean` style. A custom runtime can start up much quicker even than a functional bean implementation +of a Java lambda - it depends mostly on the number of classes you need to load at runtime. +Spring doesn't do very much here, so you can reduce the cold start time by only using primitive types in your function, for instance, +and not doing any work in custom `@PostConstruct` initializers. diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/pom.xml b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/pom.xml index 630424bb1..69db08059 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/pom.xml +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/pom.xml @@ -42,12 +42,6 @@ org.springframework spring-web - - - com.fasterxml.jackson.core - jackson-databind - org.springframework.boot spring-boot-starter @@ -93,10 +87,6 @@ true - - io.projectreactor - reactor-core - org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java new file mode 100644 index 000000000..7eef6764f --- /dev/null +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java @@ -0,0 +1,169 @@ +/* + * Copyright 2021-2021 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cloud.function.adapter.aws; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.cloud.function.context.catalog.FunctionTypeUtils; +import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper; +import org.springframework.cloud.function.json.JsonMapper; +import org.springframework.http.HttpStatus; +import org.springframework.lang.Nullable; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageHeaders; +import org.springframework.messaging.support.MessageBuilder; +import org.springframework.util.Assert; + +/** + * + * @author Oleg Zhurakousky + * + */ +final class AWSLambdaUtils { + + private static Log logger = LogFactory.getLog(AWSLambdaUtils.class); + + private AWSLambdaUtils() { + + } + + public static Message generateMessage(byte[] payload, MessageHeaders headers, + FunctionInvocationWrapper function, JsonMapper mapper) { + return generateMessage(payload, headers, function, mapper, null); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static Message generateMessage(byte[] payload, MessageHeaders headers, + FunctionInvocationWrapper function, JsonMapper mapper, @Nullable Context awsContext) { + + if (logger.isInfoEnabled()) { + logger.info("Incoming JSON for ApiGateway Event: " + new String(payload)); + } + + MessageBuilder messageBuilder = null; + Object request = mapper.fromJson(payload, Object.class); + Type inputType = function.getInputType(); + if (FunctionTypeUtils.isMessage(inputType)) { + inputType = FunctionTypeUtils.getImmediateGenericType(inputType, 0); + } + boolean mapInputType = (inputType instanceof ParameterizedType && ((Class) ((ParameterizedType) inputType).getRawType()).isAssignableFrom(Map.class)); + if (request instanceof Map) { + Map requestMap = (Map) request; + if (requestMap.containsKey("Records")) { + List> records = (List>) requestMap.get("Records"); + Assert.notEmpty(records, "Incoming event has no records: " + requestMap); + logEvent(records); + messageBuilder = MessageBuilder.withPayload(payload); + } + else if (requestMap.containsKey("httpMethod")) { // API Gateway + logger.info("Incoming request is API Gateway"); + if (inputType.getTypeName().endsWith(APIGatewayProxyRequestEvent.class.getSimpleName())) { + APIGatewayProxyRequestEvent gatewayEvent = mapper.fromJson(requestMap, APIGatewayProxyRequestEvent.class); + messageBuilder = MessageBuilder.withPayload(gatewayEvent); + } + else if (mapInputType) { + messageBuilder = MessageBuilder.withPayload(requestMap).setHeader("httpMethod", requestMap.get("httpMethod")); + } + else { + Object body = requestMap.remove("body"); + body = body instanceof String ? String.valueOf(body).getBytes(StandardCharsets.UTF_8) : mapper.toJson(body); + messageBuilder = MessageBuilder.withPayload(body).copyHeaders(requestMap); + } + } + } + if (messageBuilder == null) { + messageBuilder = MessageBuilder.withPayload(payload); + } + if (awsContext != null) { + messageBuilder.setHeader("aws-context", awsContext); + } + return messageBuilder.copyHeaders(headers).build(); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static byte[] generateOutput(Message requestMessage, Message responseMessage, + JsonMapper mapper) { + byte[] responseBytes = responseMessage.getPayload(); + if (requestMessage.getHeaders().containsKey("httpMethod") || requestMessage.getPayload() instanceof APIGatewayProxyRequestEvent) { // API Gateway + Map response = new HashMap(); + response.put("isBase64Encoded", false); + + MessageHeaders headers = responseMessage.getHeaders(); + int statusCode = headers.containsKey("statusCode") + ? (int) headers.get("statusCode") + : 200; + + response.put("statusCode", statusCode); + if (isRequestKinesis(requestMessage)) { + HttpStatus httpStatus = HttpStatus.valueOf(statusCode); + response.put("statusDescription", httpStatus.toString()); + } + + String body = new String(responseMessage.getPayload(), StandardCharsets.UTF_8).replaceAll("\\\"", "\""); + response.put("body", body); + + Map responseHeaders = new HashMap<>(); + headers.keySet().forEach(key -> responseHeaders.put(key, headers.get(key).toString())); + + response.put("headers", responseHeaders); + responseBytes = mapper.toJson(response); + } + + return responseBytes; + } + + private static void logEvent(List> records) { + if (isKinesisEvent(records.get(0))) { + logger.info("Incoming request is Kinesis Event"); + } + else if (isS3Event(records.get(0))) { + logger.info("Incoming request is S3 Event"); + } + else if (isSNSEvent(records.get(0))) { + logger.info("Incoming request is SNS Event"); + } + else { + logger.info("Incoming request is SQS Event"); + } + } + + private static boolean isRequestKinesis(Message requestMessage) { + return requestMessage.getHeaders().containsKey("Records"); + } + + private static boolean isSNSEvent(Map record) { + return record.containsKey("Sns"); + } + + private static boolean isS3Event(Map record) { + return record.containsKey("s3"); + } + + private static boolean isKinesisEvent(Map record) { + return record.containsKey("kinesis"); + } +} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeEventLoop.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeEventLoop.java new file mode 100644 index 000000000..5fa888a44 --- /dev/null +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeEventLoop.java @@ -0,0 +1,157 @@ +/* + * Copyright 2021-2021 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cloud.function.adapter.aws; + +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.text.MessageFormat; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cloud.function.context.FunctionCatalog; +import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper; +import org.springframework.cloud.function.json.JsonMapper; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageHeaders; +import org.springframework.util.Assert; +import org.springframework.web.client.RestTemplate; + +/** + * + * @author Oleg Zhurakousky + * @since 3.1.1 + * + */ +@Configuration +@ConditionalOnProperty("AWS_LAMBDA_RUNTIME_API") +public class CustomRuntimeEventLoop { + + private static Log logger = LogFactory.getLog(CustomRuntimeEventLoop.class); + + private static final String LAMBDA_VERSION_DATE = "2018-06-01"; + private static final String LAMBDA_RUNTIME_URL_TEMPLATE = "http://{0}/{1}/runtime/invocation/next"; + private static final String LAMBDA_INVOCATION_URL_TEMPLATE = "http://{0}/{1}/runtime/invocation/{2}/response"; + + @Bean + @ConditionalOnProperty("AWS_LAMBDA_RUNTIME_API") + public CommandLineRunner backgrounder(ApplicationContext applicationContext) { + return args -> eventLoop(applicationContext); + } + + @SuppressWarnings("unchecked") + static void eventLoop(ApplicationContext context) { + logger.info("Starting spring-cloud-function CustomRuntimeEventLoop"); + if (logger.isDebugEnabled()) { + logger.debug("AWS LAMBDA ENVIRONMENT: " + System.getenv()); + } + + String runtimeApi = System.getenv("AWS_LAMBDA_RUNTIME_API"); + String eventUri = MessageFormat.format(LAMBDA_RUNTIME_URL_TEMPLATE, runtimeApi, LAMBDA_VERSION_DATE); + if (logger.isDebugEnabled()) { + logger.debug("Event URI: " + eventUri); + } + + RequestEntity requestEntity = RequestEntity.get(URI.create(eventUri)).build(); + FunctionCatalog functionCatalog = context.getBean(FunctionCatalog.class); + RestTemplate rest = new RestTemplate(); + JsonMapper mapper = context.getBean(JsonMapper.class); + + logger.info("Entering event loop"); + while (true) { + logger.debug("Attempting to get new event"); + ResponseEntity response = rest.exchange(requestEntity, String.class); + if (logger.isDebugEnabled()) { + logger.debug("New Event received: " + response.getBody()); + } + + FunctionInvocationWrapper function = locateFunction(functionCatalog, response.getHeaders().getContentType()); + + Message eventMessage = AWSLambdaUtils.generateMessage(response.getBody().getBytes(StandardCharsets.UTF_8), + fromHttp(response.getHeaders()), function, mapper); + if (logger.isDebugEnabled()) { + logger.debug("Event message: " + eventMessage); + } + + String requestId = response.getHeaders().getFirst("Lambda-Runtime-Aws-Request-Id"); + String invocationUrl = MessageFormat + .format(LAMBDA_INVOCATION_URL_TEMPLATE, runtimeApi, LAMBDA_VERSION_DATE, requestId); + + Message responseMessage = (Message) function.apply(eventMessage); + + String reply = new String(responseMessage.getPayload(), StandardCharsets.UTF_8); + if (logger.isDebugEnabled()) { + logger.debug("Reply from function: " + reply); + } + + byte[] outputBody = AWSLambdaUtils.generateOutput(eventMessage, responseMessage, mapper); + ResponseEntity result = rest + .exchange(RequestEntity.post(URI.create(invocationUrl)).body(outputBody), Object.class); + + if (logger.isInfoEnabled()) { + logger.info("Result POST status: " + result.getStatusCode()); + } + } + } + + private static FunctionInvocationWrapper locateFunction(FunctionCatalog functionCatalog, MediaType contentType) { + String handlerName = System.getenv("_HANDLER"); + FunctionInvocationWrapper function = functionCatalog.lookup(handlerName, contentType.toString()); + if (function == null) { + handlerName = System.getenv("spring.cloud.function.definition"); + } + function = functionCatalog.lookup(handlerName, contentType.toString()); + Assert.notNull(function, "Failed to locate function. Tried locating default function, " + + "function by '_HANDLER' env variable as well as'spring.cloud.function.definition'."); + if (function != null && logger.isInfoEnabled()) { + logger.info("Located function " + function.getFunctionDefinition()); + } + return function; + } + + private static MessageHeaders fromHttp(HttpHeaders headers) { + Map map = new LinkedHashMap<>(); + for (String name : headers.keySet()) { + Collection values = multi(headers.get(name)); + name = name.toLowerCase(); + Object value = values == null ? null + : (values.size() == 1 ? values.iterator().next() : values); + if (name.toLowerCase().equals(HttpHeaders.CONTENT_TYPE.toLowerCase())) { + name = MessageHeaders.CONTENT_TYPE; + } + map.put(name, value); + } + return new MessageHeaders(map); + } + + private static Collection multi(Object value) { + return value instanceof Collection ? (Collection) value : Arrays.asList(value); + } +} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeInitializer.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeInitializer.java index 8dd617f2d..a83697ae6 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeInitializer.java +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeInitializer.java @@ -26,6 +26,7 @@ import org.springframework.util.StringUtils; /** * @author Dave Syer + * @author Oleg Zhurakousky */ @Order(0) public class CustomRuntimeInitializer implements ApplicationContextInitializer { @@ -35,9 +36,14 @@ public class CustomRuntimeInitializer implements ApplicationContextInitializer args -> CustomRuntimeEventLoop.eventLoop(context)); + } + } } - if (ContextFunctionCatalogInitializer.enabled + else if (ContextFunctionCatalogInitializer.enabled && context.getEnvironment().getProperty("spring.functional.enabled", Boolean.class, false)) { if (context.getBeanFactory().getBeanNamesForType(DestinationResolver.class, false, false).length == 0) { context.registerBean(LambdaDestinationResolver.class, () -> new LambdaDestinationResolver()); diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/resources/META-INF/spring.factories b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/resources/META-INF/spring.factories index aba9fec2d..52a7b45d4 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/resources/META-INF/spring.factories @@ -1,5 +1,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.springframework.cloud.function.adapter.aws.CustomRuntimeAutoConfiguration +org.springframework.cloud.function.adapter.aws.CustomRuntimeEventLoop org.springframework.context.ApplicationContextInitializer=\ org.springframework.cloud.function.adapter.aws.CustomRuntimeInitializer org.springframework.boot.env.EnvironmentPostProcessor=\ diff --git a/spring-cloud-function-samples/function-sample-aws-custom-bean/README.adoc b/spring-cloud-function-samples/function-sample-aws-custom-bean/README.adoc new file mode 100644 index 000000000..fc1fd761f --- /dev/null +++ b/spring-cloud-function-samples/function-sample-aws-custom-bean/README.adoc @@ -0,0 +1,12 @@ +This sample uses the custom runtime type on AWS lambda using @Bean style configuration. +However, changing configuration to functional bean registration is supported as well and shown in `function-sample-aws-custom` example. + +To run the app in AWS choose the "Custom Runtime" runtime type, and upload the +.zip file that gets built on the command line with `mvn package` (look +in `target`). +There are several functions defined in the `com.example.LambdaApplication`, so identify the selected function in "Handler" +You can also use function composition (e.g., `uppercase|reverse`) + +You can test any function in this example with any String as input, but the Lambda UI only allows valid JSON as +test data, so you will have to escape the input with double quotes. + diff --git a/spring-cloud-function-samples/function-sample-aws-custom-bean/pom.xml b/spring-cloud-function-samples/function-sample-aws-custom-bean/pom.xml new file mode 100644 index 000000000..03e0b190a --- /dev/null +++ b/spring-cloud-function-samples/function-sample-aws-custom-bean/pom.xml @@ -0,0 +1,210 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.2-SNAPSHOT + + + io.spring.sample + function-sample-aws-custom-bean + 3.0.0.RELEASE + AWS Custom Runtime - @Bean sample + Demo project for Spring Cloud Function with custom AWS Lambda runtime using @Bean style + + + 1.8 + 1.0.22.RELEASE + 3.1.1-SNAPSHOT + + + + + + com.amazonaws + aws-lambda-java-events + 2.2.6 + + + + org.springframework.cloud + spring-cloud-function-adapter-aws + + + org.slf4j + slf4j-jdk14 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.awaitility + awaitility + test + + + org.testcontainers + testcontainers + 1.14.3 + test + + + + + + + org.springframework.cloud + spring-cloud-function-dependencies + ${spring-cloud-function.version} + pom + import + + + + + + + + maven-surefire-plugin + + + com/example/ContainerTests.java + + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.springframework.boot.experimental + spring-boot-thin-layout + ${wrapper.version} + + + + + maven-assembly-plugin + + + zip + package + + single + + false + + + + + src/assembly/zip.xml + + + + + + + + + integration + + + + maven-surefire-plugin + 2.22.0 + + + integration-test + + test + + + + none + + + com/example/ContainerTests.java + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-cloud-function-samples/function-sample-aws-custom-bean/src/assembly/zip.xml b/spring-cloud-function-samples/function-sample-aws-custom-bean/src/assembly/zip.xml new file mode 100644 index 000000000..62e8b951e --- /dev/null +++ b/spring-cloud-function-samples/function-sample-aws-custom-bean/src/assembly/zip.xml @@ -0,0 +1,35 @@ + + zip + + zip + + + + + target/classes + / + true + + bootstrap + + + + target/classes + / + true + 0775 + + bootstrap + + + + + + /lib + false + runtime + + + \ No newline at end of file diff --git a/spring-cloud-function-samples/function-sample-aws-custom-bean/src/main/java/com/example/LambdaApplication.java b/spring-cloud-function-samples/function-sample-aws-custom-bean/src/main/java/com/example/LambdaApplication.java new file mode 100644 index 000000000..8a6f62d61 --- /dev/null +++ b/spring-cloud-function-samples/function-sample-aws-custom-bean/src/main/java/com/example/LambdaApplication.java @@ -0,0 +1,71 @@ +package com.example; + +import java.util.Arrays; +import java.util.function.Function; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.messaging.Message; +import org.springframework.util.ObjectUtils; + +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; + +@SpringBootApplication +public class LambdaApplication { + + private static Log logger = LogFactory.getLog(LambdaApplication.class); + + @Bean + public Function uppercase() { + return value -> { + logger.info("UPPERCASING: " + value); + return value.toUpperCase(); + }; + } + + @Bean + public Function extractPayloadFromGatewayEvent() { + return value -> { + logger.info("ECHO Payload from Gateway Event: " + value.getBody()); + return value.getBody(); + }; + } + + @Bean + public Function, Message> echoMessage() { + return value -> { + logger.info("ECHO MESSAGE: " + value); + return value; + }; + } + + @Bean + public Function reverse() { + return value -> { + logger.info("REVERSING: " + value); + return new StringBuilder(value).reverse().toString(); + }; + } + + + + public static void main(String[] args) { + System.out.println("=====> ENVIRONMENT: " + System.getenv("AWS_LAMBDA_RUNTIME_API")); + //FunctionalSpringApplication.run(LambdaApplication.class, args); + logger.info("==> Starting: LambdaApplication"); + if (!ObjectUtils.isEmpty(args)) { + logger.info("==> args: " + Arrays.asList(args)); + } + SpringApplication.run(LambdaApplication.class, args); + } + +// @Override +// public void initialize(GenericApplicationContext context) { +// context.registerBean("uppercase", FunctionRegistration.class, +// () -> new FunctionRegistration<>(uppercase()).type( +// FunctionType.from(String.class).to(String.class))); +// } +} diff --git a/spring-cloud-function-samples/function-sample-aws-custom-bean/src/main/resources/application.properties b/spring-cloud-function-samples/function-sample-aws-custom-bean/src/main/resources/application.properties new file mode 100644 index 000000000..dbb9aec79 --- /dev/null +++ b/spring-cloud-function-samples/function-sample-aws-custom-bean/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.main.web-application-type=none +logging.level.org.springframework.cloud=DEBUG \ No newline at end of file diff --git a/spring-cloud-function-samples/function-sample-aws-custom-bean/src/main/resources/bootstrap b/spring-cloud-function-samples/function-sample-aws-custom-bean/src/main/resources/bootstrap new file mode 100755 index 000000000..ab39ddb01 --- /dev/null +++ b/spring-cloud-function-samples/function-sample-aws-custom-bean/src/main/resources/bootstrap @@ -0,0 +1,8 @@ +#!/bin/sh + +cd ${LAMBDA_TASK_ROOT:-.} + +java -Dspring.main.web-application-type=none -Dspring.jmx.enabled=false \ + -noverify -XX:TieredStopAtLevel=1 -Xss256K -XX:MaxMetaspaceSize=128M \ + -Djava.security.egd=file:/dev/./urandom \ + -cp .:`echo lib/*.jar | tr ' ' :` com.example.LambdaApplication diff --git a/spring-cloud-function-samples/function-sample-aws-custom-bean/src/test/resources/testBootstrap b/spring-cloud-function-samples/function-sample-aws-custom-bean/src/test/resources/testBootstrap new file mode 100755 index 000000000..142d4caad --- /dev/null +++ b/spring-cloud-function-samples/function-sample-aws-custom-bean/src/test/resources/testBootstrap @@ -0,0 +1,6 @@ +#!/bin/sh + +while true +do + sleep 1 +done \ No newline at end of file