This is an example how to run PHP on AWS Lambda via Serverless Framework. Inspired by the Andy Raines repository. The difference between this repository and the one by Andy Raines is this one is using FastRoute and Pimple DI - a more lightweight approach.
AWS Lambda lets you run code without thinking about servers. Unfortunately, it does not support PHP yet.
- Clone this repository
- Run
composer install -o --no-dev
- Adjust name in serverless.yml
- Add a new route around line 10 handler.php
- Create an invokable class in src (App namespace)
- Create a service that matches the route defined in 1. and creates the class from point 2.
- Execute!
Example invokable class:
<?php
namespace App;
class SayHello {
public function __invoke(){
printf(json_encode([
'statusCode' => 200,
'body' => json_encode(['status' => 'Hello']),
]));
}
}
sls invoke local -f hello --log -d '{"httpMethod":"GET", "path": "/hello"}'
sls invoke -f hello --log -d '{"httpMethod":"GET", "path": "/hello"}'
Or just copy the exposed URL and use it.