How to load property file from classpath in AWS lambda java

Mahesh picture Mahesh · Feb 12, 2016 · Viewed 8.6k times · Source

I have written AWS lambda function in that i want to read database connection details from property file and which in my classpath, but I am not able to load that file.Here is my code:

InputStream input = DBConfiguartion.class.getResourceAsStream("appsettings");

        Reader r = new InputStreamReader(input, "UTF-8");
        Properties prop = new Properties();
        prop.load(r);

If I run this code through normal java console application that time it is working, but whenever i run it as AWS lambda function then InputStream is coming null.

Answer

bclemenzi picture bclemenzi · Mar 1, 2016

You are only one character off. Here's a working example that I have to do the same thing:

InputStream is = DBConfiguartion.class.getResourceAsStream("/lambda.properties");
Properties properties = new Properties();
properties.load(is);

This works with the following maven file structure when building the deployment jar:

  • project
  • project/src/main/java
  • project/src/main/java/com/something/DBConfiguartion.java -
  • project/src/main/resources
  • project/src/main/resources/lambda.properties