2

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.

Luuklag
  • 3,897
  • 11
  • 38
  • 57
Shan
  • 45
  • 1
  • 5
  • Take a look if it helps: https://stackoverflow.com/questions/37498124/accessdeniedexception-user-is-not-authorized-to-perform-lambdainvokefunction – Felippe Duarte Mar 24 '20 at 14:32

2 Answers2

6

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.

Mark B
  • 183,023
  • 24
  • 297
  • 295
2

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'));
user3445724
  • 126
  • 1
  • 3