Report this

What is the reason for this report?

How do I create multiple DigitalOcean Droplets with different configurations using Terraform, and assign each one a floating IP?

Posted on April 16, 2022

I’m setting up a multi-tier architecture on DigitalOcean and want to provision several Droplets with Terraform.

Each Droplet will have a unique configuration (different sizes, regions, and tags). Additionally, I need to assign a floating IP to each Droplet for easier management and failover.

How can I structure my main.tf file to handle this efficiently, and what’s the best way to manage floating IPs in Terraform?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hey!

Great question! With the recent updates, DigitalOcean has renamed Floating IPs to Reserved IPs. So, you’ll want to use the digitalocean_reserved_ip resource instead.

Here’s a quick example of how you can create multiple Droplets with different configurations and assign Reserved IPs using Terraform.

  1. You can define each Droplet with different sizes, regions, and tags by using a for_each or creating separate resources for each Droplet in your main.tf.

  2. To assign a Reserved IP to each Droplet, you need to replace the deprecated floating_ip resource with reserved_ip. Here’s an updated example:

provider "digitalocean" {
  token = var.digitalocean_token
}

resource "digitalocean_droplet" "web" {
  for_each = {
    "droplet1" = "nyc1"
    "droplet2" = "sfo2"
  }

  name   = each.key
  size   = var.droplet_sizes[each.key]
  region = each.value
  image  = "ubuntu-20-04-x64"
  tags   = ["web"]
}

resource "digitalocean_reserved_ip" "ip" {
  for_each = digitalocean_droplet.web
  region   = each.value.region
}

resource "digitalocean_reserved_ip_assignment" "assign_ip" {
  for_each        = digitalocean_droplet.web
  droplet_id      = digitalocean_droplet.web[each.key].id
  reserved_ip     = digitalocean_reserved_ip.ip[each.key].address
}
  1. To handle different sizes, you can use a map or a variable in your variables.tf like this:
variable "droplet_sizes" {
  type = map(string)
  default = {
    "droplet1" = "s-2vcpu-4gb"
    "droplet2" = "s-1vcpu-2gb"
  }
}
  1. Once you have your configurations, run:
terraform init
terraform plan
terraform apply

This will create multiple Droplets with different configurations and assign each one a Reserved IP.

If you need to expand this further, you can adjust the for_each loop or add more configurations as needed.

On another note, if you’re just getting started with Terraform and DigitalOcean, this ebook here might be helpful: Introduction to Terraform ebook.

- Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.