Giter Club home page Giter Club logo

aws-lambda-swoole-runtime's Introduction

λ Swoole Runtime for AWS Lambda

Run PHP Coroutines & Fibers as-a-Service on the AWS Lambda.

Getting started

Create your Lambda function

index.php

<?php declare(strict_types=1);

use Swoole\Coroutine;

/**
 * It is already inside a Coroutine context (i.e.: Co\run)
 */
function main(array $context): string
{
    $channel = new Coroutine\Channel(2);
    $context['greet'] ??= 'World';

    Coroutine::create(static function() use ($context, $channel): void {
        Coroutine::sleep(1);
        $channel->push("{$context['greet']}!");
    });

    Coroutine::create(static function() use ($channel): void {
        Coroutine::sleep(0.5);
        $channel->push('Hello');
    });

    return implode(', ', [$channel->pop(), $channel->pop()]);
}

The main(array $context) function here is not optional, the runtime will make a call to a main function passing a array representing the context. The return should be something scalar or a JsonSerializable object.

Dockerfile

FROM leocavalcante/aws-lambda-swoole-runtime
# The WORKDIR is already /var/task
COPY composer.* .
RUN composer install -o --prefer-dist --no-dev
# This split avoids a call to composer install on every change to a source-code file
COPY . .

Testing locally

To test AWS Lambda functions based on Container images locally, Amazon provides the AWS Lambda Runtime Interface Emulator (RIE).

It is a proxy for the Lambda Runtime API that allows you to locally test your Lambda function packaged as a container image. The emulator is a lightweight web server that converts HTTP requests into JSON events to pass to the Lambda function in the container image.

Build the image

docker build -t my-aws-lambda-function .

Also grab the 8080 port on the container, it will be where the emulator will bind to.

Run using the RIE as Entrypoint

docker run --rm -v "$(pwd)/aws-lambda-rie:/aws-lambda-rie" --entrypoint /aws-lambda-rie -p 9000:8080 my-aws-lambda-function

Make a POST request

POST http://localhost:9000/2015-03-31/functions/function/invocations
Content-Type: application/json

{"greet": "Swoole"}

Or:

curl -XPOST http://localhost:9000/2015-03-31/functions/function/invocations -d '{"greet": "Swoole"}'

You should be seeing:

HTTP/1.1 200 OK
Date: Fri, 19 Mar 2021 17:44:58 GMT
Content-Length: 16
Content-Type: text/plain; charset=utf-8

"Hello, Swoole!"

Deploying to production

1. Login to your ECR

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 884320951759.dkr.ecr.us-east-1.amazonaws.com

Don't forget to change region us-east-1 and the AWS Account ID (884320951759).

It assumes that you already have the AWS Command Line Interface (aws) and it is already configured (aws configure). And yes, a Private ECR already created.

⚠️ Also make sure that you will be using a Private ECR on the same Account that your Lambda function.

2. Build and push your image

docker build -t 884320951759.dkr.ecr.us-east-1.amazonaws.com/lambda-swoole-runtime-example .
docker push 884320951759.dkr.ecr.us-east-1.amazonaws.com/lambda-swoole-runtime-example

3. Your Swoole-powered AWS Lambda Container image is ready!

You can use the Web UI to create a Function based on it:

Create function screenshot


MIT License
Copyright (c) 2021 Leo Cavalcante

aws-lambda-swoole-runtime's People

Contributors

leocavalcante avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.