0

Create a New Load Balancer

by
Published Dec 20, 2024

To create a new load balancer instance, send a POST request to `/v2/load_balancers`. You can specify the Droplets that will sit behind the load balancer using one of two methods: * Set `droplet_ids` to a list of specific Droplet IDs. * Set `tag` to the name of a tag. All Droplets with this tag applied will be assigned to the load balancer. Additional Droplets will be automatically assigned as they are tagged. These methods are mutually exclusive.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Create a New Load Balancer
7
 * To create a new load balancer instance, send a POST request to
8
`/v2/load_balancers`.
9

10
You can specify the Droplets that will sit behind the load balancer using one
11
of two methods:
12

13
* Set `droplet_ids` to a list of specific Droplet IDs.
14
* Set `tag` to the name of a tag. All Droplets with this tag applied will be
15
  assigned to the load balancer. Additional Droplets will be automatically
16
  assigned as they are tagged.
17

18
These methods are mutually exclusive.
19

20
 */
21
export async function main(
22
  auth: Digitalocean,
23
  body:
24
    | ({ droplet_ids?: number[] } & {
25
        region?:
26
          | "ams1"
27
          | "ams2"
28
          | "ams3"
29
          | "blr1"
30
          | "fra1"
31
          | "lon1"
32
          | "nyc1"
33
          | "nyc2"
34
          | "nyc3"
35
          | "sfo1"
36
          | "sfo2"
37
          | "sfo3"
38
          | "sgp1"
39
          | "tor1"
40
          | "syd1";
41
      } & {
42
        id?: string;
43
        name?: string;
44
        project_id?: string;
45
        ip?: string;
46
        size_unit?: number;
47
        size?: "lb-small" | "lb-medium" | "lb-large";
48
        algorithm?: "round_robin" | "least_connections";
49
        status?: "new" | "active" | "errored";
50
        created_at?: string;
51
        forwarding_rules: {
52
          entry_protocol: "http" | "https" | "http2" | "http3" | "tcp" | "udp";
53
          entry_port: number;
54
          target_protocol: "http" | "https" | "http2" | "tcp" | "udp";
55
          target_port: number;
56
          certificate_id?: string;
57
          tls_passthrough?: false | true;
58
        }[];
59
        health_check?: {
60
          protocol?: "http" | "https" | "tcp";
61
          port?: number;
62
          path?: string;
63
          check_interval_seconds?: number;
64
          response_timeout_seconds?: number;
65
          unhealthy_threshold?: number;
66
          healthy_threshold?: number;
67
        };
68
        sticky_sessions?: {
69
          type?: "cookies" | "none";
70
          cookie_name?: string;
71
          cookie_ttl_seconds?: number;
72
        };
73
        redirect_http_to_https?: false | true;
74
        enable_proxy_protocol?: false | true;
75
        enable_backend_keepalive?: false | true;
76
        http_idle_timeout_seconds?: number;
77
        vpc_uuid?: string;
78
        disable_lets_encrypt_dns_records?: false | true;
79
        firewall?: { deny?: string[]; allow?: string[] };
80
        network?: "EXTERNAL" | "INTERNAL";
81
      })
82
    | ({ tag?: string } & {
83
        region?:
84
          | "ams1"
85
          | "ams2"
86
          | "ams3"
87
          | "blr1"
88
          | "fra1"
89
          | "lon1"
90
          | "nyc1"
91
          | "nyc2"
92
          | "nyc3"
93
          | "sfo1"
94
          | "sfo2"
95
          | "sfo3"
96
          | "sgp1"
97
          | "tor1"
98
          | "syd1";
99
      } & {
100
        id?: string;
101
        name?: string;
102
        project_id?: string;
103
        ip?: string;
104
        size_unit?: number;
105
        size?: "lb-small" | "lb-medium" | "lb-large";
106
        algorithm?: "round_robin" | "least_connections";
107
        status?: "new" | "active" | "errored";
108
        created_at?: string;
109
        forwarding_rules: {
110
          entry_protocol: "http" | "https" | "http2" | "http3" | "tcp" | "udp";
111
          entry_port: number;
112
          target_protocol: "http" | "https" | "http2" | "tcp" | "udp";
113
          target_port: number;
114
          certificate_id?: string;
115
          tls_passthrough?: false | true;
116
        }[];
117
        health_check?: {
118
          protocol?: "http" | "https" | "tcp";
119
          port?: number;
120
          path?: string;
121
          check_interval_seconds?: number;
122
          response_timeout_seconds?: number;
123
          unhealthy_threshold?: number;
124
          healthy_threshold?: number;
125
        };
126
        sticky_sessions?: {
127
          type?: "cookies" | "none";
128
          cookie_name?: string;
129
          cookie_ttl_seconds?: number;
130
        };
131
        redirect_http_to_https?: false | true;
132
        enable_proxy_protocol?: false | true;
133
        enable_backend_keepalive?: false | true;
134
        http_idle_timeout_seconds?: number;
135
        vpc_uuid?: string;
136
        disable_lets_encrypt_dns_records?: false | true;
137
        firewall?: { deny?: string[]; allow?: string[] };
138
        network?: "EXTERNAL" | "INTERNAL";
139
      }),
140
) {
141
  const url = new URL(`https://api.digitalocean.com/v2/load_balancers`);
142

143
  const response = await fetch(url, {
144
    method: "POST",
145
    headers: {
146
      "Content-Type": "application/json",
147
      Authorization: "Bearer " + auth.token,
148
    },
149
    body: JSON.stringify(body),
150
  });
151
  if (!response.ok) {
152
    const text = await response.text();
153
    throw new Error(`${response.status} ${text}`);
154
  }
155
  return await response.json();
156
}
157