Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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:

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. I am using the PHP sdk provided by AWS.

like image 606
Shan Avatar asked Nov 26 '25 05:11

Shan


2 Answers

You are calling getFunction which just gives you information about the Lambda function. That is not equivalent to the aws lambda invoke CLI command you are trying to translate into PHP.

You should be calling the invoke() function of the Lambda client.

like image 193
Mark B Avatar answered Nov 27 '25 18:11

Mark B


Here is a sample code on 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'));
like image 20
user3445724 Avatar answered Nov 27 '25 19:11

user3445724



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!