{"id":13527768,"url":"https://github.com/serverless/emulator","last_synced_at":"2025-04-23T22:28:16.499Z","repository":{"id":57140116,"uuid":"97613076","full_name":"serverless/emulator","owner":"serverless","description":"Serverless Emulator which lets you run serverless functions locally","archived":false,"fork":false,"pushed_at":"2017-09-03T06:00:10.000Z","size":224,"stargazers_count":78,"open_issues_count":20,"forks_count":11,"subscribers_count":29,"default_branch":"master","last_synced_at":"2024-11-02T12:35:08.251Z","etag":null,"topics":["faas-emulator","function-emulator","serverless","serverless-offline"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/serverless.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":"2017-07-18T15:09:53.000Z","updated_at":"2024-02-08T19:53:08.000Z","dependencies_parsed_at":"2022-09-05T00:20:46.553Z","dependency_job_id":null,"html_url":"https://github.com/serverless/emulator","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serverless%2Femulator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serverless%2Femulator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serverless%2Femulator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serverless%2Femulator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/serverless","download_url":"https://codeload.github.com/serverless/emulator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223764358,"owners_count":17198609,"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":["faas-emulator","function-emulator","serverless","serverless-offline"],"created_at":"2024-08-01T06:02:00.660Z","updated_at":"2024-11-08T22:37:43.728Z","avatar_url":"https://github.com/serverless.png","language":"JavaScript","readme":"# Emulator\n\n[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com)\n[![Build Status](https://travis-ci.org/serverless/emulator.svg?branch=master)](https://travis-ci.org/serverless/emulator)\n\n[Website](http://www.serverless.com) • [Newsletter](http://eepurl.com/b8dv4P) • [Gitter](https://gitter.im/serverless/serverless) • [Forum](http://forum.serverless.com) • [Meetups](https://github.com/serverless-meetups/main) • [Twitter](https://twitter.com/goserverless)\n\n\u003e Emulate your Serverless functions locally.\n\n\n## Contents\n\n- [Getting started](#getting-started)\n- [Development](#development)\n- [Functionality](#functionality)\n  + [General](#general)\n  + [Function deployment](#function-deployment)\n    + [Function config / `function.json`](#function-config--functionjson)\n  + [Function invocation](#function-invocation)\n  + [Middlewares](#middlewares)\n- [APIs](#apis)\n  + [HTTP API](#http-api)\n    + [Functions](#functions)\n      + [Deploy function](#deploy-function)\n      + [Invoke function](#invoke-function)\n    + [Utils](#utils)\n      + [Heartbeat](#heartbeat)\n\n---\n\n## Getting started\n\n1. Clone the repository\n1. Run `npm install` to install all dependencies\n1. Run `scripts/sync-storage` to sync the [example `storage` artifacts](./storage) with the Emulators `storage` location\n1. Run `npm run build` to build the project (the build artifacts can be found in `dist`)\n1. Run `npm start` to start the emulator\n\n## Development\n\nYou can run `npm run watch` to automatically re-build the files in the `src` directory when they change.\n\nAdditionally you can use Docker to run and develop everything inside a container.\n\nSpinning up the Docker container is as easy as `docker-compose run -p 4002:4002 node bash`. Make sure to run the steps in [Getting started](#getting-started).\n\nOr, if you need a build a Docker image and run it, do:\n\n```\ndocker build -t emulator .\ndocker run --rm -it -p 4002:4002 emulator\n```\n\n## Using the Emulator\n\nThe following documents provide some insights on how to configure and use the Emulator.\n\n### Options\n\nThe Emulator can be configured with the following options you can pass in via the CLI:\n\n- `--port` - `number` - Optional port (defaults to `4002`)\n\n### Starting the Emulator\n\n| Command | Description |\n| --- | --- |\n| `npm start` | Will start the Emulator at `localhost` with the default port |\n| `npm start -- --port 4711` | Starts the Emulator at `localhost` with the port `4711` |\n\n## Functionality\n\n### General\n\nThe Emulator is a software which makes it possible to emulate different cloud provider FaaS offerings (such as AWS Lambda or Google Cloud Functions) on your local machine in an offline-focused manner.\n\nIt can be used to deploy and invoke serverless functions w/o the need to go through the process of setting up and configuring a cloud provider account or deploying the functions into the infrastructure before being able to use them.\n\nThis enables new ways of doing offline-first serverless development with a way faster feedback loop.\n\nTechnically speaking the Emulator is a long running process (Daemon) which exposes an API and accepts different calls to API endpoints in order to perform actions and control its behavior.\n\n**Note:** The API is only used to control the Emulator. It is **NOT** the same as an API Gateway.\n\n### Function deployment\n\nFunctions are deployed via the [API](#apis) and are stored in the `~/.serverless/emulator` directory according to the following filesystem structure:\n\n```\n|__ storage\n    |__ functions\n        |__ \u003cfunction-id\u003e\n            |__ code\n            |__ function.json\n```\n\nThe root directory is the `storage` directory. It's a place where all artifacts will be stored.\n\nThe `functions` directory is the place where all the function-related artifacts are stored.\n\nA directory for the specific service contains directories for each individual function.\n\nThe `code` directory is the place where the actual (unzipped) function code is stored.\n\nThe `function.json` file contains important information about the function configuration (e.g. what `runtime` this function uses or which `provider` it's written for) and other metadata.\n\nThe proposed directory structure makes it easy for the Emulator to follow a convention-over-configuration approach where artifacts are stored in a predictable way without having to introduce a local DB with state information about all the deployed functions and services.\n\nOn every function deployment the following happens behind the scenes:\n\n- For every `function-id` a separate directory will be created (if not already present)\n- The `.zip` file will be extracted and moved into the `code` directory\n- The `function` configuration which is passed in via the API will be written into the `function.json` file\n\n#### Function config / `function.json`\n\nEvery function needs information about its configuration. This information is passed in via the API when the function is deployed.\n\nUpon deployment this data is persisted in the `function.json` file.\n\nThe `function.json` files can be found in `~/.serverless/emulator/storage/functions/\u003cfunction-id\u003e`.\n\nThe Emulator needs those file to e.g. make decision which [middlewares](#middlewares) to execute or which runtime-specific wrapper to use.\n\nHere's a list with different example functions and their corresponding provider-related `function.json` config files:\n\n- [Example AWS function](./examples/functions/my-service-function-aws-1)\n- [Example Google function](./examples/functions/my-service-function-google-1)\n\n### Function invocation\n\nWhen invoking a function the Emulator will simply look for the function directory (see above how the naming schema helps with the lookup), determines the `provider`, `runtime` and `handler` based on the config in the `function.json` file and starts the execution phase which will happen in a dedicated child process (more on that later).\n\nThe invocation data is extracted from the incoming API requested and passed to a so-called runtime-specific \"wrapper script\" via `stdin`. This \"wrapper script\" is responsible to setup the execution environment, require the function and pass the event payload to the function (you can think of it as a language specific container). Furthermore it will marshall the returned data and pass it back to the Emulators parent process which will then transform it into a JSON format and sends it back via an API response.\n\nThe whole invocation happens in a `child_process.spawn()` call to ensure that the Emulator won't crash when a function misbehaves.\n\nThis abstraction layer makes it easy to introduce other runtimes later on. Furthermore the way the incoming event data is handled by the Emulator is always the same and independent of the function handler signature since the wrapper encapsulates the logic to marshall and unmarshall the data which is handed over to the function.\n\nHere's a sample call to invoke an AWS function within the wrapper script (which is written in Node.js). It will require and prepare the function (according to the CLI `options`) and pass the echoed data as the function parameters (via `stdin`) to it:\n\n```bash\n# NOTE: This is not the actual payload. It's used to show the technical parts / architecture of a function invocation\n\necho '{ event: { foo: \"bar\" }, context: {}, callback: (error, result) =\u003e {} }' | runtimes/node.js \u003cfull-path-to-function-file\u003e \u003cexported-function-name\u003e\n```\n\n### Middlewares\n\nThe Emulator provides a middleware concept which makes it possible to use custom code to modify the data which is used inside the Emulator when exercising core logic (e.g. setting up the execution environment, invoking functions, etc.).\n\nThe core Emulators `runMiddlewares` functionality ensures that the raw data object which is passed into it will be copied over into an `input` object and removed from the root of the object. Furthermore it creates a blank `output` object which can be used by middlewares to store the computed results.\n\nLet's take a quick look at an example to see how this works behind the scenes.\n\nWe assume that the data which is passed into the Emulators `runMiddlewares` function has the following shape:\n\n```javascript\n{\n  foo: 'bar',\n  baz: 'qux'\n};\n```\n\nThe data will be prepared and passed into the middlewares in the following format:\n\n```javascript\n{\n  input: {\n    foo: 'bar',\n    baz: 'qux'\n  },\n  output: {}\n}\n```\n\nMiddlewares can do whatever they want with this data.\n\nHowever the computed result should be written into the `output` object since this is returned by the Emulators `runMiddlewares` function after all middlewares are executed.\n\nMiddlewares can be implemented against different lifecycle events. Right now the lifecycle events are:\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth\u003eLifecycle\u003c/th\u003e\n    \u003cth\u003eDescription\u003c/th\u003e\n    \u003cth\u003eProvided object\u003c/th\u003e\n    \u003cth\u003eExpected returned object\u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003epreLoad\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Before the function is loaded and the execution environment is configured.\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e\n        {\n          input: {\n            functionId \u003cstring\u003e,\n            functionConfig: \u003cobject\u003e\n          },\n          output: {}\n        }\n      \u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e\n        {\n          input: {\n            // ...snip...\n          },\n          output: {\n            functionId: \u003cstring\u003e,\n            env: \u003cobject\u003e\n          }\n        }\n      \u003c/code\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003epostLoad\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      After the function was loaded and the execution environment was configured.\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eTBD\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eTBD\u003c/code\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003epreInvoke\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Right before the payload is passed to the function which should be invoked.\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e\n        {\n          input: {\n            functionId: \u003cstring\u003e,\n            functionConfig: \u003cobject\u003e,\n            payload: \u003cobject\u003e\n          },\n          output: {}\n        }\n      \u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e\n        {\n          input: {\n            // ...snip...\n          },\n          output: {\n            // NOTE: those params should\n            // include the callback function parameter!\n            \u003cprovider-specific-handler-params\u003e\n          }\n        }\n      \u003c/code\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003epostInvoke\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      After the function is invoked, but before it's result is passed back via the API.\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e\n        {\n          input: {\n            functionId: \u003cstring\u003e,\n            functionConfig: \u003cobject\u003e,\n            payload: \u003cobject\u003e,\n            errorData: \u003cstring\u003e,\n            outputData: \u003cstring\u003e\n          },\n          output: {}\n        }\n      \u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e\n        {\n          input: {\n            // ...snip...\n          },\n          output: {\n            errorData: \u003cobject\u003e,\n            outputData: \u003cobject\u003e\n          }\n        }\n      \u003c/code\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nTake a look at our [`core-middlewares`](./src/core-middlewares) to see some example implementations.\n\nMiddlewares are loaded and executed in the following order:\n\n1. Load the [`core-middlewares`](./src/core-middlewares) in an alphabetical order\n1. Execute the core middlewares in the previously loaded order\n1. Load the custom middlewares in the order provided in the config file\n1. Execute the custom middlewares in the previously loaded order\n\n## APIs\n\nThe Emulator exposes different APIs which makes it possible to interact with it and perform specific actions.\n\nExamples for such actions could e.g. be the deployment or invocation of functions.\n\nRight now only an HTTP API is implemented. However other API types such as (g)RPC are imaginable.\n\n### HTTP API\n\nThe Emulator exposes a HTTP API which makes it possible for other services to interact with it via HTTP calls.\n\n#### Functions\n\n##### Deploy function\n\n`POST /v0/emulator/api/functions/deploy`\n\nRequest:\n\n- `functionId` - `string` - **required** The id of the function\n- `functionConfig` - `object`: - **required** Additional (provider dependent) function configuration\n- `zipFilePath` - `string` - **required** The path to the local zip file\n\nResponse:\n\n- `functionId` - `string` - The id of the function\n- `functionConfig` - `object` - Additional (provider dependent) function configuration\n- `zipFilePath` - `string` - The path to the local zip file\n\n##### Invoke function\n\n`POST /v0/emulator/api/functions/invoke`\n\nRequest:\n\n- `functionId` - `string` - **required** The id of the function\n- `payload` - `object` - **required** The event payload the function should receive\n- `method` - `string` - The method of invocation (`async` / `sync`) **default** `async`\n\nResponse:\n\n- Result of the function invocation.\n\n#### Utils\n\n##### Heartbeat\n\n`POST /v0/emulator/api/utils/heartbeat`\n\nRequest:\n\n- `ping` - `string` - **required** The string the Emulator should return\n\nResponse:\n\n- `pong` - `string` - The string the Emulator should return\n- `timestamp` - `integer` - Timestamp which indicates when the response was computed\n\n\n## Community\n\n* [Email Updates](http://eepurl.com/b8dv4P)\n* [Serverless Forum](http://forum.serverless.com)\n* [Gitter Chatroom](https://gitter.im/serverless/serverless)\n* [Serverless Meetups](http://www.meetup.com/serverless/)\n* [Facebook](https://www.facebook.com/serverless)\n* [Twitter](https://twitter.com/goserverless)\n* [Contact Us](mailto:hello@serverless.com)\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserverless%2Femulator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fserverless%2Femulator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserverless%2Femulator/lists"}