Skip to content

Commit d894f4c

Browse files
authored
35 create docker compose file to run the function locally (#48)
* docker-compose Signed-off-by: muhamadto <muhamadto@gmail.com>
1 parent ea04b59 commit d894f4c

File tree

12 files changed

+152
-70
lines changed

12 files changed

+152
-70
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@ $ ./mvnw -ntp clean verify -U --settings ./settings-spring.xml
2828

2929
### Locally
3030

31+
#### Using `docker-compose`
32+
1. Run the following commands
33+
```shell
34+
$ docker-compose up
35+
```
36+
37+
#### Using `mvnw`
3138
1. Set `spring.main.web-application-type` to `servlet` for local development
3239
2. Run the following commands
3340
```shell
34-
$ ./mvnw -ntp clean package -U -Pnative --settings ./settings-spring.xml
41+
$ ./mvnw -ntp clean package -U -Pnative -pl spring-native-aws-lambda-function --settings ./settings-spring.xml
3542
$ ./spring-native-aws-lambda-function/target/spring-native-aws-lambda-function
3643
```
3744
The service starts in less than 100 ms

cdk.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"app": "mvn exec:java -pl spring-native-aws-lambda-infra -Dexec.mainClass=com.coffeebeans.springnativeawslambda.infra.Application"
2+
"app": "./mvnw exec:java -pl spring-native-aws-lambda-infra -Dexec.mainClass=com.coffeebeans.springnativeawslambda.infra.Application"
33
}

docker-compose.yml

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
1+
# Licensed to Muhammad Hamadto
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# See the NOTICE file distributed with this work for additional information regarding copyright ownership.
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
116
version: '3.9'
217
services:
318
spring-native-aws-lambda-function:
4-
image: ghcr.io/muhamadto/spring-native-amazonlinux2-builder:20-amazonlinux2
19+
image: ghcr.io/muhamadto/spring-native-amazonlinux2-builder:20-amazonlinux2-awscliv2
520
ports:
6-
- 8080:8080
7-
networks:
8-
default:
9-
aliases:
10-
- spring-native-aws-lambda-function
21+
- "8080:8080"
22+
- "5005:5005"
1123
volumes:
12-
- ./:/opt/build
13-
- ${M2_REPO}:/home/ci/.m2
14-
working_dir: /opt/build
24+
- ./:/app
25+
- ${M2_REPO}:/home/worker/.m2
26+
working_dir: /app
27+
user: worker
1528
environment:
1629
SPRING_MAIN_WEBAPPLICATIONTYPE: servlet
17-
SPRING_PROFILES_ACTIVE: compose
18-
AWS_HOST: localstack
19-
AWS_ACCESS_KEY_ID: local
20-
AWS_SECRET_ACCESS_KEY: local
2130
AWS_DEFAULT_REGION: ap-southeast-2
2231
AWS_REGION: ap-southeast-2
32+
AWS_ACCESS_KEY_ID: local
33+
AWS_SECRET_ACCESS_KEY: local
2334
MAVEN_OPTS: |
2435
-DskipTests=true
2536
-Dcheckstyle.skip=true
@@ -28,39 +39,48 @@ services:
2839
-Dskip.it=true
2940
-Dmaven.javadoc.skip=true
3041
-Dmaven.source.skip=true
31-
-Dspring-boot.run.profiles=compose
42+
-Dspring-boot.run.profiles=local
3243
entrypoint:
3344
- bash
3445
- -c
35-
command: >
36-
"
37-
./mvnw -pl spring-native-aws-lambda-function clean package -Pnative -DskipTests --settings ./settings-spring.xml &&
38-
spring-native-aws-lambda-function/target/spring-native-aws-lambda-function
39-
"
46+
command: |
47+
'
48+
aws --version &&
49+
50+
source /usr/local/bin/awscliv2-util/aws &&
51+
52+
print_info_message "block" "Creating 'spring-native-aws-lambda-function'" &&
53+
54+
print_info_message "divider" "Package GraalVM function" &&
55+
56+
./mvnw -ntp clean package -U -Pnative -pl spring-native-aws-lambda-function --settings ./settings-spring.xml &&
57+
58+
lambda_create_function lambda-FUNCTION provided.al2 512 ./spring-native-aws-lambda-function/target/spring-native-aws-lambda-function-native-zip.zip spring-native-aws-lambda-function &&
59+
lambda_wait_for_function lambda-FUNCTION &&
60+
lambda_list_functions &&
61+
62+
print_info_message "block" "Successfully creating 'spring-native-aws-lambda-function'" &&
63+
64+
exit 0
65+
'
4066
depends_on:
4167
- localstack
4268

4369
localstack:
70+
container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
4471
image: localstack/localstack:latest
45-
logging:
46-
driver: none
4772
ports:
48-
- 4576:4576
49-
- ${EDGE_PORT-4566}:${EDGE_PORT-4566}
50-
- ${PORT_WEB_UI-8088}:${PORT_WEB_UI-8088}
73+
- "127.0.0.1:4566:4566" # LocalStack Gateway
74+
- "127.0.0.1:4510-4559:4510-4559" # external services port range
5175
environment:
52-
AWS_DEFAULT_REGION: ap-southeast-2
53-
AWS_ACCESS_KEY_ID: local
54-
AWS_SECRET_ACCESS_KEY: local
55-
SERVICES: sns,sqs,lambda, apigateway
56-
DEBUG: 1
57-
LS_LOG: debug # seems to give more logs than DEBUG: 1
58-
DATA_DIR: ${DATA_DIR- }
59-
PORT_WEB_UI: ${PORT_WEB_UI-8088}
60-
EDGE_PORT: ${EDGE_PORT-4566}
61-
DOCKER_HOST: unix:///var/run/docker.sock
62-
HOSTNAME_EXTERNAL: localstack
76+
- DEBUG=${DEBUG-}
77+
- DOCKER_HOST=unix:///var/run/docker.sock
78+
- LOCALSTACK_HOST=localstack
6379
volumes:
64-
# If you have access issues on Mac, consider using an alias docker-compose="TMPDIR=/private$TMPDIR /usr/local/bin/docker-compose"
65-
- /${TMPDIR:-/tmp/localstack}:/tmp/localstack
66-
- /var/run/docker.sock:/var/run/docker.sock
80+
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
81+
- "/var/run/docker.sock:/var/run/docker.sock"
82+
83+
84+
85+
86+

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@
162162
</dependency>
163163
<!-- JACKSON ENDS -->
164164

165+
<!-- AWS CDK -->
166+
<dependency>
167+
<groupId>software.amazon.awscdk</groupId>
168+
<artifactId>aws-cdk-lib</artifactId>
169+
<version>2.90.0</version>
170+
</dependency>
171+
<!-- AWS CDK ENDS -->
172+
165173
<!-- TEST -->
166174
<dependency>
167175
<groupId>cloud.pianola</groupId>

settings-spring.xml

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,7 @@
1919
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
2020
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2121
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
22-
<!-- <profiles>-->
23-
<!-- <profile>-->
24-
<!-- <id>spring</id>-->
25-
<!-- <repositories>-->
26-
<!-- <repository>-->
27-
<!-- <id>spring-releases</id>-->
28-
<!-- <name>Spring Releases</name>-->
29-
<!-- <url>https://repo.spring.io/release</url>-->
30-
<!-- </repository>-->
31-
<!-- </repositories>-->
32-
33-
<!-- <pluginRepositories>-->
34-
<!-- <pluginRepository>-->
35-
<!-- <id>spring-releases</id>-->
36-
<!-- <name>Spring Releases</name>-->
37-
<!-- <url>https://repo.spring.io/release</url>-->
38-
<!-- </pluginRepository>-->
39-
<!-- </pluginRepositories>-->
40-
<!-- </profile>-->
41-
<!-- </profiles>-->
42-
<!-- <activeProfiles>-->
43-
<!-- <activeProfile>spring</activeProfile>-->
44-
<!-- </activeProfiles>-->
22+
<activeProfiles>
23+
<activeProfile>native</activeProfile>
24+
</activeProfiles>
4525
</settings>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<lombok.version>1.18.26</lombok.version>
4646
<aws-lambda-events.version>3.11.1</aws-lambda-events.version>
4747
<aws-lambda-java-core.version>1.2.2</aws-lambda-java-core.version>
48+
<aws-lambda-java-serialization.version>1.1.2</aws-lambda-java-serialization.version>
4849
<spring-cloud.version>2022.0.1</spring-cloud.version>
4950
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
5051
<repackage.classifier/>
@@ -82,6 +83,13 @@
8283
<version>${aws-lambda-java-core.version}</version>
8384
<scope>provided</scope>
8485
</dependency>
86+
<dependency>
87+
<groupId>com.amazonaws</groupId>
88+
<artifactId>aws-lambda-java-serialization</artifactId>
89+
<version>${aws-lambda-java-serialization.version}</version>
90+
<scope>provided</scope>
91+
</dependency>
92+
8593
<!-- AWS LAMBDA ENDS -->
8694

8795
<!-- SPRING CLOUD -->

spring-native-aws-lambda-function/src/main/java/com/coffeebeans/springnativeawslambda/function/LambdaExceptionHandler.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
44
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
55
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
67
import java.util.Map;
7-
import lombok.extern.slf4j.Slf4j;
8+
import lombok.extern.log4j.Log4j2;
89
import org.springframework.web.bind.annotation.ExceptionHandler;
910
import org.springframework.web.bind.annotation.RestControllerAdvice;
1011

11-
@Slf4j
12+
@Log4j2
1213
@RestControllerAdvice
1314
public class LambdaExceptionHandler {
1415

@@ -26,6 +27,20 @@ public APIGatewayProxyResponseEvent handleException(
2627
+ " }");
2728
}
2829

30+
@ExceptionHandler(InvalidDefinitionException.class)
31+
public APIGatewayProxyResponseEvent handleException(
32+
final InvalidDefinitionException e,
33+
final APIGatewayProxyRequestEvent request) {
34+
log.error("Error processing request: {}", e.getMessage());
35+
return new APIGatewayProxyResponseEvent()
36+
.withStatusCode(400)
37+
.withHeaders(Map.of("X-Requested-Id", request.getRequestContext().getRequestId()))
38+
.withBody("{\n"
39+
+ " \"message\": \"Request body is Invalid\",\n"
40+
+ " \"errorCode\": \"INVALID_INPUT\n"
41+
+ " }");
42+
}
43+
2944
@ExceptionHandler(JsonProcessingException.class)
3045
public APIGatewayProxyResponseEvent handleException(
3146
final JsonProcessingException e,

spring-native-aws-lambda-function/src/main/java/com/coffeebeans/springnativeawslambda/function/functions/ExampleFunction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
import jakarta.validation.constraints.NotNull;
2828
import java.util.function.Function;
2929
import lombok.SneakyThrows;
30+
import lombok.extern.log4j.Log4j2;
3031
import lombok.extern.slf4j.Slf4j;
3132
import org.springframework.stereotype.Component;
3233
import org.springframework.validation.annotation.Validated;
3334

3435
@Component
35-
@Slf4j
36+
@Log4j2
3637
@Validated
3738
public class ExampleFunction implements Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
3839

spring-native-aws-lambda-function/src/main/resources/application.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# Licensed to Muhammad Hamadto
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# See the NOTICE file distributed with this work for additional information regarding copyright ownership.
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
116
spring:
217
main:
318
banner-mode: off
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<Appenders>
4+
<Console name="JsonAppender" target="SYSTEM_OUT">
5+
<JsonTemplateLayout eventTemplateUri="classpath:LambdaJsonLayout.json" />
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Logger name="JsonLogger" level="INFO" additivity="false">
10+
<AppenderRef ref="JsonAppender"/>
11+
</Logger>
12+
<Root level="info">
13+
<AppenderRef ref="JsonAppender"/>
14+
</Root>
15+
</Loggers>
16+
</Configuration>

0 commit comments

Comments
 (0)