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!
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.
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
.
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
}
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"
}
}
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.