--Exécuter des programmes Java sur AWS Lambda et afficher les propriétés du système et les variables d'environnement
├── 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>> {
/**
*Point d'entrée lors de l'exécution de la fonction AWS Lambda(Méthode du gestionnaire)est.
*
* @param input Données d'entrée
* @param context Objet de contexte AWS Lambda
* @renvoyer les données de sortie
*/
@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));
//Obtenir des informations système
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>
Générez un fichier JAR dans Maven.
$ mvn package
Un fichier JAR appelé systeminfo-1.0.jar sera généré dans le répertoire cible. Déployez-le (téléchargez-le) dans la fonction 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"
}
}
Recommended Posts