Terraform.io is a helpful tool aimed at moving your infrastructure configuration into code.

Among others, Terraform includes a DigitalOcean (referral link) provider for defining resources (i.e. Droplets, Domains, SSH Keys), which is great for us as some of our infrastructure runs on DigitalOcean (the other main one of course being Amazon’s AWS).

Unfortunately you’ll quickly notice that a terraform refresh shows no output, and there is no way to import existing infrastructure! Certainly makes it harder to get going with Terraform for now but it looks like this feature is still being worked on.

However with a little bit of JSON-munging we can convince terraform refresh to find our Droplets. Fortunately all it takes is the Droplet IDs. You can find these by logging into your DO account and looking at the IDs of each Droplet in the list (if you have a lot you might want to consider using the DO API to get this list). Then create your terraform.tfstate file with a “skeleton” containing the IDs:

{
    "resources": {
        "digitalocean_droplet.my_name_here": {
            "id": "ID HERE",
            "attributes": {
                "id": "ID HERE"
            }
        }
    }
}

Running terraform refresh should now find each of these resources, and if all goes well you will now see the .tfstate file update with the attributes fetched by the API.

{
    "version": 1,
    "serial": 1,
    "modules": [
        {
            "path": [
                "root"
            ],
            "resources": {
                "digitalocean_droplet.your_droplet_name": {
                    "type": "digitalocean_droplet",
                    "depends_on": [
                        "digitalocean_ssh_key.default"
                    ],
                    "primary": {
                        "id": "0000000",
                        "attributes": {
                            "id": "0000000",
                            "image": "ubuntu-14.04-x64"
                        }
                    }
                }
            }
        }
    ]
}

From here you should be able to terraform plan as normal :)