简体   繁体   English

如何从 laravel 调用现有的 AWS Lambda function?

[英]How to invoke an existing AWS Lambda function from laravel?

I am trying to invoke an existing aws-lambda function from my php code in laravel to get the data and save in my database.我正在尝试从 laravel 中的 php 代码调用现有的 aws-lambda function 以获取数据并保存在我的数据库中。 The actual command I want to invoke is the following:我要调用的实际命令如下:

aws lambda invoke --function-name expo2020-metrics --region eu-west-3 response.json

Here is the code I'm using to invoke my function:这是我用来调用我的 function 的代码:

use Aws\Lambda\LambdaClient;use Aws\Credentials\Credentials;

$credentials = new Credentials('YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY');
    $client = LambdaClient::factory(array(
        'credentials' => $credentials,
        'region' => 'eu-west-3',
        'version' => 'latest'
    ));
    $result = $client->getFunction(array(
        'FunctionName' => 'expo2020-metrics',
    ));

But I'm getting the following response:但我收到以下回复:

{"Message":"User: arn:aws:iam::677537359471:user/customer/expo2020-metrics is not authorized to perform: lambda: GetFunction on resource: arn:aws:lambda:eu-west-3:677537359471:function:expo2020-metrics"}

I'm not sure that I'm using the right code to invoke the function or not.我不确定我是否使用了正确的代码来调用 function。 I am using the PHP sdk provided by AWS.我用的是AWS提供的PHP sdk。

You are calling getFunction which just gives you information about the Lambda function.您正在调用getFunction ,它只是为您提供有关 Lambda 函数的信息。 That is not equivalent to the aws lambda invoke CLI command you are trying to translate into PHP.这不等同于您尝试转换为 PHP 的aws lambda invoke CLI 命令。

You should be calling the invoke() function of the Lambda client.您应该调用 Lambda 客户端的 invoke() 函数

Here is a sample code on Gist: https://gist.github.com/robinvdvleuten/e7259939267ad3eb1dfdc20a344cc94a .这是 Gist 上的示例代码: https://gist.github.com/robinvdvleuten/e7259939267ad3eb1dfdc20a344cc94a

require_once __DIR__.'/vendor/autoload.php';

use Aws\Lambda\LambdaClient;

$client = LambdaClient::factory([
    'version' => 'latest',
    'region'  => 'eu-west-1',
    'credentials' => [
        'key'    => 'YOUR_AWS_ACCESS_KEY_ID',
        'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
     ]
]);

$result = $client->invoke([
    'FunctionName' => 'hello-world',
]);

var_dump($result->get('Payload'));

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

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