Working with the Machines API
Machines are operated over a simple REST API. First, visit the Machines documentation to get ready for this guide.Grab or create a runtime Docker image
Your function-as-a-service needs a well-defined runtime environment that can boot user code and then shut it down when it’s idle. Here we’ll use a simple Fastify server running behind a small Golang HTTP proxy that exits after a few minutes of inactivity.Create an app
Pick a unique name and create the required application to group machines together. This application is also the gateway to accessing the machines from the internet.network field. This is necessary to isolate the app from other private networks on the organization. Learn more about custom private networks.
Allocate an IP address to the app. This makes our app accessible at the IP, and via my-app-name.fly.dev.
Launch a new machine
Launch a user machine by supplying a Docker image and a networking configuration.https://my-app-name.fly.dev. If no requests arrive within 120 seconds (default but configurable), the machine will exit, but a request will force it to start back up automatically.
Launch a Machine in another region
That machine was launched in the region nearest to you. Say you want your user function to run fast for users in Australia. Let’s launch another machine there. The Anycast IP you allocated will route traffic to the nearest region with a deployed machine. If it’s stopped, it will start up automatically.Stop a machine
In a FaaS environment, you’ll usually want to wait for a machine to exit. You can stop them with an API call, however, if needed.Start an existing machine
Once created, a machine may be stopped and started until you delete it. The good news is, images stay cached after first launch. Starting an existing machine is very fast. Use the existing machine ID again to start it:Get details about the deployment
Now we have a couple machines running, we can usefly logs -a my-app-name to see what’s going on. Also, for an overview:
Delete a machine
When you’re done, delete the machine:Additional notes
The Machines API is young and full of potential. We haven’t worked everything out, so please post on our forums with questions! Here are a few things worth noting: The wake-on-request behavior described above will be unpredictable when running more than one machine per region. Standardflyctl operations like deploy, scale, autoscaling aren’t currently supported by Machines.
ssh is supported. You can SSH to a machine using its IPv6 address (from fly machines list):
fly ssh console -s: