andrewbaxter / terraform-provider-fly

Terraform provider for the Fly.io API
https://registry.terraform.io/providers/andrewbaxter/fly/latest
BSD 3-Clause "New" or "Revised" License
19 stars 2 forks source link

Image missing from Fly config on Dashboard #12

Open oscarhermoso opened 2 days ago

oscarhermoso commented 2 days ago

Steps to reproduce

Given Terraform like below:

resource "fly_machine" "app" {
  for_each = var.machine_names

  app    = "exampleapp"
  name   = each.key
  region = "syd"

  cpus     = var.cpus
  memory   = var.memory_mb
  image    = "supabase/stripe-sync-engine"
  env      = var.env
  services = var.services
  cmd      = var.cmd
}

Expected

Expect that after deployment, the build is included in the Configuration shown on the dashboard

app = "exampleapp"

[build]
  image = "supabase/stripe-sync-engine"

[env]
AUTO_EXPAND_LISTS = "true"
PORT = "8080"

[[metrics]]

[[services]]
internal_port = 8_080
protocol = "tcp"

  [[services.ports]]
  handlers = [ "http" ]
  port = 80

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

Actual

Build is missing from the output

app = "exampleapp"

- [build]
-   image = "supabase/stripe-sync-engine"

[env]
AUTO_EXPAND_LISTS = "true"
PORT = "8080"

[[metrics]]

[[services]]
internal_port = 8_080
protocol = "tcp"

  [[services.ports]]
  handlers = [ "http" ]
  port = 80

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

This causes following issues if you try to issue a fly deploy command from the CLI without a fly.toml

Screenshot from 2024-11-29 13-14-26

fly deploy -a exampleapp
==> Verifying app config
Validating --config path unset--
✓ Configuration is valid
--> Verified app config
==> Building image
==> Building image
Error: failed to fetch an image or build from source: app does not have a Dockerfile or buildpacks configured. See https://fly.io/docs/reference/configuration/#the-build-section

I believe this is caused by the fact that image is specified at the top level - but it actually needs to be defined within build:

https://github.com/andrewbaxter/terraform-provider-fly/blob/bf858c979e73627cb1c22c0d10461e6d211d0b92/machineapi/machines.go#L81-L91

https://fly.io/docs/reference/configuration/#specify-a-docker-image

I imagine this used to work but Fly changed the API at some point in time.

andrewbaxter commented 2 days ago

Thanks for the report and the thorough research!

Interesting, I just checked the rest api and it seems to still show image at the root: https://fly.io/docs/machines/api/machines-resource/#create-a-machine - AFAICT flyctl which uses fly-go (I think?) also has it in the same place: https://github.com/superfly/fly-go/blob/main/machine_types.go#L670

If it's not too much to ask, can you check the http requests like in https://github.com/andrewbaxter/terraform-provider-fly?tab=readme-ov-file#tips-for-adding-functionality ? It might be obvious from that, and I might not have time right away but I can creating a machine in flyctl with http logging on for comparison at some point too.

Also is there a flyctl command to get the machine details? What does it say for the image when you run that?

FWIW I'm not familiar with using fly deploy in conjunction with terraform. My guess is that it builds, uploads the docker image, then changes the image property of the machine to point to the uploaded image, in which case I'd expect that error regardless.

I'm straying further and further here, but when I was doing stuff I used terraform to build and upload the image with the image linked to the fly machine and that seemed to work well (of course, long ago, so if the api has changed that's fairly irrelevant now).

oscarhermoso commented 2 days ago

My proposed solution does not work.

I first attempted moving "image" inside of a new "build" struct` - but this fails with a 400 response (same as https://github.com/andrewbaxter/terraform-provider-fly/issues/9)

I have also tried including "image": "..." and "build": { "image": "..." } in the same payload - this successfully deploys but the Configuration page of the Fly.io dashboard is still missing the image = "..."

I will add logging for HTTP requests and see what I can find.

andrewbaxter commented 1 day ago

Interesting, sorry I don't have a good answer for you here. Keep me posted though and I'll help where I can.