FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

First cut of java8 implementation by mhart · Pull Request #48 · lambci/docker-lambda · GitHub

This repository was archived by the owner on Jan 15, 2023. It is now read-only.
/ docker-lambda Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .gradle  (1) .jar  (1) .java  (3) .md  (3) .prefs  (3) .sh  (1) .xml  (2) No extension  (2) dotfile  (7) All 9 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 6 additions & 0 deletions .gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
node_modules
base/diff
*.pyc
.gradle
java8/run/lambda-runtime-mock/target
base/dump-java8/target
examples/java/build
examples/java/target
examples/java/bin
43 changes: 25 additions & 18 deletions README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ same library versions that exist on AWS Lambda and then deploy using
the [AWS CLI](https://aws.amazon.com/cli/).

This project consists of a set of Docker images for each of the supported Lambda runtimes
(Node.js 0.10, 4.3 and 6.10, Python 2.7 and 3.6, and Java 8\*).
(Node.js 0.10, 4.3 and 6.10, Python 2.7 and 3.6, and Java 8).

There are also a set of build images that include packages like gcc-c++, git,
zip and the aws-cli for compiling and deploying.

There's also an npm module to make it convenient to invoke from Node.js

\* NB: The Java 8 test runner is not yet complete, but the
language is installed in the images so can be manually tested

Prerequisites
-------------

Expand Down Expand Up @@ -52,10 +49,20 @@ docker run -v "$PWD":/var/task lambci/lambda:nodejs6.10
# Test a lambda_function.lambda_handler function from the current directory on Python2.7
docker run -v "$PWD":/var/task lambci/lambda:python2.7

# Test a function from the current directory on Java 8
# The directory must be laid out in the same way the Lambda zip file is,
# with top-level package source directories and a `lib` directory for third-party jars
# http://docs.aws.amazon.com/lambda/latest/dg/create-deployment-pkg-zip-java.html
# The default handler is "index.Handler", but you'll likely have your own package and class
docker run -v "$PWD":/var/task lambci/lambda:java8 org.myorg.MyHandler

# Run custom commands on the default container
docker run --entrypoint node lambci/lambda -v
```

You can see more examples of how to build docker images and run different
runtimes in the [examples](./examples) directory.

To use the build images, for compilation, deployment, etc:

```sh
Expand All @@ -65,11 +72,11 @@ docker run -v "$PWD":/var/task lambci/lambda:build
# To use a different runtime from the default Node.js v4.3
docker run -v "$PWD":/var/task lambci/lambda:build-nodejs6.10

# Run custom commands on the build container
docker run lambci/lambda:build java -version
# Run custom commands on a build container
docker run lambci/lambda:build aws --version

# To run an interactive session on the build container
docker run -it lambci/lambda:build bash
# To run an interactive session on a build container
docker run -it lambci/lambda:build-python3.6 bash
```

Using the Node.js module (`npm install docker-lambda`) – for example in tests:
Expand Down Expand Up @@ -122,14 +129,14 @@ Questions

* *Wut, how?*

By tarring the full filesystem in Lambda, uploading that to S3, and then
piping into Docker to create a new image from scratch – then creating
mock modules that will be required/included in place of the actual native
modules that communicate with the real Lambda coordinating services. Only the
native modules are mocked out – the actual parent JS/PY runner files are left
alone, so their behaviors don't need to be replicated (like the
overriding of `console.log`, and custom defined properties like
`callbackWaitsForEmptyEventLoop`)
By [tarring the full filesystem in Lambda, uploading that to S3](./base/dump-nodejs43.js),
and then [piping into Docker to create a new image from scratch](./base/create-base.sh) –
then [creating mock modules](./nodejs4.3/run/awslambda-mock.js) that will be
required/included in place of the actual native modules that communicate with
the real Lambda coordinating services. Only the native modules are mocked
out – the actual parent JS/PY/Java runner files are left alone, so their behaviors
don't need to be replicated (like the overriding of `console.log`, and custom
defined properties like `callbackWaitsForEmptyEventLoop`)

* *What's missing from the images?*

Expand All @@ -152,19 +159,19 @@ Questions
Documentation
------------

TODO

Docker tags (follow the Lambda runtime names):
- `latest` / `nodejs4.3`
- `nodejs`
- `nodejs6.10`
- `python2.7`
- `python3.6`
- `java8`
- `build` / `build-nodejs4.3`
- `build-nodejs`
- `build-nodejs6.10`
- `build-python2.7`
- `build-python3.6`
- `build-java8`

Env vars:
- `AWS_LAMBDA_FUNCTION_NAME`
Expand Down
20 changes: 20 additions & 0 deletions base/dump-java8/.classpath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions base/dump-java8/.project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>dump-java8</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
71 changes: 71 additions & 0 deletions base/dump-java8/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.lambci.lambda</groupId>
<artifactId>dump-java8</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>com.amazonaws:aws-lambda-java-events</exclude>
<exclude>com.amazonaws:aws-lambda-java-core</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.124</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.lambci.lambda;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.Map;
import java.util.Scanner;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;

public class DumpJava8 implements RequestHandler<Object, PutObjectResult> {

@Override
public PutObjectResult handleRequest(Object input, Context context) {
String filename = "java8.tgz";
String cmd = "tar -cpzf /tmp/" + filename + " --numeric-owner --ignore-failed-read /var/runtime /var/lang";
// String filename = "java8.fs.txt";
// String cmd = "find / -ls | grep -v /proc | grep -v /var/runtime | grep -v /var/task | grep -v /var/lang | sort -k11 > /tmp/" + filename;
AmazonS3 s3client = AmazonS3ClientBuilder.defaultClient();

System.out.println(ManagementFactory.getRuntimeMXBean().getInputArguments().toString());
System.out.println(System.getProperty("sun.java.command"));
System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("java.library.path"));
System.out.println(System.getProperty("user.dir"));
System.out.println(System.getProperty("user.home"));
System.out.println(System.getProperty("user.name"));
System.out.println(new File(".").getAbsolutePath());
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.println(envName + "=" + env.get(envName));
}

try {
Process process = Runtime.getRuntime().exec(new String[] { "sh", "-c", cmd });

try (Scanner stdoutScanner = new Scanner(process.getInputStream());
Scanner stderrScanner = new Scanner(process.getErrorStream())) {
// Echo all stdout first
while (stdoutScanner.hasNextLine()) {
System.out.println(stdoutScanner.nextLine());
}
// Then echo stderr
while (stderrScanner.hasNextLine()) {
System.err.println(stderrScanner.nextLine());
}
}

process.waitFor();
if (process.exitValue() != 0) {
return null;
}

System.out.println("Zipping done! Uploading...");

return s3client.putObject(new PutObjectRequest("lambci", "fs/" + filename, new File("/tmp/" + filename))
.withCannedAcl(CannedAccessControlList.PublicRead));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
20 changes: 20 additions & 0 deletions examples/java/.classpath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions examples/java/.project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>example-handler</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
5 changes: 5 additions & 0 deletions examples/java/.settings/org.eclipse.jdt.core.prefs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
14 changes: 14 additions & 0 deletions examples/java/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Java 8 docker-lambda example

This example requires [Gradle](https://gradle.org/) to be installed to build
and layout the classes and jars correctly.

Run with:

```sh
# Will place the compiled code in `./build/docker`
gradle build

# Then you can run using that directory as the task directory
docker run -v "$PWD/build/docker":/var/task lambci/lambda:java8 org.lambci.lambda.ExampleHandler '{"some": "event"}'
```
31 changes: 31 additions & 0 deletions examples/java/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apply plugin: 'java'

repositories {
mavenCentral()
}

dependencies {
compile (
'com.amazonaws:aws-lambda-java-core:1.1.0',
'com.amazonaws:aws-lambda-java-events:1.1.0'
)
}

task buildZip(type: Zip) {
from compileJava
from processResources
into('lib') {
from configurations.runtime
}
}

task buildDocker(type: Copy) {
from compileJava
from processResources
into('lib') {
from configurations.runtime
}
into 'build/docker'
}

build.dependsOn buildDocker
Loading

Back | FazBrowse Home | New Git URL