Skip to content

Commit c82a5a8

Browse files
author
Serkan ÖZAL
authored
add ability to get handler class name through "_HANDLER" environment variable like in real AWS (#67)
1 parent b50dfd3 commit c82a5a8

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Simply add the following dependency to your `pom.xml` file:
6060
<dependency>
6161
<groupId>cloud.localstack</groupId>
6262
<artifactId>localstack-utils</artifactId>
63-
<version>0.2.13</version>
63+
<version>0.2.14</version>
6464
</dependency>
6565
```
6666

@@ -108,6 +108,7 @@ make build
108108

109109
## Change Log
110110

111+
* v0.2.14: Add ability to get handler class name through `_HANDLER` environment variable like on real **AWS Lambda** platform and **Lambci** environment
111112
* v0.2.11: Enable specification of "platform" when configuring container
112113
* v0.2.10: Add Lambda async utils for AWS SDK v2; add support for specifying bind mounts and init scripts via `@LocalstackDockerProperties`; add PowerMock integration for easy patching of AWS SDK to use local endpoints; add support for configuring the Docker image name via `@LocalstackDockerProperties`; add tests for templated emails
113114
* v0.2.8: Allow overwriting the port binding via environment variables

Diff for: pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>cloud.localstack</groupId>
55
<artifactId>localstack-utils</artifactId>
66
<packaging>jar</packaging>
7-
<version>0.2.13</version>
7+
<version>0.2.14</version>
88
<name>localstack-utils</name>
99

1010
<description>Java utilities for the LocalStack platform.</description>

Diff for: src/main/java/cloud/localstack/LambdaExecutor.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public class LambdaExecutor {
4242

4343
@SuppressWarnings("unchecked")
4444
public static void main(String[] args) throws Exception {
45-
if(args.length < 2) {
45+
if (args.length != 1 && args.length != 2) {
4646
System.err.println("Usage: java " + LambdaExecutor.class.getSimpleName() +
47-
" <lambdaClass> <recordsFilePath>");
47+
" [<lambdaClass>] <recordsFilePath>");
4848
System.exit(1);
4949
}
5050

51-
String fileContent = readFile(args[1]);
51+
String fileContent = args.length == 1 ? readFile(args[0]) : readFile(args[1]);
5252
ObjectMapper reader = new ObjectMapper();
5353
reader.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
5454
reader.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@@ -57,7 +57,17 @@ public static void main(String[] args) throws Exception {
5757
List<Map<String,Object>> records = (List<Map<String, Object>>) get(map, "Records");
5858
Object inputObject = map;
5959

60-
Object handler = getHandler(args[0]);
60+
Object handler;
61+
if (args.length == 2) {
62+
handler = getHandler(args[0]);
63+
} else {
64+
String handlerEnvVar = System.getenv("_HANDLER");
65+
if (handlerEnvVar == null) {
66+
System.err.println("Handler must be provided by '_HANDLER' environment variable");
67+
System.exit(1);
68+
}
69+
handler = getHandler(handlerEnvVar);
70+
}
6171
if (records == null) {
6272
Optional<Object> deserialisedInput = getInputObject(reader, fileContent, handler);
6373
if (deserialisedInput.isPresent()) {
@@ -135,19 +145,19 @@ private static Object getHandler(String handlerName) throws NoSuchMethodExceptio
135145

136146
public static <T> T get(Map<String,T> map, String key) {
137147
T result = map.get(key);
138-
if(result != null) {
148+
if (result != null) {
139149
return result;
140150
}
141151
key = StringUtils.uncapitalize(key);
142152
result = map.get(key);
143-
if(result != null) {
153+
if (result != null) {
144154
return result;
145155
}
146156
return map.get(key.toLowerCase());
147157
}
148158

149159
public static String readFile(String file) throws Exception {
150-
if(!file.startsWith("/")) {
160+
if (!file.startsWith("/")) {
151161
file = System.getProperty("user.dir") + "/" + file;
152162
}
153163
return Files.lines(Paths.get(file), StandardCharsets.UTF_8).collect(Collectors.joining());

0 commit comments

Comments
 (0)