<?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 Christoph Peter on Medium]]></title>
        <description><![CDATA[Stories by Christoph Peter on Medium]]></description>
        <link>https://medium.com/@christoph_peter?source=rss-229ae2b2c9e4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*IjRQBDvkpGYn5WFa.jpg</url>
            <title>Stories by Christoph Peter on Medium</title>
            <link>https://medium.com/@christoph_peter?source=rss-229ae2b2c9e4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 05:05:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@christoph_peter/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[Migrating an express based API to AWS Lambda functions & API Gateway with JAWS]]></title>
            <link>https://medium.com/@christoph_peter/migrating-an-express-based-api-to-aws-lambda-functions-api-gateway-with-jaws-3d948af1891?source=rss-229ae2b2c9e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d948af1891</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <dc:creator><![CDATA[Christoph Peter]]></dc:creator>
            <pubDate>Sun, 11 Oct 2015 18:35:40 GMT</pubDate>
            <atom:updated>2015-10-11T18:35:40.391Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KpGrF_W1z6ffd3CSvrUk_w.jpeg" /><figcaption>JAWS Framework</figcaption></figure><p>I looked at AWS Lambda functions when Amazon first released them. I was excited by the idea, and then really turned off by how cumbersome the configuration and deployment process was. I challenge anyone to setup a larger API using amazons API Gateway GUI. When building an API I really only want to specify two things (ignoring security, logging etc …).</p><ol><li><strong>The handler</strong> it self that should be passed a couple of input parameters and return some JSON when its done. Express and Lambda have very similar signatures in that regard. Express has(req, res, next) =&gt; { res.json(data)}, Lambda has (event, context) =&gt; { context.done(null, data)}</li><li><strong>The route</strong> / URL where the method is accessible. Express lets me do this router.post(‘/api/v1/endpoint/:param1/:param2’) , Lambda needs API Gateway and then again its a lot of clicking, configuration &amp; parameter mappings which is where I stopped the first 2 times I played with lambda.</li></ol><p>I stumbled on JAWS <a href="https://github.com/jaws-framework/JAWS">https://github.com/jaws-framework/JAWS</a> and it seems to abstract away most of the annoying boiler plate config needed to map a simple piece of code to a publicly accessible URL. So I set out to migrate an existing api that syncs data from toggl.com (timetracking SaaS) to our internal dashboard which is a purely client side React application aside from this one server side syncing process. So here goes…</p><h4>Install JAWS &amp; Create a Project</h4><p>Setup an AWS user, here are simple instructions on how to do that (<a href="https://github.com/jaws-framework/JAWS/blob/master/docs/account_setup.md">https://github.com/jaws-framework/JAWS/blob/master/docs/account_setup.md</a>)</p><p><strong>npm install jaws -g</strong> (<a href="https://github.com/jaws-framework/JAWS#quick-start">https://github.com/jaws-framework/JAWS#quick-start</a>) , create a project with <strong>jaws project create </strong>and follow the instructions</p><h4>Migrate an Endpoint</h4><p>You want to migrate your endpoints from express to a lambda function. I had two routes /toggl/sync and toggl/sync/project/:projectId/ which I wanted to port so I could turn off the server.</p><p>JAWS is very opinionated about project structure but thats fine. <a href="https://github.com/jaws-framework/JAWS/blob/master/docs/project_structure.md">https://github.com/jaws-framework/JAWS/blob/master/docs/project_structure.md</a> To to create your first lambda function handler for your endpoint use the following command:</p><p><strong>jaws module create toggl sync</strong></p><p>This creates a new module in the aws_module folder. A module is really just a folder with 4 files. Some config and a place for you to put your code.</p><ol><li>handler.js a simple file that basically just calls your code which is defined in index.js file. you can leave that as is. Not really sure why its even needed.</li><li>awsm.json the config file that lets you declaratively specify all the config parameters for aws lambda and aws api gateway. a bit confusing at first but much better than dealing with the aws GUI.</li><li>index.js, this is where your actual code goes. In my case I have two libraries that handle the sync process with toggl. I put those in the lib folder in required them. JAWS will take care of packaging your libraries during deployment. That makes reusing existing code really easy. I literally just copy and pasted them from my express app.</li></ol><h4>Compare the new lambda handler the original express controller code</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9beb20fde81882078b5577edf700cb23/href">https://medium.com/media/9beb20fde81882078b5577edf700cb23/href</a></iframe><p>my old express controller</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f29b88adac4469d87949590c913ebb36/href">https://medium.com/media/f29b88adac4469d87949590c913ebb36/href</a></iframe><p>In both cases its really just glue code to my business logic and that is all it should be in my point of view.</p><h4>Local Testing</h4><p>With JAWS you can simple call <strong>jaws run </strong>to locally run a lambda function, but make sure you are inside the correct folder, you need to be in the specific module folder. So in my case I want to cd aws_modules/toggle/sync and then call <strong>jaws run</strong>. This will run your handler.js -&gt; index.js and pass in whatever data is inside your event.json in my case it was only one parameter called access_token.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86b25fa85c554d1f3abf3c59e6751e05/href">https://medium.com/media/86b25fa85c554d1f3abf3c59e6751e05/href</a></iframe><p>The value of access_token will be accessible from event.access_token, and in my code I can pass that on to some auth logic.</p><h4>Tweaking the awsm.json config for deployment</h4><ol><li>How does my access_token get from the HTTP request hitting Api Gateway to the event which in turn gets passed to the handler. We need to add a little bit of mapping code. line 35 &amp; 38 do the trick. more on input mapping can be found here (<a href="http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html">http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html</a>)</li><li>Making it work with ES6. Line 9 enables babel support. done.</li><li>Fix timeout after 6 seconds. I don’t know why thats the default but on line 23 increase the value if you have longer running lambda functions like this sync process.</li><li>I need CORS for this to work with my client side app. Line 47 will add the CORS headers.</li><li>For my second endpoint the mapping looks like this to capture the parameter from the url path. L35: “Path”: “toggl/project/{projectId}/sync/” projectId can then again be mapped via the $input.params(‘projectId’) variable.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44d28cbd17a108a6ab1f3b14d8c4b0bf/href">https://medium.com/media/44d28cbd17a108a6ab1f3b14d8c4b0bf/href</a></iframe><h4>Deploying</h4><p>To deploy a project simply call<strong> jaws dash, </strong>this will start a small dialog asking you which pieces of the project you want to deploy and then jaws will work its magic. It will do a ton of stuff, setting up the API Gateway config, lambda function, basically everything I complained about at the top. The initial deploy will take a while but after that its fairly quick and you can do partial deploys e.g.: just the lambda function or the gateway configuration depending on what changes you made.</p><h4>Calling the endpoints</h4><p>Once the deployment is done you will be presented with a URL to call your endpoints. Notice the “dev” in the url, that is the “Stage” of your API Gateway. You can have multiple like dev, stage, prod but I have not explored that yet. The URLs are not pretty so you could do something with aws cloudfront or cloudflare.com to use your own custom domain.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2989d44fa3eb668be9d8ddec720ee93c/href">https://medium.com/media/2989d44fa3eb668be9d8ddec720ee93c/href</a></iframe><p>Paste this URL in your browser and hit enter. SUCCESS. Your lambda function should run and give you the expected JSON output. My client side React dashboard can now call this URL instead of my express app to trigger the sync. No server needed and depending on your traffic very likely for free.</p><h4>Debugging</h4><p>Even if everything works locally and the deployment is successful things might not work, everything that goes on your lambda functions is logged to aws cloudwatch.</p><h4>Final thoughts &amp; considerations</h4><ol><li>My access_token is a custom auth logic and not related to the auth methods which aws API Gateway provides. It would be nice to have some sort of middleware like in express otherwise the auth code needs to be included in every handler. I am sure there are ways.</li><li>Lambda functions just gained a cron like scheduling so I can now run the sync automatically once a day.</li><li>The event.json is a pretty neat way to test things as you could specify multiple versions with different parameters to test different scenarios. I have not explored a way to automate that. On the other hand given the simplicity of the “glue code” index.js you could also just test your libraries directly.</li><li>I sometimes get {“message”: “Endpoint request timed out”} errors when calling the endpoint, I have not figured out the cause for these. Cloudwatch shows no errors in the logs.</li><li>There is still a ton of boiler plate code involved but since its auto generated and only needs minor adjustment I am fine with that.</li><li>Jaws is still in beta, but if the number of github stars are any indication this will be pretty big. Its also very actively being worked on. Thanks to the creators of JAWS!</li><li>There is currently no way to cleanly delete a project and if you manually remove resources via the aws management console it can break your jaws deployment. This feature is should be in the works though <a href="https://github.com/jaws-framework/JAWS/issues/177">https://github.com/jaws-framework/JAWS/issues/177</a>.</li><li>A quick search has not turned up anything but it would seem to be the ultimate serverless backend awesomeness to take a graphql server like graphql-js and expose its single endpoint via a lambda function + api gateway.</li><li>Another interesting use of lambda functions I came across is dexter by betaworks <a href="https://rundexter.com/">https://rundexter.com/</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d948af1891" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>