DAlperin / terraform-provider-fly-io

Mozilla Public License 2.0
9 stars 2 forks source link

Unexpected error on import of app resource #10

Closed hb9cwp closed 2 years ago

hb9cwp commented 2 years ago

Upon trying to import the state of an app resource that I had launched on Fly.io earlier using flyctl while following the Learn tutorial "Import Terraform Configuration" :

$ terraform import fly_app.exampleApp https-helloworld-11
fly_app.exampleApp: Importing from ID "https-helloworld-11"...
╷
│ Error: State Write Error
│ 
│ An unexpected error was encountered trying to write an attribute to the state. This is always an error in the provider. Please
│ report the following to the provider developer:
│ 
│ error getting attribute type in schema: AttributeName("id") still remains in the path: could not find attribute or block "id"
│ in schema

Outside of Terraform, start the app $ fly launch --name https-helloworld-11 using the Dockerfile that runs a tiny static Web server written in Go "GoStatic":

FROM pierrezemb/gostatic
COPY ./public/ /srv/http/
# enable logging, /health endpoint, ...
ENTRYPOINT ["/goStatic"]
CMD ["-log-level=debug", "-enable-health"]

Then, try to import the (state of) app into the Terraform, see the "emtpy" .tf configuration below:

$ export FLY_TOKEN=$(fly auth token)
$ terraform init
$ terraform -v
Terraform v1.2.1     (very latest from yesterday)
on linux_amd64       (in Crostini/Debian Linux within ChromeOS on a Chromebook)
+ provider registry.terraform.io/dalperin/fly-io v0.0.3
$ terraform validate
Success! The configuration is valid.
$ terraform import fly_app.exampleApp https-helloworld-11
...

Terraform configuration file, still mostly "empty", but prepared to be un-commented & merged once TF state is imported successfully:

$ cat https-helloworld_cert.tf
terraform {
  required_providers {
    fly = {
      #source = "dov.dev/fly/fly-provider"
      source  = "DAlperin/fly-io"
      version = "0.0.3"
    }
  }
}

provider "fly" {
  #  https://registry.terraform.io/providers/DAlperin/fly-io/latest/docs#example-usage
  # Don't do this:
  #flytoken = "abc123"    # If not set checks env for FLY_TOKEN

  # Use the FLY_TOKEN env variable instead.
  # $ export FLY_TOKEN=$(fly auth token)
  # $ echo $FLY_TOKEN
}

resource "fly_app" "exampleApp" {
  name = "https-helloworld-11"
}

/*
resource "fly_app" "exampleApp" {
  name = "https-helloworld-11"
  preferred_region = "ams"
  #regions = ["ewr", "ord", "lax", "mia"]       # for autoscaling
}

resource "fly_cert" "exampleCert" {
  app = "https-helloworld-11"
  hostname = "example.com"
  depends_on = [fly_app.exampleApp]
}
*/
DAlperin commented 2 years ago

Ah ok I see whats happening here. Flys api internally uses name and id interchangeably, so in your example both the name and id of your app would be https-helloworld-11. Terraform it seems expects that the "primary key" if it were is a field called id not name. Let me look into the best way to make terraform not complain about that.

Fly is now very kindly sponsoring my work on this provider. So it should move over there sometime this week and become rapidly more stable and ready for production use.

Thank you for the report! I'll keep you posted. I am doing a big cleanup today as I prepare to migrate it to the fly repo and I am hoping to clear this issue up

DAlperin commented 2 years ago

Should be fixed by v0.0.4 https://registry.terraform.io/providers/DAlperin/fly-io/0.0.4

Feel free to re-open or file new issue if it breaks in a new way. The fly team is working on a slight api modification that will allow importing all resources seamless, so those should work soon too.

hb9cwp commented 2 years ago

Great, works now, thanks for the quick response! Also, glad to hear about Fly supporting you.

Just for the records, here is what I did for testing based on the above, above bumping the required version from 0.0.3 up to 0.0.4 in the .tf configuration above:

$ terraform init -upgrade

Initializing the backend...

Initializing provider plugins...
- Finding dalperin/fly-io versions matching "0.0.4"...
- Installing dalperin/fly-io v0.0.4...
- Installed dalperin/fly-io v0.0.4 (self-signed, key ID 741FCF3CF97659FF)
...
Terraform has been successfully initialized!
...

$ terraform import fly_app.exampleApp https-helloworld-11
fly_app.exampleApp: Importing from ID "https-helloworld-11"...
fly_app.exampleApp: Import prepared!
  Prepared fly_app for import
fly_app.exampleApp: Refreshing state... [id=https-helloworld-11]

Import successful!