Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase the timeout for AWS Lambda client

I am trying to scan a file using AWS Lambda, and I am getting timeout since the scan function is taking longer than expected.

I would like to increase the timeout for my client since this process is @Async and I can handle few more seconds.

This is my method:

   @Override
   @Async
   public void scanFile( String s3Bucket, String fileName, String path, String documentId, String createdBy ) throws IOException {

      FileScanInput input = new FileScanInput();
      input.setS3Bucket( s3Bucket );
      input.setS3Key( path );

      logger.debug( "Scan file: " + path + ", in S3 bucket:  " + s3Bucket );
      if ( fileScanService == null ) {
         fileScanService = buildFileScanService();
      }

      FileScanOutput fileScanOutput = fileScanService.scanFile( input );
//      TODO: if the scan process failed, ask what should be the next step.
//      for now, the file stays in S3.
      Assert.notNull( fileScanOutput );
      String status = fileScanOutput.getStatus();

      // in case the document owner was not found an infected file was file. Email approved emails
      if ( status.equalsIgnoreCase( VIRUS_SCAN_INFECTED ) ) {
         // delete file on S3
         this.deleteFile( s3Bucket, path );
         String toEmail = personInfoMapper.findStudentEmail( createdBy );
         try {
            sendVirusDetectedEmail( fileName, toEmail );
         }
         catch ( Exception e ) {
            logger.error( e.getMessage() );
         }

         //         we clean up the metadata table in case there was a virus since this is running async.
         metadataDao.cleanUpMetadata( documentId );

         logger.error( "The file is infected." );
         throw new VirusFoundException( "File is infected." );
      }
   }


   public final FileScanService buildFileScanService() {
      return LambdaInvokerFactory.builder().lambdaClient( AWSLambdaClientBuilder.defaultClient() ).build( FileScanService.class );
   }

And this is the resource configs for my Lambda function. enter image description here

Update I also noticed that my Lambda function actually does its job, which means the issue is basically in this line FileScanOutput fileScanOutput = fileScanService.scanFile( input );

fileScanOutput doesn't get initialized instead I get timeout issue.

My other classes look like:

public interface FileScanService {

   @LambdaFunction( functionName = "s3-antivirus-api-scan" )
   public FileScanOutput scanFile( FileScanInput fileScanInput );
}

public class FileScanInput {

   private String s3Bucket;
   private String s3Key;

   public String getS3Bucket() {
      return s3Bucket;
   }

   public void setS3Bucket( String value ) {
      s3Bucket = value;
   }

   public String getS3Key() {
      return s3Key;
   }

   public void setS3Key( String value ) {
      s3Key = value;
   }
}



public class FileScanOutput {

   private String status;

   public FileScanOutput() {
   }

   public FileScanOutput( String status ) {
      this.status = status;
   }

   public String getStatus() {
      return status;
   }

   public void setStatus( String value ) {
      status = value;
   }
}
like image 844
Awad Avatar asked Apr 16 '26 17:04

Awad


1 Answers

When you say your client is timing out, do you mean your Lambda SDK client? If so, you may need to pass a longer socket timeout when creating your client:

AWSLambdaClientBuilder.standard()
  .withClientConfiguration(new ClientConfiguration()
    .withSocketTimeout(SOCKET_TIMEOUT_IN_MS))
  .build();

The default socket timeout is 50 seconds: https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/ClientConfiguration.java#L43

Your Lambda function itself will continue running regardless of whether the socket is closed on the client side, which is likely why you see the function completing the job.

like image 135
pbeardshear Avatar answered Apr 19 '26 09:04

pbeardshear



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!