<?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 Pratheek Hegde on Medium]]></title>
        <description><![CDATA[Stories by Pratheek Hegde on Medium]]></description>
        <link>https://medium.com/@pratheekhegde?source=rss-aba551e1a1f3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_QhZrBbH_bwfLc9vIW23kg.jpeg</url>
            <title>Stories by Pratheek Hegde on Medium</title>
            <link>https://medium.com/@pratheekhegde?source=rss-aba551e1a1f3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 04:15:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pratheekhegde/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[Setting caching headers for a SPA in NGINX]]></title>
            <link>https://medium.com/@pratheekhegde/setting-caching-headers-for-a-spa-in-nginx-eb2f75f52441?source=rss-aba551e1a1f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb2f75f52441</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[caching]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[servers]]></category>
            <dc:creator><![CDATA[Pratheek Hegde]]></dc:creator>
            <pubDate>Wed, 17 Oct 2018 11:54:15 GMT</pubDate>
            <atom:updated>2018-11-24T12:25:39.310Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*P9PdkCzC2vBU1leyoK0hHg.png" /></figure><p>When your frontend app is a SPA, all the assets get loaded into the browser and routing happens within the browser unlike a SSR app or conventional/legacy web apps where every page is spat out by the server. If caching is misconfigured or not configured, you will have a <strong>horrifying</strong> time during deployments. Muscle memories of your developers will make them hit hard refresh when they hear the word “<strong>Code Deployed</strong>”. But your customers will rant and rave when their web page gets mangled in the middle of something important because of your deployment.</p><p>Having read on the internet before “<strong>Browsers and Web servers have been configured by default handle basic caching</strong>” made me procrastinate my learning on caching until one day It started annoying QA and started killing developer’s productivity. That day I told myself “<strong>You are not gonna sleep tonight!</strong>”</p><h4>Primary Requirement</h4><p>The configuration which I’m going to explain will work only if your SPA uses <strong>webpack</strong> or any other bundler which can be configured to append random characters to file names in the final distribution folder on every build (<strong>revving</strong>). This is quite a standard practice in modern web development. I’m pretty sure it will be happening in your system without your knowledge.</p><blockquote>Checkout <strong>Revved resources </strong>section at <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching">https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching</a></blockquote><h3><strong>Status Quo</strong></h3><ul><li>2 WebApps segregated by NGINX locations paths.</li><li>AWS ELB is sitting in front of the NGINX Web Server.</li><li>AWS Cloudfront is sitting in front of AWS ELB. (Actual caching is done here)</li><li>NGINX is sending out last-modified and etag headers.</li><li>I have some faint idea on how caching works.</li></ul><h3>Configure caching in NGINX</h3><p>The headers which we are going to need are cache-control, etag and vary. vary is added by NGINX by default, so we don’t have to worry about it. We need to add the other two headers in our configs at the right place to get caching working.</p><p>We have to configure the following things:</p><ol><li>disable caching for index.html ( Every time browser will ask for a fresh copy of index.html)</li><li>Enable caching for static assets (css, js, fonts, images) and set the expiry as long as you need ( eg: 1year).</li></ol><h4>1. Lets disable caching for index.html</h4><p>My current config.</p><pre>location /app1 {<br>    alias /home/ubuntu/app1/;<br>    try_files $uri $uri/ /index.html;<br>}</pre><p>After disabling caching.</p><pre>location /app1{<br>     alias /home/ubuntu/app1/;<br>     try_files $uri $uri/ /index.html;<br>     add_header Cache-Control &quot;no-store, no-cache, must-revalidate&quot;;<br>}</pre><h4>How this will work?</h4><p>When I hit /app1 from my browser NGINX will serve the index.html from /home/ubuntu/app1 directory to my browser, at the same time it will also excecute the add_header directive which will add the Cache-Control &quot;no-store, no-cache, must-revalidate&quot;; to the response header. The header conveys the following instructions to my browser.</p><ul><li>no-store : don’t cache/store any of the response in the browser.</li><li>no-cache : ask every-time(every request) with the server “Can I show the cached content I have to the user?”</li><li>must-revalidate : once the cache expires don’t serve the stale resource. ask the server and revalidate.</li></ul><p>The combination of these three values will disable caching for the response which is received from the server.</p><h4>2. Lets enable caching for static assets</h4><p>My current config.</p><pre>#for app1 static files<br> location /app1/static {<br>     alias /home/ubuntu/app1/static/;<br> }</pre><p>After enabling caching.</p><pre>#for app1 static files<br> location /app1/static {<br>     alias /home/ubuntu/app1/static/;<br>     expires 1y;<br>     add_header Cache-Control &quot;public&quot;;<br>     access_log off;<br>}</pre><h4>How this will work?</h4><p>We enable aggressive caching for static files by setting Cache-Control to &quot;public&quot; and setting expires header to 1y . We do this because our frontend build system generates new file names (<strong>revving</strong>) for the static assets every time we build and new file names invalidates the cache when browsers requests it. These static files are referred in index.html which we have disabled caching completely. I disable access logs for static assets as it adds noise to my logs.</p><p>Thats it! This must set up the caching headers for your beautiful app.</p><h3>NGINX add-header Gotcha</h3><p>We usually add the headers which we want to be common for all the location blocks in the server block of the config. But beware that these headers will not get applied when you add any header inside a location block.</p><blockquote><a href="http://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header">http://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header</a></blockquote><blockquote>There could be several add_header directives. These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level.</blockquote><pre>server {<br>    # X-Frame-Options is to prevent from clickJacking attack<br>    add_header X-Frame-Options SAMEORIGIN;</pre><pre>    # disable content-type sniffing on some browsers.<br>    add_header X-Content-Type-Options nosniff;</pre><pre>    # This header enables the Cross-site scripting (XSS) filter<br>    add_header X-XSS-Protection &quot;1; mode=block&quot;;</pre><pre>    # This will enforce HTTP browsing into HTTPS and avoid ssl stripping attack<br>    add_header Strict-Transport-Security &quot;max-age=31536000; includeSubdomains;&quot;;</pre><pre>    add_header Referrer-Policy &quot;no-referrer-when-downgrade&quot;;</pre><pre>    location /app1 {<br>        alias /home/ubuntu/app1/;<br>        try_files $uri $uri/ /index.html;<br>         add_header Cache-Control &quot;no-store, no-cache, must-revalidate&quot;;    <br>    }</pre><p>In the above example the security headers in the beginning will not be applied to /app1 block. Make sure you either duplicate it or have it written in a separate .conf file and import it in every location block.</p><h3>Bonus</h3><ol><li>Enabling CORS for fonts when serving through a different CDN domain.</li></ol><pre>location /app1/static/fonts {<br>     alias /home/ubuntu/app1/static/fonts/;<br>     add_header “Access-Control-Allow-Origin” *;     <br>     expires 1y;<br>     add_header Cache-Control “public”;<br> }</pre><p>Adding the Access-Control-Allow-Origin header will instruct the browsers to allow loading fonts from a different sub domain. Note that I also enabled aggressive caching for fonts too.</p><p>2. Adding vary by gzip</p><pre># Enables response header of &quot;Vary: Accept-Encoding&quot;<br>gzip_vary on;</pre><p>This will add Vary: Accept-Encoding header to the publicly cacheable, compressible resources and makes sure that the browser will get the correct encoded cached response.</p><p>3. NGINX HTTP to HTTPS Redirection</p><pre># Get the actual IP of the client through load balancer in the logs<br>  real_ip_header     X-Forwarded-For;<br>  set_real_ip_from   0.0.0.0/0;</pre><pre>if ($http_x_forwarded_proto = &#39;http&#39;) {<br>    return 301 <a href="https://$host$request_uri/">https://$host$request_uri</a>;<br>}</pre><p>Add the above in your server block and open port 80 along with 443 in your AWS ELB. This redirect http to https and also log the actual client ip n your logs.</p><p>Putting all the above things together, this how the final config would look like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf0610e941a258b95ae874646f445d50/href">https://medium.com/media/bf0610e941a258b95ae874646f445d50/href</a></iframe><blockquote>Article Originally published on <a href="https://www.zeolearn.com/magazine/setting-caching-headers-for-a-spa-in-nginx-cache">ZeoLearn</a>.</blockquote><p>🤘🏻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb2f75f52441" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lambda function for Automated Manual RDS Snapshots.]]></title>
            <link>https://medium.com/@pratheekhegde/lamda-function-for-automated-manual-rds-snapshots-d84e1d002019?source=rss-aba551e1a1f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/d84e1d002019</guid>
            <category><![CDATA[snapshot]]></category>
            <category><![CDATA[rds]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[backup]]></category>
            <dc:creator><![CDATA[Pratheek Hegde]]></dc:creator>
            <pubDate>Sun, 12 Aug 2018 06:30:44 GMT</pubDate>
            <atom:updated>2018-08-12T06:36:09.987Z</atom:updated>
            <content:encoded><![CDATA[<h3>AWS Lambda function for Automated Manual RDS Snapshots</h3><p>RDS Automated snapshots can be set to a max retention of 35 days, which means AWS deletes your snapshot when it is 35 days old.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*-lonN9hGheOCJql8f6Ilng.png" /><figcaption>Max period is only <strong>35</strong> days 👿.</figcaption></figure><p><strong>Q: </strong>What if I want to take a snapshot without any retention period?</p><p><strong>A:</strong> Just take a manual snapshot from the AWS Console 🙂.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*DotIckdKNnoiQ-ssRIaXfw.png" /><figcaption>Select the instance and click <strong>Take snapshot</strong> from the instance actions.</figcaption></figure><p><strong>Q:</strong> Alright, is there an option in the AWS Console to automate this manual snapshot?</p><p><strong>A:</strong> No 😒.</p><p><strong>Q</strong>: So every time I need to login to the console and do this task if I want snapshots with no retention period?</p><p><strong>A</strong>: Yes 😒, but wait there is another way 😏!</p><h3>Automated Manual RDS Snapshots</h3><p>With <strong>AWS SDK</strong> we can take a manual snapshot programatically, which means we can write a script to do this and run it in a AWS Lambda function periodically!</p><p>Here is the script below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c79fbe5b3b7b15fd5c8aac7e9438bdea/href">https://medium.com/media/c79fbe5b3b7b15fd5c8aac7e9438bdea/href</a></iframe><p>Create an<strong> IAM User</strong> of type <strong>Programmatic access </strong>with a <strong>Policy</strong> having <strong>RDS </strong>CreateDBSnapshot <strong>Permission</strong>. Generate <strong>Access key ID </strong>and <strong>Secret access key </strong>for this user and enter it in the <strong>Environment variables</strong> section of the Lambda Function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HMLgforKIP8LYQrulMrsrQ.png" /><figcaption>Add the keys in the <strong>Environment variables</strong> section of the Lambda.</figcaption></figure><p>Next create a rule in <strong>CloudWatch Events</strong> to trigger every 15 days (any frequency of your choice).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iLFuexwxoUIgx7ufuFtggw.png" /><figcaption>Select the created Lambda in the target section of the Events rule.</figcaption></figure><p>Now we have CloudWatch Events triggering the Lambda every 15 days to create our RDS Snapshots with no retention period 😈.</p><h3>Setup Email Notification 😌</h3><p>You can also have success/failure email notification on every trigger of the Lambda function.</p><p>To setup notifications first update the <strong>IAM</strong> <strong>Policy</strong> you created before with SNS Publish Permission.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bLdJvRv0fRP1YxH95AqGlg.png" /><figcaption>Add the SNS <strong>Publish</strong> permission<strong> </strong>to the policy you created for <strong>CreateDBSnapshot.</strong></figcaption></figure><p>Create a SNS Topic and create a subscription for the email you want to notify.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/973/1*yFeVSx_L_Orcx5dh3ElbLg.png" /><figcaption>Creating a subscription to a SNS Topic.</figcaption></figure><p>Copy the <strong>SNS</strong> topic’s <strong>ARN</strong> and add it as a variable SNS_TOPIC_ARN to the <strong>Environment variables</strong> section of the Lamdba.</p><p>Finally update your Lambda script to the one below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/596c4a1722b7025124db8b9e5aa26edc/href">https://medium.com/media/596c4a1722b7025124db8b9e5aa26edc/href</a></iframe><p>Now you will start receiving email notifications every 15 days when the snapshot procedure starts and/or if it fails 😃.</p><blockquote>Remember RDS has a limit of 100 manual snapshots per region. If you feel that’s not enough, you can request AWS to increase the limit.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d84e1d002019" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Displaying application Breadcrumbs in Vue.js]]></title>
            <link>https://medium.com/@pratheekhegde/displaying-application-breadcrumbs-in-vue-js-85456dc8a370?source=rss-aba551e1a1f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/85456dc8a370</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vue-router]]></category>
            <category><![CDATA[vue]]></category>
            <dc:creator><![CDATA[Pratheek Hegde]]></dc:creator>
            <pubDate>Thu, 29 Mar 2018 12:25:21 GMT</pubDate>
            <atom:updated>2018-09-07T03:44:13.450Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*zyNSb0UXhP8TfxYbj-GNWg.png" /><figcaption><a href="https://vuejs.org/">https://vuejs.org/</a></figcaption></figure><p>Breadcrumbs are essential if your application has many nested screens. It allows users to keep track of their locations when they are navigating around the app.</p><blockquote>Source code for this post is <a href="https://github.com/pratheekhegde/vue-dynamic-breadcrumbs">here</a>.</blockquote><p>In many frameworks, naive way of implementing it would be by adding breadcrumb metadata in the route definition in an array and then iterating it in every component which needs to show the crumbs. The problem with this approach is we are hardcoding the path and we can’t show crumbs for dynamic params.</p><p>If we are to do what I said just now in <strong>Vue.js, </strong>we could do it this way.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5157c74c4ed28d71099bbaa9f0b58b0d/href">https://medium.com/media/5157c74c4ed28d71099bbaa9f0b58b0d/href</a></iframe><p>In the snippet above :moon param belongs to the first child of the third route /saturn. This param is determined at runtime and I can’t specify it’s crumb in the breadCrumb property of it’s route definition.</p><p>The static crumbs defined above can be displayed by iterating $route.meta.breadCrumbs array in any component.</p><h4>Dynamic Breadcrumb component</h4><p>vue-router provides a $route object which represents the state of the current active route. I feel this object provides us enough information to display the crumbs at runtime. The properties I’m interested in are $route.path and $route.matched.</p><blockquote>$route.path — A string that equals the path of the current route, always resolved as an absolute path. e.g. &quot;/foo/bar&quot;.</blockquote><blockquote>$route.matched — An Array containing route records for all nested path segments of the current route. Route records are the copies of the objects in the routes configuration Array (and in children Arrays).</blockquote><blockquote><a href="https://router.vuejs.org/en/api/route-object.html"><strong>The Route Object</strong></a><strong> — </strong><strong>vue-router</strong></blockquote><p>Alright, Lets get started. Say I have loaded a component with path /earth/moon, I have added the text for the crumbs in the meta property of my route definitions like below and I want the bread crumbs as Earth &gt; Moon.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51af71288029b6f0d954ac6bd2f33655/href">https://medium.com/media/51af71288029b6f0d954ac6bd2f33655/href</a></iframe><p>The current absolute path for the component can be accessed through $route.path and inside a component I can access it through this.$route.path.</p><pre>// this.$route.path will be equal to &#39;/earth/moon&#39;</pre><pre>let pathArray = this.$route.path.split(&quot;/&quot;)</pre><pre>// pathArray is now [&quot;&quot;, &quot;earth&quot;, &quot;moon&quot;]</pre><pre>// we don&#39;t want the first item as it&#39;s empty<br>pathArray.shift()</pre><pre>// pathArray is now [&quot;earth&quot;, &quot;moon&quot;]</pre><p>Now I need to access the breadCrumb property from the meta property of the component’s route definition, for this I will be using $route.matched. For the component /earth/moon the $route.matched would contain two objects as it has matched two routes i.e. /earth and /earth/moon.</p><pre>0: {path: &quot;/earth&quot;, regex: /^\/earth(?:\/(?=$))?$/i, components: {…}, instances: {…}, …}</pre><pre>1: {path: &quot;/earth/moon&quot;, regex: /^\/earth\/moon(?:\/(?=$))?$/i, components: {…}, instances: {…}, …}</pre><p>These two objects are the copies of the objects in the routes configuration Array of the /earth and /earth/moon routes.</p><p>I can assess breadCrumb property for the /earth component like this</p><pre>this.$route.matched[0].meta.breadCrumb</pre><p>and for /earth/moon like this</p><pre>this.$route.matched[1].meta.breadCrumb</pre><p>Next I will create a new Array of Objects where each Object will have property to and text for each item in the pathArray . Each Object in the Array will represent a crumb. I will name this Array as breadCrumbArray .</p><p>Lately, I’m liking to write in Functional Programming style 🤓, so I will use reduce method to simplify this Array creation process.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/289a1e0c71438b536ed6da764aad8d63/href">https://medium.com/media/289a1e0c71438b536ed6da764aad8d63/href</a></iframe><p>In the above snippet path property stores the string of the item in pathArray as is, to property will store path for the :to property for the router-link which will triggered when clicking the crumb. In the to property we prefix the previous item’s path if it exist else we assign the current item’s pathto it.</p><p>The resulting array of the reducer is assigned to breadcrumbs variable and it will look like this.</p><pre>[ <br>    { &quot;path&quot;: &quot;earth&quot;, &quot;to&quot;: &quot;/earth&quot;, &quot;text&quot;: &quot;Earth&quot; }, <br>    { &quot;path&quot;: &quot;moon&quot;, &quot;to&quot;: &quot;/earth/moon&quot;, &quot;text&quot;: &quot;Moon&quot; } <br>]</pre><p>The last step would be to wrap the above logic in a vue component. I will use the <a href="https://bootstrap-vue.js.org/docs/components/breadcrumb">breadcrumb</a> component of <a href="https://bootstrap-vue.js.org/">bootstrap-vue</a> to display the bread crumbs.</p><p>The final breadcrumb component will look like this</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b7a1e36b8cef84d5cee6f0200ef7255/href">https://medium.com/media/1b7a1e36b8cef84d5cee6f0200ef7255/href</a></iframe><p>The above logic will also handle dynamic parms as we are picking up the constructed path from $route.path. The crumb text for dynamic params will be the pathitself. You can see this at line 23 in the above snippet, I’m assigning the path if the breadCrumb property doesn’t exist.</p><p>Live demo demonstrating everything explained above is <a href="https://vue-dynamic-breadcrumbs.netlify.com/">here</a>.</p><blockquote>Article Originally published on <a href="https://www.zeolearn.com/magazine/displaying-application-Breadcrumbs-in-Vue.js">ZeoLearn</a></blockquote><p>🤘🏻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85456dc8a370" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hide/Unhide comments when reading source on Github]]></title>
            <link>https://medium.com/@pratheekhegde/hide-unhide-comments-when-reading-source-on-github-d71cc5e5d61d?source=rss-aba551e1a1f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/d71cc5e5d61d</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[developer-productivity]]></category>
            <dc:creator><![CDATA[Pratheek Hegde]]></dc:creator>
            <pubDate>Sun, 12 Nov 2017 16:57:51 GMT</pubDate>
            <atom:updated>2018-02-13T17:09:48.013Z</atom:updated>
            <content:encoded><![CDATA[<p>Sometimes when I browse code on Github I find it frustrating to comprehend the code because of the verbose commenting by the author. I really appreciate the effort put by the author on the comments but sometimes they add some noise when I quickly want to run my eyes over it. So here is a quick trick to hide and unhide comments when you are viewing any source on Github.</p><h4>Hide the comments</h4><p>Open up the source on Github whose comments you want to hide. Fire up Chrome Inspector and switch to the console tab. Paste the below snippet and hit the enter key.</p><pre>// for hiding comments<br>var comments = $$(&quot;.pl-c&quot;);<br> <br>for (c in comments) {         <br>  comments[c].parentNode.parentNode.style.display=&#39;none&#39;;<br>}</pre><p>Voila! you must see all the comments vanish. You are left with only the source.</p><h4>Unhide the comments</h4><p>Paste the below snippet and hit the enter key again to get back the hidden comments.</p><pre>// show comments<br>var comments = $$(&quot;.pl-c&quot;);</pre><pre>for (c in comments) {         <br>  comments[c].parentNode.parentNode.style.display=&#39;&#39;;<br>}</pre><p>And it’s back the way it was. Instead you could refresh the page too. 😬</p><p>Hope, it helps you. Ciao!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d71cc5e5d61d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My first simple High Available/Scalable Architecture on AWS]]></title>
            <link>https://medium.com/@pratheekhegde/my-first-simple-high-available-scalable-architecture-on-aws-ea1a2cba29c3?source=rss-aba551e1a1f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea1a2cba29c3</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[scalable]]></category>
            <category><![CDATA[high-availability]]></category>
            <category><![CDATA[api-gateway]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Pratheek Hegde]]></dc:creator>
            <pubDate>Sun, 05 Nov 2017 09:42:11 GMT</pubDate>
            <atom:updated>2017-11-16T18:04:21.667Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A1DtLRO3abrta6WprwcaKQ.png" /><figcaption><a href="https://github.com/awslabs/startup-kit-templates">https://github.com/awslabs/startup-kit-templates</a></figcaption></figure><p>I’ve been playing with AWS for a year now. When I heard Scalable, High Availability architecture back in college days, I wondered what do these mean? I knew we had to run a lot of servers in data centres, but has this got something to do with configuring the application what we are building? I thought this was all about tweaking <strong>MySQL</strong>, <strong>PostgreSQL</strong> configs or configuring number of workers in <a href="https://www.nginx.com/"><strong>NGINX</strong></a> or something to do with our application design. This is the era of cloud, times have gone where you would procure hardwares and set up your own data centres (Well, you could still do it now with <a href="https://www.openstack.org/"><strong>Open Stack</strong></a> if you think <a href="https://aws.amazon.com/"><strong>AWS</strong></a>, <a href="https://azure.microsoft.com/"><strong>Azure</strong></a> or any other Cloud providers can’t handle you 🤪). Now you can literally set up a <strong>Virtual Private Cloud(</strong><a href="https://aws.amazon.com/vpc/"><strong>AWS</strong></a><strong>)</strong> sitting wherever you are right now just with a web browser. That’s what even I’ve been trying for the past 1 year and here is what I have.</p><h4><strong>DISCLAIMER &amp; PREFACE</strong>:</h4><ol><li>Architectures are opinionated and always subjective.</li><li>Having some idea with AWS services could make you finish this post swiftly. For those who don’t I’ve linked the AWS Lingos to their docs.</li><li>AWS has a lot of services, most of these can be implemented with their own service like <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html"><strong>EC2</strong></a>. So my policy is to use AWS services only if i feel its necessary. The prime reason for my policy is, if say there comes a day AWS can’t handle my application load/scale (Unlikely to happen), I could still replicate my architecture on bare metal machines easily.</li><li>I have not discussed anything about containerising or container orchestration(eg: Docker, Docker swarm, Kubernetes). For starters lot of things can be achieved with AWS <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html"><strong>AMI</strong></a>s. Maybe in my next posts I will talk on them.</li><li>Again my opinions are subjective, bear with me 👨🏻‍💻.</li></ol><h4>1. API Gateway</h4><p>API gateway is the entry point of the public traffic. This will/must be the only way your services should be consumed by the outer world. I’d like to quote the definition of <strong>API Gateway</strong> defined in one of the blog post by <a href="https://www.nginx.com/"><strong>NGINX</strong></a> <a href="https://www.nginx.com/blog/building-microservices-using-an-api-gateway/">here</a>.</p><blockquote>An <strong>API Gateway</strong> is a server that is the single entry point into the system. It is similar to the <a href="http://en.wikipedia.org/wiki/Facade_pattern">Facade</a> pattern from object‑oriented design. The API Gateway encapsulates the internal system architecture and provides an API that is tailored to each client. It might have other responsibilities such as authentication, monitoring, load balancing, caching, request shaping and management, and static response handling.</blockquote><p>So we are going to have an <strong>API gateway</strong> in all the <a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html"><strong>Availability Zones </strong></a>of the region which you are planning to use. There will be a <a href="https://aws.amazon.com/documentation/elastic-load-balancing/"><strong>ELB</strong></a> in front of this which will be distributing the load.</p><p>To implement this concept.</p><ol><li>Create a <strong>t2.small</strong> <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html"><strong>EC2</strong></a> instance with just <strong>NGINX</strong> installed( You can create an <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html"><strong>AMI</strong></a> for this and keep before hand for the <a href="https://docs.aws.amazon.com/autoscaling/latest/userguide/LaunchConfiguration.html"><strong>Launch configuration</strong></a>). We will be configuring <a href="https://www.nginx.com/"><strong>NGINX</strong></a> as a <a href="https://en.wikipedia.org/wiki/Reverse_proxy"><strong>reverse proxy</strong></a> to route all requests from the outside world to their respective micro services.</li><li>Create a <a href="https://docs.aws.amazon.com/autoscaling/latest/userguide/LaunchConfiguration.html"><strong>Launch configuration</strong></a> for this and attach it to an <a href="https://docs.aws.amazon.com/autoscaling/latest/userguide/AutoScalingGroup.html"><strong>Auto Scaling group</strong></a>. Based on your anticipated load you can set the min and max instances values in the <a href="https://docs.aws.amazon.com/autoscaling/latest/userguide/AutoScalingGroup.html"><strong>Auto Scaling group</strong></a>.</li><li>Create a <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html"><strong>Target group</strong></a> for the <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html"><strong>Application Load Balancer</strong></a> which you will be creating in the next step and link the created <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html"><strong>Target group</strong></a> to the <a href="https://docs.aws.amazon.com/autoscaling/latest/userguide/AutoScalingGroup.html"><strong>Auto Scaling group</strong></a><strong> </strong>created in the previous step.</li><li>Create a internet-facing <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html"><strong>Application Load Balancer</strong></a> and link it to the <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html"><strong>Target group</strong></a> which you created in previous step.</li></ol><p>And now you have a load balanced API Gateway which can scale based on load.</p><p>Your <strong>API Gateway </strong>will proxy all the requests to your micro services. The interesting thing here is they will be proxying to the internal load balancers of your micro services. Here is a sample snippet of how your <strong>NGINX</strong> config could look.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ElwDVZpTx5DHlRHg1LP4Hg.png" /><figcaption>NGINX API Gateway configuration snippet.</figcaption></figure><p>Other details in the config are irrelevant here, what’s important here is how you proxy the requests to the respective internal load balancers of your micro services.</p><blockquote>This proxy routing can be done with the help of <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html"><strong>AWS ELB listener rules</strong></a>. Why did I implement it with <a href="https://www.nginx.com/"><strong>NGINX</strong></a> ? <strong>Disclaimer &amp; Preface</strong> Point 5.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y5TEcXqBuhLxmyoZuPbtWA.png" /><figcaption>Path based routing in AWS ELBs without NGINX</figcaption></figure><h4>2. Your Micro services</h4><p>Splitting your monolithic codebase in to micro services has advantages and limitations. It’s discourse is beyond the scope of this post. Either ways you are gonna have at least one micro service. So you can apply the next describe steps to it.</p><blockquote>Following <a href="https://12factor.net/"><strong>The twelve-factor</strong></a> app methodology keep each of your micro service stateless. If there are any shared data move it to <a href="https://www.mongodb.com/"><strong>MongoDB</strong></a>, <a href="https://redis.io/"><strong>Redis</strong></a> or some other data store. Eg: if you are having a socket.io micro service, use <a href="https://github.com/socketio/socket.io-redis"><strong>socket.io-redis</strong></a> adapter to sync the socket room ids between other identical instances.</blockquote><p>We are going to do pretty much same to our micro service what we did with our <strong>API gateway</strong>. I’m gonna list the steps what you have to do for a single micro service, you can repeat the same for all your micro services.</p><ol><li>Package your micro service in an <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html"><strong>AMI</strong></a>.</li><li>Create a <a href="https://docs.aws.amazon.com/autoscaling/latest/userguide/LaunchConfiguration.html"><strong>Launch configuration</strong></a> for this and attach it to an <a href="https://docs.aws.amazon.com/autoscaling/latest/userguide/AutoScalingGroup.html"><strong>Auto Scaling group</strong></a>. Based on your anticipated load you can set the min and max instances values in the <a href="https://docs.aws.amazon.com/autoscaling/latest/userguide/AutoScalingGroup.html"><strong>Auto Scaling group</strong></a>.</li><li>Create a <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html"><strong>Target group</strong></a> for the <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html"><strong>Application Load Balancer</strong></a> which you will be creating next step and link the created <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html"><strong>Target group</strong></a> to the <a href="https://docs.aws.amazon.com/autoscaling/latest/userguide/AutoScalingGroup.html"><strong>Auto Scaling group</strong></a><strong> </strong>created in the previous step.</li><li>Create an internal <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html"><strong>Application Load Balancer</strong></a> and link it to the <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html"><strong>Target group</strong></a> which you created in previous step.</li><li>The DNS Name of the internal <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html"><strong>Application Load Balancer</strong></a> could look something like this <strong>internal-myproduct-production-orders-ms-elb-123456.ap-south-1.elb.amazonaws.com. </strong>Add this to your <strong>API Gateway</strong>’s <strong>NGINX</strong> configuration.</li></ol><p>Here we go. Now we have all our micro services load balanced. They will scale based on loads. Since we have replicated in multiple <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html"><strong>Availability zones</strong></a>. We have some high availability with respect to the region which we are using.</p><p>So when there is a traffic spike in the <strong>API Gateway</strong>, multiple <strong>API Gateways</strong> will get spawned, at the same time our <strong>API Gateways</strong> will pass the load to their respective micro services. Now the traffic shifts to our micro services and they too react to it by spawning more instances of themselves.</p><p>And we have a very simple High Available, Scalable Architecture. Thanks to <a href="https://aws.amazon.com/"><strong>AWS</strong></a>.</p><h4>3. Databases</h4><p>When I’m on AWS, I prefer <a href="https://aws.amazon.com/rds/"><strong>RDS</strong></a>, <a href="https://aws.amazon.com/rds/aurora/"><strong>Aurora</strong></a>, <a href="https://aws.amazon.com/dynamodb/"><strong>DynamoDB</strong></a> and their other cloud databases than hosting databases myself. AWS takes the best care of our databases. While creating their managed DB instances we have to specify the Multi AZ availability, backups, encryptions and other configs and we are good to go. Later we have to make our micro services point to the endpoints of this managed database instances.</p><h4>4. Horizontal and Vertical Scaling</h4><p>In this design horizontal scaling can be achieved be changing the max and min instances count in the <a href="https://docs.aws.amazon.com/autoscaling/latest/userguide/AutoScalingGroup.html"><strong>Auto Scaling group</strong></a> configuration and Vertical Scaling can be achieved by changing the instance type in the <a href="https://docs.aws.amazon.com/autoscaling/latest/userguide/LaunchConfiguration.html"><strong>Launch configuration</strong></a>.</p><h4><strong>References:</strong></h4><ul><li><a href="http://microservices.io/patterns/apigateway.html">Microservices Pattern: API gateway pattern</a></li><li><a href="http://highscalability.com/blog/2016/1/11/a-beginners-guide-to-scaling-to-11-million-users-on-amazons.html">A Beginner&#39;s Guide to Scaling to 11 Million+ Users on Amazon&#39;s AWS - High Scalability -</a></li><li><a href="https://aws.amazon.com/architecture/">AWS Architecture Center</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea1a2cba29c3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prettify your EC2 Hostnames]]></title>
            <link>https://medium.com/@pratheekhegde/prettify-your-ec2-hostnames-1d89f4a7c76d?source=rss-aba551e1a1f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d89f4a7c76d</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Pratheek Hegde]]></dc:creator>
            <pubDate>Sat, 04 Nov 2017 19:04:46 GMT</pubDate>
            <atom:updated>2018-02-13T17:10:45.793Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6GlLu0HL3kS4gdpawgC7xg.png" /><figcaption>AWS EC2</figcaption></figure><p>Every time we spin up an EC2 instance the hostname would be set to its internal ip. The <strong>SSH</strong> keys are generated and injected to the machine by the <a href="https://cloud-init.io/"><strong>cloud-init </strong></a>scripts on boot but I’m not sure if the hostnames are also set by <a href="https://cloud-init.io/"><strong>cloud-init</strong></a>. Anyways they look ugly for me. When I <strong>SSH</strong> into these boxes through <a href="https://en.wikipedia.org/wiki/Bastion_host"><strong>bastian</strong></a> boxes or when I restore my <strong>tmux</strong> sessions, I always lose track of the instance I’m in. Few time I did run wrong commands in the wrong instances.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*yT8Ms13IqWs6P9M92gEeew.png" /><figcaption>The ugly hostname.</figcaption></figure><p>So I wanted neat hostnames. I had seen in movies and many other blog posts. They have pretty urls like db1-production-newyork-dc-2.company.net, db2-test-oregon-dc-4.mycompany.net.<strong> </strong>These could be public DNS or local private DNS within the data centre. So I wanted to try something like this. If not the private DNS at least I will set the hostnames and make it look little pretty.</p><h4>DISCLAIMER:</h4><ol><li>Amateur here 👨🏻‍💻.</li><li>There could be better ways to do same thing what I’m trying to do, I’d love to learn them.</li></ol><blockquote>The hostname must be set in the <em>/etc/hostname</em> file and the <a href="https://en.wikipedia.org/wiki/Fully_qualified_domain_name"><strong>FQDN</strong></a> must be aliased in <em>/etc/hosts</em>. For more details you can refer the man page of hostname <a href="http://manpages.ubuntu.com/manpages/xenial/en/man1/hostname.1.html"><strong>here</strong></a><strong>.</strong></blockquote><h4>Setting Hostname with persistence on boot.</h4><ol><li>run sudo vim /etc/hostname and update the existing hostname to the name you want. (On boot this file is read and hostname is set, that’s how we get persistence on boot.) I’m gonna set mine to db2-staging-newyork-dc-4. Here db2 is the resource type (database number 2), staging is the environment, newyork is the region where our data centre is present and 4 (data centre number 4).</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*eQ4lXEYwTuY9aMseroiZog.png" /><figcaption>New hostname</figcaption></figure><p>2. update the /etc/hosts file with the <a href="https://en.wikipedia.org/wiki/Fully_qualified_domain_name"><strong>FQDN</strong></a>. Our <a href="https://en.wikipedia.org/wiki/Fully_qualified_domain_name"><strong>FQDN</strong></a><strong> </strong>will be db2-staging-newyork-dc-4.mycompany.net. Here we are aliasing localhost as we want even localhost to work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xNlpvEoNPy45GHabjLtpAg.png" /><figcaption>Fully Qualified Domain Name (FQDN)</figcaption></figure><p>3. run sudo hostname -f /etc/hostname.<strong> </strong>This will set the hostname of the machine from the hostname file.</p><p>4. Now you can exit the shell and login again to see your new hostname.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*-xFTAx3KSInBDt8HHgVwvA.png" /><figcaption>The pretty hostname</figcaption></figure><p>5. verify by rebooting the machine once and pinging your hostname.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EMqR44Y5YdviqxeXpD-9IQ.png" /><figcaption>Verifying the new hostname</figcaption></figure><p>And now you have a pretty hostname.</p><p>6. You could automate these steps by putting it in a shell script and running it while launching the instance. But, you might need another service to track these names and to generate unique names for each machine.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d89f4a7c76d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PDF generation with python using PhantomJS]]></title>
            <link>https://medium.com/talentspear/pdf-generation-with-python-using-phantomjs-fb8a6fc8ac8c?source=rss-aba551e1a1f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb8a6fc8ac8c</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[phantomjs]]></category>
            <category><![CDATA[pdf]]></category>
            <category><![CDATA[tech]]></category>
            <dc:creator><![CDATA[Pratheek Hegde]]></dc:creator>
            <pubDate>Fri, 30 Sep 2016 17:27:01 GMT</pubDate>
            <atom:updated>2016-11-12T18:06:45.344Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iHEcnxJ1hsx_wJeNUsvEvQ.jpeg" /></figure><p>Recently I bumped into a simple task of <strong>PDF</strong> generation for getting a snapshot of user profile. Basically, I had to spit all the fields of a user profile to a PDF. I<strong> </strong>had <strong>Python</strong> and <strong>AngularJS</strong> at my disposal. I threw few searches and figured out that <a href="http://pdfmake.org/"><strong>pdfmake</strong></a> was something good to start with for the front-end. I did not want to spend much time designing the template, <a href="http://pdfmake.org/"><strong>pdfmake</strong></a> requires everything to be described in a json file. <strong>No</strong>! I’m not gonna do this, screw you <a href="http://pdfmake.org/"><strong>pdfmake</strong></a> !</p><p>I recalled reading Screen Capture in <a href="http://phantomjs.org/">PhantomJS</a> website, but I thought it would complicate stuff as I’d be bringing in selenium. This is what google gave me in my quick search. Or maybe I was lazy to dig more. I wanted this task to be done in a simplest way possible. Alright, let me share what I did and what should you do to get the same.</p><blockquote>I Assume that you have the pre-requisites (Python) and all other setup ready.</blockquote><p>Here we go with the tutorial … ╮ (. ❛ ᴗ ❛.) ╭</p><h3>1. Get PhantomJS</h3><p>For <strong>Windows </strong>and <strong>Mac</strong>, <a href="http://phantomjs.org/download.html">Download</a> phantom from their website and extract (unzip) the content. The executable phantomjs is ready to use.</p><p>You can follow the instructions <a href="http://phantomjs.org/download.html">here</a>. It should not be hard to get it up and running.</p><pre>phantomjs</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*KmollcJP0O1u02MhN02Plw.png" /><figcaption>Your PhantomJS is ready.</figcaption></figure><h3>2. Get rasterize.js</h3><p>Get this file from the <a href="http://phantomjs.org/examples/">examples</a> section of the <strong>PhantomJs</strong> website or you can get a copy <a href="https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js">here</a> ( My link could break anytime). This file is what does the PDF magic.</p><h3>3. Cook your base html template</h3><p>The PDF is created by taking a screenshot of the rendered HTML file. So, we need a base HTML template file with placeholders, python replaces the placeholders with data and generates the HTML file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96b8b878d29aa512ee774bceff55bb71/href">https://medium.com/media/96b8b878d29aa512ee774bceff55bb71/href</a></iframe><blockquote>Dont freak out when you see <strong>$fullName</strong> in the <strong>base_template.html, </strong>will tell you what is that shortly.</blockquote><h3>4. Start coding</h3><p>You can start with this python file below and then modify it to suit what you are working on.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1391143e902cef86fa57162b7deeba7e/href">https://medium.com/media/1391143e902cef86fa57162b7deeba7e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac29be2a9c06824ba77aea8af915023c/href">https://medium.com/media/ac29be2a9c06824ba77aea8af915023c/href</a></iframe><p>In this python file I read the <strong>base_template.html</strong> and <strong>profile.json </strong>(This file has my data which needs to be splashed over the PDF) and replace all the placeholders present in the <strong>base_template.html </strong>with the data from <strong>profile.json. </strong>I <strong>use safe_substitute</strong> method to do this replacement job. Basically it replaces the placeholders (starting with <strong>$</strong>) in the template file which has same name as the keys in the json file. For eg: the key <strong>fullName </strong>in my <strong>profile.json </strong>will be replaced wherever there is <strong>$fullName</strong> in my <strong>base_template.html</strong>. In this way I could throw texts to my PDF.</p><p>This <strong>safe_substitute</strong> method returns me the content of base_template.html after doing it’s work, I am calling the returned content as <strong>ready_for_generation. </strong>Next I save this ready_for_generation to the disk as <strong>ready_for_generation.html</strong></p><p>Now comes the coolest part of all, generating the PDF. I now use the <strong>call</strong> method of <strong>python</strong> to invoke the <strong>phantomjs</strong> executable. I pass <strong>rasterize.js</strong>, <strong>ready_for_generation.html</strong> (File to be made as PDF)<strong> and your_PDF.pdf </strong>(The name of the PDF file to be created)<strong> </strong>as parameters<strong>. </strong>After the execution of this line (Line 37) a file <strong>your_PDF.pdf </strong>will be created. The last step would be is to delete the <strong>ready_for_generation.html </strong>file, as it is of no use for us anymore.</p><h3>5. Winding Up</h3><p>Now that we have the PDF in our hand what more can be done to make this task more awesome?</p><ul><li>Use <a href="http://jinja.pocoo.org/"><strong>Jinja</strong></a><strong> </strong>template engine for the generation of <strong>ready_for_generation.html </strong>file. Using <a href="http://jinja.pocoo.org/"><strong>Jinja</strong></a> You can generate dynamic html. (When you want to display a list in your base_template.html)</li><li>In <strong>rasterize.js</strong> file you might wanna play with <strong>page.viewportSize </strong>values <strong>width</strong> and <strong>height</strong> , else your PDF might look deformed. There are also other values you can play like <strong>page.zoomFactor</strong>, <strong>page.paperSize</strong> .etc</li></ul><pre>page.viewportSize = { width: <strong>600</strong>, height: <strong>600</strong> };</pre><h3>6. Conclusion</h3><p>I never thought I would be posting this, So I did not document the errors when I was working. Apologies for that. I hope this guide will help you add this feature to your app but with much less head–scratching, wailing and gnashing of teeth ! Please let me know if you have any questions and I’d be happy to help.</p><p>Find the Source Code <a href="https://github.com/pratheekhegde/python-phantomjs-pdf-tutorial">here</a>.</p><h3>Peace ✌</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb8a6fc8ac8c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/talentspear/pdf-generation-with-python-using-phantomjs-fb8a6fc8ac8c">PDF generation with python using PhantomJS</a> was originally published in <a href="https://medium.com/talentspear">Talentspear</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>