You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用AWS Lambda函数遭遇SDK读取超时问题求助

问题:AWS Lambda调用超时(Read timed out)但函数实际执行成功

报错信息

Error: java.util.concurrent.ExecutionException: com.amazonaws.SdkClientException: Unable to execute HTTP request: Read timed out The functions ends succesfully and this time it took 11 minutes and 14.607 seconds to execute

相关代码

AWS客户端配置代码

public void configureLambdaClient() throws Exception{
        try {
            ClientConfiguration clientConfiguration = new ClientConfiguration();
            clientConfiguration.setSocketTimeout(900000);
            clientConfiguration.setConnectionTimeout(5000);
            clientConfiguration.setMaxErrorRetry(0);
            clientConfiguration.setRequestTimeout(1200000);
            clientConfiguration.setClientExecutionTimeout(1200000);
            log.debug("Get socket timeout: " + clientConfiguration.getSocketTimeout());
            log.debug("Max error retries: " + clientConfiguration.getMaxErrorRetry());

            this.awsLambda = AWSLambdaClientBuilder.standard()
                    .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                    .withRegion(Regions.EU_WEST_1).withClientConfiguration(clientConfiguration).build();
        }catch(Exception e){
            throw new Exception();
        }
    }

异步调用Lambda代码

public CompletableFuture<String> executeAsyncFunction() throws Exception {
        long start = System.currentTimeMillis();
        this.configureLambdaClient(); //Resets client configuration
        return CompletableFuture.supplyAsync(() -> {
            try {
                log.debug("Creating " + function + " Invoke request.");

                InvokeRequest invokeRequest = new InvokeRequest()
                        .withFunctionName(function).
                        withPayload(jsonPayload).
                        withLogType("Tail").
                        withInvocationType("RequestResponse");
                InvokeResult invokeResult = null;

                log.debug("Invoking " + function);

                invokeResult = this.awsLambda.invoke(invokeRequest);
                return new String(invokeResult.getPayload().array(), StandardCharsets.UTF_8);
            } catch (Exception e) {
                throw new CompletionException("Error: Couldn't execute the function, please contact the developers, ", e);
            } finally {
                long finish = System.currentTimeMillis();
                long timeElapsed = finish - start;
                log.debug("TIME OF EXECUTION: " + timeElapsed);
            }
        });
    }

结果获取代码

public String executeFunction() throws Exception {
        CompletableFuture<String> execution = executeAsyncFunction();
        Thread.sleep(FIVE_SECONDS);
        while (true) {
            if (execution.isDone()) {
                log.debug("Function done, retrieving answer");
                return (String) execution.get();
            }
            log.debug("Function isn't done yet, waiting a minute for a response");
            Thread.sleep(MINUTE);
        }
    }

环境说明

  • AWS端Lambda函数超时时间已设为15分钟
  • 执行时长小于10分钟的函数无此问题,仅当机器学习模型数据量增加导致函数执行时长超过10分钟后触发该错误

解决建议

  1. 确认客户端超时配置生效:代码中设置了15分钟的SocketTimeout,需在调用前再次打印实际生效的超时参数,排查是否存在SDK配置覆盖或加载失败的情况。
  2. 排查网络中间件超时限制:若部署环境存在代理、负载均衡等中间件,需同步调整这些组件的超时时间至15分钟以上,避免中间件提前断开长连接。
  3. 切换至真正的异步调用模式:当前使用的RequestResponse仍为同步等待模式,改为InvocationType=Event异步调用,客户端无需等待响应,通过SQS、SNS或CloudWatch Events接收执行完成通知,从根源避免长连接超时。
  4. 优化Lambda执行效率:针对机器学习场景,优化模型加载逻辑(如使用层共享模型)、采用分批数据处理、更换高效序列化方式,缩短函数执行时长。
  5. 升级AWS SDK版本:旧版本SDK可能存在长连接超时的已知bug,升级至最新版Lambda SDK,确认是否修复相关问题。
  6. 检测网络稳定性:长连接场景下网络波动易触发超时,测试客户端与Lambda所在区域的网络连通性,排查是否存在间歇性丢包或高延迟问题。

内容的提问来源于stack exchange,提问作者Yoru no kir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 10:48:14