PHPackages                             imdigital/magento-serverless - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. imdigital/magento-serverless

ActiveMagento2-module

imdigital/magento-serverless
============================

Use serverless functions to extend Magento 2

1.0.3(3y ago)44141OSL-3.0PHPPHP &gt;=8.1

Since Mar 1Pushed 3y ago3 watchersCompare

[ Source](https://github.com/Imagination-Media/magento-serverless)[ Packagist](https://packagist.org/packages/imdigital/magento-serverless)[ RSS](/packages/imdigital-magento-serverless/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (1)

[![Magento logo](https://camo.githubusercontent.com/e2fb4d9e3a6606febb15922c2a6b8d6bb7c0761c40a565a5c0e4c99914920e7d/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f656e2f7468756d622f352f35332f4d6167656e746f2e7376672f3132383070782d4d6167656e746f2e7376672e706e67)](https://camo.githubusercontent.com/e2fb4d9e3a6606febb15922c2a6b8d6bb7c0761c40a565a5c0e4c99914920e7d/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f656e2f7468756d622f352f35332f4d6167656e746f2e7376672f3132383070782d4d6167656e746f2e7376672e706e67)

 Serverless Framework for Magento 2
------------------------------------

[](#--serverless-framework-for-magento-2)

#### **Implement custom functionality, data validation and much more using serverless functions out of the Magento codebase.**

[](#implement-custom-functionality-data-validation-and-much-more-using-serverless-functions-out-of-the-magento-codebase)

With this Magento module, developers can implement data validation, add custom functionality, and much more using serverless functions that aren't part of the codebase.

[![](https://camo.githubusercontent.com/cb059cf7f9dae23755c6fd6f78095293ddf85f13a500bc22a5e7a3c2cf7cde88/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c616e67756167652d5048502d626c7565)](https://camo.githubusercontent.com/cb059cf7f9dae23755c6fd6f78095293ddf85f13a500bc22a5e7a3c2cf7cde88/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c616e67756167652d5048502d626c7565) [![](https://camo.githubusercontent.com/14c677a2107f7f89b4b0a134e6f745e3117c0ec18e6e18a668ab8ce35f6866c9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f45636f6d6d657263652d4d6167656e746f2d6f72616e6765)](https://camo.githubusercontent.com/14c677a2107f7f89b4b0a134e6f745e3117c0ec18e6e18a668ab8ce35f6866c9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f45636f6d6d657263652d4d6167656e746f2d6f72616e6765) [![](https://camo.githubusercontent.com/8439195364c770e54aea8fa85d97c52e1c5c33779cd500cf5e6a9cad95950998/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4372656174656425323042792d494d4469676974616c2d6f72616e6765)](https://camo.githubusercontent.com/8439195364c770e54aea8fa85d97c52e1c5c33779cd500cf5e6a9cad95950998/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4372656174656425323042792d494d4469676974616c2d6f72616e6765)

What's serverless?
==================

[](#whats-serverless)

Serverless computing is a method of providing backend services on an as-used basis. A serverless provider allows users to write and deploy code without the hassle of worrying about the underlying infrastructure. A company that gets backend services from a serverless vendor is charged based on their computation and do not have to reserve and pay for a fixed amount of bandwidth or number of servers, as the service is auto-scaling. Note that despite the name serverless, physical servers are still used but developers do not need to be aware of them.

In the early days of the web, anyone who wanted to build a web application had to own the physical hardware required to run a server, which is a cumbersome and expensive undertaking.

Then came cloud computing, where fixed numbers of servers or amounts of server space could be rented remotely. Developers and companies who rent these fixed units of server space generally over-purchase to ensure that a spike in traffic or activity will not exceed their monthly limits and break their applications. This means that much of the server space that gets paid for can go to waste. Cloud vendors have introduced auto-scaling models to address the issue, but even with auto-scaling an unwanted spike in activity, such as a DDoS Attack, could end up being very expensive.

[![](https://camo.githubusercontent.com/51ed3488586c7ec28e994b657f14851371d5df56db1508f4b1af4d245dc734b9/68747470733a2f2f63662d6173736574732e7777772e636c6f7564666c6172652e636f6d2f736c74336c633674657633372f376e79496769656372666539573654666d4a52704e682f64666335343334363539653331333030643139313864343136336466623236332f62656e65666974732d6f662d7365727665726c6573732e737667)](https://camo.githubusercontent.com/51ed3488586c7ec28e994b657f14851371d5df56db1508f4b1af4d245dc734b9/68747470733a2f2f63662d6173736574732e7777772e636c6f7564666c6172652e636f6d2f736c74336c633674657633372f376e79496769656372666539573654666d4a52704e682f64666335343334363539653331333030643139313864343136336466623236332f62656e65666974732d6f662d7365727665726c6573732e737667)

Serverless computing allows developers to purchase backend services on a flexible ‘pay-as-you-go’ basis, meaning that developers only have to pay for the services they use. This is like switching from a cell phone data plan with a monthly fixed limit, to one that only charges for each byte of data that actually gets used.

The term ‘serverless’ is somewhat misleading, as there are still servers providing these backend services, but all of the server space and infrastructure concerns are handled by the vendor. Serverless means that the developers can do their work without having to worry about servers at all.

***Source: ***

Why using serverless in Magento?
================================

[](#why-using-serverless-in-magento)

Magento is well-known and preferable in the e-commerce space when there's a need for customizations as it's open-source.

With Magento, we can develop and add new functionality, modify an existing one, add some business logic, data validation, etc.

But that's its gift and curse because Magento has frequent updates to its core, security patches, etc, and usually, a webshop built with Magento will have dozens (if not hundreds) of modules and customizations, making this process painful and a lot of times, and difficult to deal with.

With serverless functions we can achieve almost the same things, not having to install modules or modify the Magento code base. Essentially, we will customizations running out of the Magento codebase, hosted somewhere else, and this will facilitate the maintainability and upgradeability as we will manage these customizations out of the codebase and we can even use different programming languages for these functions if we want.

Differences between standard extensibility VS serverless
--------------------------------------------------------

[](#differences-between-standard-extensibility-vs-serverless)

### Standard extensibility approach

[](#standard-extensibility-approach)

Standard customizations are going to be modules/plugins that we develop or install directly on the Magento e-commerce platform. That's the usual and standard approach to extending the Magento functionality.

These plugins will add new functionality by rewriting/intercepting the core code, introduce new business logics, and data validation. They are part of the code base, on a layer up to the Magento core.

[![Magento extensibility through modules/extensions](https://github.com/Imagination-Media/magento-serverless/raw/master/screenshots/magento-standard-architecture.png)](https://github.com/Imagination-Media/magento-serverless/blob/master/screenshots/magento-standard-architecture.png)
*Magento standard extensibility diagram*

### Serverless approach

[](#serverless-approach)

Using the serverless approach we can choose our preferable hosting providers such as Google Cloud, AWS, or Azure, develop our custom functionality as serverless functions using any programming language that they support (it doesn't need to be PHP as Magento), deploy and manage this code directly on the hosting provider, and that's it, these serverless functions will be triggered from Magento once an event happens. For example, let's say we want to use a cart validation that doesn't allow customers to order more than 3 units of a single SKU per week. We develop and deploy a serverless function that's triggered on the add-to-cart event, the serverless function will get the request data from Magento, validate, and then return if it's fine or not.

In that way, we can not only use any programming language to write this code, but we can easily maintain and release new versions of it without having to interfere with the website at all. And this will make it a lot easier to manage future Magento upgrades as the core will remain almost untouchable as there are almost no custom extensions rewriting it anymore.

[![Magento extensibility through serverless functions](https://github.com/Imagination-Media/magento-serverless/raw/master/screenshots/magento-serverless-architecture.png)](https://github.com/Imagination-Media/magento-serverless/blob/master/screenshots/magento-serverless-architecture.png)
*Magento serverless extensibility diagram*

### So why use serverless?

[](#so-why-use-serverless)

- Freedom of using any programming language to customize the platform (doesn't necessarily require a Magento developer).
- Maintainability. You can easily maintain, deploy, and manage customizations without having to deploy the website. You isolate pieces of functionality.
- Extensions are not part of the codebase. Leave the codebase for the core, and for the storefront theme. Handle the rest out of it.
- Cheaper. With headless you pay for only what you use. Forget about expensive cloud instances and replications.
- End of Magento upgrade nightmares. Leave the codebase for the Magento core. Make your life a lot easier when dealing with platform upgrades. Upgrade quicker.

### An alternative to Adobe.io

[](#an-alternative-to-adobeio)

Adobe recently release something similar to this service, but only for Adobe Commerce customers through Adobe.io. But this requires customers to use their service to deploy and manage serverless functions and again, it's only available for Adobe Commerce customers. This module is a way to not only offer this type of implementation to also Open-Source customers, but also to give the option of choosing the hosting provider that you prefer to run your serverless functions.

The framework
=============

[](#the-framework)

How it works
------------

[](#how-it-works)

Magento developers are well familiar with [observers](https://developer.adobe.com/commerce/php/development/components/events-and-observers/event-list/). They are events triggered within the platform when a specific action happens. For example, a product was added to the cart. A **checkout\_cart\_product\_add\_before** event is triggered and allow modules/plugins to intercept it, get its data, and do something. This a well-common practice by extension vendors and developers. We can do a lot of things we these events. But, these events can only be observed and intercepted by modules installed on the Magento codebase. Well, until now.

This Magento module will make Magento able to not only trigger these events making it possible to install modules to intercept it, but we can now also register serverless functions deployed in one of the supported hosting providers and make them able to also receive the data from the dispatched event and do something. In that way, we can have code out of the codebase able to modify the business logic, add custom validations, etc, without having to develop another Magento extension to be part of its codebase.

Magento uses the ***Magento\\Framework\\Event\\Manager\\Proxy*** class to analyze events and see if they can be caught by an installed Magento extension. So, our framework replaces this class by using our ***ImDigital\\Serverless\\Event\\Manager\\Proxy*** which makes it possible to not only use observers from installed modules but also observers from serverless functions. So, in case an event is triggered, Magento will first look through the installed modules, see if there are any observers to be executed, executed them, and then do the same for the registered serverless functions. So, it will loop through the serverless functions registered to catch that event and will run them one by one.

 ```
sequenceDiagram
    participant Customer
    participant Magento
    participant Module
    participant Serverless Function

    Customer->>Magento: Customer action
    Magento->>Magento: Trigger event
    Magento->>+Module: Check observers
    loop Through modules
        Module-->>-Magento: Observer check
        Magento->>Module: Execute observer
        Module-->>Magento: Observer result
    end
    Magento->>+Serverless Function: Check watchers
    loop Through serverless functions
        Serverless Function-->>-Magento: Watcher check
        Magento->>Serverless Function: Execute function
        Serverless Function-->>Magento: Function result
    end
    Magento-->>Customer: Return result

```

      Loading *UML sequence diagram explaining the framework workflow*

The complete list with available observers in Magento is available at . Any of these actions can be supported by our serverless framework. The data will be transformed into a JSON object and then passed to the serverless function.

This module will add a new database table called ***serverless\_functions***. On this table, we will register the serverless functions that are going to be connected with Magento and will watch for events. In this table we set the name of the function on the hosting provider, a short description explaining what the function is gonna do, what's the cloud provider, the Magento observed event, a flag to enable/disable the serverless function, and the cloud configuration which is the field where we set the cloud access key, regions, etc. The cloud configuration key is going to have an encrypted value, encrypted with the Magento encryption key.

[![Magento extensibility through serverless functions](https://github.com/Imagination-Media/magento-serverless/raw/master/screenshots/serverless-db.png)](https://github.com/Imagination-Media/magento-serverless/blob/master/screenshots/serverless-db.png)
*The serverless\_functions database table*

How the serverless functions are called
---------------------------------------

[](#how-the-serverless-functions-are-called)

Each hosting provider is going to have its own Magento serverless package, meaning that they are going to extend this serverless module, adding the code that's going to be responsible to make the call to the serverless function on the hosting provider to get back the data.

Every cloud provider package must have a Provider.php class which is the one where this logic is implemented. This call will implement the ***ImDigital\\Serverless\\Api\\Data\\CloudProviderInterface*** interface which has the methods they all must implement to make that happen. In that way, all cloud providers are going to follow the same standard.

```
