Examine the system information of AWS Lambda operating environment in Java

Overview

--Run Java programs on AWS Lambda and output system properties and environment variables

Source code

File list

├── 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

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.

Example of execution result on AWS Lambda + Java 8

{
  "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"
  }
}

Reference material

-AWS Lambda Runtime -AWS Lambda -Environment variables that can be used with Lambda functions -AWS Lambda

Recommended Posts

Examine the system information of AWS Lambda operating environment in Java
Find out the list of fonts available in AWS Lambda + Java
[Java] Get the file in the jar regardless of the environment
The origin of Java lambda expressions
Examine the list of timezone IDs available in the Java ZoneId class
Get the result of POST in Java
Examine the memory usage of Java elements
The story of writing Java in Emacs
Easily monitor the indoor environment-⑥ Acquire operation information of industrial automation equipment in Java (OPC-UA / Eclipse Milo)-
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the description order of Java system properties
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
Feel the passage of time even in Java
Library "OSHI" to get system information in Java
Import files of the same hierarchy in Java
Replacing system environment variables by reflection in java
Code that deletes all files of the specified prefix in AWS S3 (Java)
Validate the identity token of a user authenticated with AWS Cognito in Java
Prepare the execution environment of Tomcat in IntelliJ Community
[For beginners] Quickly understand the basics of Java 8 Lambda
[Java] Integer information of characters in a text file acquired by the read () method
Use Java lambda expressions outside of the Stream API
Examine the boundaries of the "32GB memory wall" in Elasticsearch
Change the storage quality of JPEG images in Java
SSL in the local environment of Docker / Rails / puma
Differences in code when using the length system in Java
First AWS Lambda (I tried to see what kind of environment it works in)
Summarize the additional elements of the Optional class in Java 9
Was done in the base year of the Java calendar week
A quick explanation of the five types of static in Java
Install by specifying the version of Django in the Docker environment
Setting the baseURL in the axios module of Docker environment Nuxt
Create a SlackBot with AWS lambda & API Gateway in Java
[Java] To know the type information of type parameters at runtime
Count the number of digits after the decimal point in Java
Implement the same function as C, C ++ system ("cls"); in Java
How to derive the last day of the month in Java
Get EXIF information in Java
JavaFX environment construction in Java 13
Implementation of gzip in java
Implementation of tri-tree in Java
Easily create virtual S3 and test the integration between AWS Lambda and S3 services in your local environment
Graph the sensor information of Raspberry Pi in Java and check it with a web browser
[Java] Get date information 10 days later using milliseconds in the Date class
Install gem in Serverless Framework and AWS Lambda with Ruby environment
[Java] Get the dates of the past Monday and Sunday in order
Output the difference between each field of two objects in Java
I tried the input / output type of Java Lambda ~ Map edition ~
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Get the public URL of a private Flickr file in Java
Let's create a TODO application in Java 5 Switch the display of TODO
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
Access the network interface in Java
[Java] Delete the elements of List
Guess the character code in Java
Exceptions encountered in the AWS SDK