Search code examples
terraformdigital-oceanopentofu

How do I append a DO droplet to a project with terraform/tofu?


I want to create a new droplet, then add it to an existing project, which has other existing droplets in it already.

I can see how to create a new project and put the droplet in that - https://registry.terraform.io/providers/digitalocean/digitalocean/latest/docs/resources/project

I can see how to put the droplet in an existing project - https://registry.terraform.io/providers/digitalocean/digitalocean/latest/docs/resources/project_resources

But both of these options just sets the resources value to be a list of the single droplet, which is going to wipe out anything else that's already there, surely?

How do I append a new droplet to an existing project? Essentially what I want is something like

resources = resources.append(digitalocean_droplet.artbio_se.urn)

Solution

  • First of all please read about "state" concept in the terraform: https://developer.hashicorp.com/terraform/language/state

    If you want to add something already existing, you need to:

    • create terraform configuration for that (create *.tf file with resource description);
    • import your existing infrastructure to the terraform state by using terraform import command;

    But this will work only if your terraform provider support of existing infrastructure.

    For example digitalocean_project support import, how described in their documentation:

    Projects can be imported using the id returned from DigitalOcean, e.g.
    terraform import digitalocean_project.myproject 245bcfd0-7f31-4ce6-a2bc-475a116cca97
    

    Also digitalocean_droplet support import, like:

    terraform import digitalocean_droplet.mydroplet 100823
    

    And digitalocean have a resource digitalocean_project_resources which allow you to assign resources to a already existing DigitalOcean Project.

    And if you already have project and want to add existings droplets and create a new one you need:

    • create configuration like described here and describe all existing droplets in the configuration;
    • import all existing droplets as I wrote previously;
    • run terraform plan and make sure that no new droplets will be created, that means your import worked correctly;
    • add a new droplet to this configuration and run terraform plan and you will see that terraform wants create only the new droplet;