How to read a file in AWS Lambda Function written in Java ?

aws lambda java json
aws lambda multiple handlers java
aws lambda read local file python
aws lambda get query parameters java
aws lambda http request java
aws lambda java api
aws lambda java 11
aws lambda example

I have written an AWS Lambda Handler as below :

package com.lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import java.io.*;

public class TestDetailsHandler implements RequestStreamHandler {


    public void  handleRequest(InputStream input,OutputStream output,Context context){

        // Get Lambda Logger
        LambdaLogger logger = context.getLogger();

        // Receive the input from Inputstream throw exception if any

        File starting = new File(System.getProperty("user.dir"));
            System.out.println("Source Location" + starting);

           File cityFile = new File(starting + "City.db");
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(cityFile);

            System.out.println("Total file size to read (in bytes) : "
                    + fis.available());

            int content;
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                System.out.print((char) content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

}

Its read a file : City.db , available in resources folder, even I kept to everywhere see below :

But it showing following message on execution of this lambda function :

START RequestId: 5216ea47-fc43-11e5-96d5-83c1dcdad75d Version: $LATEST
Source Location/
java.io.FileNotFoundException: /city.db (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at com.lambda.TestDetailsHandler.handleRequest(TestDetailsHandler.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:511)
    at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:972)
    at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:231)
    at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:59)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:93)
END RequestId: 5216ea47-fc43-11e5-96d5-83c1dcdad75d
REPORT RequestId: 5216ea47-fc43-11e5-96d5-83c1dcdad75d  Duration: 58.02 ms  Billed Duration: 100 ms     Memory Size: 1024 MB    Max Memory Used: 50 MB  

Contents of the Pom.xml file :

<?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.lambda</groupId>
    <artifactId>testdetails</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>test-handler</name>

    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.1.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>



        </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

I have used various ways to keep file here and there , but at the end its not working. May you please let me know what is wrong here ?

However in my another project where I have kept xyz.properties file in resources folder and reading from a PropertyManager file, its working fine. When I tested it on my system its working fine, but on AWS Lambda function it doesn't work.


I have made following changes in my code and now its works perfect :

Majorly changed following two lines :

ClassLoader classLoader = getClass().getClassLoader();

File cityFile = new File(classLoader.getResource("City.db").getFile());

package com.lambda;


import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import java.io.*;

public class TestDetailsHandler implements RequestStreamHandler {


    public void  handleRequest(InputStream input,OutputStream output,Context context){

        // Get Lambda Logger
        LambdaLogger logger = context.getLogger();

        // Receive the input from Inputstream throw exception if any

        ClassLoader classLoader = getClass().getClassLoader();

        File cityFile = new File(classLoader.getResource("City.db").getFile());
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(cityFile);

            System.out.println("Total file size to read (in bytes) : "
                    + fis.available());

            int content;
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                System.out.print((char) content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

How to read a file in AWS Lambda, How to read a file in AWS Lambda Function written in Java ? Posted by: Sumit Arora. Posted on: Apr 6, 2016 11:12 PM  Go back to the DynamoDB console and select your table. Click on the Triggers tab. Click on Create trigger, and then choose Existing Lambda function. Choose the Validation function from the drop-down, select a batch size, and check Enable trigger.


This is how I did it, let's say this is how your project structure looks like -

And you want to read the file config.properties which is inside the project-dir/resources directory.

The code for reading the content of the file would be -

InputStream input = null;
try {
    Path path = Paths.get(PropertyUtility.class.getResource("/").toURI());

    // The path for config file in Lambda Instance -
    String resourceLoc = path + "/resources/config.properties";

    input = new FileInputStream(resourceLoc);
} catch(Exception e) {
    // Do whatever
}

If you are following this project structure and using this code, then it will work in AWS Lambda.

PropertyUtility is just a utility class that I have created to read the contents of the config file. The PropertyUtility class looks like this -

As you can see in the above code, the path of the config file is different in the local system and in Lambda Instance.

In your local machine, PropertyUtility.class.getResource("/") points to bin, that is why you have to do path.getParent(), to point it to the project-directory which is HelloLambda in this example.

For the Lambda Instance, PropertyUtility.class.getResource("/") points directly to the project-directory.

Tutorial: How to Create, Upload, and Invoke an AWS Lambda Function, Your Lambda function's handler is the method in your function code that processes events. You deserialize the input and write output to an output stream. The following example uses buffered reader and writer types to work with the input the Java Class Library (JCL) to create thumbnails from uploaded image files. Java code below reads the contents of the text file you want to read from S3 bucket, scans the file line-by-line and then writes it to another text file before uploading it to same or another S3 bucket using AWS Lambda function. You can use any Java IDE to write Lambda function.


If the file in located under resources directory, then the following solution should work:

String fileName = "resources/config.json";

Path path = Paths.get(this.getClass().getResource("/").toURI());
Path resourceLocation = path.resolve(fileName);

try(InputStream configStream = Files.newInputStream(resourceLocation)) {
            //use your file stream as you need.
}

Here the most important part is "resources/config.json", it must not be "/resources/config.json", because the file location is /var/task/resources/config.json in lambda, I checked.

Hope this helps who still face problem in reading file in aws lambda.

AWS Lambda function handler in Java, In the serverless world, AWS Lambda and Amazon API Gateway are the HTTP to take an application written in Java with frameworks such as Spring, Spring Boot, The proxyStream method takes care of reading our input stream into and The pom file also uses the Maven Shade plugin to generate an  AWS Lambda works by running your code on a high-availability compute infrastructure and performs all the administration of the compute resources, which includes: Server and operating system maintenance. Capacity provisioning and automatic scaling. Code and security patch deployment. Code monitoring and logging.


If the file is located under resources folder, you can use it directly in lambda by using something like the following code:

final BufferedReader br = new BufferedReader(new FileReader("/flows/cancellation/MessageArray.json"));

I wanted to read a json file, you can have different use case, but the code works.

Running APIs Written in Java on AWS Lambda, Best practices for using AWS Lambda. For example, if you are writing to an Amazon S3 bucket, instead of hard-coding the bucket For functions authored in Java or . packages authored in Java by putting your dependency .jar files in a separate Before invoking the function, Lambda continues to read records from the  With this interface, Lambda passes your handler an input stream and output stream. The handler reads bytes from the input stream, writes to the output stream, and returns void. The following example uses buffered reader and writer types to work with the input and output streams.


Best Practices for Working with AWS Lambda Functions, The deployment package is a .zip file containing your Lambda function code and The following is example Java code that reads incoming Amazon S3 events S3 destination bucket System.out.println("Writing to: " + dstBucket + "/" + dstKey);​  AWS Lambda is a serverless computing service provided by Amazon to reduce the configuration of servers, OS, Scalability, etc. AWS Lambda is capable of executing code on AWS Cloud. It runs in response to events on different AWS resources, which triggers AWS Lambda functions.


Sample Amazon S3 function code, How to Write AWS Lambda Function with Java 8 from an SQS queue to a Lambda function, or a change in the state of a file within S3. You are responsible for understanding what charges you may incur by using AWS. Suppose you want to create a thumbnail for each image file that is uploaded to a bucket. You can create a Lambda function ( CreateThumbnail ) that Amazon S3 can invoke when objects are created. Then, the Lambda function can read the image object from the source bucket and create a thumbnail image target bucket.


AWS Lambda in Java: Examples & Instructions, on the AWS Lambda FaaS platform to support receiving, processing, and/or creating files. Additionally Create a new ProcessCSV Lambda function to read a file from S3. 3. The next step is to write Java code to generate the S3 bucket file. Let us see these steps with the help of an example which shows the basic interaction between Amazon S3 and AWS Lambda. User will upload a file in Amazon S3 bucket. Once the file is uploaded, it will trigger AWS Lambda function in the background which will display an output in the form of a console message that the file is uploaded.