Question

Can I assign a firewall while creating a droplet via API?

I would like to provision droplets for my clients via the API and everything works great except I don’t see anything in the documentation for assigning a cloud firewall while creating a droplet.

Is there any way to automatically do this at the same time? An example of the code I am currently using:


// Setup droplet details
$data = array(
"name" => "$hostname",
"region" => "$location",
"size" => "s-1vcpu-1gb",
"monitoring" => "true",
"image" => "centos-7-x64",
"user_data" => "$user_data",
);

# Convert droplet details into JSON
$data_string = json_encode($data);

// Setup actually sending to API
$ch = curl_init('https://api.digitalocean.com/v2/droplets');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Authorization: Bearer $TOKEN",
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);

$result = curl_exec($ch);

// Actually send to API and show result
print_r($result);

}    
?>


Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

I would suggest taking a look at something like terraform to do your droplet provisioning.

resource "digitalocean_droplet" "web" {
  name      = "web-1"
  size      = "512mb"
  image     = "centos-7-x64"
  region    = "nyc3"
}

resource "digitalocean_firewall" "web" {
  name = "only-22-80-and-443"

  droplet_ids = ["${digitalocean_droplet.web.id}"]

  inbound_rule = [
    {
      protocol           = "tcp"
      port_range         = "22"
      source_addresses   = ["192.168.1.0/24", "2002:1:2::/48"]
    },
    {
      protocol           = "tcp"
      port_range         = "80"
      source_addresses   = ["0.0.0.0/0", "::/0"]
    },
    {
      protocol           = "tcp"
      port_range         = "443"
      source_addresses   = ["0.0.0.0/0", "::/0"]
    },
  ]

  outbound_rule = [
    {
      protocol                = "tcp"
      port_range              = "53"
      destination_addresses   = ["0.0.0.0/0", "::/0"]
    },
    {
      protocol                = "udp"
      port_range              = "53"
      destination_addresses   = ["0.0.0.0/0", "::/0"]
    },
  ]
}

https://www.terraform.io/docs/providers/do/index.html

I don’t see anyway to add a tag to firewalls in the web interface, is this maybe something that can only be done by API?

One of possible solutions is to use tags. You can create a firewall and assign it a tag, and then use that tag on Droplet creation. That way, all rules defined in the firewall will be applied to all Droplets with the assigned tag.