{"id":44510,"date":"2021-08-25T11:05:46","date_gmt":"2021-08-25T05:35:46","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=44510"},"modified":"2022-01-07T17:34:14","modified_gmt":"2022-01-07T12:04:14","slug":"how-to-use-aws-lambda-in-drupal","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-use-aws-lambda-in-drupal\/","title":{"rendered":"How to use AWS Lambda in Drupal"},"content":{"rendered":"<p><span style=\"font-weight: 400;\"><a href=\"https:\/\/www.tothenew.com\/blog\/introduction-to-aws-lambda\/\">AWS <b>Lambda@Edge<\/b><\/a> 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, <b>scale, and maintain servers to run your applications, databases, and storage systems<\/b>. AWS globally distributes your web application to run out of dozens of AWS data centers across the regions<\/span><\/p>\n<p>AWS Lambda is used to develop <a href=\"https:\/\/www.tothenew.com\/blog\/accelerate-your-cloud-performance-using-these-6-uncommon-aws-services\/\">serverless architecture<\/a>\u00a0based applications and services. The concept of Serverless architecture is &#8220;<b>Focus on your application, not the infrastructure&#8221;<\/b>. It means application developers can focus on writing business logic instead of server infrastructure, cost, scaling, and availability.<\/p>\n<p>With <b>AWS Lambda<\/b>, you can run your code virtually for any type of application or service without managing AWS infrastructure. It can be used to develop <b>microservices<\/b> and these services can be bound with any application to perform a specific task.<\/p>\n<p>AWS Lambda functions can be uploaded in zip format on container image and the Lambda function automatically <b>allocates memory, execution power, and execute your Lambda functions<\/b>. The Lambda functions can be executed based on the predefined events. Lambda functions can be executed by more than <b>200 AWS services<\/b>, call it directly from your web application or mobile application. AWS Lambda functions can be written in your favorite programming languages such as <b>Python, Node.js, Go, Java, Ruby,<\/b> and many more. <b>AWS SAM or Docker CLI can be used to build, test and <a href=\"https:\/\/www.tothenew.com\/blog\/aws-codedeploy-and-github\/\">deploy your functions<\/a>.<\/b><\/p>\n<h4><b>Advantage of AWS Lambda<\/b><\/h4>\n<ul>\n<li>No server to manage<\/li>\n<li>Lower costs and scalability.<\/li>\n<li>Faster development and deployment.<\/li>\n<li>Reduced expenses on human resources.<\/li>\n<li>Focus on business needs.<\/li>\n<li>The simplest way to run your code in the cloud<\/li>\n<li>Automatically scales and high availability (HA)<\/li>\n<li>Connects and extends cloud services<\/li>\n<li>Code can run closer to the end-user, decreasing latency<\/li>\n<\/ul>\n<h4><b>How it Works<\/b><\/h4>\n<h3><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54450 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-1.png\" alt=\"\" width=\"949\" height=\"282\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-1.png 949w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-1-300x89.png 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-1-768x228.png 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-1-624x185.png 624w\" sizes=\"(max-width: 949px) 100vw, 949px\" \/><\/h3>\n<h4><b>Prerequisite to Use AWS Lambda Function in Drupal 8\/9<\/b><\/h4>\n<ul>\n<li>AWS Cloud platform<\/li>\n<li>The programming language supported by the AWS Lambda functions<\/li>\n<li>Event to execute the Lambda functions<\/li>\n<li><a href=\"https:\/\/www.tothenew.com\/blog\/why-choose-drupal-over-other-cms\/\">Drupal 8\/9 Application<\/a><\/li>\n<\/ul>\n<h4><b>Use Cases of AWS Lambda Functions in Drupal<\/b><\/h4>\n<ul>\n<li>Clear CloudFront cache.<\/li>\n<li>Generate Reports and send over email.<\/li>\n<li><a href=\"https:\/\/www.tothenew.com\/blog\/schedule-lambda-on-cron-expression-triggers\/\">Run Scheduler<\/a><\/li>\n<li><a href=\"https:\/\/www.tothenew.com\/blog\/image-optimization-using-jpegtran-with-aws-lambda\/\">Image processing<\/a> after upload to AWS S3 Bucket.<\/li>\n<li>Push MySql data to the Search engine, Queue, etc.<\/li>\n<li>Send bulk emails to customers.<\/li>\n<li>Real-time File Processing.<\/li>\n<li>Real-time Stream Processing.<\/li>\n<li>Data processing.<\/li>\n<\/ul>\n<h4><b>Clear CloudFront Cache Example<\/b><\/h4>\n<ul>\n<li><b>Download AWS SDK<\/b> via composer manager <b>composer require aws\/aws-sdk-php<\/b><\/li>\n<li>Create a module name <b>examples<\/b> with all required files.<\/li>\n<li>Create examples<b>.module<\/b> file in the root<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">File: <em>modules\/custom\/examples\/examples.module<\/em><\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-54452\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-2.png\" alt=\"\" width=\"1536\" height=\"1134\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-2.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-2-300x221.png 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-2-1024x756.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-2-768x567.png 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-2-624x461.png 624w\" sizes=\"(max-width: 1536px) 100vw, 1536px\" \/><br \/>\n<span style=\"font-weight: 400;\">2. File: <em>modules\/custom\/examples\/src\/Service\/AwsLambdaService.php<\/em><\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-54453\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-3.png\" alt=\"\" width=\"1536\" height=\"4770\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-3.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-3-97x300.png 97w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-3-330x1024.png 330w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-3-768x2385.png 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-3-495x1536.png 495w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-3-659x2048.png 659w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-3-624x1938.png 624w\" sizes=\"(max-width: 1536px) 100vw, 1536px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">3. AWS Lambda function written in Python to clear the cache.<\/span><br \/>\nFile: <em>clear_cache.py<\/em><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-54454\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-4.png\" alt=\"\" width=\"949\" height=\"345\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-4.png 949w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-4-300x109.png 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-4-768x279.png 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/21-4-624x227.png 624w\" sizes=\"(max-width: 949px) 100vw, 949px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">4. Upload your python code in zip format on AWS and get the Function name as below image<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-53901\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/08\/aws-lambda-function-cms-cache-clear.png\" alt=\"AWS Lambda function configuration\" width=\"1153\" height=\"399\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/08\/aws-lambda-function-cms-cache-clear.png 1153w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/aws-lambda-function-cms-cache-clear-300x104.png 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/aws-lambda-function-cms-cache-clear-1024x354.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/aws-lambda-function-cms-cache-clear-768x266.png 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/08\/aws-lambda-function-cms-cache-clear-624x216.png 624w\" sizes=\"(max-width: 1153px) 100vw, 1153px\" \/><\/p>\n<h4><b>Disadvantage of Lambda Functions (Server Less Architecture)<\/b><\/h4>\n<ul>\n<li>Testing and debugging become more challenging<\/li>\n<li>Lambda functions are not built for long-running processes<\/li>\n<li>Reduced overall control.<\/li>\n<li>Testing locally becomes tricky.<\/li>\n<\/ul>\n<h4><b>Conclusion<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">With AWS Lambda, we can <a href=\"https:\/\/www.tothenew.com\/blog\/best-practices-to-optimize-performance-in-drupal-8\/\">minimize Drupal processing, computation &amp; memory<\/a>. In the above example, clearing the CloudFront cache is done by the <b>AWS Lambda function<\/b>. Like the above example, we can create more AWS Lambda functions to perform more complex tasks and that can be executed by <b>Invoking Lambda function from Drupal, executing Lambda function by AWS scheduler or AWS services.<\/b><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1038,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":92},"categories":[1174,2348,3602,1],"tags":[1679,3601,4884,4886],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/44510"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1038"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=44510"}],"version-history":[{"count":14,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/44510\/revisions"}],"predecessor-version":[{"id":54455,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/44510\/revisions\/54455"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=44510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=44510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=44510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}