How to Optimize Billing Costs and Shut Down Machines When Not in Use?

I’m looking for ways to minimize the costs of running my app, especially during periods of inactivity. My app doesn’t need to run 24/7, and I’d like to shut it down or scale it down when it’s not being used, similar to how Heroku’s free dynos would sleep after 30 minutes of inactivity.

Here’s my fly.toml:

# fly.toml app configuration file generated for appname on 2025-02-12T10:03:56+01:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'appname'
primary_region = 'arn'
kill_signal = 'SIGINT'
kill_timeout = '5s'

[experimental]
  auto_rollback = true

[build]
  builder = 'heroku/builder:22'

[env]
  PORT = '8080'

[[services]]
  protocol = 'tcp'
  internal_port = 8080
  processes = ['app']

  [[services.ports]]
    port = 80
    handlers = ['http']
    force_https = true

  [[services.ports]]
    port = 443
    handlers = ['tls', 'http']

  [services.concurrency]
    type = 'connections'
    hard_limit = 10
    soft_limit = 5

  [[services.tcp_checks]]
    interval = '60s'
    timeout = '2s'
    grace_period = '1s'

[[vm]]
  memory = '256mb'
  cpu_kind = 'shared'
  cpus = 1

Any advice, tips, or examples would be greatly appreciated! Thanks in advance for your help.

I’m working on integrating Fly devops stuff at my app level. So when I need a machine, I will spin one up, and it will auto-exit when it is done. Depending on what “in use” means to you, you might be able to periodically scan for machines that are alive, and scale them down (even to zero) if your metrics reach a particular floor.

Expanding on Moreno’s comment: replace your entire [[services]] block in your fly.toml with the following:

[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = "suspend"
  auto_start_machines = true

Not all applications behave well with suspend. Something that is guaranteed to work is stop. The difference is that suspended apps start basically instantly, stopped apps may take a second or two, depending on your app’s startup.

1 Like

I ended up just adding those two properties inside my [[services]] block instead of replacing the whole configuration, and it seems to be working (I checked this morning and my machine was actually suspended)

[[services]]
  protocol = 'tcp'
  internal_port = 8080
  processes = ['app']
  auto_stop_machines = "suspend"
  auto_start_machines = true

Since I couldn’t find anything about this, I’m still trying to fully understand how auto_stop_machines works.

  • How long does it take for an idle machine to suspend?
  • If I manually suspend the machine does auto_start_machines = true still work?
  • If my app isn’t used at all and is always suspended, should I expect to be billed nothing?

Ideally I want to avoid any unnecessary costs since I’m the only one who uses my app.

it’s in the docs, read about soft limits and the likes Fly Proxy autostop/autostart · Fly Docs

1 Like