简体   繁体   English

无法在 aws lambda 中使用 curl

[英]Cannot use curl in aws lambda

I want to execute curl query from one lambda function to another.我想从一个 lambda 函数到另一个执行 curl 查询。 But I have error in postman:但我在邮递员中有错误:

{"message": "Endpoint request timed out"} {"message": "端点请求超时"}

I tried to handle this error.我试图处理这个错误。 I got this error message:我收到此错误消息:

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

PHP-FPM seems to be running already, this might be because Lambda stopped the bootstrap process but didn't leave us an opportunity to stop PHP-FPM. PHP-FPM 似乎已经在运行,这可能是因为 Lambda 停止了引导过程,但没有给我们留下停止 PHP-FPM 的机会。 Stopping PHP-FPM now to restart from a blank slate.现在停止 PHP-FPM 以从空白状态重新启动。 Fatal error: Uncaught Bref\\Runtime\\FastCgi\\FastCgiCommunicationFailed: Error communicating with PHP-FPM to read the HTTP response.致命错误:未捕获的 Bref\\Runtime\\FastCgi\\FastCgiCommunicationFailed:与 PHP-FPM 通信以读取 HTTP 响应时出错。 A root cause of this can be that the Lambda (or PHP) timed out, for example when trying to connect to a remote API or database, if this happens continuously check for those!其根本原因可能是 Lambda(或 PHP)超时,例如在尝试连接到远程 API 或数据库时,如果发生这种情况,请持续检查这些! Original exception message: hollodotme\\FastCGI\\Exceptions\\TimedoutException Read timed out in /var/task/vendor/bref/bref/src/Runtime/PhpFpm.php:126 Stack trace:原始异常消息:hollodotme\\FastCGI\\Exceptions\\TimedoutException Read timed out in /var/task/vendor/bref/bref/src/Runtime/PhpFpm.php:126 堆栈跟踪:

0 /opt/bootstrap(30): Bref\\Runtime\\PhpFpm->proxy(Array) 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)) 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)) 2 /opt/bootstrap(34): Bref\\Runtime\\LambdaRuntime->processNextEvent(Object(Closure))

This is my code:这是我的代码:


            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);

When I use guzzlehttp:当我使用 guzzlehttp 时:


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

I get only:我只得到:

Task timed out after 30.03 seconds任务在 30.03 秒后超时

The problem was in VPC.问题出在 VPC 中。

Since the lambda function was in the VPC, it could not access external resources.由于 lambda 函数在 VPC 中,因此无法访问外部资源。

The simplest solution is to create a lambda funtion without the VPC.最简单的解决方案是创建一个没有 VPC 的 lambda 函数。

The full answer can be found at this URL:完整答案可在此 URL 中找到:

AWS lambda invoke not calling another lambda function - Node.js AWS lambda 调用不调用另一个 lambda 函数 - Node.js

The anwer of johni约翰尼的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM