--Run Java programs on AWS Lambda and output system properties and environment variables
├── pom.xml
└── src
└── main
└── java
└── com
└── example
└── SystemInfo.java
SystemInfo.java
package com.example;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
public class SystemInfo implements RequestHandler<Map<String, Object>, Map<String, Object>> {
/**
*Entry point when executing AWS Lambda function(Handler method)is.
*
* @param input input data
* @param context AWS Lambda Context object
* @return output data
*/
@Override
public Map<String, Object> handleRequest(Map<String, Object> input, Context context) {
Map<String, String> time = new HashMap<String, String>();
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
time.put("jst", LocalDateTime.now(ZoneId.of("JST", ZoneId.SHORT_IDS)).format(format));
time.put("est", LocalDateTime.now(ZoneId.of("EST", ZoneId.SHORT_IDS)).format(format));
time.put("system", LocalDateTime.now(ZoneId.systemDefault()).format(format));
//Get system information
Map output = new HashMap<String, Object>();
output.put("system", System.getProperties());
output.put("env", System.getenv());
output.put("time", time);
return output;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>systeminfo</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>systeminfo</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- https://maven.apache.org/plugins/maven-shade-plugin/ -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Generate a JAR file with Maven.
$ mvn package
A JAR file called systeminfo-1.0.jar will be generated in the target directory, and you can deploy (upload) it to a function in AWS Lambda.
{
"system": {
"java.runtime.name": "OpenJDK Runtime Environment",
"sun.boot.library.path": "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre/lib/amd64",
"java.vm.version": "25.201-b09",
"java.vm.vendor": "Oracle Corporation",
"java.vendor.url": "http://java.oracle.com/",
"path.separator": ":",
"java.vm.name": "OpenJDK 64-Bit Server VM",
"file.encoding.pkg": "sun.io",
"user.country": "US",
"sun.java.launcher": "SUN_STANDARD",
"sun.os.patch.level": "unknown",
"java.vm.specification.name": "Java Virtual Machine Specification",
"user.dir": "/",
"java.runtime.version": "1.8.0_201-b09",
"java.awt.graphicsenv": "sun.awt.X11GraphicsEnvironment",
"java.endorsed.dirs": "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre/lib/endorsed",
"os.arch": "amd64",
"java.io.tmpdir": "/tmp",
"line.separator": "\n",
"java.vm.specification.vendor": "Oracle Corporation",
"os.name": "Linux",
"sun.jnu.encoding": "UTF-8",
"java.library.path": "/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib",
"java.specification.name": "Java Platform API Specification",
"java.class.version": "52.0",
"java.net.preferIPv4Stack": "true",
"sun.management.compiler": "HotSpot 64-Bit Tiered Compilers",
"os.version": "4.14.133-97.112.amzn2.x86_64",
"user.home": "/home/sbx_user1051",
"user.timezone": "UTC",
"java.awt.printerjob": "sun.print.PSPrinterJob",
"file.encoding": "UTF-8",
"java.specification.version": "1.8",
"java.class.path": "/var/runtime/lib/LambdaJavaRTEntry-1.0.jar",
"user.name": "sbx_user1051",
"java.vm.specification.version": "1.8",
"sun.java.command": "/var/runtime/lib/LambdaJavaRTEntry-1.0.jar",
"java.home": "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre",
"sun.arch.data.model": "64",
"user.language": "en",
"java.specification.vendor": "Oracle Corporation",
"awt.toolkit": "sun.awt.X11.XToolkit",
"java.vm.info": "mixed mode, sharing",
"java.version": "1.8.0_201",
"java.ext.dirs": "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre/lib/ext:/usr/java/packages/lib/ext",
"sun.boot.class.path": "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre/lib/resources.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre/lib/rt.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre/lib/jsse.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre/lib/jce.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre/lib/charsets.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre/lib/jfr.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-0.43.amzn1.x86_64/jre/classes",
"java.vendor": "Oracle Corporation",
"file.separator": "/",
"java.vendor.url.bug": "http://bugreport.sun.com/bugreport/",
"sun.io.unicode.encoding": "UnicodeLittle",
"sun.cpu.endian": "little",
"sun.cpu.isalist": ""
},
"time": {
"system": "2019/09/01 06:08:39",
"est": "2019/09/01 01:08:39",
"jst": "2019/09/01 15:08:39"
},
"env": {
"PATH": "/usr/local/bin:/usr/bin/:/bin:/opt/bin",
"_AWS_XRAY_DAEMON_ADDRESS": "XXX.XXX.XX.X",
"LAMBDA_TASK_ROOT": "/var/task",
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "512",
"TZ": ":UTC",
"AWS_SECRET_ACCESS_KEY": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"AWS_EXECUTION_ENV": "AWS_Lambda_java8",
"AWS_DEFAULT_REGION": "ap-northeast-1",
"AWS_LAMBDA_LOG_GROUP_NAME": "/aws/lambda/mySystemInfo",
"_HANDLER": "com.example.SystemInfo::handleRequest",
"LANG": "en_US.UTF-8",
"LAMBDA_RUNTIME_DIR": "/var/runtime",
"AWS_SESSION_TOKEN": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"AWS_ACCESS_KEY_ID": "XXXXXXXXXXXXXXXXXXXX",
"LD_LIBRARY_PATH": "/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib",
"_X_AMZN_TRACE_ID": "Root=X-XXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXX;Parent=XXXXXXXXXXXXXXXX;Sampled=0",
"AWS_SECRET_KEY": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"AWS_REGION": "ap-northeast-1",
"AWS_LAMBDA_LOG_STREAM_NAME": "2019/09/01/[$LATEST]XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"AWS_XRAY_DAEMON_ADDRESS": "XXX.XXX.XX.X:2000",
"_AWS_XRAY_DAEMON_PORT": "2000",
"AWS_XRAY_CONTEXT_MISSING": "LOG_ERROR",
"AWS_LAMBDA_FUNCTION_VERSION": "$LATEST",
"AWS_ACCESS_KEY": "XXXXXXXXXXXXXXXXXXXX",
"AWS_LAMBDA_FUNCTION_NAME": "mySystemInfo"
}
}
-AWS Lambda Runtime -AWS Lambda -Environment variables that can be used with Lambda functions -AWS Lambda
Recommended Posts