<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Constantine Kurianoff on Medium]]></title>
        <description><![CDATA[Stories by Constantine Kurianoff on Medium]]></description>
        <link>https://medium.com/@kurianoff?source=rss-53b29febbce4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*gRVz1EUAdU-E_HVD</url>
            <title>Stories by Constantine Kurianoff on Medium</title>
            <link>https://medium.com/@kurianoff?source=rss-53b29febbce4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 01:38:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kurianoff/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deploy Serverless React App with Node.js Express Backend to AWS with Terraform in Under 15 Minutes]]></title>
            <link>https://medium.com/@kurianoff/deploy-serverless-react-app-with-node-js-express-backend-to-aws-with-terraform-in-under-15-minutes-2386bf0c58e9?source=rss-53b29febbce4------2</link>
            <guid isPermaLink="false">https://medium.com/p/2386bf0c58e9</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[serverless]]></category>
            <dc:creator><![CDATA[Constantine Kurianoff]]></dc:creator>
            <pubDate>Sun, 01 May 2022 17:18:40 GMT</pubDate>
            <atom:updated>2022-07-15T18:20:04.193Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article we will deploy a <strong>2-tier (frontend+backend)</strong> React app to AWS, based on a simple example. The article will show what quick changes to boilerplate React and Node.js app are needed to make the app fully serverless and AWS-compatible, so you can leverage the same approach in your own React/Node.js-based project.</p><p><strong>IMPORTANT.</strong> This article does not cover the case of 3+tier apps (frontend, backend, middleware, database), but it should be easy to use these instructions for the first two tiers, and then set up / wire up the database separately.</p><h3>Sample React App</h3><p>In this article, we will deploy this <a href="https://github.com/terraformita/terraform-aws-serverless-app/tree/main/examples/simple">sample React App</a> to AWS.</p><p>Clone the <a href="https://github.com/terraformita/terraform-aws-serverless-app">GitHub repository</a> and change directory to the sample React App:</p><pre>git clone <a href="/service/mailto:git@github.com">git@github.com</a>:terraformita/terraform-aws-serverless-app.git</pre><pre>cd terraformita-aws-serverless-app/examples/simple</pre><h3>Concepts</h3><p>When it comes to AWS deployment, it is important to understand which AWS services will be leveraged to make it happen. Let’s not make a secret of it:</p><p>- <strong>Frontend</strong> will be hosted with <strong>AWS S3</strong> in the S3 bucket;<br>- <strong>Backend</strong> will be hosted as an <strong>AWS Lambda</strong> function (slight code modification will be required comparing to standard Express approach);<br>- To make Frontend and Backend work as a <strong>single website</strong>, the <strong>AWS API Gateway</strong> will be used to proxy all frontend/backend requests</p><p>The whole thing will be completely serverless.</p><h3>React App Structure</h3><p>Let’s take a sample React App from <a href="https://github.com/terraformita/terraform-aws-serverless-app/tree/main/examples/simple">this GitHub repository</a>.</p><p>The app has the following file/folder structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*_oatXIqHiAhh67wTvLwSAw.png" /></figure><p><strong>Frontend</strong> app is created based on very illustrative article of Jarrett Retz, “<a href="https://rapidapi.com/blog/how-to-use-an-api-with-react/">How to Use an API with ReactJS</a>” (although the approach described there is based on non-functional components, it is still valid), and consists of 2 files: <br>- <strong>index.js</strong> that contains the React App itself, and<br>- <strong>index.html</strong> that embeds the React App</p><p>It is a single-page app that looks like this at run-time:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3BQ1q7ZuSwCb9WUWmwkYnQ.png" /><figcaption>React App UI</figcaption></figure><p><strong>Backend</strong> app consists of <strong>index.js</strong>, which defines the application API. The API consists of only one method: <em>/api/info</em>, which is sufficient for this article.</p><p>When user clicks on “Get API Info” button on the app’s user interface, it calls backend API’s <em>/api/info</em> endpoint, and the app’s UI will change to the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4vrVEjJEi2GrgGGq17H7Rg.png" /><figcaption>React App UI when API is called</figcaption></figure><h3>Packaging React App for AWS</h3><h4><strong>Backend</strong></h4><p><em>The backend is deployed to AWS as an AWS Lambda function</em>, so we need to do 3(three) small changes to make it AWS Lambda-compatible.</p><p><strong>=&gt; Code Changes and Packaging</strong><br>We will use node.js library called <strong>Serverless-HTTP</strong> <a href="https://www.npmjs.com/package/serverless-http">https://www.npmjs.com/package/serverless-http</a> to wrap express app in a lambda function.</p><p><a href="https://github.com/terraformita/terraform-aws-serverless-app/blob/main/examples/simple/backend/nodejs/index.js#L1"><em>Small Change #1.</em></a><strong> </strong>Let’s add a line to the top of our index.js that imports serverless-http:</p><pre>const serverless = require(&#39;serverless-http&#39;);</pre><p><a href="https://github.com/terraformita/terraform-aws-serverless-app/blob/main/examples/simple/backend/nodejs/index.js#L26"><em>Small Change #2.</em></a> Let’s comment out the line that runs express app listener locally, and wrap it up with the serverless-http, thus converting it to the Lambda handler:</p><pre>/* COMMENT THIS LINE OUT */<br>// app.listen(3000, () =&gt; console.log(`Listening on: 3000`));</pre><pre>/* ADD THIS LINE TO MAKE THE APP LAMBDA-COMPATIBLE */<br>module.exports.handler = serverless(app);</pre><p>After these changes are made, the Lambda function <strong>handler</strong> is exported as index.handler , where:<br>- index is the backend’s main file name without extension (index.js)<br>- handler is the name of exported function (from module.exports.handler)</p><p><em>Small Change #3.</em> Our node.js backend must reside in nodejs/ directory, because when AWS lambda is deployed, it will be split onto 2 parts: the code and the libraries, and it is important that the libraries are stored under nodejs/node_modules , otherwise Lambda function will not be able to find them.</p><p>In our example the backend is already properly placed into the nodejs folder.</p><p><strong>=&gt; Compilation</strong><br>Change directory to backend/ and run the following commands to properly initialize the backend and download all required modules:</p><pre>cd backend/<br>npm init -y</pre><pre>npm install --save express<br>npm install --save serverless-http</pre><p>Verify that the directory structure of the backend matches the following:</p><pre>backend/<br> - nodejs/<br>  - node_modules/</pre><pre>  - package.json<br>  - package-lock.json<br>  - index.js</pre><h4>Frontend</h4><p>When the app is deployed to AWS API Gateway, the final URL will contain “stage” suffix (like /dev, or /staging, or /prod— depending on the stage name you specify). We should account for that, so the React App knows that suffix.</p><p><strong>=&gt; Code Changes and Packaging</strong><br>There are two places where the API Gateway suffix should be added:</p><ul><li>This suffix should be prepended to all API endpoints (e.g. /api/info becomes /dev/api/info ), so we should update it in the <a href="https://github.com/terraformita/terraform-aws-serverless-app/blob/main/examples/simple/frontend/src/index.js#L3">config section</a> offrontend/src/index.js :</li></ul><pre>const config = {<br>  apiPrefix: &#39;/dev&#39;<br>};</pre><p>(we call it “api prefix” because it goes before the API endpoint path)</p><ul><li>This same suffix should be accounted for in the <a href="https://github.com/terraformita/terraform-aws-serverless-app/blob/main/examples/simple/frontend/public/index.html#L23">index.html</a> page that serves the app:</li></ul><pre>&lt;script src=&quot;/dev/js/index.js&quot;&gt;&lt;/script&gt;</pre><p>(note /dev prefix before /js/index.js)</p><p><strong>=&gt; Compilation</strong><br>Change directory to frontend/ and run the following commands to properly initialize the frontend and download all required modules:</p><pre>cd frontend/<br>npm init -y</pre><pre>npm install --save-dev babel-cli<br>npm install babel-preset-react-app@3</pre><p>Compile and publish the compiled JS to the public/ folder:</p><pre>npx babel src --out-dir public/js --presets react-app/prod</pre><p>Verify that the directory structure of the frontend matches the following:</p><pre>frontend/<br> - node_modules/</pre><pre> - package.json<br> - package-lock.json</pre><pre> - public/<br>  - index.html<br>  - js/<br>   - index.js</pre><pre> - src<br>  - index.js</pre><h3>Deploy React App to AWS</h3><p>Now, as we compiled and packaged both <strong>backend</strong> and <strong>frontend</strong>, it’s time to deploy.</p><p><strong>=&gt; Install Terraform</strong><br>Nothing to add here. <a href="https://learn.hashicorp.com/tutorials/terraform/install-cli">Install Terraform</a> if you don’t already have it.</p><p><strong>=&gt; Create AWS Account and Download Access Keys</strong></p><ul><li><a href="https://aws.amazon.com/account/sign-up">Create AWS account</a> if you don’t have one</li><li><a href="https://aws.amazon.com/premiumsupport/knowledge-center/create-access-key/">Create AWS Access Keys</a></li></ul><p><strong>=&gt; Add AWS Access Keys to </strong><strong>~/.aws/credentials file</strong><br>Open up your ~/.aws/credentials file and put the downloaded access keys there:</p><pre>[default]<br>aws_access_key_id=&lt;YOUR AWS ACCESS KEY GOES HERE&gt;<br>aws_secret_access_key=&lt;YOUR SECRET ACCESS KEY GOES HERE&gt;</pre><p>The [default] is the name of AWS profile; it is a good practice to name it differently if you manage multiple AWS accounts.</p><p><strong>=&gt; Initialize Terraform</strong><br>Change directory to our <a href="https://github.com/terraformita/terraform-aws-serverless-app/tree/main/examples/simple">React App sample</a>, where the main.tf file resides.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*cqD5wzV6LHB1y4NAkrDj8Q.png" /><figcaption>React App directory</figcaption></figure><p>Initialize terraform in React App directory:</p><pre>terraform init</pre><p>and expect the success message:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pWuJ-ZKaqfmri081TvI_xg.png" /></figure><p><strong>=&gt; Deploy React App</strong><br>Run the following command to deploy the app:</p><pre>AWS_PROFILE=default terraform apply</pre><ul><li>Replacedefault with the actual name of your AWS profile.</li></ul><p>Expect terraform to create 49 resources on your AWS account. As it was mentioned in the beginning of this article, most of it are the pieces of AWS API Gateway, then an S3 bucket for the frontend, and an AWS Lambda function serving as a backend API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*EbFHiA1MK1OAptqVhoseYQ.png" /></figure><p>Terraform will then ask you to confirm that you are OK creating the resources:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_RIlc67nY5taTaeVhPSOfg.png" /></figure><p>Please enter yes and hit ENTER . This will get the deployment going.<br>The deployment should take around ~2 minutes to complete.</p><p><strong>=&gt; Deployment Results</strong><br>Once the deployment is complete, you will see the output like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4RHGLxvby5guwwi5OTLRbA.png" /></figure><p>Look at the aws_url field: it provides the URL to the deployed application: <a href="https://3efzlnn9c5.execute-api.us-east-1.amazonaws.com/dev">https://3efzlnn9c5.execute-api.us-east-1.amazonaws.com/dev</a></p><p><strong>Let’s open it up in the browser and test it!</strong></p><h3>Terraform Configuration for the App</h3><p>Let’s quickly review the terraform configuration of the app that we just deployed:</p><pre>module &quot;app&quot; {<br>  source  = &quot;terraformita/serverless-app/aws&quot;<br>  version = &quot;0.1.7&quot;</pre><pre>  name       = &quot;sample-react-app&quot;<br>  stage_name = &quot;dev&quot;</pre><pre>  # FRONTEND CONFIGURATION<br>  frontend = {<br>    path = &quot;/&quot;</pre><pre>    description = &quot;Frontend of sample react app&quot;<br>    entrypoint  = &quot;index.html&quot;<br>    source      = &quot;${path.module}/frontend/public&quot;<br>  }</pre><pre>  # BACKEND CONFIGURATION<br>  backend = {<br>    path = &quot;/api&quot;</pre><pre>    name        = &quot;sample-react-app-backend&quot;<br>    description = &quot;Backend of sample react app&quot;<br>    source     = &quot;${path.module}/backend/backend.zip&quot;<br>    entrypoint = &quot;index.handler&quot;<br>    runtime    = &quot;nodejs14.x&quot;<br>    memory_mb  = 128</pre><pre>    modules = [{<br>      source  = &quot;${path.module}/backend/nodejs_modules.zip&quot;<br>      runtime = &quot;nodejs14.x&quot;<br>    }]<br>  }</pre><pre>  # LOGGING CONFIGURATION<br>  enable_access_logging    = true<br>  enable_execution_logging = true</pre><pre>  tags = local.tags<br>}</pre><p>Terraform module terraformita/serverless-app/aws version 0.1.7 serves as the deployment tool for the app’sbackend and frontend.</p><p><strong>backend configuration consists of the following parameters:</strong><br>- path — the web path of the backend API (usually /api )<br>- name — machine-readable name of the backend app (needed for proper resource naming in AWS) — must contain no spaces<br>- source — relative path (that starts with ${path.module} which means <em>this directory</em>) to ZIP archive of the backend (the archive should contain all backend javascript files, except modules)<br>- entrypoint — the main file name without .js extension + name of the entry function (in our case index.handler )<br>- runtime — a node.js runtime<br>- memory — memory size in megabytes for the backend to run (should match <a href="https://docs.aws.amazon.com/lambda/latest/operatorguide/computing-power.html">supported memory size</a> of AWS Lambda function)</p><p><strong>backend.modules configuration</strong> points to a ZIP archive containing nodejs/node_modules directory (must start with nodejs/ , otherwise AWS won’t be able to find javascript modules).</p><p><strong>frontend configuration consists of the following parameters:</strong><br>- path — the web path of the frontend (usually / )<br>- entrypoint — a name of ‘index’ file of the frontend (usually index.html )<br>- source — relative path (that starts with ${path.module} which means <em>this directory</em>) to the folder containing all frontend files that should be published onto the website.</p><p>When you run terraform apply on sample react app, you will see two backend-related ZIP files to appear in the same folder — one with the backend code, another with backend node_modules. Unzip and explore them to get a better idea of how each of them should be structured.</p><h3>Conclusion</h3><p>Deploying React/Vue.js/Angular apps to AWS with Terraform is relatively easy, and it only takes a few customizations to make your app compatible with AWS Lambda that serves as an app backend.</p><p>Also, <a href="https://registry.terraform.io/modules/terraformita/serverless-app/aws/latest">Serverless App</a> Terraform module helps simplify the deployment. Additional features of the same module include:</p><p>- ability to use custom domain name with SSL certificate<br>- ability to protect the web app with user authentication.</p><p>I will try to explain these features in more detail in the future articles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2386bf0c58e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>