vercel / fun

ƒun - Local serverless function λ development runtime
Apache License 2.0
482 stars 26 forks source link

ƒun

Build Status

Local serverless function λ development runtime.

Example

Given a Lambda function like this one:

// example/index.js
exports.handler = function(event, context, callback) {
    callback(null, { hello: 'world' });
};

You can invoke this function locally using the code below:

import { createFunction } from '@vercel/fun';

async function main() {
    // Starts up the necessary server to be able to invoke the function
    const fn = await createFunction({
        Code: {
            // `ZipFile` works, or an already unzipped directory may be specified
            Directory: __dirname + '/example'
        },
        Handler: 'index.handler',
        Runtime: 'nodejs8.10',
        Environment: {
            Variables: {
                HELLO: 'world'
            }
        },
        MemorySize: 512
    });

    // Invoke the function with a custom payload. A new instance of the function
    // will be initialized if there is not an available one ready to process.
    const res = await fn({ hello: 'world' });

    console.log(res);
    // Prints: { hello: 'world' }

    // Once we are done with the function, destroy it so that the processes are
    // cleaned up, and the API server is shut down (useful for hot-reloading).
    await fn.destroy();
}

main().catch(console.error);

Caveats

ƒun provides an execution environment that closely resembles the real Lambda environment, with some key differences that are documented here:

Runtimes

ƒun aims to support all runtimes that AWS Lambda provides. Currently implemented are: