<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nitish Kumar</title>
    <description>The latest articles on Forem by Nitish Kumar (@nitishkmr).</description>
    <link>https://forem.com/nitishkmr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F770140%2F48879dfd-4f52-4ffa-9b96-91713e0fff13.jpg</url>
      <title>Forem: Nitish Kumar</title>
      <link>https://forem.com/nitishkmr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nitishkmr"/>
    <language>en</language>
    <item>
      <title>How to enable Gzip compression in Laravel while hosting on AWS Lambda</title>
      <dc:creator>Nitish Kumar</dc:creator>
      <pubDate>Thu, 09 Dec 2021 14:00:31 +0000</pubDate>
      <link>https://forem.com/noetic/how-to-enable-gzip-compression-in-laravel-while-hosting-on-aws-lambda-1ldn</link>
      <guid>https://forem.com/noetic/how-to-enable-gzip-compression-in-laravel-while-hosting-on-aws-lambda-1ldn</guid>
      <description>&lt;p&gt;As I came across &lt;a href="https://aws.amazon.com/lambda/"&gt;AWS Lambda Serverless&lt;/a&gt; services, it intrigued me and I wanted to learn more about its features. &lt;/p&gt;

&lt;p&gt;Since my background is in PHP development and there were no platforms available to host PHP environments, it made sense to adopt Lambda into our development. &lt;/p&gt;

&lt;p&gt;It’s a developer’s dream, not having to worry about scaling the cloud infrastructure, hefty bills, and offers low maintenance to boot.&lt;/p&gt;

&lt;p&gt;Gradually, I learned about &lt;a href="https://bref.sh/"&gt;bref&lt;/a&gt;, a package that made it easy to host your PHP application on serverless. I started working on this package, finding the documentation and tutorials online. I successfully hosted my Laravel application on Lambda.&lt;/p&gt;

&lt;p&gt;I came across various challenges during deployments.&lt;/p&gt;

&lt;p&gt;A few days back, I was trying to fix my website from an SEO perspective, while doing a technical audit of my website, I came across a compression issue with my website. All the crawlers were asking to use a compression mechanism in my responses.&lt;/p&gt;

&lt;p&gt;Digging deeper, I learned that AWS doesn't have compression enabled for its HTTP APIs. I spoke to the AWS support team and they asked me to implement a few changes in response, which will enable binary support for lambda response, AWS expects its response should have below attributes in response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"body": gzippedResponse.toString("base64"),
"isBase64Encoded": true,
"statusCode": 200,
"headers": {
   "Content-Encoding": "gzip"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I wanted to take the same approach with my Laravel application. I found one article about &lt;a href="https://bannister.me/blog/gzip-compression-on-laravel-vapor/"&gt;Gzip compression with Laravel Vapor&lt;/a&gt;, since I wasn't using Vapor, I had to research and implement in my own tech stack.&lt;/p&gt;

&lt;p&gt;The documentation I found was easy to follow and helped me make the changes to my app. Now when I run my app, it works perfectly!&lt;/p&gt;

&lt;p&gt;I configured my serverless.yml file with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;provider:
    # ...
    apiGateway:
        binaryMediaTypes:
            - '*/*'
    environment:
        BREF_BINARY_RESPONSES: '1'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I made middleware as mentioned in the article:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class GzipEncodeResponse
{
    public function handle(Request $request, Closure $next)
    {
        $response = $next($request);

        if (in_array('gzip', $request-&amp;gt;getEncodings()) &amp;amp;&amp;amp; function_exists('gzencode')) {
            $response-&amp;gt;setContent(gzencode($response-&amp;gt;getContent(), 9));
            $response-&amp;gt;headers-&amp;gt;add([
                'Content-Encoding' =&amp;gt; 'gzip',
                'X-Vapor-Base64-Encode' =&amp;gt; 'True',
            ]);
        }
        return $response;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then added this middleware in my kernel.php file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protected $middlewareGroups = [
    'web' =&amp;gt; [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \App\Http\Middleware\GzipEncodeResponse::class,
    ],

    'api' =&amp;gt; [
        'throttle:api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,     
    ],
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And did &lt;code&gt;serverless deploy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Everything was working perfectly as expected. I achieved my gzip compression in my HTTP APIs&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>serverless</category>
      <category>aws</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
