<?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>DEV Community: Phani Kumar</title>
    <description>The latest articles on DEV Community by Phani Kumar (@slotlocker).</description>
    <link>https://dev.to/slotlocker</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%2F15667%2Ff52b17f9-f909-4b99-aa17-1d6a4e413ca2.png</url>
      <title>DEV Community: Phani Kumar</title>
      <link>https://dev.to/slotlocker</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/slotlocker"/>
    <language>en</language>
    <item>
      <title>Deploying Next.js on AWS Lambda</title>
      <dc:creator>Phani Kumar</dc:creator>
      <pubDate>Thu, 16 Aug 2018 14:52:57 +0000</pubDate>
      <link>https://dev.to/slotlocker/deploying-nextjs-on-aws-lambda-4f9b</link>
      <guid>https://dev.to/slotlocker/deploying-nextjs-on-aws-lambda-4f9b</guid>
      <description>&lt;p&gt;&lt;a href="https://www.belong.co" rel="noopener noreferrer"&gt;Belong&lt;/a&gt; is a company with a simple vision — “Help folks discover where they Belong”. One way in which we help companies be discovered by these folks is by providing talent branding and engagement solutions.&lt;/p&gt;

&lt;p&gt;Some of these solutions involved building microsites with about 4–5 pages. Since, Belong caters to many customers, we quickly realised that we will end up having many such microsites with a lot of things in common technically. So, we started discussing how do we set up an efficient development &amp;amp; deployment pipeline.&lt;/p&gt;

&lt;p&gt;As developers working on these projects, we wanted to utilise a stack that checked the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Code Reusability (DRY)&lt;/strong&gt;: These sites have multiple UI components/functionalities that can be shared between them. Hence, we decided to build the site in React, since we could utilise a library of components we have already built! (&lt;a href="https://github.com/belongco/belong-ui" rel="noopener noreferrer"&gt;You can check out the library here&lt;/a&gt; !) .&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Server Side rendering:&lt;/strong&gt; Our microsites are usually a good mix of static and dynamic pages. Each of these sites need be &lt;a href="https://developers.google.com/search/docs/data-types/job-posting" rel="noopener noreferrer"&gt;optimised for search engine crawlers&lt;/a&gt;. Hence, we need to render React on the server side.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Great development experience:&lt;/strong&gt; For us the following comprise great experiences:&lt;/p&gt;

&lt;p&gt;i. Easy to setup up on local machines&lt;/p&gt;

&lt;p&gt;ii. Hot module replacement (HMR)&lt;/p&gt;

&lt;p&gt;After evaluating multiple tools, we found &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt; to be the most promising framework for our use case. It’s incredibly easy to setup and also provides support for hot module replacement.*&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Isolation &amp;amp; Reliability:&lt;/strong&gt; Given that these sites represent our client’s brand, we need a setup that is highly reliable. Nobody wants 2AM pagers on downtime/degraded performance. Additionally, a bug in one client site should not affect other clients.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Every client has 2 site instances, staging and production. Thus, need a scalable process to maintain these sites without having to reconfigure each server independently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Low expenses:&lt;/strong&gt; Need to optimise costs for nearly 2*(no. of clients) instances. Each of these clients would have different workloads based on their talent brand and hiring needs. Needed a setup where manual tuning of each client instance is avoided.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Serverless Architecture checks of all these points, and hence we decided to deploy our Next.js apps on &lt;a href="https://aws.amazon.com/lambda/" rel="noopener noreferrer"&gt;AWS Lambda&lt;/a&gt;. It offers considerable amount of reliability whilst keeping the costs down.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Repository structure
&lt;/h1&gt;

&lt;p&gt;We have a single repository to maintain all the microsites with each customer having a separate branch.&lt;/p&gt;

&lt;p&gt;The master branch serves as a template for the microsites. When a new customer signs up, a new branch is forked from master and the necessary changes are made to the serverless.yml file (Example: the domain name that maps to the API gateway endpoint). The customer specific assets are also checked into this branch.&lt;/p&gt;

&lt;p&gt;The serverless config accepts a bunch of environment variables and parameters that help gain control over which resources are deployed.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is the deployment like?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://serverless.com/" rel="noopener noreferrer"&gt;Serverless&lt;/a&gt; uses Cloudformation under the hood to bring up the resources as defined in the configuration file(&lt;a href="https://github.com/belongco/nextjs-serverless-setup/blob/master/serverless.sample.yml" rel="noopener noreferrer"&gt;serverless.yml&lt;/a&gt;). Although, it primarily supports definitions of Lambda functions and their triggers, one can also use native Cloudformation templates to bring up other resources like DynamoDB tables and the like. The following illustration highlights our deployment process,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F48it9uaszatb27h72b5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F48it9uaszatb27h72b5f.png" alt="Deployment Process" width="800" height="322"&gt;&lt;/a&gt;&lt;em&gt;Deployment Process&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When a new branch/existing branch is pushed, &lt;a href="https://github.com/Strider-CD/strider" rel="noopener noreferrer"&gt;Strider&lt;/a&gt;, our CI system picks it up and&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Clones the repository&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Installs the package dependencies&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Runs the linter&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Builds the Next.js project&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Exports the static files to S3 and the STATIC_PATH bearing the S3 URL is set in the Lambda environment for the application to utilise&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deploys the serverless configuration with the right parameters based on the environment.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This greatly reduces the complexity that comes with deploying a plethora of microsites while also keeping the costs down. Consider a scenario where we have to deploy microsites for 4 customers while maintaining isolation. Traditionally, we would have had to spin up 8 instances which would have cost us north of $160(assuming we pick the smallest t2.small instance type with no hardware abstraction using Kubernetes and the likes).&lt;/p&gt;

&lt;p&gt;With serverless, given the fact that the number of requests most of these microssites handle is pretty low, &lt;strong&gt;the cost adds up to only $10!&lt;/strong&gt; That is a huge cost saving that would scale impressively as the number of microsites go up.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: In scenarios where you plan to handle a million requests/day and above, it would be economical to deploy the application on traditional servers/containers as opposed to using the serverless architecture.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While the setup works seamlessly now, the path to get here was not all that straightforward. As we started to experiment with setting up the Next.js application on AWS Lambda, we stumbled upon a set of challenges with serving static files.&lt;/p&gt;

&lt;p&gt;In retrospect, we would not recommend serving static files via Lambda for two reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Increases the cost since we get billed for each request&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sometimes we might need to serve large files(videos, GIFs) which cannot be optimised to work around the &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/limits.html" rel="noopener noreferrer"&gt;limits&lt;/a&gt; of Lambda.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which is why we had to include the step in the CI pipeline that would push the static files to S3 as part of the deployment process and made the path available to the application via the Lambda environment variables.&lt;/p&gt;

&lt;p&gt;If you still want to serve the static files via Lambda (cause you are a rebel 😉), make sure you whitelist the correct mime-types in the API gateway configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sidenote&lt;/strong&gt;: If you want your serverless deployments to connect to your services running inside a VPC, then you’d have to &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/vpc.html#vpc-configuring" rel="noopener noreferrer"&gt;configure&lt;/a&gt; the Lambda to run in a subnet that has a NAT gateway/instance attached to it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Testing serverless locally
&lt;/h1&gt;

&lt;p&gt;For the most part, you don’t have to push to Lambda to check if the application is working as intended. The serverless framework provides a great way to test the functions &lt;a href="https://serverless.com/framework/docs/providers/aws/cli-reference/invoke-local/" rel="noopener noreferrer"&gt;locally&lt;/a&gt;, which helps save a ton of time and effort.&lt;/p&gt;

&lt;h1&gt;
  
  
  DIY
&lt;/h1&gt;

&lt;p&gt;Along with these learnings we also wish to share a simple starter kit on Github, which can aid you to evaluate this architecture/setup!&lt;/p&gt;

&lt;p&gt;Github Link: &lt;a href="https://github.com/belongco/nextjs-serverless-setup" rel="noopener noreferrer"&gt;https://github.com/belongco/nextjs-serverless-setup&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The starter kit will:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Setup the Lambda function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configure DynamoDB table for the app to work with&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Setup the API gateway, map its endpoint to a custom domain and create the Route53 entry for the same(uses a few plugins to do the domain mapping)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Challenges ahead:
&lt;/h1&gt;

&lt;p&gt;Our application use case also requires us to run some long running crons and Lambda has an inherent limit on the execution time which doesn’t make it a good candidate for running tasks that exceed 300 seconds.&lt;br&gt;
We are evaluating &lt;a href="https://aws.amazon.com/fargate/" rel="noopener noreferrer"&gt;AWS Fargate&lt;/a&gt; and &lt;a href="https://aws.amazon.com/batch/" rel="noopener noreferrer"&gt;AWS Batch&lt;/a&gt; to get those tasks running. Hopefully, we should be back with another blog post covering the implementation details of the same.&lt;/p&gt;

&lt;h1&gt;
  
  
  About Us:
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://twitter.com/kaarthek" rel="noopener noreferrer"&gt;Vivek Kaarthek&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I am a frontend engineer working with Product teams at Belong. I love building web apps with JS and then regretting why I built them with JS 😅! If not coding, you will find me playing FIFA ⚽️, reading history 📖 or learning cartooning 🎨 !!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://twitter.com/slotlocker" rel="noopener noreferrer"&gt;Phani&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I like complicating things. 💥&lt;/p&gt;

&lt;p&gt;👩‍🎨 Illustration by &lt;a href="https://www.behance.net/AnukritiV" rel="noopener noreferrer"&gt;Anukriti Vijayavargia&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: This post was originally posted to Medium &lt;a href="https://medium.com/@vkaarthek/deploying-next-js-on-aws-lambda-2b8e8ab23697" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;

</description>
      <category>serverless</category>
      <category>nextjs</category>
      <category>devops</category>
      <category>react</category>
    </item>
  </channel>
</rss>
