The Simplest Way to Make Cloud Functions and TimescaleDB Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

You pair cloud functions and timescaledb to ingest and query time-series data on demand. Event triggers wake up short-lived compute nodes that write metrics into time-partitioned tables. This setup fits sensor logs, billing events, and app metrics very well. You pay for compute only when new events arrive in your system. Meanwhile, the database groups your rows by time for fast reads and storage rule checks. Yet linking stateless functions to a stateful database brings network and security risk

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You pair cloud functions and timescaledb to ingest and query time-series data on demand. Event triggers wake up short-lived compute nodes that write metrics into time-partitioned tables.

This setup fits sensor logs, billing events, and app metrics very well. You pay for compute only when new events arrive in your system. Meanwhile, the database groups your rows by time for fast reads and storage rule checks. Yet linking stateless functions to a stateful database brings network and security risks.

How cloud functions and timescaledb share data and roles

A cloud function wakes up when an event hits an API or queue. The function reads its settings to find the database host and secret key. Next, it opens a network session over TLS to the database server. Once logged in, the function writes a batch of events or runs a query.

Good setups separate roles by what the function needs to do. Ingest functions get a role that can only insert rows into specific tables. Read functions get a separate role that can only select from summary views. Neither role should have rights to drop tables, change schemas, or create users.

Database sessions take time and memory to start up on the server. When traffic spikes, hundreds of functions might wake up at the exact same second. Each instance tries to open its own fresh database session right away. Teams use native session pooling on the database side to handle these spikes.

Common failure modes and how to scope network paths

The biggest risk in this setup is using one shared database admin account. Teams often put a master password into a shared secret store for every function. If one function has a bug, an attacker can read or delete all data. Also, when all functions share one user name, database logs lose value. You cannot tell which function or developer ran a slow or harmful query.

Broad network access is another common mistake when teams link these two tools. Serverless workers often run on dynamic IP addresses from a public cloud pool. Opening the database port to the whole internet invites brute-force attacks. Instead, route function traffic through a private virtual network connector. Then set the database firewall to accept traffic only from that private subnet.

Cloud Functions IAM + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Developers also need to run live queries to fix bugs or check data. Sharing the function password with staff creates a major blind spot for audits. Give humans their own log in flow tied to your identity provider. Also, apply data masking to rewrite emails, phone numbers, card numbers, or national IDs in query results.

Managing schema updates and data retention

Time-series tables grow fast and need scheduled upkeep to stay fast. Do not let normal event functions change table settings or drop old data chunks. Keep schema changes inside a dedicated deployment job with its own scoped role.

For daily upkeep like compressing or dropping old partitions, use built-in database background jobs. If you trigger upkeep from a scheduled cloud function, give it a strict role. That role should only run the specific stored procedures needed for table care. Keeping ingest, read, and upkeep roles separate stops small bugs from causing outages.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

How do we stop serverless traffic spikes from maxing out database sessions?

Turn on a server-side session pooler in front of your database engine. Also set a hard upper limit on how many function instances can run at once. Keep database sessions open across warm function runs instead of closing them every time.

Should our functions use the same database role for writing and reading data?

No, you should split read and write duties into separate database roles. Give ingest functions insert-only rights on raw event tables. Give dashboard or API functions select-only rights on the specific views they need.

How should engineers inspect production time-series data during an incident?

Engineers should never copy the static database keys used by your cloud functions. Require them to log in with their own identity and log every query they run. Use field masking to rewrite card numbers and emails in the returned rows.

The query runs. The emails stay hidden.