How to use AWS Lambda in Drupal

25 / Aug / 2021 by Jeet Lal 0 comments

AWS Lambda@Edge allows you to run applications and services without worrying about managing infrastructure. Your application still runs on servers, but all the server management is done by the Cloud provider. You no longer have to provision, scale, and maintain servers to run your applications, databases, and storage systems. AWS globally distributes your web application to run out of dozens of AWS data centers across the regions

AWS Lambda is used to develop serverless architecture based applications and services. The concept of Serverless architecture is “Focus on your application, not the infrastructure”. It means application developers can focus on writing business logic instead of server infrastructure, cost, scaling, and availability.

With AWS Lambda, you can run your code virtually for any type of application or service without managing AWS infrastructure. It can be used to develop microservices and these services can be bound with any application to perform a specific task.

AWS Lambda functions can be uploaded in zip format on container image and the Lambda function automatically allocates memory, execution power, and execute your Lambda functions. The Lambda functions can be executed based on the predefined events. Lambda functions can be executed by more than 200 AWS services, call it directly from your web application or mobile application. AWS Lambda functions can be written in your favorite programming languages such as Python, Node.js, Go, Java, Ruby, and many more. AWS SAM or Docker CLI can be used to build, test and deploy your functions.

Advantage of AWS Lambda

  • No server to manage
  • Lower costs and scalability.
  • Faster development and deployment.
  • Reduced expenses on human resources.
  • Focus on business needs.
  • The simplest way to run your code in the cloud
  • Automatically scales and high availability (HA)
  • Connects and extends cloud services
  • Code can run closer to the end-user, decreasing latency

How it Works

Prerequisite to Use AWS Lambda Function in Drupal 8/9

  • AWS Cloud platform
  • The programming language supported by the AWS Lambda functions
  • Event to execute the Lambda functions
  • Drupal 8/9 Application

Use Cases of AWS Lambda Functions in Drupal

  • Clear CloudFront cache.
  • Generate Reports and send over email.
  • Run Scheduler
  • Image processing after upload to AWS S3 Bucket.
  • Push MySql data to the Search engine, Queue, etc.
  • Send bulk emails to customers.
  • Real-time File Processing.
  • Real-time Stream Processing.
  • Data processing.

Clear CloudFront Cache Example

  • Download AWS SDK via composer manager composer require aws/aws-sdk-php
  • Create a module name examples with all required files.
  • Create examples.module file in the root

File: modules/custom/examples/examples.module


2. File: modules/custom/examples/src/Service/AwsLambdaService.php

3. AWS Lambda function written in Python to clear the cache.
File: clear_cache.py

4. Upload your python code in zip format on AWS and get the Function name as below image

AWS Lambda function configuration

Disadvantage of Lambda Functions (Server Less Architecture)

  • Testing and debugging become more challenging
  • Lambda functions are not built for long-running processes
  • Reduced overall control.
  • Testing locally becomes tricky.

Conclusion

With AWS Lambda, we can minimize Drupal processing, computation & memory. In the above example, clearing the CloudFront cache is done by the AWS Lambda function. Like the above example, we can create more AWS Lambda functions to perform more complex tasks and that can be executed by Invoking Lambda function from Drupal, executing Lambda function by AWS scheduler or AWS services.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *