# Manage Cloud with Pulumi

Manage your Cloud servers, firewalls and SSH keys as code with Pulumi.

## Install the provider

[code-tabs]
```bash
[label TypeScript]
npm install @better_stack/pulumi
```
```bash
[label Python]
pip install pulumi-betterstack
```
[/code-tabs]

Pulumi downloads the provider plugin on your first `pulumi up`.

## Configure the provider

Create a global API token in [Settings → API tokens → Global tokens](https://betterstack.com/settings/global-api-tokens). Store it as a secret when Pulumi asks for it, and set the [data center](https://betterstack.com/docs/cloud/servers/regions/) your resources are in:

```bash
[label Configure your stack]
pulumi config set --secret betterstack:apiToken
pulumi config set betterstack:availabilityZone us-west-2a
```

## Create your first server

Add your SSH key, a firewall that lets in SSH and HTTPS, and a server that uses both. Replace `t123` with your team's ID from `betterstack team list` in the [Cloud CLI](https://betterstack.com/docs/cloud/getting-started/cli/):

[code-tabs]
```typescript
[label TypeScript]
import * as fs from "fs";
import * as os from "os";
import * as betterstack from "@better_stack/pulumi";

const teamId = "t123";

const key = new betterstack.SshKey("laptop", {
    teamId,
    slug: "my-laptop",
    publicKey: fs.readFileSync(`${os.homedir()}/.ssh/id_ed25519.pub`, "utf8"),
});

const firewall = new betterstack.Firewall("web", { teamId, name: "web" });

for (const [name, port] of [["ssh", 22], ["https", 443]] as const) {
    new betterstack.FirewallRule(name, {
        firewallId: firewall.id,
        direction: "inbound",
        protocol: "tcp",
        ipAddresses: ["0.0.0.0/0", "::/0"],
        portFrom: port,
    });
}

const server = new betterstack.Server("web", {
    teamId,
    name: "web-1",
    serverType: "AM1",
    image: "ubuntu-26-04",
    sshKeys: [key.slug],
    firewallIds: [firewall.id],
});

export const serverIp = server.ipAddresses.apply(addresses => addresses.find(a => a.type === "ipv4")!.ip);
```
```python
[label Python]
import os
import pulumi
import pulumi_betterstack as betterstack

team_id = "t123"

key = betterstack.SshKey("laptop",
    team_id=team_id,
    slug="my-laptop",
    public_key=open(os.path.expanduser("~/.ssh/id_ed25519.pub")).read())

firewall = betterstack.Firewall("web", team_id=team_id, name="web")

for name, port in [("ssh", 22), ("https", 443)]:
    betterstack.FirewallRule(name,
        firewall_id=firewall.id,
        direction="inbound",
        protocol="tcp",
        ip_addresses=["0.0.0.0/0", "::/0"],
        port_from=port)

server = betterstack.Server("web",
    team_id=team_id,
    name="web-1",
    server_type="AM1",
    image="ubuntu-26-04",
    ssh_keys=[key.slug],
    firewall_ids=[firewall.id])

pulumi.export("serverIp", server.ip_addresses.apply(
    lambda addresses: next(a.ip for a in addresses if a.type == "ipv4")))
```
[/code-tabs]

```bash
[label Create the server]
pulumi up
```

Connect as `root`:

```bash
[label Connect to the server]
ssh root@$(pulumi stack output serverIp)
```

A firewall created this way has no inbound rules of its own, so keep the SSH rule to reach the server.

[info]
#### Removing a server?
Servers start with `deletionProtection` on. To remove one with `pulumi destroy`, set `deletionProtection: false` and run `pulumi up` first.
[/info]

## Need help?

Please let us know at hello@betterstack.com.  
We're happy to help! 🙏
