GithubHelp home page GithubHelp logo

vercel / fun Goto Github PK

View Code? Open in Web Editor NEW
475.0 52.0 24.0 379 KB

ƒun - Local serverless function λ development runtime

License: Apache License 2.0

JavaScript 1.92% TypeScript 72.51% Shell 11.91% Go 8.66% Python 4.23% Dockerfile 0.78%

fun's Introduction

ƒ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:

  • Lambdas processes are ran as your own user, not the sbx_user1051 user.
  • Processes are not sandboxed nor chrooted, so do not rely on hard-coded locations like /var/task, /var/runtime, /opt, etc. Instead, your function code should use the environment variables that represent these locations (namely LAMBDA_TASK_ROOT and LAMBDA_RUNTIME_DIR).
  • Processes are frozen by sending the SIGSTOP signal to the lambda process, and unfrozen by sending the SIGCONT signal, not using the cgroup freezer.
  • Lambdas that compile to native executables (i.e. Go) will need to be compiled for your operating system. So if you are on macOS, then the binary needs to be executable on macOS.

Runtimes

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

  • nodejs for Node.js Lambda functions using the system node binary
  • nodejs6.10 for Node.js Lambda functions using a downloaded Node v6.10.0 binary
  • nodejs8.10 for Node.js Lambda functions using a downloaded Node v8.10.0 binary
  • nodejs10.x for Node.js Lambda functions using a downloaded Node v10.15.3 binary
  • nodejs12.x for Node.js Lambda functions using a downloaded Node v12.22.7 binary
  • nodejs14.x for Node.js Lambda functions using a downloaded Node v14.18.1 binary
  • python for Python Lambda functions using the system python binary
  • python2.7 for Python Lambda functions using a downloaded Python v2.7.12 binary
  • python3 for Python Lambda functions using the system python3 binary
  • python3.6 for Python Lambda functions using a downloaded Python v3.6.8 binary
  • python3.7 for Python Lambda functions using a downloaded Python v3.7.2 binary
  • go1.x for Lambda functions written in Go - binary must be compiled for your platform
  • provided for custom runtimes

fun's People

Contributors

amio avatar andybitz avatar dependabot[bot] avatar dfrankland avatar dpeek avatar joecohens avatar leo avatar sionicion avatar stkevintan avatar styfle avatar thasophearak avatar tootallnate avatar trek 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  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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fun's Issues

Needs `python` runtimes

  • python base runtime will implement the runtime bootstrap (like the nodejs base runtime)
  • python2.7 downloads Python 2.7.12 and uses the python runtime with the python binary in $PATH
  • python3.6 downloads Python 3.6.8 and uses the python runtime with the python binary in $PATH
  • python3.7 downloads Python 3.7.2 and uses the python runtime with the python binary in $PATH

https://docs.aws.amazon.com/lambda/latest/dg/python-programming-model.html
https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html

No way to set AWS_ACCESS_KEY_ID?

I see there are a set of env vars we're not allowed to set, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY included. Problem is when calling AWS SDK from inside a function I'd like to not have to pass those from somewhere else (which matches what happens in production).

Is there some other way of passing these in? If not could there be? :)

python3.7 doesn't seem to be able to find dependencies

I am trying to use the python3.7 runtime and it errors out on

ModuleNotFoundError: No module named 'requests'

requests is a dependency of a module I am including in the function.
What could be the cause of this and why does it successfully include the module itself, then fails on it's dependency?

Everything is installed and works fine without fun.

How do you include dependencies?

I tried using @zeit/ncc to compile to one file too, but i always get this error:

Error: Unable to import module 'index'

This is what i am trying to do.

const Octokit = require("@octokit/rest");
const octokit = new Octokit();

exports.handler = (event, context, callback) => {
  octokit.repos
    .listForOrg({
      org: "octokit",
      type: "public"
    })
    .then(repos => {
      callback(repos.data);
    });
};

i have tried to const Octokit = require("@octokit/rest"); inside the handler too.

Any thoughts?

running with this setup:

  const fn = await createFunction({
    Code: {
      // `ZipFile` works, or an already unzipped directory may be specified
      Directory: __dirname + "/dist"
    },
    Handler: "index.handler",
    Runtime: "nodejs"
  });

Python runtime sometimes prints error message when being killed

Traceback (most recent call last):
  File "/Users/nrajlich/Library/Caches/co.zeit.fun/runtimes/python/bootstrap.py", line 145, in <module>
    lambda_runtime_main()
  File "/Users/nrajlich/Library/Caches/co.zeit.fun/runtimes/python/bootstrap.py", line 128, in lambda_runtime_main
    (event, context) = lambda_runtime_next_invocation()
  File "/Users/nrajlich/Library/Caches/co.zeit.fun/runtimes/python/bootstrap.py", line 56, in lambda_runtime_next_invocation
    res = LambdaRequest('invocation/next')
  File "/Users/nrajlich/Library/Caches/co.zeit.fun/runtimes/python/bootstrap.py", line 33, in __init__
    req = urllib2.urlopen(url, data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1227, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1200, in do_open
    r = h.getresponse(buffering=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1132, in getresponse
    response.begin()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 453, in begin
    version, status, reason = self._read_status()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 409, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 480, in readline
    data = self._sock.recv(self._rbufsize)
socket.error: [Errno 54] Connection reset by peer

Should figure out a way to suppress this error message.

For `vercel dev`, it said `HTTP Error 500: Internal Server Error` (python3)

There is something wrong with vercel dev command in my Windows 10.

  • Vercel CLI 20.1.0 dev (beta)
  • Win10 Professional 1909
  • Python 3.8.5 x86_64
  • Without using venv
  • Workspace Tree:
├─.vercel
│      project.json
│      README.txt
└─api
        date.py

(* The content of date.py is same as this file as follows.)

# date.py
from http.server import BaseHTTPRequestHandler
from datetime import datetime

class handler(BaseHTTPRequestHandler):

  def do_GET(self):
    self.send_response(200)
    self.send_header('Content-type', 'text/plain')
    self.end_headers()
    self.wfile.write(str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')).encode())
    return

I ran command vercel dev, then it said urllib.error.HTTPError: HTTP Error 500: Internal Server Error when it called req = urllib.request.urlopen(url, data) in line 31 in bootstrap.py.

After I add print(url) in bootstrap.py, line 31.
The logs showed:

PS C:\Users\AT\Desktop\test_vercel_dev> vercel dev
Vercel CLI 20.1.0 dev (beta) — https://vercel.com/feedback

Ready! Available at http://localhost:3000
Building @vercel/python@latest:api/date.py
Installing required dependencies...
Built @vercel/python@latest:api/date.py [3s]
using HTTP Handler
using HTTP Handler
http://127.0.0.1:13320/2018-06-01/runtime/invocation/next (<======= print(url) output!!!)
<class 'OSError'>
http://127.0.0.1:13320/2018-06-01/runtime/invocation/b6544f1d-ec26-40f2-bb76-02f69451c86c/error (<======= print(url) output!!!)
http://127.0.0.1:13320/2018-06-01/runtime/invocation/next (<======= print(url) output!!!)
http://127.0.0.1:13320/2018-06-01/runtime/invocation/next (<======= print(url) output!!!)
TypeError: Object prototype may only be an Object or null: undefined
at Function.setPrototypeOf ()
at new LambdaError (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7870:16)
at Lambda. (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7990:27)
at Generator.next ()
at fulfilled (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:7898:58)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
Traceback (most recent call last):
File "C:\Users\AT\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 148, in
TypeError: Cannot read property 'resolve' of null
at RuntimeServer. (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8526:31)
at Generator.next ()
at C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8433:71
at new Promise ()
at module.exports.webpack_modules.58371.__awaiter (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8429:12)
at RuntimeServer.handleNextInvocation (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8517:16)
at RuntimeServer. (C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8488:33)
at Generator.next ()
at C:\Users\AT\AppData\Roaming\npm\node_modules\vercel\dist\index.js:8433:71
at new Promise ()
lambda_runtime_main()
File "C:\Users\AT\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 131, in lambda_runtime_main
(event, context) = lambda_runtime_next_invocation()
File "C:\Users\AT\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 57, in lambda_runtime_next_invocation
res = LambdaRequest('invocation/next')
File "C:\Users\AT\AppData\Local\co.zeit.fun\Cache\runtimes\python\bootstrap.py", line 32, in init
req = urllib.request.urlopen(url, data) (<======= this line crashed!!!)
File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 640, in http_response
response = self.parent.error(
File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Users\AT\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 500: Internal Server Error


In addition: I did the same thing in WSL(Ubuntu18.04) in this Windows PC, it works well.

So maybe it could not reproduce in other PC.

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.