Security
Serverless Security: Risks and Best Practices for Functions
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيServerless platforms let you deploy small functions that run on demand, with no operating system to patch and no server to keep running. That removes a whole category of work, but it does not remove security work. It changes where the risks are. Instead of open ports and outdated packages on a VM, you deal with over-permissive roles, untrusted event data, leaked environment variables and bills that grow when someone abuses your endpoints. This guide explains the shared responsibility model for serverless, the most common risks, and a practical checklist you can apply whether you use a managed functions platform or run a self-hosted functions framework on your own servers.
Who is responsible for what
| Layer | Managed serverless platform | Self-hosted on a VPS |
|---|---|---|
| Physical hardware and data center | Provider | Hosting provider |
| Hypervisor and host OS | Provider | Hosting provider / you |
| Runtime patching (language versions) | Provider, once you choose a supported runtime | You |
| Function code and dependencies | You | You |
| Permissions and identity | You | You |
| Secrets and configuration | You | You |
| Input validation and business logic | You | You |
| Logging, alerting, cost limits | You | You |
The bottom half of the table never moves. Whatever platform you pick, your code, permissions and data handling are your responsibility. If you are still deciding whether serverless suits your project at all, read our overview of serverless architecture pros and cons.
The main serverless risks
Over-permissive roles
Each function usually runs with an identity that can call other cloud services. A common shortcut is to give every function one broad role. If any function has a flaw, the attacker inherits all of those permissions: reading every storage bucket, writing to every table, or creating new resources.
Event injection
Functions are triggered by many sources: HTTP requests, queue messages, file uploads, database changes. Developers often validate HTTP input carefully and then trust everything else. But a file name, a message body or an email subject can carry the same SQL, command or template injection payloads as a web form.
Secrets in the wrong place
API keys hard-coded in source, committed to Git, or printed in logs are a frequent cause of incidents. Environment variables are better than code, but they are often visible to anyone who can view the function configuration.
Vulnerable dependencies
A function may be twenty lines of your code and thousands of lines from packages. Those dependencies run with the function's permissions.
Denial of wallet
Serverless scales automatically, which is usually a benefit. An attacker who floods a public endpoint does not take it down; instead they can drive up your bill. A recursive trigger, such as a function that writes to the same bucket that triggers it, can do the same by accident.
Weak visibility
Functions are short-lived, so there is no server to log into after an incident. Without structured logs and alerts, an attack may go unnoticed.
A practical security checklist
Permissions
- Give each function its own role with only the actions and resources it needs.
- Prefer specific resource names over wildcards like
*. - Review roles regularly and remove permissions that logs show are never used.
Input handling
- Validate every event against a schema, whatever its source.
- Use parameterized queries for databases and never build shell commands from input.
- Set size limits on request bodies and uploaded files.
Secrets
- Store secrets in a dedicated secrets manager or encrypted parameter store and load them at runtime.
- Rotate keys on a schedule and immediately after anyone with access leaves.
- Make sure logging code never prints tokens, passwords or full request headers.
- Scan repositories for secrets before pushing, for example with a pre-commit hook using a tool such as gitleaks.
Dependencies and builds
- Pin dependency versions with a lock file.
- Run
npm audit,pip-auditor an equivalent in your build pipeline. - Remove unused packages; a smaller bundle is also a smaller attack surface.
Endpoints and cost control
- Put public functions behind an API gateway with authentication and request limits.
- Set a maximum concurrency for each function so a flood cannot scale without bound.
- Set short timeouts that match the real work, which limits both cost and abuse.
- Configure billing alerts and budgets with your provider.
- Check that no function can trigger itself in a loop.
Logging and monitoring
- Emit structured logs with a request ID for every invocation.
- Alert on spikes in errors, invocations, duration and permission-denied events.
- Keep audit logs of configuration and permission changes.
Self-hosted functions on your own servers
Some teams run open-source functions frameworks on top of containers or Kubernetes on their own VPS fleet. That gives predictable monthly costs and full control over data location, but it puts the host back on your list: OS updates, a closed firewall, container isolation and TLS on the gateway. A predictable flat-rate server also removes the denial-of-wallet risk, since a flood can slow you down but cannot multiply your bill. If you want to experiment with that model, start with a small server from our pricing page and grow from there.
FAQ
Is serverless more secure than a VPS?
Neither is automatically more secure. Serverless removes OS patching from your list, which eliminates a common source of breaches. A VPS gives you full control over the network and runtime. In both cases, most real incidents come from application code, permissions and leaked secrets, which remain your job.
Do serverless functions need a firewall?
There are no ports for you to manage on a managed platform, but public HTTP functions still need protection at the application layer: authentication, request limits and input validation, usually provided by an API gateway and optionally a WAF.
How do I stop a surprise serverless bill?
Combine several limits: maximum concurrency per function, short timeouts, authentication on public endpoints, request limits at the gateway and billing alerts. Also test that functions cannot trigger themselves recursively, since loops are a common cause of unexpected costs.