3.6 KiB
In this sample, you'll build a native GraalVM image with spring-cloud-function and set it up to run in AWS Lambda.
The sample contains two functions - uppercase and reverse - so you can see how to route requests. A provided RoutingFunction will send messages to a handler function specified in a header named: spring.cloud.function.definition (demonstrated in the test section). The routing value can also be passed as an environment variable. If using API Gateway, you can pass this value as an HTTP header.
Example function definition
@Bean
public Function<String, String> uppercase() {
return v -> {
System.out.println("Uppercasing " + v);
return v.toUpperCase();
};
}
Note: If your function takes a Spring Message as an input parameter (e.g., Function<Message, ..>), the Lambda Context object will be available in the message header
aws-context. See AWSLambdaUtils.java for details.
To build the sample on macOS (Apple silicon arm64)
You first need to build the function, then you will deploy it to AWS Lambda.
Step 1 - Build the native image
Before starting the build, you must clone or download the code in function-sample-aws-native.
-
Change into the project directory:
spring-cloud-function-samples/function-sample-aws-native -
Run the following to build a Docker container image which will be used to create the Lambda function zip file.
docker build -t "al2-graalvm19:native-function" . -
Start the container
docker run -dit -v `pwd`:`pwd` -w `pwd` -v ~/.m2:/root/.m2 al2-graalvm19:native-function -
In Docker, open the image terminal.
Your working directory should default to the project root. Verify by running
lsto view the files. -
From inside the container, build the Lambda function:
./mvnw clean -Pnative native:compile -DskipTests
After the build finishes, you need to deploy the function.
Step 2 - Deploy your function
You will first create the function, and then you will upload the zipped native image from the build process.
Create the function
- Login to the Amazon Web Services console.
- Navigate to the Lambda service.
- Choose
Create Function. - For function name, enter
native-func-sample. - For runtime, select
Provide your own bootstrap on Amazon Linux 2. - For architecture, select
arm64. - Choose
Create Functionagain.
Upload the zip image
- Choose
Upload from, then.zip file. - From the
targetdirectory, select the .zip file created by the build. - Wait for the image to upload.
Step 3 - Test your function
Your test event will provide the information needed to select the uppercase or reverse handler functions.
-
From the Lambda console, navigate to the
Testtab. -
For test data, enter the following JSON:
{ "payload": "hello", "headers": { "spring.cloud.function.definition": "uppercase" } } -
Choose Test. You should see uppercased output for the payload value: "HELLO"
-
Change the test data to the following JSON:
{ "payload": "hello", "headers": { "spring.cloud.function.definition": "reverse" } } -
Choose Test. You should see reversed output for the payload value: "olleh"
Congratulations! You have built and deployed a Graal native image to AWS Lambda.