# GCP and SkyPilot from a swarm

Already use SkyPilot on GCP? Four steps let swarm agents launch clusters in your project.
Page with a video: https://acyclic.dev/guides/gcp-skypilot

## Optional: install the CLI

Skip if `acyclic --version` already works. Signing in opens a browser once.

```sh
curl -fsSL https://acyclic.dev/install.sh | sh
acyclic swarm login --site https://acyclic.dev
```

## 1. Allow SSH through IAP

Swarm boxes reach your VMs through Google's IAP, so port 22 must accept it. Skip the last command if your network already allows SSH from anywhere.

```sh
PROJECT=$(gcloud config get project)
gcloud services enable iap.googleapis.com --project $PROJECT
gcloud compute firewall-rules create allow-iap-ssh --project $PROJECT \
  --network default --allow tcp:22 --source-ranges 35.235.240.0/20
```

## 2. Copy your key into the folder

Use the service-account key SkyPilot already uses, in the folder you start the swarm from.

```sh
cp "$GOOGLE_APPLICATION_CREDENTIALS" gcp-sa.key
```

## 3. Start the swarm

Start the task with these lines. Each agent signs in, then uses `sky` as you do.

```sh
acyclic swarm run --dir . --include gcp-sa.key --watch --task "First run: acyclic-gcp-setup --key gcp-sa.key
Launch clusters with: sky launch -y -i 15 --down ...
When done, run: sky down -a -y

Then: <your task>"
```

## 4. Clean up

Clusters delete themselves after 15 idle minutes. Check, then remove the key copy.

```sh
sky status --refresh
rm gcp-sa.key
```

## No key? Use your gcloud login

Pack it instead of step 2, upload it with `--include gcloud.tgz`, and make the first task line `mkdir -p ~/.config/gcloud && tar -xzf gcloud.tgz -C ~/.config/gcloud && acyclic-gcp-setup`.

```sh
tar -czf gcloud.tgz -C ~/.config/gcloud credentials.db configurations \
  active_config application_default_credentials.json
```

## Optional: a key just for the swarm

Create a separate account and key instead of step 2:

```sh
SA=acyclic-swarm@$PROJECT.iam.gserviceaccount.com
gcloud iam service-accounts create acyclic-swarm --project $PROJECT
for role in editor iap.tunnelResourceAccessor iam.serviceAccountUser; do
  gcloud projects add-iam-policy-binding $PROJECT --member serviceAccount:$SA \
    --role roles/$role --condition None --quiet > /dev/null
done
gcloud iam service-accounts keys create gcp-sa.key --iam-account $SA
```

Delete it when the swarm is done:

```sh
gcloud iam service-accounts delete $SA --quiet
rm gcp-sa.key
```

## Take the result

Wait for the swarm to finish, then apply its patch to your checkout. The change is staged, so review it with `git diff --cached`.

```sh
acyclic swarm watch <id>
acyclic swarm pull <id> --apply
git diff --cached
```

## Cost

Swarms are billed at cost (sandbox time plus model usage) against prepaid credit, then a card. New accounts get $5 of credit. GCP bills you directly for the VMs. Each swarm has a budget cap, and the dashboard's [Billing](https://acyclic.dev/dashboard/billing) page shows every charge.

## How it works

- `acyclic-gcp-setup` installs `gcloud` and SkyPilot in the agent's box and signs in with your key.
- `sky launch` creates VMs in your project, billed to you, just like from your laptop.
- The agent connects to them over SSH through IAP, runs the job and keeps the results.

## Notes

- Every agent can use the key, so set a [budget alert](https://console.cloud.google.com/billing/budgets).
- The key's account needs the IAP tunnel role (`roles/iap.tunnelResourceAccessor`). Project owners have it.
- GCP only for now.
