Example code for AWS Lambda and the Serverless framework using FastRoute and Pimple DI
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitattributes
.gitignore
README.md
buildphp.sh
composer.json
dockerfile.buildphp
handler.js
handler.php
php
serverless.yml

README.md

Serverless-PHP

serverless language license

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.

Installation

Prerequisites

Setup

  1. Clone this repository
  2. Run composer install -o --no-dev
  3. Adjust name in serverless.yml

Usage

  1. Add a new route around line 10 handler.php
  2. Create an invokable class in src (App namespace)
  3. Create a service that matches the route defined in 1. and creates the class from point 2.
  4. Execute!

Example invokable class:

<?php

namespace App;

class SayHello {
    public function __invoke(){
        printf(json_encode([
            'statusCode' => 200,
            'body' => json_encode(['status' => 'Hello']),
        ]));
    }
}

Running

Locally

sls invoke local -f hello --log -d '{"httpMethod":"GET", "path": "/hello"}'

AWS

sls invoke -f hello --log -d '{"httpMethod":"GET", "path": "/hello"}'

Or just copy the exposed URL and use it.