Skip to content

Commit ea35706

Browse files
committed
Add native web sample
1 parent 494f60b commit ea35706

File tree

22 files changed

+1315
-0
lines changed

22 files changed

+1315
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ asciidoctor.css
44
*~
55
.#*
66
*#
7+
src/main/resources/META-INF/native-image
78
target/
89
build/
910
bin/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.7/apache-maven-3.8.7-bin.zip
18+
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
FROM arm64v8/amazonlinux:2
2+
3+
RUN yum -y update \
4+
&& yum install -y tar unzip gzip bzip2-devel ed gcc gcc-c++ gcc-gfortran \
5+
less libcurl-devel openssl openssl-devel readline-devel xz-devel \
6+
zlib-devel glibc-static libcxx libcxx-devel llvm-toolset-7 zlib-static \
7+
&& rm -rf /var/cache/yum
8+
9+
#ENV GRAAL_VERSION 22.3.1
10+
ENV GRAAL_FOLDERNAME graalvm-community-openjdk-21.0.1+12.1
11+
#ENV GRAAL_FILENAME graalvm-community-jdk-21.0.1_linux-x64_bin.tar.gz
12+
13+
RUN curl -4 -L https://github.com/graalvm/graalvm-ce-builds/releases/download/jdk-21.0.1/graalvm-community-jdk-21.0.1_linux-aarch64_bin.tar.gz | tar -xvz
14+
RUN mv $GRAAL_FOLDERNAME /usr/lib/graalvm
15+
RUN rm -rf $GRAAL_FOLDERNAME
16+
17+
# Graal maven plugin requires Maven 3.3.x
18+
ENV MVN_VERSION 3.6.3
19+
ENV MVN_FOLDERNAME apache-maven-${MVN_VERSION}
20+
ENV MVN_FILENAME apache-maven-${MVN_VERSION}-bin.tar.gz
21+
RUN curl -4 -L https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/${MVN_VERSION}/${MVN_FILENAME} | tar -xvz
22+
RUN mv $MVN_FOLDERNAME /usr/lib/maven
23+
RUN rm -rf $MVN_FOLDERNAME
24+
25+
# Gradle
26+
ENV GRADLE_VERSION 7.4.1
27+
ENV GRADLE_FOLDERNAME gradle-${GRADLE_VERSION}
28+
ENV GRADLE_FILENAME gradle-${GRADLE_VERSION}-bin.zip
29+
RUN curl -LO https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip
30+
RUN unzip gradle-${GRADLE_VERSION}-bin.zip
31+
RUN mv $GRADLE_FOLDERNAME /usr/lib/gradle
32+
RUN rm -rf $GRADLE_FOLDERNAME
33+
34+
VOLUME /project
35+
WORKDIR /project
36+
37+
38+
39+
#RUN /usr/lib/graalvm/bin/gu install native-image
40+
#RUN ln -s /usr/lib/graalvm/bin/native-image /usr/bin/native-image
41+
RUN ln -s /usr/lib/maven/bin/mvn /usr/bin/mvn
42+
RUN ln -s /usr/lib/gradle/bin/gradle /usr/bin/gradle
43+
44+
ENV JAVA_HOME /usr/lib/graalvm
45+
46+
WORKDIR /function-sample-aws-native
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
In this sample, you'll build a native GraalVM image for running web workloads in AWS Lambda.
2+
3+
The sample contains a typical Spring MVC application with MVC controllers which will
4+
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.
5+
6+
**Example function definition**
7+
```
8+
@Bean
9+
public Function<String, String> uppercase() {
10+
return v -> {
11+
System.out.println("Uppercasing " + v);
12+
return v.toUpperCase();
13+
};
14+
}
15+
```
16+
17+
> 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.
18+
19+
20+
## To build the sample on macOS (Apple silicon arm64)
21+
22+
You first need to build the function, then you will deploy it to AWS Lambda.
23+
24+
### Step 1 - Build the native image
25+
26+
Before starting the build, you must clone or download the code in **function-sample-aws-native**.
27+
28+
1. Change into the project directory: `spring-cloud-function-samples/function-sample-aws-native`
29+
2. Run the following to build a Docker container image which will be used to create the Lambda function zip file.
30+
```
31+
docker build -t "al2-graalvm19:native-function" .
32+
```
33+
3. Start the container
34+
```
35+
docker run -dit -v `pwd`:`pwd` -w `pwd` -v ~/.m2:/root/.m2 al2-graalvm19:native-function
36+
```
37+
4. In Docker, open the image terminal.
38+
39+
> Your working directory should default to the project root. Verify by running `ls` to view the files.
40+
41+
6. From inside the container, build the Lambda function:
42+
```
43+
./mvnw clean -Pnative native:compile -DskipTests
44+
```
45+
46+
After the build finishes, you need to deploy the function.
47+
48+
49+
### Step 2 - Deploy your function
50+
51+
You will first create the function, and then you will upload the zipped native image from the build process.
52+
53+
**Create the function**
54+
1. Login to the **Amazon Web Services console**.
55+
2. Navigate to the **Lambda service**.
56+
3. Choose `Create Function`.
57+
4. For **function name**, enter `native-func-sample`.
58+
5. For runtime, select `Provide your own bootstrap on Amazon Linux 2`.
59+
6. For architecture, select `arm64`.
60+
7. Choose `Create Function` again.
61+
62+
**Upload the zip image**
63+
1. Choose `Upload from`, then `.zip file`.
64+
2. From the `target` directory, select the .zip file created by the build.
65+
3. Wait for the image to upload.
66+
67+
### Step 3 - Test your function
68+
69+
Your test event will provide the information needed to select the `uppercase` or `reverse` handler functions.
70+
71+
1. From the Lambda console, navigate to the `Test` tab.
72+
2. For test data, enter the following JSON:
73+
```JSON
74+
{
75+
"payload": "hello",
76+
"headers": {
77+
"spring.cloud.function.definition": "uppercase"
78+
}
79+
}
80+
```
81+
3. Choose **Test**.
82+
You should see uppercased output for the payload value: "HELLO"
83+
84+
4. Change the test data to the following JSON:
85+
```JSON
86+
{
87+
"payload": "hello",
88+
"headers": {
89+
"spring.cloud.function.definition": "reverse"
90+
}
91+
}
92+
```
93+
5. Choose **Test**.
94+
You should see reversed output for the payload value: "OLLEH"
95+
96+
97+
**Congratulations!** You have built and deployed a Graal native image to AWS Lambda.

0 commit comments

Comments
 (0)