https://github.com/fly-apps/fly-run-this-function-on-another-machine
This is a simple example on how to spawn a Fly.io machine and run a function from there.
https://github.com/fly-apps/fly-run-this-function-on-another-machine
Last synced: 12 months ago
JSON representation
This is a simple example on how to spawn a Fly.io machine and run a function from there.
- Host: GitHub
- URL: https://github.com/fly-apps/fly-run-this-function-on-another-machine
- Owner: fly-apps
- Created: 2023-06-07T19:16:09.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-24T00:34:59.000Z (over 2 years ago)
- Last Synced: 2025-04-22T21:38:13.228Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 742 KB
- Stars: 82
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## FLAME for JavaScript: Run a function on another Machine

This is a simple example app demonstrating how to run a particular function on a temporary copy of your app, using the FLAME pattern. Checkout the blog post that explains this code in more detail: https://fly.io/javascript-journal/flame-for-javascript-rethinking-serverless
FLAME (Fleeting Lambda Application for Modular Execution), which was first introduced by Chris McCord, creator of the Phoenix framework, was originally demo'ed in an Elixir library. This app is a case study on how it can be implemented in vanilla JavaScript. FLAME is a new way to horizontally auto-scale your application code in a more modular way. You can read more about it in Chris's post here: https://fly.io/blog/rethinking-serverless-with-flame/
## Prerequisites
While FLAME is a cloud-provider-agnostic pattern, this project uses Fly.io's Machines API to auto-scale your code. As such, **your app will need a [Fly token](https://fly.io/docs/flyctl/auth-token/) to be able to spawn new machines.**
If you already have the `flyctl` CLI installed, you can run the following to obtain a new token:
```cmd
$ flyctl auth token [flags]
```
## Deploying this demo
First launch this app.
```sh
$ fly launch --copy-config --no-public-ips --no-deploy
Creating app in /Users/lubien/workspace/run-this-on-another-machine
Scanning source code
Detected a Dockerfile app
? Choose an app name (leave blank to generate one): another-machines
? Select Organization: Lubien (personal)
? Choose a region for deployment: Sao Paulo, Brazil (gru)
App will use 'gru' region as primary
```
The let's set the deploy token
```sh
$ fly secrets set FLY_API_TOKEN=$(fly auth token)
Secrets are staged for the first deployment
```
Make your first deployment and remember the machine ID.
```sh
$ fly deploy
...
Watch your app at https://fly.io/apps/another-machines/monitoring
Updating existing machines in 'another-machines' with rolling strategy
Machine 3287114c3d7085 [app] has state: started
[1/1] Checking that 3287114c3d7085 [app] is up and running
Finished deploying
```
SSH into the main machine and run `node /app/index.mjs`
```sh
$ fly ssh console -s
? Select VM: gru: 3287114c3d7085 fdaa:0:3335:a7b:1f60:1b01:ce26:2 muddy-violet-4491
Connecting to fdaa:0:3335:a7b:1f60:1b01:ce26:2... complete
root@3287114c3d7085:/app# node /app/index.mjs
```
If you look into your app Monitoring page, there will be a new machine.
## Next steps:
- ~~Stop machines after they're done~~
- ~~Reuse stopped machines~~
- Pool machines?