DEV Community

Cover image for Configuring Digital Ocean droplets with cloud-init
Dmitriy A. for appfleet

Posted on • Originally published at appfleet.com

Configuring Digital Ocean droplets with cloud-init

Introduction

Oftentimes there will be cases where you will want to automate the provisioning and configuration of your Digital Ocean droplets. There are a plethora of tools out there, however, cloud-init is an industry-standard that is used to initialize and configure instances with user-data.

What is Terraform?

Terraform is an Infrastructure-as-code tool that allows users to build, change, and version your infrastructure safely and efficiently. It uses a high-level syntax to declaratively provision and manage infrastructure, allowing the ability to break down the configuration into smaller chunks for better organization, re-use, and maintainability. Information on installing and running Terraform can be found here. By passing the user_data parameter into a Terraform.yaml file, you can use automation to configure your Digital Ocean droplet at boot time. More on that below.

Using Terraform to configure Digital Ocean droplets with cloud-init

If Terraform is your preferred infrastructure provisioning method then you can find the Digital Ocean Terraform Provider at the Github Repo here.

For any infrastructure provider, when using Terraform as a provisioning tool you will always need to specify the provider block as seen here:

provider "digitalocean" {
    token = var.do_token
}

Here's an example module that utilizes user-data to configure a droplet at boot time:

# Create a server
resource "digitalocean_droplet" "my-dream-server-1" {
    image  = "ubuntu-18-04-x64"
    name   = "web-1"
    region = "nyc2"
    size   = "s-1vcpu-1gb"
    user_data = "I2Nsb3VkLWNvbmZpZwpwYWNrYWdlczoKICAtIGlmdG9wCiAgLSBubW9uCg=="
}

With this module, you have a resource that is designating digitalocean_droplet as the type of resource you want to provision, and using variables such as project_id and user_data to handle the provisioning. When you provide the string for user_data, you are designating a startup script that the bare-metal server will run on boot-up.

Using cloud-init to configure Digital Ocean droplets

You can provision new servers via the API to fetch user data for your Packet device via the cloud-init service. This allows you to automate various server configuration tasks by fetching user data directives upon server deployment. Your provided tasks will be executed when your server boots for the first time. There are two ways of doing this - shell scripts or cloud-init directives. We're going to talk about cloud-init directives.

Cloud-Init directives are executed when your server boots for the first time, but the syntax is slightly different. Your scenario must start with #cloud-config line, otherwise user data directives will be rejected. For further reference, I recommend checking the cloud-init official documentation: https://cloudinit.readthedocs.io/en/latest/index.html

A simple example of a cloud-init script that would be passed is:

#cloud-config

packages:

 - httpd

 - mariadb-server

runcmd:

 - systemctl start httpd

 - sudo systemctl enable httpd

 - [ sh, -c, "chmod 755 /var/tmp" ]

In order to pass this data scenario to the Digital Ocean API, it must first be converted into base64 format. On a Linux system you would do the following for your test.yaml file:

:# base64 test.yaml

I2Nsb3VkLWNvbmZpZwpwYWNrYWdlczogCiAtIGh0dHBkIAogLSBtYXJpYWRiLXNlcnZlcgpydW5j

bWQ6CiAtIHN5c3RlbWN0bCBzdGFydCBodHRwZAogLSBzdWRvIHN5c3RlbWN0bCBlbmFibGUgaHR0

cGQKIC0gWyBzaCwgLWMsICJjaG1vZCA3NTUgL3Zhci90bXAiIF0K

This output text then has to be fetched via Digital Ocean API user_data parameter when ordering a new server.

Putting it all together

To see this in action, specify the resources provider so that you can designate Digital Ocean as the platform you're provisioning to. Your final script should look like this:

# Set provider

provider "digitlocean" {
    token = var.do_token
}

# Create a server
resource "digitalocean_droplet" "my-dream-server-1" {
    image  = "ubuntu-18-04-x64"
    name   = "web-1"
    region = "nyc2"
    size   = "s-1vcpu-1gb"
    user_data = "I2Nsb3VkLWNvbmZpZwpwYWNrYWdlczoKICAtIGlmdG9wCiAgLSBubW9uCg=="
}

Finishing up

That's all there is to it! Terraform is a really great tool for automating infrastructure once you understand the syntax and how it works.

Top comments (2)

Collapse
 
joshoftt profile image
Josh-of-TT

Worked through this for a few hours - and couldn't get it to work.

Hope others have better luck.

Collapse
 
alicesowerby profile image
Alice Sowerby

Hey there! Since this guide was written we have rebranded to Equinix Metal, and updated some of our Terraform URLs. Would you consider updating this guide perhaps?