|
| 1 | +# Cloud Connect on a Development Machine |
| 2 | + |
| 3 | +Works with Spice CLI v2.2 or later. |
| 4 | + |
| 5 | +Use this recipe to connect a local Spice instance to Spice Cloud. You will deploy live changes, deliver a secret, and reconnect the instance. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- A Spice Cloud organization where you are an owner or admin |
| 10 | +- Docker |
| 11 | +- Spice CLI v2.2 or later |
| 12 | + |
| 13 | +Install the Spice CLI: |
| 14 | + |
| 15 | +```shell |
| 16 | +curl https://install.spiceai.org | /bin/bash |
| 17 | +``` |
| 18 | + |
| 19 | +## 1. Connect the instance |
| 20 | + |
| 21 | +Clone the cookbook and open this recipe: |
| 22 | + |
| 23 | +```shell |
| 24 | +git clone https://github.com/spiceai/cookbook.git |
| 25 | +cd cookbook/cloud-connect-dev |
| 26 | +spice connect |
| 27 | +``` |
| 28 | + |
| 29 | +Log in to Spice Cloud when the command prompts you. Select an organization. Accept `cloud-connect-dev` as the project name. |
| 30 | + |
| 31 | +If that name is in use, accept the suggested name. |
| 32 | + |
| 33 | +Leave the runtime open. Open the monitor link in the output. |
| 34 | + |
| 35 | +In a second terminal, check the connection: |
| 36 | + |
| 37 | +```shell |
| 38 | +cd cookbook/cloud-connect-dev |
| 39 | +spice connect status |
| 40 | +``` |
| 41 | + |
| 42 | +## 2. Deploy a live change |
| 43 | + |
| 44 | +A deployment replaces the Spicepod the instance runs. In the Spice Cloud portal, open the project Spicepod and paste in the contents of this recipe's local `spicepod.yaml`, so the deployed Spicepod still defines the `data` dataset. |
| 45 | + |
| 46 | +Add this view to the project Spicepod, below the datasets: |
| 47 | + |
| 48 | +```yaml |
| 49 | +views: |
| 50 | + - name: recent |
| 51 | + sql: SELECT * FROM data LIMIT 10 |
| 52 | +``` |
| 53 | +
|
| 54 | +Deploy the Spicepod. You do not have to restart the instance. |
| 55 | +
|
| 56 | +Query the view: |
| 57 | +
|
| 58 | +```shell |
| 59 | +spice sql |
| 60 | +``` |
| 61 | + |
| 62 | +```sql |
| 63 | +SELECT count(*) FROM recent; |
| 64 | +``` |
| 65 | + |
| 66 | +## 3. Deliver a secret |
| 67 | + |
| 68 | +Create a password and start PostgreSQL: |
| 69 | + |
| 70 | +```shell |
| 71 | +export SPICE_DEMO_PG_PASSWORD="$(openssl rand -hex 16)" |
| 72 | +docker compose up -d |
| 73 | +``` |
| 74 | + |
| 75 | +Confirm that PostgreSQL contains the sample data: |
| 76 | + |
| 77 | +```shell |
| 78 | +docker compose exec postgres \ |
| 79 | + psql -U spice_reader -d spice_demo \ |
| 80 | + -c 'SELECT count(*) FROM public.orders;' |
| 81 | +``` |
| 82 | + |
| 83 | +In the portal, open the project's **Settings → Secrets** and add: |
| 84 | + |
| 85 | +| Name | Value | |
| 86 | +| ------------- | ---------------------------------- | |
| 87 | +| `PG_PASSWORD` | Value of `$SPICE_DEMO_PG_PASSWORD` | |
| 88 | + |
| 89 | +Secret names are matched exactly. A Spicepod that references a name the project does not define is rejected when you deploy it, and the portal names the closest match it holds. |
| 90 | + |
| 91 | +Add this dataset to the project Spicepod in the portal — not to the local `spicepod.yaml`: |
| 92 | + |
| 93 | +```yaml |
| 94 | +datasets: |
| 95 | + - from: postgres:public.orders |
| 96 | + name: orders |
| 97 | + params: |
| 98 | + pg_host: localhost |
| 99 | + pg_port: "55432" |
| 100 | + pg_db: spice_demo |
| 101 | + pg_user: spice_reader |
| 102 | + pg_pass: ${secrets:PG_PASSWORD} |
| 103 | + pg_sslmode: disable |
| 104 | +``` |
| 105 | +
|
| 106 | +Deploy the Spicepod. Then query the PostgreSQL data: |
| 107 | +
|
| 108 | +```shell |
| 109 | +spice sql |
| 110 | +``` |
| 111 | + |
| 112 | +```sql |
| 113 | +SELECT customer, total_cents FROM orders ORDER BY id LIMIT 3; |
| 114 | +``` |
| 115 | + |
| 116 | +The query returns: |
| 117 | + |
| 118 | +```text |
| 119 | ++----------+-------------+ |
| 120 | +| customer | total_cents | |
| 121 | +| varchar | int32 | |
| 122 | ++----------+-------------+ |
| 123 | +| acme | 12900 | |
| 124 | +| globex | 4550 | |
| 125 | +| initech | 31875 | |
| 126 | ++----------+-------------+ |
| 127 | +``` |
| 128 | + |
| 129 | +Confirm the delivery: |
| 130 | + |
| 131 | +```shell |
| 132 | +spice connect status |
| 133 | +``` |
| 134 | + |
| 135 | +```text |
| 136 | + secrets: 1 delivered: PG_PASSWORD |
| 137 | +``` |
| 138 | + |
| 139 | +Status reports secret names. Values stay in the runtime process. |
| 140 | + |
| 141 | +Three properties of a delivered secret: |
| 142 | + |
| 143 | +- The Spicepod declares no `secrets:` section. Delivered secrets are a built-in store, so the same Spicepod runs unchanged as a managed app and on a self-hosted instance. |
| 144 | +- A local value wins. The built-in store has the lowest precedence, so an `env` or `.env.local` value for `PG_PASSWORD` overrides the delivered one. Unset it to use the delivered value. |
| 145 | +- Only the first delivery applies live. Rotating a secret that a component already resolved requires a restart, like a `runtime` change. |
| 146 | + |
| 147 | +If you deploy the dataset before adding the secret, the runtime names the unresolved reference and that dataset fails to load. Add the secret and deploy again. |
| 148 | + |
| 149 | +## 4. Deploy a change that requires a restart |
| 150 | + |
| 151 | +Add this setting to the project Spicepod: |
| 152 | + |
| 153 | +```yaml |
| 154 | +runtime: |
| 155 | + task_history: |
| 156 | + captured_output: truncated |
| 157 | +``` |
| 158 | +
|
| 159 | +Deploy the Spicepod. The runtime names the sections that need a start, in the terminal running the instance: |
| 160 | +
|
| 161 | +```text |
| 162 | +INFO Spice Cloud Connect: applied the deployed spicepod (1 datasets, 0 models, 0 catalogs, 1 views); runtime takes effect when this instance next starts |
| 163 | +``` |
| 164 | +
|
| 165 | +The project in Spice Cloud reports the same pending sections. The instance continues to serve queries until you restart it. |
| 166 | +
|
| 167 | +## 5. Restart and reconnect |
| 168 | +
|
| 169 | +In the first terminal, press `Ctrl-C`. Start the instance again from the same directory: |
| 170 | + |
| 171 | +```shell |
| 172 | +spice run |
| 173 | +``` |
| 174 | + |
| 175 | +You do not need to run `spice connect` again. The existing identity reconnects the instance. |
| 176 | + |
| 177 | +The instance now serves the deployed `runtime` settings, and the deployment reports nothing pending. |
| 178 | + |
| 179 | +> **Warning:** Spice Cloud invalidates the identity if the instance stays offline for more than 30 days. Enroll the instance again to reconnect it. |
| 180 | + |
| 181 | +## 6. Validate the recipe |
| 182 | + |
| 183 | +Run the local checks: |
| 184 | + |
| 185 | +```shell |
| 186 | +./validate.sh |
| 187 | +``` |
| 188 | + |
| 189 | +These checks do not connect to Spice Cloud or use credentials. |
| 190 | + |
| 191 | +## 7. Clean up |
| 192 | + |
| 193 | +Stop the runtime. Remove the Cloud Connect instance and project: |
| 194 | + |
| 195 | +```shell |
| 196 | +spice connect remove |
| 197 | +``` |
| 198 | + |
| 199 | +Stop PostgreSQL and delete its volume: |
| 200 | + |
| 201 | +```shell |
| 202 | +docker compose down -v |
| 203 | +unset SPICE_DEMO_PG_PASSWORD |
| 204 | +``` |
| 205 | + |
| 206 | +## Run as a service |
| 207 | + |
| 208 | +To keep the instance running after you close the terminal, see [Cloud Connect as a service](https://spiceai.org/docs/deployment/cloud/cloud-connect/service). |
| 209 | + |
| 210 | +Windows does not support the managed service. |
| 211 | + |
| 212 | +## Learn more |
| 213 | + |
| 214 | +- [Cloud Connect](https://spiceai.org/docs/deployment/cloud/cloud-connect) |
| 215 | +- [`spice connect` reference](https://spiceai.org/docs/cli/reference/connect) |
0 commit comments