|
| 1 | +--- |
| 2 | +title: "Building Serverless Functions in Taubyte" |
| 3 | +author: Zaoui Amine |
| 4 | +featured: true |
| 5 | +draft: false |
| 6 | +tags: |
| 7 | + - tutorials |
| 8 | + - serverless |
| 9 | + - functions |
| 10 | + - golang |
| 11 | + - wasm |
| 12 | + - cloud |
| 13 | +image: |
| 14 | + src: /blog/images/serverless-functions.png |
| 15 | + alt: Building Serverless Functions in Taubyte |
| 16 | +summary: Learn how to create, configure, and deploy serverless functions in Taubyte. This hands-on guide walks through building a simple ping-pong function in Go, from creation to testing both locally and in production. |
| 17 | +date: 2026-01-14T12:30:00Z |
| 18 | +categories: [Hand-on Learning] |
| 19 | +--- |
| 20 | + |
| 21 | +Serverless functions are the building blocks of modern cloud applications. In Taubyte, functions are compiled to WebAssembly and executed in a lightweight, secure sandbox. Let's build one from scratch. |
| 22 | + |
| 23 | +## Creating a Function |
| 24 | + |
| 25 | +From your project dashboard, navigate to **Functions** in the sidebar and click the **+** button. |
| 26 | + |
| 27 | +You have two options for creating a function: |
| 28 | + |
| 29 | +### Option 1: Start from Scratch |
| 30 | + |
| 31 | +Fill in the function details manually: |
| 32 | + |
| 33 | +| Field | Description | Example | |
| 34 | +|-------|-------------|---------| |
| 35 | +| Name | Unique identifier | `ping_pong` | |
| 36 | +| Protocol | Trigger type | `HTTPS` | |
| 37 | +| Description | What the function does | `Returns pong to a ping` | |
| 38 | +| Tags | Optional labels | `demo, http` | |
| 39 | +| Timeout | Maximum execution time | `10s` | |
| 40 | +| Memory | Allocated memory | `10MB` | |
| 41 | +| Method | HTTP method | `GET` | |
| 42 | +| Domain | Which domain to use | `GeneratedDomain` | |
| 43 | +| Path | URL path trigger | `/ping` | |
| 44 | +| Source | Code location | `.` (inline) or library name | |
| 45 | +| Entry Point | Function name in code | `ping` | |
| 46 | + |
| 47 | +### Option 2: Use a Template (Recommended) |
| 48 | + |
| 49 | +Templates accelerate development by providing working examples: |
| 50 | + |
| 51 | +1. Click **Template Select** |
| 52 | +2. Choose a language (Go, Rust, or AssemblyScript) |
| 53 | +3. Select a template (e.g., `ping_pong`) |
| 54 | +4. The template fills in most fields automatically |
| 55 | +5. Select your domain from the dropdown |
| 56 | + |
| 57 | +## Understanding the Configuration |
| 58 | + |
| 59 | +Click the **YAML** tab to see the configuration in raw format: |
| 60 | + |
| 61 | +```yaml |
| 62 | +id: '' |
| 63 | +description: Returns pong to a ping over HTTP |
| 64 | +tags: [] |
| 65 | +source: . |
| 66 | +trigger: |
| 67 | + type: https |
| 68 | + method: GET |
| 69 | + paths: |
| 70 | + - /ping |
| 71 | +domains: |
| 72 | + - GeneratedDomain |
| 73 | +execution: |
| 74 | + timeout: 10s |
| 75 | + memory: 10MB |
| 76 | + call: ping |
| 77 | +``` |
| 78 | +
|
| 79 | +Key fields: |
| 80 | +- **`source`**: Use `.` for inline code or a library name for external code |
| 81 | +- **`execution.call`**: The function name that must be exported by your WebAssembly module |
| 82 | + |
| 83 | +> **Tip**: You can also upload a YAML configuration file by clicking the upload button in the bottom left. |
| 84 | + |
| 85 | +## Writing the Code |
| 86 | + |
| 87 | +Switch to the **Code** tab to view and edit your function's source code. |
| 88 | + |
| 89 | +Here's a simple Go ping-pong function: |
| 90 | + |
| 91 | +```go |
| 92 | +package lib |
| 93 | +
|
| 94 | +import ( |
| 95 | + "github.com/taubyte/go-sdk/event" |
| 96 | +) |
| 97 | +
|
| 98 | +//export ping |
| 99 | +func ping(e event.Event) uint32 { |
| 100 | + h, err := e.HTTP() |
| 101 | + if err != nil { |
| 102 | + return 1 |
| 103 | + } |
| 104 | +
|
| 105 | + h.Write([]byte("PONG")) |
| 106 | + return 0 |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Code Guidelines |
| 111 | + |
| 112 | +1. **Package name**: Use any name **except** `main`—it's reserved for the build container |
| 113 | +2. **Taubyte SDK**: Use `github.com/taubyte/go-sdk` for fast, low-memory operations |
| 114 | +3. **Function export**: Annotate with `//export functionName` (TinyGo requirement) |
| 115 | +4. **Event system**: Functions receive an `event.Event` for lightweight execution |
| 116 | +5. **The `.taubyte` folder**: Contains build configurations—essential for proper execution |
| 117 | + |
| 118 | +## Pushing Your Changes |
| 119 | + |
| 120 | +Click **Done** when your function is ready, then: |
| 121 | + |
| 122 | +1. Click the **green button** in the bottom right to push changes |
| 123 | +2. Open the **domains** folder and find `GeneratedDomain.yaml` |
| 124 | +3. Copy the domain FQDN—you'll need it for testing |
| 125 | +4. Enter a commit message |
| 126 | +5. Push to GitHub |
| 127 | + |
| 128 | +## Triggering Builds |
| 129 | + |
| 130 | +### In Production |
| 131 | + |
| 132 | +Pushing to GitHub automatically triggers a build via webhooks. |
| 133 | + |
| 134 | +### In Dream (Local Development) |
| 135 | + |
| 136 | +GitHub can't access your local nodes, so trigger builds manually: |
| 137 | + |
| 138 | +**Terminal method:** |
| 139 | +```bash |
| 140 | +dream inject push-all |
| 141 | +``` |
| 142 | + |
| 143 | +**Console method:** |
| 144 | +1. Go to [console.taubyte.com](https://console.taubyte.com) |
| 145 | +2. Click **Dreamland** in the sidebar |
| 146 | +3. Select **Network → blackhole** |
| 147 | +4. From the top-right menu, choose **Push All** |
| 148 | + |
| 149 | +## Monitoring Builds |
| 150 | + |
| 151 | +Navigate to **Builds** in the sidebar. You'll see jobs for: |
| 152 | +- **Configuration build**: Quick, processes YAML files |
| 153 | +- **Code build**: Compiles your function to WebAssembly |
| 154 | + |
| 155 | +Click the **stack icon** next to a completed build to view function logs. |
| 156 | + |
| 157 | +## Testing Your Function |
| 158 | + |
| 159 | +### Find Your HTTP Port |
| 160 | + |
| 161 | +First, get the substrate HTTP port: |
| 162 | + |
| 163 | +```bash |
| 164 | +dream status substrate |
| 165 | +``` |
| 166 | + |
| 167 | +Output: |
| 168 | +```bash |
| 169 | +┌─────────────────────┬────────┬───────┐ |
| 170 | +│ substrate@blackhole │ http │ 14529 │ |
| 171 | +└─────────────────────┴────────┴───────┘ |
| 172 | +``` |
| 173 | + |
| 174 | +### Test with cURL |
| 175 | + |
| 176 | +Using the host header: |
| 177 | + |
| 178 | +```bash |
| 179 | +curl -H "Host: your-domain.blackhole.localtau" http://127.0.0.1:14529/ping |
| 180 | +``` |
| 181 | + |
| 182 | +Output: |
| 183 | +```bash |
| 184 | +PONG |
| 185 | +``` |
| 186 | + |
| 187 | +### Simplify Local Testing |
| 188 | + |
| 189 | +Add your generated domain to `/etc/hosts` for easier testing: |
| 190 | + |
| 191 | +```bash |
| 192 | +sudo nano /etc/hosts |
| 193 | +``` |
| 194 | + |
| 195 | +Add this line: |
| 196 | +```bash |
| 197 | +127.0.0.1 your-domain.blackhole.localtau |
| 198 | +``` |
| 199 | + |
| 200 | +Now you can test without the Host header: |
| 201 | + |
| 202 | +```bash |
| 203 | +curl http://your-domain.blackhole.localtau:14529/ping |
| 204 | +``` |
| 205 | + |
| 206 | +### Test via Console |
| 207 | + |
| 208 | +1. Navigate to **Functions** in the sidebar |
| 209 | +2. Find your function in the list |
| 210 | +3. Click the **lightning icon** to open it in a new tab |
| 211 | + |
| 212 | +## Troubleshooting |
| 213 | + |
| 214 | +| Issue | Solution | |
| 215 | +|-------|----------| |
| 216 | +| Function not responding | Verify port matches `dream status substrate` output | |
| 217 | +| Build failed | Check the **Builds** tab for error messages | |
| 218 | +| Changes not appearing | Run `dream inject push-all` again | |
| 219 | +| "PONG" not returning | Ensure entry point matches the exported function name | |
| 220 | + |
| 221 | + |
| 222 | +## Conclusion |
| 223 | + |
| 224 | +You've now learned how to create, configure, and deploy serverless functions in Taubyte. The process is straightforward: |
| 225 | + |
| 226 | +1. Create the function (manually or from a template) |
| 227 | +2. Write your code in Go, Rust, or AssemblyScript |
| 228 | +3. Push to GitHub |
| 229 | +4. Trigger a build (automatic in production, manual in Dream) |
| 230 | +5. Test your function |
| 231 | + |
| 232 | +Functions compile to WebAssembly for secure, fast, and portable execution across your entire cloud infrastructure. |
| 233 | + |
| 234 | +Next, explore [Libraries](/blog/posts/taubyte-libraries) to learn how to share code across multiple functions. |
| 235 | + |
0 commit comments