fly-autoscaler image. The app runs within your organization so you have full control over it. You can customize the autoscaler to work with your specific scaling needs.
Quickstart
To get up and running, you’ll set up the app, configure secrets, set the configuration, and deploy the autoscaler. As a prerequisite, you need an existing target application that you want to scale and a user-defined metric to scale on. In this example, you’ll scale on a Prometheus metric calledqueue_depth but you can replace that with your own. You can also scale based on Temporal workflows.
Create the autoscaler application
First, create a new Fly.io app that will run the autoscaler. Replace themy-autoscaler name with a unique name for your autoscaler application:
Create a deploy token
The first auth token you’ll need is one that has permissions to deploy your target app:Create a token to read from Prometheus
The next auth token you’ll need is one that has permissions to read from your organization’s Prometheus data on Fly:Configure your autoscaler fly.toml
Next, set up a fly.toml configuration file for your autoscaler to set environment variables. Replace the my-autoscaler, my-target-app, and my-org with values for your situation.
FAS_PROMETHEUS_ADDRESSdefines the Prometheus URL endpoint to query.FAS_PROMETHEUS_METRIC_NAMEdefines the local variable name that the metric result will be stored as. This example stores the query result value asqdepth.FAS_PROMETHEUS_QUERYdefines the Prometheus query to run. This example computes the sum of a user-definedqueue_depthmetric.
FAS_APP_NAMEis the name of the target application to scale.FAS_CREATED_MACHINE_COUNTdefines an Expr expression to calculate the required number of Machines. The autoscaler creates or destroys Machines to reach the required number.
min() function to prevent the autoscaler from scaling more than 50 Machines.
Deploy the autoscaler
The autoscaler only works on a single Machine, so you’ll use the--ha option to turn off the high availability feature that creates two Machines:
queue_depth gauge increases.
Note: The autoscaler creates new Machines in an application by cloning existing Machines. It will not scale to zero and will always keep at least one Machine running.
More use cases
Start and stop instead of create and destroy Machines
If you already have a pool of created Machines that you want to autoscale, you can use theFAS_STARTED_MACHINE_COUNT expression to stop and start Machines instead of creating and destroying them with FAS_CREATED_MACHINE_COUNT.
When you use FAS_STARTED_MACHINE_COUNT, the autoscaler sends a termination signal to the Machines instead of destroying them when scaling. It will also automatically cap the number of Machines that can be started to the number of pre-created Machines.
When you scale by starting and stopping existing Machines, your Machines will start up quickly. You’ll pay for the Machine’s CPU and RAM when they’re running and for the rootfs when they’re stopped.
When you scale by creating and destroying Machines, your Machines will be slightly slower to reach a started state since it takes longer to create a Machine than to start one. You won’t need to create a “pool” of Machines. You’ll only pay for the Machine’s CPU and RAM when they’re running and won’t need to pay for rootfs since the Machines are destroyed when not needed.
Scale multiple applications
You can scale multiple independent applications with the same autoscaler by using a wildcard expression for your application name. Your applications must all share a common prefix and they must all be in the same organization. To enable multi-app scaling, you will need to use an organization-wide auth token rather than an app-specific deploy token:fly.toml config:
$APP_NAME or ${APP_NAME} in your Prometheus query to identify the current application being scaled:
Scale based on pending Temporal work
The Temporal metrics collector periodically checks for the total number of workflows in a “running” state. By default, it will check every 15 seconds. You can connect to your Temporal namespace using theFAS_TEMPORAL_ environment variables. For example:
FAS_CREATED_MACHINE_COUNT="workflow_count / 10. If you want to ensure you don’t exceed a specific number of Machines, then you can use a min() expression to cap it: FAS_CREATED_MACHINE_COUNT="min(50, workflow_count / 10). This ensures that no more than 50 Machines get created, regardless of how many workflows are executing.
You’ll also need to load the certificate and key data as secrets from your ca.pem and ca.key files:
Configuration reference
The quickstart describes how to configure the autoscaler with environment variables. You can also configure the autoscaler with a YAML config file if you don’t want to use environment variables or if you want to configure more than one metric collector. See the reference fly-autoscaler.yml file for an example and more details.Autoscaler config
FAS_APP_NAME: The name of the target app to scale.FAS_CREATED_MACHINE_COUNT: An Expr expression to calculate the required number of Machines. The autoscaler creates or destroys Machines to reach the required number.FAS_STARTED_MACHINE_COUNT: An Expr expression to calculate the required number of Machines. The autoscaler starts or stops Machines to reach the required number.
Prometheus collector
FAS_PROMETHEUS_ADDRESS: The URL of the Prometheus endpoint to query.FAS_PROMETHEUS_METRIC_NAME: The local variable name that the metric result will be stored as.FAS_PROMETHEUS_QUERY: The Prometheus query to run.
Temporal collector
FAS_TEMPORAL_ADDRESS: The URL of the Temporal endpoint to query.FAS_TEMPORAL_METRIC_NAME: The local variable name that the metric result will be stored as.FAS_TEMPORAL_NAMESPACE: The Temporal namespace name.FAS_TEMPORAL_CERT_DATA: The namespace CA certificate data (ca.pem).FAS_TEMPORAL_KEY_DATA: The namespace CA key data (ca.key).