In one way or another, we're often asked, “is Functions the same as AWS Lambda?”
Though Functions and Lambda are event-driven services and overlap with respect to elegant solutions that both services can support, that's where the similarities end. The difference lays in what scenarios and use cases each solves, and in this post, we'll break down the best uses for each technology.
Lambda allows an event trigger to invoke code, and/or another one of Amazon’s many services. For example, a Lambda function might be triggered when a social media stream is loaded into Kinesis, and the function generates trend data and writes to DynamoDB. This is all running within the AWS ecosystem, and the metric the user is billed by is compute time.
Functions are invoked specifically by PubNub real-time messages or via conventional HTTP requests. The Functions solution for NoSQL data storage is via a globally replicated KV Store. PubNub does not charge users based on their compute time or read/write throughput on a database. Users are charged per transaction, meaning once per Function event handler execution. So in the scenario of storing tremendous real-time data, a similar solution to Lambda can be implemented using Functions and the KV Store, but with a lower cost.
What are the key differences between Functions and AWS Lambda?
Invocation
Functions are invoked whenever a PubNub real-time message is sent over the pub-sub network. This can be set to trigger on one or many channels within a user’s subscribe key. The pattern is meant to enrich an in-transit, real-time message with additional data, before it reaches each of its subscribers. Functions can also be invoked by a conventional REST API call. The processing scope is meant to be narrow and single purpose, like a microservice.
Lambda Functions are invoked by any Amazon service in an event-driven fashion using an AWS SDK. The pattern is meant to trigger other services and do computation on data as it is submitted. The data can then be accessed by other Amazon services in immediately.
The difference here is that Functions are primarily for in-transit data enrichment, while Lambda Functions are supposed to perform specific compute in an event-driven manner.
Permissions
Lambda Functions require permissions to be granted to any other Amazon service that the developer wants to have invoke it using IAM.
Comparatively, Functions are invoked on the specific channel and subscribe key that a developer chooses. Any device with an internet connection that knows the subscribe key and channel for the Function can invoke the Function (unless Access Manager is active and provisions are not set).
What are some examples of in-transit message enrichment?
A private message validation service can be implemented in a Function. With this in place, a client can send a message to a server using PubNub, and the Function can validate the integrity of the sender or message payload while it is in-transit from sender to receiver. Secret encryption keys can be stored and algorithms for crypto can be simply implemented inside the Function ecosystem.
Another example is a chat app message can be sent from one user in English and translated while in-transit to French using a Function and a language translation API, like Microsoft Translator or Amazon Translate.
How are Functions and Lambda Functions similar?
Both services offer a place to deploy a codebase once, and the serverless backend globally distributes instances of the program automatically. This is an industry standard feature of FaaS (Functions-as-a-service). The developer does not need to think about building containers, load balancers, or performing several deploys to data centers around the world, every time changes are made to the code.
The code that a dev team deploys is automatically invoked by events that are directed to the Function trigger. The Function execution can fire other services within the AWS or PubNub environment, or any other third-party API.
Storage is offered within both platforms. Functions has the KV Store, a NoSQL database with read and write times under 3ms. AWS has DynamoDB for NoSQL storage which can be connected to Lambda Functions.
Both Lambda and PubNub have the ability to invoke other services within their own ecosystem. PubNub can use a Function for Publishing to other channels and also making grants in Access Manager. Lambda of course is able to trigger read/write to Redshift, Simple Email Service, ect.
When Should I Use PubNub and When Should I Use Lambda?
Lambda is ideal for situations where processing power is necessary, and PubNub is ideal when real-time speed and state read/write is necessary.
Functions
- Translate text message from one user to another in a chat application using a third-party API: Translate the message from one language that the sender uses to another language that the receiver understands.
- Session token validation for a user-auth in real time, as a user is interacting with the web or mobile app.
- Filtering of real-time stream messages: Use Functions for marshaling logic to forward certain messages to different receivers or stop them from reaching any receiver. Marshaling can also be used for stopping a stream or starting the flow of another stream.
- Aggregating messages: In the case where an IoT device sends several messages at once, Functions can aggregate these messages, turning several into one.
- Message augmentation: Functions can be a trusted middleman for augmenting messages with timestamps, additional user data like name or avatar from a secure server.
Lambda
- Generating a thumbnail image whenever a user uploads a full-sized image to an S3 Bucket.
- Social media stream is loaded into Kinesis. Lambda generates hashtag data and stores it in DynamoDB for business users to query.
- Shipping order is placed and stored in DynamoDB. Lambda runs to generate analytics data and forwards it to Redshift storage warehouse.
- Daily backups of Amazon Elastic Block Stores that are made automatically.
- Immediate and automated log file analysis from Cloudwatch, watching for a specific criteria to be met and invoke another service.
Open source third-party vendor integration
PubNub has an open source catalog of already-written Functions that enable rapid integration of third-party services into the serverless execution flow.
Say you need real time text translation in a chat application. The BLOCKS Catalog already has a module for Watson Translation and AWS Translate that can be used to translate in-transit text, or in the form of a REST API. This feature allows a chat application to be made where a user sends text in english, and their partner receives the same message in Mandarin, in real time.
Any API can be connected to a Function whether it be custom, private, or public. Lambda Function examples are available on GitHub, however, the solutions are not as numerous, or as easy to integrate as the API partners referenced in the PubNub BLOCKS Catalog. Functions can be imported and globally deployed in just a few seconds.