Skip to content

Example code for AWS Lambda and the Serverless framework using FastRoute and Pimple DI

Notifications You must be signed in to change notification settings

accesto/serverless-php

Repository files navigation

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.

About

Example code for AWS Lambda and the Serverless framework using FastRoute and Pimple DI

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published