What are Serverless Functions?by Iwan Price-Evans on Cloud • June 16, 2022
Serverless functions abstract functionality away from server infrastructure by running isolated, event-driven functions that have single-use applications. Serverless functions are hosted on managed infrastructure and are programmatic functions that typically have a single use.
Serverless architecture allows development teams to concentrate on developing code instead of spending time managing the infrastructure their applications use.
Serverless programming was first implemented as function-as-a-service (FaaS) infrastructure by organizations like AWS. These systems had properties that were similar to serverless functions.
What are serverless functions used for?
Business logic can be implemented as a serverless function. This type of function doesn't maintain any data (is stateless) and is used and destroyed on demand (is ephemeral). Serverless functions are designed to trigger when a specific condition is met and often only run for seconds.
For example, a serverless function could be created to check a login, upload a file, trigger a signup to a newsletter, or place a cart order.
Serverless functions are suitable for pretty much any programming scenario where an event can be triggered by an API call. They can be run on-demand, scheduled, or called by webhooks from third-party services.
It's important to note that serverless functions must be self-contained and not rely on external code or software to run. This isolation enables their rapid trigger/execute/stop lifecycle.
How do serverless functions work?
Typically serverless functions are written by a developer for a single purpose. The developer will create a small amount of code that can run independently. The function code is deployed to the serverless infrastructure where it will be hosted and executed.
The code must have a defined event that will trigger the hosting cloud service to execute the function. HTTP request events are commonly used for this. The event can be triggered by a user action such as a button click.
When the event is triggered, the cloud service will immediately create an instance of the function and run it. Some cloud services may be able to maintain running instances of functions depending on their purpose.
The serverless function will complete execution, returning whatever output was requested, such as displaying a success message to the user.
What are the benefits of serverless functions?
Serverless programming provides business value:
- The serverless provider maintains the function's code and manages its execution. This means the development teams can concentrate on other code or projects.
- Using pay-as-you-go serverless functions means you only pay for the cloud server resources you use.
- Developers don't have to manage servers and don't need to learn another web app framework or infrastructure management tool.
- Serverless functions are maintained to have near-perfect uptime.
- Serverless functions can scale to cope with any volume of incoming network requests.