stackery / php-lambda-layer

PHP Runtime Layer for AWS Lambda
Other
321 stars 71 forks source link

Event is not an API Gateway request #18

Closed umutc closed 5 years ago

umutc commented 5 years ago

Hello there,

I wanna use this layer to poling SQS mail messages in queue and send them via Simple Mail Service. But i am getting this 500 error on lamda function when test or trigger sos.

Response

{
  "statusCode": 500,
  "body": "Event is not an API Gateway request"
}

Template

AWSTemplateFormatVersion: 2010-09-09
Description: Magistum Serverless
Transform: AWS::Serverless-2016-10-31
Resources:
  MailPoller:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub ${AWS::StackName}-mail-poller
      Description: This function reads the SQS mail queue and send them via AWS Simple Mail Service.
      CodeUri: src/php
      Runtime: provided
      Handler: index.php
      MemorySize: 512
      Timeout: 900
      Tracing: Active
      Layers:
        - !Sub arn:aws:lambda:${AWS::Region}:887080169480:layer:php71:5
      Policies:
        - SQSPollerPolicy:
            QueueName: Magistum-mail-queue
      Environment:
        Variables:
          queueUrl: https://sqs.eu-central-1.amazonaws.com/303814004728/Magistum-mail-queue

Code PHP

require 'vendor/autoload.php';

use Aws\Sqs\SqsClient;
use Aws\Exception\AwsException;

$queueName = "Magistum-mail-queue";

$client = new SqsClient([
    'profile' => 'default',
    'region' => 'eu-central-1',
    'version' => '2012-11-05'
]);

try {
    $result = $client->createQueue(array(
        'QueueName' => $queueName,
        'Attributes' => array(
            'ReceiveMessageWaitTimeSeconds' => 20
        ),
    ));
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}

Thanks...

txase commented 5 years ago

Hi @umutc,

PHP is a general purpose programming language, but it also is executed differently in different contexts. Different aspects of the runtime environment are available when you execute a script in response to a web request vs when you execute a script from the command line.

This project aims to support invocations specifically from AWS API Gateway or Application Load Balancer. It executes PHP scripts with the semantics appropriate for handling HTTP requests.

The real solution to your issue would be a different PHP Lambda Layer that behaves differently.