【问题标题】:Cannot use curl in aws lambda无法在 aws lambda 中使用 curl
【发布时间】:2020-04-08 18:52:25
【问题描述】:

我想从一个 lambda 函数到另一个执行 curl 查询。但是邮递员有错误:

{"message": "端点请求超时"}

我试图处理这个错误。我收到此错误消息:

2019-12-16T09:59:27.830Z 4189c32a-1e9c-4898-a59e-6149c49eaab2 任务在 30.03 秒后超时

PHP-FPM 似乎已经在运行,这可能是因为 Lambda 停止了引导过程,但没有给我们留下停止 PHP-FPM 的机会。现在停止 PHP-FPM 以从空白状态重新开始。 致命错误:未捕获的 Bref\Runtime\FastCgi\FastCgiCommunicationFailed:与 PHP-FPM 通信以读取 HTTP 响应时出错。根本原因可能是 Lambda(或 PHP)超时,例如在尝试连接到远程 API 或数据库时,如果发生这种情况,请不断检查!原始异常消息:hollodotme\FastCGI\Exceptions\TimedoutException 在 /var/task/vendor/bref/bref/src/Runtime/PhpFpm.php:126 中读取超时 堆栈跟踪:

0 /opt/bootstrap(30): Bref\Runtime\PhpFpm->proxy(Array)

1 /var/task/vendor/bref/bref/src/Runtime/LambdaRuntime.php(92): {closure}(Array, Object(Bref\Context\Context))

2 /opt/bootstrap(34): Bref\Runtime\LambdaRuntime->processNextEvent(Object(Closure))

这是我的代码:


            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => false,    // don't return headers
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 500,      // timeout on connect
            CURLOPT_TIMEOUT        => 500,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
            CURLOPT_USERAGENT      => "",
            CURLOPT_COOKIESESSION  => false,
//            CURLOPT_COOKIEJAR     => $this->ckfile, // Cookies
//            CURLOPT_COOKIEFILE     => $this->ckfile, //Cookies...yum


        );

        //Go go go!
        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );

        $output['content'] = curl_exec( $ch );
        $output['err']     = curl_errno( $ch );
        $output['errmsg']  = curl_error( $ch );
        $output['header']  = curl_getinfo( $ch );
        dd($output);

当我使用 guzzlehttp:


  $client = new \GuzzleHttp\Client();
            $request = $client->get($url);
            $response = $request->getBody();

我只得到:

任务在 30.03 秒后超时

【问题讨论】:

  • 您能否添加有关函数本身的更多信息并查看以下内容 - stackoverflow.com/questions/53898894/…
  • 我查阅了一些资料,发现 Lambda 无法连接到 VPC 之外的任何东西。我认为这是 api 网关的问题。我找到了可以在其中创建 VCP 链接的选项卡,这些链接提供对 Amazon Virtual Private Cloud (VPC) 中 HTTP(S) 资源的访问。但现在它不起作用:)

标签: php amazon-web-services curl aws-lambda


【解决方案1】:

问题出在 VPC 中。

由于 lambda 函数位于 VPC 中,因此无法访问外部资源。

最简单的解决方案是在没有 VPC 的情况下创建 lambda 函数。

完整的答案可以在这个网址找到:

AWS lambda invoke not calling another lambda function - Node.js

约翰尼的回答

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    相关资源
    最近更新 更多