Skip to content

Commit 635b218

Browse files
committed
Created handler for aws lambda workflow requests.
0 parents  commit 635b218

File tree

6 files changed

+394
-0
lines changed

6 files changed

+394
-0
lines changed

.gitignore

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Maven template
3+
target/
4+
pom.xml.tag
5+
pom.xml.releaseBackup
6+
pom.xml.versionsBackup
7+
pom.xml.next
8+
release.properties
9+
dependency-reduced-pom.xml
10+
buildNumber.properties
11+
.mvn/timing.properties
12+
.mvn/wrapper/maven-wrapper.jar
13+
### Java template
14+
# Compiled class file
15+
*.class
16+
17+
# Log file
18+
*.log
19+
20+
# BlueJ files
21+
*.ctxt
22+
23+
# Mobile Tools for Java (J2ME)
24+
.mtj.tmp/
25+
26+
# Package Files #
27+
*.jar
28+
*.war
29+
*.nar
30+
*.ear
31+
*.zip
32+
*.tar.gz
33+
*.rar
34+
35+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
36+
hs_err_pid*
37+
### JetBrains template
38+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
39+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
40+
41+
# User-specific stuff
42+
.idea/**/workspace.xml
43+
.idea/**/tasks.xml
44+
.idea/**/usage.statistics.xml
45+
.idea/**/dictionaries
46+
.idea/**/shelf
47+
48+
# Sensitive or high-churn files
49+
.idea/**/dataSources/
50+
.idea/**/dataSources.ids
51+
.idea/**/dataSources.local.xml
52+
.idea/**/sqlDataSources.xml
53+
.idea/**/dynamic.xml
54+
.idea/**/uiDesigner.xml
55+
.idea/**/dbnavigator.xml
56+
57+
# Gradle
58+
.idea/**/gradle.xml
59+
.idea/**/libraries
60+
61+
# Gradle and Maven with auto-import
62+
# When using Gradle or Maven with auto-import, you should exclude module files,
63+
# since they will be recreated, and may cause churn. Uncomment if using
64+
# auto-import.
65+
# .idea/modules.xml
66+
# .idea/*.iml
67+
# .idea/modules
68+
69+
# CMake
70+
cmake-build-*/
71+
72+
# Mongo Explorer plugin
73+
.idea/**/mongoSettings.xml
74+
75+
# File-based project format
76+
*.iws
77+
78+
# IntelliJ
79+
out/
80+
81+
# mpeltonen/sbt-idea plugin
82+
.idea_modules/
83+
84+
# JIRA plugin
85+
atlassian-ide-plugin.xml
86+
87+
# Cursive Clojure plugin
88+
.idea/replstate.xml
89+
90+
# Crashlytics plugin (for Android Studio and IntelliJ)
91+
com_crashlytics_export_strings.xml
92+
crashlytics.properties
93+
crashlytics-build.properties
94+
fabric.properties
95+
96+
# Editor-based Rest Client
97+
.idea/httpRequests
98+
99+
.gitignore
100+
.idea/
101+
Handler.iml
102+
src/test/

pom.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>hyperflow</groupId>
8+
<artifactId>workflow-java-handler</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<dependencies>
11+
<dependency>
12+
<groupId>com.amazonaws</groupId>
13+
<artifactId>aws-lambda-java-core</artifactId>
14+
<version>1.2.0</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>com.amazonaws</groupId>
18+
<artifactId>aws-java-sdk-s3</artifactId>
19+
<version>1.11.409</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.apache.commons</groupId>
23+
<artifactId>commons-lang3</artifactId>
24+
<version>3.8</version>
25+
</dependency>
26+
</dependencies>
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-shade-plugin</artifactId>
32+
<version>3.2.0</version>
33+
<configuration>
34+
<createDependencyReducedPom>false</createDependencyReducedPom>
35+
</configuration>
36+
<executions>
37+
<execution>
38+
<phase>package</phase>
39+
<goals>
40+
<goal>shade</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-compiler-plugin</artifactId>
48+
<configuration>
49+
<source>8</source>
50+
<target>8</target>
51+
</configuration>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
</project>

src/main/java/Handler.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import com.amazonaws.services.lambda.runtime.Context;
2+
import com.amazonaws.services.lambda.runtime.LambdaLogger;
3+
import com.amazonaws.services.s3.model.S3Object;
4+
import com.amazonaws.util.IOUtils;
5+
6+
import java.time.Duration;
7+
import java.time.Instant;
8+
import java.util.Map;
9+
10+
public class Handler {
11+
12+
private static final String FOLDER_PATH = "/tmp/";
13+
private static final String BUCKET_KEY = "bucket";
14+
15+
public long handleRequest(Request request, Context context) throws Exception {
16+
17+
Instant instant1 = Instant.now();
18+
19+
LambdaLogger logger = context.getLogger();
20+
21+
logger.log("executable: " + request.getExecutable());
22+
logger.log("args: " + request.getArgs());
23+
logger.log("inputs: " + request.getInputs());
24+
logger.log("outputs: " + request.getOutputs());
25+
logger.log("bucket: " + request.getOptions().get(BUCKET_KEY));
26+
logger.log("prefix: " + request.getOptions().get("prefix"));
27+
28+
downloadData(request, logger);
29+
execute(request.getExecutable(), String.join(" ", request.getArgs()), logger);
30+
uploadData(request, logger);
31+
32+
Instant instant2 = Instant.now();
33+
34+
return Duration.between(instant1, instant2).getSeconds();
35+
}
36+
37+
private void downloadData(Request request, LambdaLogger logger) throws Exception {
38+
for (Map<String, Object> input : request.getInputs()) {
39+
String fileName = input.get("name").toString();
40+
String key = request.getOptions().get("prefix") + "/" + fileName;
41+
logger.log("Downloading " + request.getOptions().get(BUCKET_KEY) + "/" + key);
42+
S3Object s3Object = S3Utils.getObject(request.getOptions().get(BUCKET_KEY), key);
43+
S3Utils.saveToFile(s3Object, FOLDER_PATH + fileName);
44+
}
45+
}
46+
47+
private void execute(String executable, String args, LambdaLogger logger) throws Exception {
48+
logger.log("Executing " + executable);
49+
50+
Process chmod = Runtime.getRuntime().exec("chmod -R 755 " + FOLDER_PATH);
51+
chmod.waitFor();
52+
Process process = Runtime.getRuntime().exec(FOLDER_PATH + executable + " " + args);
53+
process.waitFor();
54+
55+
String outputMsg = IOUtils.toString(process.getInputStream());
56+
String errorMsg = IOUtils.toString(process.getErrorStream());
57+
58+
logger.log("Stdout: " + outputMsg);
59+
logger.log("Stderr: " + errorMsg);
60+
}
61+
62+
private void uploadData(Request request, LambdaLogger logger) {
63+
for (Map<String, String> input : request.getOutputs()) {
64+
String fileName = input.get("name");
65+
String filePath = FOLDER_PATH + fileName;
66+
//String key = request.getOptions().get("prefix") + "/" + fileName;
67+
String key = "test" + "/" + fileName;
68+
logger.log("Uploading " + request.getOptions().get(BUCKET_KEY) + "/" + key);
69+
S3Utils.putObject(request.getOptions().get(BUCKET_KEY), key, filePath);
70+
}
71+
}
72+
73+
}

src/main/java/Request.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.util.List;
2+
import java.util.Map;
3+
4+
public class Request {
5+
6+
private String executable;
7+
private List<String> args;
8+
private Map<String, String> env;
9+
private List<Map<String, Object>> inputs;
10+
private List<Map<String, String>> outputs;
11+
private Map<String, String> options;
12+
13+
public String getExecutable() {
14+
return executable;
15+
}
16+
17+
public void setExecutable(String executable) {
18+
this.executable = executable;
19+
}
20+
21+
public List<String> getArgs() {
22+
return args;
23+
}
24+
25+
public void setArgs(List<String> args) {
26+
this.args = args;
27+
}
28+
29+
public Map<String, String> getEnv() {
30+
return env;
31+
}
32+
33+
public void setEnv(Map<String, String> env) {
34+
this.env = env;
35+
}
36+
37+
public List<Map<String, Object>> getInputs() {
38+
return inputs;
39+
}
40+
41+
public void setInputs(List<Map<String, Object>> inputs) {
42+
this.inputs = inputs;
43+
}
44+
45+
public List<Map<String, String>> getOutputs() {
46+
return outputs;
47+
}
48+
49+
public void setOutputs(List<Map<String, String>> outputs) {
50+
this.outputs = outputs;
51+
}
52+
53+
public Map<String, String> getOptions() {
54+
return options;
55+
}
56+
57+
public void setOptions(Map<String, String> options) {
58+
this.options = options;
59+
}
60+
}

src/main/java/S3Utils.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import com.amazonaws.services.s3.AmazonS3;
2+
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
3+
import com.amazonaws.services.s3.model.S3Object;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import java.nio.file.StandardCopyOption;
11+
import java.util.Objects;
12+
import java.util.logging.Level;
13+
import java.util.logging.Logger;
14+
15+
public class S3Utils {
16+
17+
private static final Logger logger = Logger.getGlobal();
18+
private static final AmazonS3 S3 = AmazonS3ClientBuilder.defaultClient();
19+
20+
private S3Utils() {
21+
}
22+
23+
public static S3Object getObject(String bucketName, String key) {
24+
S3Object s3Object;
25+
try {
26+
s3Object = S3.getObject(bucketName, key);
27+
} catch (Exception e) {
28+
logger.log(Level.SEVERE, "Error during downloading " + bucketName + "/" + key);
29+
throw e;
30+
}
31+
return s3Object;
32+
}
33+
34+
public static void putObject(String bucketName, String key, String filePath) {
35+
File file = new File(filePath);
36+
try {
37+
S3.putObject(bucketName, key, file);
38+
} catch (Exception e) {
39+
logger.log(Level.SEVERE, "Error during uploading " + filePath + "into" + bucketName + "/" + key);
40+
throw e;
41+
}
42+
}
43+
44+
public static void saveToFile(S3Object s3Object, String fileName) throws IOException {
45+
try (InputStream in = s3Object.getObjectContent()) {
46+
Objects.requireNonNull(s3Object);
47+
Files.copy(in, Paths.get(fileName), StandardCopyOption.REPLACE_EXISTING);
48+
} catch (Exception e) {
49+
logger.log(Level.SEVERE, "Error during saving to file: " + fileName);
50+
throw e;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)