|
1 |
| -# Introduction |
| 1 | +In this sample, you'll build a native GraalVM image with `spring-cloud-function` and set it up to run in AWS Lambda. |
2 | 2 |
|
3 |
| -This example shows GraaalVM native spring-cloud-function application. The application itself is very simple and contains two functions - `uppercase` & `lowercase`. |
4 |
| -Unless specific value is specified as `handler`, the application will fall back on `RoutingFunction` where you can pass the routing instruction |
5 |
| -via `spring.cloud.function.definition` Message header. If using API Gateway you can pass such header as HTTP header. |
| 3 | +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. |
6 | 4 |
|
7 |
| -# To run |
| 5 | +**Example function definition** |
| 6 | +``` |
| 7 | +@Bean |
| 8 | +public Function<String, String> uppercase() { |
| 9 | + return v -> { |
| 10 | + System.out.println("Uppercasing " + v); |
| 11 | + return v.toUpperCase(); |
| 12 | + }; |
| 13 | +} |
| 14 | +``` |
8 | 15 |
|
9 |
| -## If you are on OSX Apple M1 Pro (arch64) |
| 16 | +> 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](https://github.com/spring-cloud/spring-cloud-function/blob/main/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java#L67C44-L67C55) for details. |
10 | 17 |
|
11 |
| -You first need to build a Docker image where you will actually build project. |
12 |
| -To do that execute the following command form [project directory |
13 | 18 |
|
14 |
| -``` |
15 |
| -docker build -t "al2-graalvm19:native-uppercase" . |
16 |
| -``` |
17 |
| -Start the container |
| 19 | +## To build the sample on macOS (Apple silicon arm64) |
18 | 20 |
|
19 |
| -``` |
20 |
| -docker run -dit -v `pwd`:`pwd` -w `pwd` -v ~/.m2:/root/.m2 al2-graalvm19:native-uppercase |
21 |
| -``` |
22 |
| - |
23 |
| -Now navigate to the image terminal. Your working directory is alredy set for the root of the project. You can verify it by executing `ls`. |
| 21 | +You first need to build the function, then you will deploy it to AWS Lambda. |
24 | 22 |
|
25 |
| -Build the project: |
| 23 | +### Step 1 - Build the native image |
26 | 24 |
|
27 |
| -``` |
28 |
| -./mvnw clean -Pnative native:compile -DskipTests |
29 |
| -``` |
| 25 | +Before starting the build, you must clone or download the code in **function-sample-aws-native**. |
| 26 | + |
| 27 | +1. Change into the project directory: `spring-cloud-function-samples/function-sample-aws-native` |
| 28 | +2. Run the following to build a Docker container image which will be used to create the Lambda function zip file. |
| 29 | + ``` |
| 30 | + docker build -t "al2-graalvm19:native-function" . |
| 31 | + ``` |
| 32 | +3. Start the container |
| 33 | + ``` |
| 34 | + docker run -dit -v `pwd`:`pwd` -w `pwd` -v ~/.m2:/root/.m2 al2-graalvm19:native-function |
| 35 | + ``` |
| 36 | +4. In Docker, open the image terminal. |
| 37 | + |
| 38 | + > Your working directory should default to the project root. Verify by running `ls` to view the files. |
| 39 | +
|
| 40 | +6. From inside the container, build the Lambda function: |
| 41 | + ``` |
| 42 | + ./mvnw clean -Pnative native:compile -DskipTests |
| 43 | + ``` |
| 44 | + |
| 45 | +After the build finishes, you need to deploy the function. |
| 46 | + |
| 47 | + |
| 48 | +### Step 2 - Deploy your function |
| 49 | + |
| 50 | +You will first create the function, and then you will upload the zipped native image from the build process. |
| 51 | + |
| 52 | +**Create the function** |
| 53 | +1. Login to the **Amazon Web Services console**. |
| 54 | +2. Navigate to the **Lambda service**. |
| 55 | +3. Choose `Create Function`. |
| 56 | +4. For **function name**, enter `native-func-sample`. |
| 57 | +5. For runtime, select `Provide your own bootstrap on Amazon Linux 2`. |
| 58 | +6. For architecture, select `arm64`. |
| 59 | +7. Choose `Create Function` again. |
30 | 60 |
|
31 |
| -Once the build finishes, you can deploy it. |
| 61 | +**Upload the zip image** |
| 62 | +1. Choose `Upload from`, then `.zip file`. |
| 63 | +2. From the `target` directory, select the .zip file created by the build. |
| 64 | +3. Wait for the image to upload. |
32 | 65 |
|
33 |
| -## Deploying to AWS LAmbda |
| 66 | +### Step 3 - Test your function |
34 | 67 |
|
35 |
| -Start *AWS Dashboard* and navigate to **AWS Lambda** Services |
| 68 | +Your test event will provide the information needed to select the `uppercase` or `reverse` handler functions. |
36 | 69 |
|
37 |
| -Click on `Create Function`. Enter `uppercase` for *function name*. For the runtime select `Provide your own bootstrap on Amazon Linux 2`. |
38 |
| -Make sure you select the proper architecture (`x86_64` or `arm64`). |
| 70 | +1. From the Lambda console, navigate to the `Test` tab. |
| 71 | +2. For test data, enter the following JSON: |
| 72 | + ```JSON |
| 73 | + { |
| 74 | + "payload": "hello", |
| 75 | + "headers": { |
| 76 | + "spring.cloud.function.definition": "uppercase" |
| 77 | + } |
| 78 | + } |
| 79 | + ``` |
| 80 | +3. Choose **Test**. |
| 81 | + You should see uppercased output for the payload value: "HELLO" |
39 | 82 |
|
40 |
| -Click on `Create Function` again. |
| 83 | +4. Change the test data to the following JSON: |
| 84 | + ```JSON |
| 85 | + { |
| 86 | + "payload": "hello", |
| 87 | + "headers": { |
| 88 | + "spring.cloud.function.definition": "reverse" |
| 89 | + } |
| 90 | + } |
| 91 | + ``` |
| 92 | +5. Choose **Test**. |
| 93 | + You should see reversed output for the payload value: "OLLEH" |
41 | 94 |
|
42 |
| -Next you need to upload your project, so click on `Upload From` and point to the ZIP file that was created by the build process (in the `target` directory). |
43 | 95 |
|
44 |
| -Once the file is uploaded navigate to the `Test` tab. You can change the input data or use the default. Basically you need to pas a String in a JSON format such as `"hello"` and you should see the output `"HELLO"`. |
| 96 | +**Congratulations!** You have built and deployed a Graal native image to AWS Lambda. |
0 commit comments