Skip to content

Commit

Permalink
* Reinstated native:compile in prepare-package phase. This is to …
Browse files Browse the repository at this point in the history
…ensure the `spring-native-aws-lambda-function-native-zip.zip` include would include the `spring-native-aws-lambda-function` native executable.

Run `native:compile` in `prepare-package` so `spring-native-aws-lambda-function.jar.original` would include `spring-native-aws-lambda-function` file

Signed-off-by: matto <muhamadto@gmail.com>
  • Loading branch information
muhamadto committed Dec 31, 2023
1 parent 5568221 commit 00a2d03
Show file tree
Hide file tree
Showing 23 changed files with 975 additions and 1,662 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
.idea/
badges/
**/target/
cdk.out/
volume/
.gitignore
CODE_OF_CONDUCT.md
HELP.md
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
release:
runs-on: ubuntu-latest
container:
image: ghcr.io/muhamadto/spring-native-amazonlinux2-builder:20-amazonlinux2
image: ghcr.io/muhamadto/spring-native-amazonlinux2-builder:21-amazonlinux2
options: --user=worker:ci
permissions:
id-token: write
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
ENV: ${{ env.ENV }}
COST_CENTRE: ${{ env.COST_CENTRE }}
run: ./mvnw -ntp -Pnative clean package -DskipTests=true
run: ./mvnw -ntp clean package -U -Pnative -DskipTests native:compile
- name: cdk diff
uses: muhamadto/aws-cdk-github-actions@v3
with:
Expand Down
10 changes: 4 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
target/
!spring-native-aws-lambda-function/.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
**/target/
cdk.out/
volume/
**/maven-wrapper.jar

### STS ###
.apt_generated
Expand Down Expand Up @@ -30,5 +30,3 @@ build/

### VS Code ###
.vscode/

**/maven-wrapper.jar
79 changes: 29 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=muhamadto_spring-native-aws-lambda&metric=bugs)](https://sonarcloud.io/summary/new_code?id=muhamadto_spring-native-aws-lambda)
[![SonarCloud Coverage](https://sonarcloud.io/api/project_badges/measure?project=muhamadto_spring-native-aws-lambda&metric=coverage)](https://sonarcloud.io/component_measures?id=muhamadto_spring-native-aws-lambda&metric=new_coverage&view=list)

| Component | Version |
|---------------|----------|
| JDK | 20 |
| Spring Cloud | 2022.0.1 |
| Spring Boot | 3.1.2 |
| Component | Version |
|--------------|----------|
| JDK | 21 |
| Spring Cloud | 2023.0.0 |
| Spring Boot | 3.2.1 |

## Test

```bash
$ sdk use java 22.2.r17-grl
$ ./mvnw -ntp clean verify -U --settings ./settings-spring.xml
$ ./mvnw -ntp clean verify -U
```

## Building and Running
Expand All @@ -33,12 +33,30 @@ $ ./mvnw -ntp clean verify -U --settings ./settings-spring.xml
```shell
$ docker-compose up
```
2. Make a call
```shell
$ curl --location --request POST 'http://localhost:4566/restapis/<restApiId>/test/_user_request_/test' \
--header 'Content-Type: application/json' \
--data-raw '{
"body": "{ \"name\": \"CoffeeBeans\" }"
}'
```
The service responds
```json
[
{
"name": "CoffeeBeans",
"saved": true
}
]
```

#### Using `mvnw`
1. Set `spring.main.web-application-type` to `servlet` for local development
2. Run the following commands

1. Run the following commands
```shell
$ ./mvnw -ntp clean package -U -Pnative -pl spring-native-aws-lambda-function --settings ./settings-spring.xml
$ export SPRING_PROFILES_ACTIVE=local
$ ./mvnw -ntp clean package -U -Pnative -DskipTests -pl spring-native-aws-lambda-function
$ ./spring-native-aws-lambda-function/target/spring-native-aws-lambda-function
```
The service starts in less than 100 ms
Expand All @@ -59,7 +77,7 @@ $ ./mvnw -ntp clean verify -U --settings ./settings-spring.xml
$ curl --location --request POST 'http://localhost:8080' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "CoffeeBeans"
"body": "{ \"name\": \"CoffeeBeans\" }"
}'
```
The service responds
Expand Down Expand Up @@ -339,43 +357,4 @@ Now that the setup is done you can deploy to AWS.
"name": "CoffeeBeans"
}'
```
3. Et voila! It runs with 500 ms for cold start.

## Maven Repository

This project uses experimental dependencies from Spring.

* One way to pul them is to add `repositories` and `pluginRepositories` elements to `pom.xml`.

* An alternative add them to `settings.xml` as following

```xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>spring-native-demo</id>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>spring-native-demo</activeProfile>
</activeProfiles>
</settings>
```
3. Et voila! It runs with 500 ms for cold start.
26 changes: 16 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@

version: "3.9"
services:
spring-native-aws-lambda-function:
image: ghcr.io/muhamadto/spring-native-amazonlinux2-builder:20-amazonlinux2-awscliv2
ports:
- "8080:8080"
- "5005:5005"
spring-native-aws-lambda-function-infra:
image: ghcr.io/muhamadto/spring-native-amazonlinux2-builder:21-amazonlinux2-awscliv2
volumes:
- ./:/app
- ${M2_REPO}:/home/worker/.m2
Expand Down Expand Up @@ -54,15 +51,24 @@ services:
print_info_message "divider" "Package GraalVM function" &&
./mvnw -ntp clean package -U -Pnative -pl spring-native-aws-lambda-function --settings ./settings-spring.xml &&
./mvnw -ntp clean package -U -Pnative -DskipTests -pl spring-native-aws-lambda-function &&
print_info_message "divider" "Creating LAMBDA function" &&
lambda_create_function lambda-FUNCTION provided.al2023 512 ./spring-native-aws-lambda-function/target/spring-native-aws-lambda-function-native-zip.zip spring-native-aws-lambda-function &&
lambda_wait_for_function lambda-FUNCTION &&
lambda_list_functions &&
print_info_message "block" "Successfully creating 'spring-native-aws-lambda-function'" &&
exit 0
print_info_message "divider" "Creating API Gateway" &&
REST_API_ID="$(apigateway_create_restApi "somerestapiname")" &&
RESOURCE_ID="$(apigateway_create_resource "$$REST_API_ID" "somePathId")"
apigateway_create_method "$$REST_API_ID" "$$RESOURCE_ID" "POST" &&
apigateway_create_lambda_integration "$$REST_API_ID" "$$RESOURCE_ID" "POST" "arn:aws:apigateway:ap-southeast-2:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-southeast-2:000000000000:function:lambda-FUNCTION/invocations" &&
apigateway_create_deployment "$$REST_API_ID" "test" &&
--rest-api-id &&
print_info_message "plain" "Endpoint available at: http://localhost:4566/restapis/$$REST_API_ID/test/_user_request_/test" &&
print_info_message "block" "Successfully creating 'spring-native-aws-lambda-function'"
'
depends_on:
- localstack
Expand Down
25 changes: 0 additions & 25 deletions settings-spring.xml

This file was deleted.

11 changes: 10 additions & 1 deletion spring-native-aws-lambda-function/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,23 @@
<buildArg>--verbose</buildArg>
<buildArg>--no-fallback</buildArg>
<buildArg>--enable-preview</buildArg>
<buildArg>--gc=G1</buildArg>
<!-- <buildArg>&#45;&#45;gc=G1</buildArg>-->
<buildArg>-march=native</buildArg>
<buildArg>--strict-image-heap</buildArg>
<buildArg>--enable-url-protocols=http</buildArg>
<buildArg>-H:+AddAllCharsets</buildArg>
<buildArg>-H:+ReportExceptionStackTraces</buildArg>
</buildArgs>
</configuration>
<executions>
<execution>
<id>build-native</id>
<phase>prepare-package</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
Expand Down
14 changes: 14 additions & 0 deletions spring-native-aws-lambda-infra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,18 @@
</dependency>
<!-- TEST ENDS -->
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 00a2d03

Please sign in to comment.