Skip to content

Commit

Permalink
Use CDK and github actions to deploy to AWS
Browse files Browse the repository at this point in the history
Signed-off-by: matto <muhamadto@gmail.com>
  • Loading branch information
muhamadto committed Dec 29, 2023
1 parent 5568221 commit c96de34
Show file tree
Hide file tree
Showing 21 changed files with 963 additions and 1,651 deletions.
2 changes: 1 addition & 1 deletion .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
72 changes: 25 additions & 47 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,29 @@ $ ./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 '{
"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
```shell
$ ./mvnw -ntp clean package -U -Pnative -pl spring-native-aws-lambda-function --settings ./settings-spring.xml
$ ./mvnw -ntp clean package -U -Pnative -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 Down Expand Up @@ -339,43 +356,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: 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 -pl spring-native-aws-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="$(aws apigateway --endpoint-url="http://localstack:4566" create-rest-api --name 'someapigateway' --query 'id' --output text)" &&
ROOT_RESOURCE_ID=$(aws apigateway --endpoint-url="http://localstack:4566" get-resources --rest-api-id "$$REST_API_ID" --query 'items[0].id' --output text)
RESOURCE_ID=$(aws apigateway --endpoint-url="http://localstack:4566" create-resource --rest-api-id $$REST_API_ID --parent-id $$ROOT_RESOURCE_ID --path-part '{somethingPathId}' --query 'id' --output text)
aws apigateway --endpoint-url="http://localstack:4566" put-method --rest-api-id $$REST_API_ID --resource-id $$RESOURCE_ID --http-method POST --request-parameters 'method.request.path.somethingPathId=true' --authorization-type 'NONE' &&
aws apigateway --endpoint-url="http://localstack:4566" put-integration --rest-api-id $$REST_API_ID --resource-id $$RESOURCE_ID --http-method POST --type AWS_PROXY --integration-http-method POST --uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-southeast-2:000000000000:function:lambda-FUNCTION/invocations --passthrough-behavior WHEN_NO_MATCH &&
aws apigateway --endpoint-url="http://localstack:4566" create-deployment --rest-api-id $$REST_API_ID --stage-name test &&
print_info_message "plain" "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.

9 changes: 9 additions & 0 deletions spring-native-aws-lambda-function/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@
<buildArg>-H:+ReportExceptionStackTraces</buildArg>
</buildArgs>
</configuration>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
<phase>package</phase>
</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 c96de34

Please sign in to comment.