{"id":13700057,"url":"https://github.com/leocavalcante/aws-lambda-swoole-runtime","last_synced_at":"2025-07-13T11:36:00.838Z","repository":{"id":41565511,"uuid":"347777935","full_name":"leocavalcante/aws-lambda-swoole-runtime","owner":"leocavalcante","description":"λ Run PHP Coroutines \u0026 Fibers as-a-Service on the AWS Lambda.","archived":false,"fork":false,"pushed_at":"2021-03-19T18:06:49.000Z","size":6849,"stargazers_count":40,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-29T22:41:18.404Z","etag":null,"topics":["aws","aws-lambda","custom-runtime","function-as-a-service","lambda","php","serverless","swoole"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leocavalcante.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-14T23:15:38.000Z","updated_at":"2024-02-26T08:59:17.000Z","dependencies_parsed_at":"2022-09-11T22:00:34.961Z","dependency_job_id":null,"html_url":"https://github.com/leocavalcante/aws-lambda-swoole-runtime","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leocavalcante%2Faws-lambda-swoole-runtime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leocavalcante%2Faws-lambda-swoole-runtime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leocavalcante%2Faws-lambda-swoole-runtime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leocavalcante%2Faws-lambda-swoole-runtime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leocavalcante","download_url":"https://codeload.github.com/leocavalcante/aws-lambda-swoole-runtime/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250390451,"owners_count":21422706,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-lambda","custom-runtime","function-as-a-service","lambda","php","serverless","swoole"],"created_at":"2024-08-02T20:00:47.901Z","updated_at":"2025-04-23T07:29:38.192Z","avatar_url":"https://github.com/leocavalcante.png","language":"PHP","funding_links":[],"categories":["Serverless"],"sub_categories":[],"readme":"# λ Swoole Runtime for AWS Lambda\n\nRun PHP Coroutines \u0026 Fibers as-a-Service on the AWS Lambda.\n\n## Getting started\n\n### Create your Lambda function\n\n#### index.php\n```php\n\u003c?php declare(strict_types=1);\n\nuse Swoole\\Coroutine;\n\n/**\n * It is already inside a Coroutine context (i.e.: Co\\run)\n */\nfunction main(array $context): string\n{\n    $channel = new Coroutine\\Channel(2);\n    $context['greet'] ??= 'World';\n\n    Coroutine::create(static function() use ($context, $channel): void {\n        Coroutine::sleep(1);\n        $channel-\u003epush(\"{$context['greet']}!\");\n    });\n\n    Coroutine::create(static function() use ($channel): void {\n        Coroutine::sleep(0.5);\n        $channel-\u003epush('Hello');\n    });\n\n    return implode(', ', [$channel-\u003epop(), $channel-\u003epop()]);\n}\n```\nThe `main(array $context)` function here **is not optional**, the runtime will make a call to a `main` function passing a `array` representing the context.\nThe return should be something scalar or a `JsonSerializable` object.\n\n#### Dockerfile\n```Dockerfile\nFROM leocavalcante/aws-lambda-swoole-runtime\n# The WORKDIR is already /var/task\nCOPY composer.* .\nRUN composer install -o --prefer-dist --no-dev\n# This split avoids a call to composer install on every change to a source-code file\nCOPY . .\n```\n\n### Testing locally\n\nTo test AWS Lambda functions based on Container images locally, Amazon provides the [AWS Lambda Runtime Interface Emulator (RIE)](https://docs.aws.amazon.com/lambda/latest/dg/images-test.html).\n\n\u003e 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.\n\n#### Build the image\n```bash\ndocker build -t my-aws-lambda-function .\n```\nAlso grab the `8080` port on the container, it will be where the emulator will bind to.\n\n#### Run using the RIE as Entrypoint\n```bash\ndocker run --rm -v \"$(pwd)/aws-lambda-rie:/aws-lambda-rie\" --entrypoint /aws-lambda-rie -p 9000:8080 my-aws-lambda-function\n```\n\n#### Make a POST request\n```http request\nPOST http://localhost:9000/2015-03-31/functions/function/invocations\nContent-Type: application/json\n\n{\"greet\": \"Swoole\"}\n```\n\nOr:\n```bash\ncurl -XPOST http://localhost:9000/2015-03-31/functions/function/invocations -d '{\"greet\": \"Swoole\"}'\n```\n\nYou should be seeing:\n\n```http reponse\nHTTP/1.1 200 OK\nDate: Fri, 19 Mar 2021 17:44:58 GMT\nContent-Length: 16\nContent-Type: text/plain; charset=utf-8\n\n\"Hello, Swoole!\"\n```\n\n### Deploying to production\n\n#### 1. Login to your ECR\n```bash\naws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 884320951759.dkr.ecr.us-east-1.amazonaws.com\n```\nDon't forget to change region `us-east-1` and the AWS Account ID (`884320951759`).\n\n\u003e It assumes that you already have the [AWS Command Line Interface (`aws`)](https://aws.amazon.com/cli/) and it is [already configured (`aws configure`)](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html). **And yes, a Private ECR already created.**\n\n⚠️ Also make sure that you will be using a Private ECR on the same Account that your Lambda function.\n\n#### 2. Build and push your image\n```bash\ndocker build -t 884320951759.dkr.ecr.us-east-1.amazonaws.com/lambda-swoole-runtime-example .\ndocker push 884320951759.dkr.ecr.us-east-1.amazonaws.com/lambda-swoole-runtime-example\n```\n\n#### 3. Your Swoole-powered AWS Lambda Container image is ready!\n\nYou can use the Web UI to create a **Function** based on it:\n\n![Create function screenshot](create-function-screenshot.gif)\n\n---\n\u003cp align=\"center\"\u003e\nMIT License\u003cbr\u003e\nCopyright (c) 2021 Leo Cavalcante\n\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleocavalcante%2Faws-lambda-swoole-runtime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleocavalcante%2Faws-lambda-swoole-runtime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleocavalcante%2Faws-lambda-swoole-runtime/lists"}