[ Skip to main content ]
← Back to blog

Terraform State with DigitalOcean

 |  Infrastructure + stack Web + software development

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 :)


Share this post:

We'd love to hear from you

Technology enables us to do some awesome things. We love how a simple greeting or question can turn into something amazing.

Contact us
Magento - Wellington, Auckland

Magento

If your business is serious about ecommerce success, we can help. Magento offers flexible, feature-rich solutions that address a wide range of business sizes, needs and budgets.

PHP - Wellington, Auckland

PHP

PHP is a widely-used, general-purpose scripting language, that is especially suited for web development and can be embedded into HTML.

Silverstripe - Wellington, Auckland

Silverstripe

Silverstripe CMS is a web content management system used by governments, businesses and non-profit organisations around the world. It is the Common Web Platform for New Zealand’s Public Sector.

Symfony - Wellington, Auckland

Symfony

Symfony is a PHP framework for web projects. Speed up the creation and maintenance of your PHP web applications. Replace the repetitive coding tasks by power, control and pleasure.