Skip to content

Commit 3f77949

Browse files
Created infrastructure for java21 (#53)
* Initial Java 21 changes * Updating workflows * Fixing tests. Commenting out right now "allowAllOutbound" as this should be configured in the provided securityGroups * Created infrastructure for java21 * Run CDK in java 21 with image `ghcr.io/muhamadto/spring-native-amazonlinux2-builder:21-amazonlinux2` * Replaced `com.coffeebeans.springnativeawslambda.infra.lambda.CustomRuntime2Function` with `com.coffeebeans.springnativeawslambda.infra.lambda.CustomRuntime2023FunctionJava21` and updated tests. * `docker-compose.yml` now adopting `apigateway` functions from `ghcr.io/muhamadto/spring-native-amazonlinux2-builder:21-amazonlinux2-awscliv2`. However, the lambda is not loading and need some more work. * Reinstated `native:compile` in `prepare-package` phase. This is to ensure the `spring-native-aws-lambda-function-native-zip.zip` include would include the `spring-native-aws-lambda-function` native executable. Signed-off-by: matto <muhamadto@gmail.com> * Updated README.md Signed-off-by: matto <muhamadto@gmail.com> * Fixed apigateway to lambda integration Signed-off-by: matto <muhamadto@gmail.com> --------- Signed-off-by: matto <muhamadto@gmail.com> Co-authored-by: Andres <andresoviedo00@gmail.com>
1 parent 7b695f7 commit 3f77949

File tree

22 files changed

+988
-1672
lines changed

22 files changed

+988
-1672
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
.idea/
33
badges/
44
**/target/
5+
cdk.out/
6+
volume/
57
.gitignore
68
CODE_OF_CONDUCT.md
79
HELP.md

.github/workflows/release.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
release:
2525
runs-on: ubuntu-latest
2626
container:
27-
image: ghcr.io/muhamadto/spring-native-amazonlinux2-builder:20-amazonlinux2
27+
image: ghcr.io/muhamadto/spring-native-amazonlinux2-builder:21-amazonlinux2
2828
options: --user=worker:ci
2929
permissions:
3030
id-token: write
@@ -42,6 +42,7 @@ jobs:
4242
duration_seconds: 3600
4343
role-session-name: github-actions-example-lambda
4444
aws-region: ap-southeast-2
45+
mask-aws-account-id: 'true'
4546
- name: Checkout repository
4647
uses: actions/checkout@v3
4748
- name: Cache Maven packages
@@ -56,14 +57,16 @@ jobs:
5657
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
5758
ENV: ${{ env.ENV }}
5859
COST_CENTRE: ${{ env.COST_CENTRE }}
59-
run: ./mvnw -ntp -Pnative clean package -DskipTests=true
60+
run: |
61+
./mvnw -ntp clean verify -DskipTests -pl spring-native-aws-lambda-infra
62+
./mvnw -ntp clean -Pnative -DskipTests native:compile package -pl spring-native-aws-lambda-function
6063
- name: cdk diff
61-
uses: muhamadto/aws-cdk-github-actions@v3
64+
uses: muhamadto/aws-cdk-github-actions@v5
6265
with:
6366
cdk_subcommand: 'diff'
6467
actions_comment: false
6568
- name: cdk deploy
66-
uses: muhamadto/aws-cdk-github-actions@v3
69+
uses: muhamadto/aws-cdk-github-actions@v5
6770
with:
6871
cdk_subcommand: 'deploy'
6972
cdk_args: '--require-approval never'

.gitignore

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
target/
2-
!spring-native-aws-lambda-function/.mvn/wrapper/maven-wrapper.jar
3-
!**/src/main/**/target/
4-
!**/src/test/**/target/
1+
**/target/
2+
cdk.out/
3+
volume/
4+
**/maven-wrapper.jar
55

66
### STS ###
77
.apt_generated
@@ -30,5 +30,3 @@ build/
3030

3131
### VS Code ###
3232
.vscode/
33-
34-
**/maven-wrapper.jar

README.md

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
[![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)
1212
[![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)
1313

14-
| Component | Version |
15-
|---------------|----------|
16-
| JDK | 20 |
17-
| Spring Cloud | 2022.0.1 |
18-
| Spring Boot | 3.1.2 |
14+
| Component | Version |
15+
|--------------|----------|
16+
| JDK | 21 |
17+
| Spring Cloud | 2023.0.0 |
18+
| Spring Boot | 3.2.1 |
1919

2020
## Test
2121

2222
```bash
23-
$ sdk use java 22.2.r17-grl
24-
$ ./mvnw -ntp clean verify -U --settings ./settings-spring.xml
23+
$ sdk use java 21.0.1-graal
24+
$ ./mvnw -ntp clean verify -U
2525
```
2626

2727
## Building and Running
@@ -33,12 +33,30 @@ $ ./mvnw -ntp clean verify -U --settings ./settings-spring.xml
3333
```shell
3434
$ docker-compose up
3535
```
36+
2. Make a call
37+
```shell
38+
$ curl --location --request POST 'http://localhost:4566/restapis/<restApiId>/compose/_user_request_/somePathId' \
39+
--header 'Content-Type: application/json' \
40+
--data-raw '{
41+
"body": "{ \"name\": \"CoffeeBeans\" }"
42+
}'
43+
```
44+
The service responds
45+
```json
46+
[
47+
{
48+
"name": "CoffeeBeans",
49+
"saved": true
50+
}
51+
]
52+
```
3653

3754
#### Using `mvnw`
38-
1. Set `spring.main.web-application-type` to `servlet` for local development
39-
2. Run the following commands
55+
56+
1. Run the following commands
4057
```shell
41-
$ ./mvnw -ntp clean -Pnative -DskipTests native:compile package -pl spring-native-aws-lambda-function --settings ./settings-spring.xml
58+
$ export SPRING_PROFILES_ACTIVE=local
59+
$ ./mvnw -ntp clean -Pnative -DskipTests native:compile package -pl spring-native-aws-lambda-function
4260
$ ./spring-native-aws-lambda-function/target/spring-native-aws-lambda-function
4361
```
4462
The service starts in less than 100 ms
@@ -54,12 +72,12 @@ $ ./mvnw -ntp clean verify -U --settings ./settings-spring.xml
5472
2022-12-07 02:56:51.763 INFO 42417 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
5573
2022-12-07 02:56:51.763 INFO 42417 --- [ main] c.c.s.Application : Started Application in 0.084 seconds (JVM running for 0.087)
5674
```
57-
3. Make a call
75+
2. Make a call
5876
```shell
5977
$ curl --location --request POST 'http://localhost:8080' \
6078
--header 'Content-Type: application/json' \
6179
--data-raw '{
62-
"name": "CoffeeBeans"
80+
"body": "{ \"name\": \"CoffeeBeans\" }"
6381
}'
6482
```
6583
The service responds
@@ -339,43 +357,4 @@ Now that the setup is done you can deploy to AWS.
339357
"name": "CoffeeBeans"
340358
}'
341359
```
342-
3. Et voila! It runs with 500 ms for cold start.
343-
344-
## Maven Repository
345-
346-
This project uses experimental dependencies from Spring.
347-
348-
* One way to pul them is to add `repositories` and `pluginRepositories` elements to `pom.xml`.
349-
350-
* An alternative add them to `settings.xml` as following
351-
352-
```xml
353-
354-
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
355-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
356-
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
357-
<profiles>
358-
<profile>
359-
<id>spring-native-demo</id>
360-
<repositories>
361-
<repository>
362-
<id>spring-releases</id>
363-
<name>Spring Releases</name>
364-
<url>https://repo.spring.io/release</url>
365-
</repository>
366-
</repositories>
367-
368-
<pluginRepositories>
369-
<pluginRepository>
370-
<id>spring-releases</id>
371-
<name>Spring Releases</name>
372-
<url>https://repo.spring.io/release</url>
373-
</pluginRepository>
374-
</pluginRepositories>
375-
</profile>
376-
</profiles>
377-
<activeProfiles>
378-
<activeProfile>spring-native-demo</activeProfile>
379-
</activeProfiles>
380-
</settings>
381-
```
360+
3. Et voila! It runs with 500 ms for cold start.

docker-compose.yml

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@
1515

1616
version: "3.9"
1717
services:
18-
spring-native-aws-lambda-function:
19-
image: ghcr.io/muhamadto/spring-native-amazonlinux2-builder:20-amazonlinux2-awscliv2
20-
ports:
21-
- "8080:8080"
22-
- "5005:5005"
18+
spring-native-aws-lambda-function-infra:
19+
image: muhamadto/spring-native-amazonlinux2-builder:21-amazonlinux2-awscliv2
2320
volumes:
2421
- ./:/app
2522
- ${M2_REPO}:/home/worker/.m2
2623
working_dir: /app
2724
user: worker
2825
environment:
29-
SPRING_MAIN_WEBAPPLICATIONTYPE: servlet
3026
AWS_DEFAULT_REGION: ap-southeast-2
3127
AWS_REGION: ap-southeast-2
3228
AWS_ACCESS_KEY_ID: local
3329
AWS_SECRET_ACCESS_KEY: local
3430
AWS_ENDPOINT_URL: http://localstack:4566
31+
BUILD_ARTIFACT: 'true'
32+
FUNCTION_NAME: spring-native-aws-lambda-function
33+
STAGE: compose
3534
MAVEN_OPTS: |
3635
-DskipTests=true
3736
-Dcheckstyle.skip=true
@@ -46,23 +45,41 @@ services:
4645
- -c
4746
command: |
4847
'
48+
function package_spring_native_lambda() {
49+
if [ "$$BUILD_ARTIFACT" = "true" ]; then
50+
./mvnw -ntp clean -Pnative -DskipTests native:compile package -pl "$$FUNCTION_NAME"
51+
else
52+
print_info_message "plain" "BUILD_ARTIFACT environment variable is not set. Skipping Maven build."
53+
fi
54+
}
55+
4956
aws --version &&
5057
5158
source /usr/local/bin/awscliv2-util/aws &&
5259
53-
print_info_message "block" "Creating 'spring-native-aws-lambda-function'" &&
60+
print_info_message "block" "Creating '"$$FUNCTION_NAME"'" &&
5461
5562
print_info_message "divider" "Package GraalVM function" &&
63+
64+
package_spring_native_lambda &&
5665
57-
./mvnw -ntp clean package -U -Pnative -pl spring-native-aws-lambda-function --settings ./settings-spring.xml &&
58-
59-
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 &&
60-
lambda_wait_for_function lambda-FUNCTION &&
66+
print_info_message "divider" "Creating LAMBDA function" &&
67+
lambda_create_function "$$FUNCTION_NAME" provided.al2023 512 ./"$$FUNCTION_NAME"/target/"$$FUNCTION_NAME"-native-zip.zip "$$FUNCTION_NAME" &&
68+
lambda_wait_for_function "$$FUNCTION_NAME" &&
6169
lambda_list_functions &&
70+
LAMBDA_ARN="$(lambda_get_function_arn "$$FUNCTION_NAME")" &&
6271
63-
print_info_message "block" "Successfully creating 'spring-native-aws-lambda-function'" &&
64-
65-
exit 0
72+
print_info_message "divider" "Creating API Gateway" &&
73+
REST_API_ID="$(apigateway_create_restApi "somerestapiname")" &&
74+
RESOURCE_ID="$(apigateway_create_resource "$$REST_API_ID" "somePathId")"
75+
apigateway_create_method "$$REST_API_ID" "$$RESOURCE_ID" "POST" &&
76+
apigateway_create_lambda_integration "$$REST_API_ID" "$$RESOURCE_ID" "POST" "$$LAMBDA_ARN" "ap-southeast-2" &&
77+
apigateway_create_deployment "$$REST_API_ID" "$$STAGE" &&
78+
apigateway_list_restapis &&
79+
80+
print_info_message "plain" "Endpoint available at: http://localhost:4566/restapis/$$REST_API_ID/$$STAGE/_user_request_/somePathId" &&
81+
82+
print_info_message "block" "Successfully creating '"$$FUNCTION_NAME"'"
6683
'
6784
depends_on:
6885
- localstack

settings-spring.xml

Lines changed: 0 additions & 25 deletions
This file was deleted.

spring-native-aws-lambda-infra/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,18 @@
118118
</dependency>
119119
<!-- TEST ENDS -->
120120
</dependencies>
121+
122+
<build>
123+
<finalName>${project.artifactId}</finalName>
124+
<plugins>
125+
<plugin>
126+
<groupId>org.apache.maven.plugins</groupId>
127+
<artifactId>maven-deploy-plugin</artifactId>
128+
<version>3.1.1</version>
129+
<configuration>
130+
<skip>true</skip>
131+
</configuration>
132+
</plugin>
133+
</plugins>
134+
</build>
121135
</project>

0 commit comments

Comments
 (0)