Calling Laravel Routes hosted on AWS Lambda using AWS API Gateway

I am trying to host a laravel application on aws lambda and call it’s routes using aws api gateway. The application is basically a web crawler which fetches web pages from rss feeds.

When I make a call to a laravel route using the url generated by api gateway, laravel redirects the request to ‘/home’ path. However, when I test it on ec2 instance using “node debug” , it works like a charm. Below are the details of the steps I followed.

I followed a fantastic blog written by Chris White for hosting a laravel application on aws lambda.

Steps:

  1. Generating a PHP CGI Binary

    I used “amzn-ami-hvm-2016.03.3.x86_64-gp2” ec2 instance to build my php-cgi binary file

./configure –prefix=/tmp/php-7.0.11/compiled/ –without-pear
–enable-shared=no –enable-static=yes –enable-phar –enable-json –disable-all –with-openssl –with-curl –enable-libxml –enable-simplexml –enable-xml –with-mhash –with-gd –enable-exif –with-freetype-dir –enable-mbstring –enable-sockets –enable-pdo –with-pdo-mysql –enable-tokenizer –enable-session –enable-filter

  1. NodeJS Lambda function to spawn PHP CGI process – php.js file

process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];

var parser = require('http-string-parser');
var spawn = require('child_process').spawn;
exports.handler = function(event, context) {

  var requestMethod = event.httpMethod || 'GET';
  var serverName = event.headers ? event.headers.Host : '';
  var requestUri = event.path || '';
  var headers = {};

  if (event.headers) {
    Object.keys(event.headers).map(function(key) {
      headers['HTTP_' + key.toUpperCase()] = event.headers[key];
    });
  }

  var request_env = Object.assign({
    REDIRECT_STATUS: 1,
    REQUEST_METHOD: requestMethod,
    SCRIPT_FILENAME: 'api/public/index.php',
    SCRIPT_NAME: '/index.php',
    PATH_INFO: '/',
    SERVER_NAME: serverName,
    SERVER_PROTOCOL: 'HTTP/1.1',
    REQUEST_URI: requestUri
  }, headers);

  var php = spawn('./php-cgi', ['api/public/index.php'], {
    env: request_env
  });

  var response = '';

  php.stdout.on('data', function(data) {
    response += data.toString('utf-8');
  });

  php.stderr.on('data', function(data) {
    console.log("STDERR: " + data.toString());
  });

  php.on('close', function(code) {

    var parsedResponse = parser.parseResponse(response);
    context.succeed({
      isBase64Encoded: false,
      statusCode: parsedResponse.statusCode || 200,
      headers: parsedResponse.headers,
      body: parsedResponse.body
    });

  });

}
  1. Prepare AWS Lambda bundle

    The directory structure of my HelloLambda.zip bundle is

├── api

├── node_modules

├── php-cgi

└── php.js

The api folder consists of my laravel application.

  1. API Gateway – I created a api gateway for my HelloLambda function. The api has league & channel as path parameters. The GET method uses lambda proxy integration. The api shows following url in stages view

https://***.execute-api.us-east-1.amazonaws.com/prod/HelloLambda/espnheadlinesimage/{league}/{channel}

api-gateway-screenshot.jpg

  1. Make a GET request to api providing the league and channel parameter.

https://***.execute-api.us-east-1.amazonaws.com/prod/HelloLambda/espnheadlinesimage/nhl/28

When I make this get request, the browser shows a message

Redirecting to http://***.execute-api.us-east-1.amazonaws.com/home

My laravel routes.php file consists of below route

Route::get('espnheadlinesimage/{league}/{channel}', 'EspnController@headlinesimage');

Any help would be highly appreciated. Thanks in advance.


Source: stackoverflow-php