<?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 Nimit Jain on Medium]]></title>
        <description><![CDATA[Stories by Nimit Jain on Medium]]></description>
        <link>https://medium.com/@scorpion.nimit?source=rss-2caeffbb7cf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*iNRbwOKQTDl2I7ojABvxPg.png</url>
            <title>Stories by Nimit Jain on Medium</title>
            <link>https://medium.com/@scorpion.nimit?source=rss-2caeffbb7cf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 16:07:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@scorpion.nimit/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[How to create a Firebase Cloud Function with static outbound IP]]></title>
            <link>https://medium.com/@scorpion.nimit/how-to-create-a-firebase-cloud-function-with-static-outbound-ip-8086bbbdbbfe?source=rss-2caeffbb7cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/8086bbbdbbfe</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[firebase-cloud-functions]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[vpc]]></category>
            <category><![CDATA[static-ip]]></category>
            <dc:creator><![CDATA[Nimit Jain]]></dc:creator>
            <pubDate>Mon, 28 Jun 2021 21:11:41 GMT</pubDate>
            <atom:updated>2021-06-28T21:19:26.618Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>In just 6 simple steps!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JY_tIxBVH36aJqTlKEOdpA.jpeg" /></figure><p>Most of you have already created server-less functions which generates dynamic IP address(es). Mostly all this happens at the backend and you don’t have to worry about it. Trouble begins when you need to have a static outbound IP address of your function. It is generally required as a safety measure (for whitelisting) when integrating with external APIs (like PayTM payout APIs, Msg91 APIs etc). So here in this article, we will see how to create a <strong>firebase cloud function </strong>that has a static outbound IP.</p><p>By default, all the firebase cloud functions created have dynamic outbound IPs. To have a static outbound IP, you need to connect your cloud function to a <a href="https://cloud.google.com/vpc">VPC</a> Connector which is connected to a <a href="https://cloud.google.com/nat/docs/overview">Cloud NAT gateway</a> config using which the traffic originating from the VPC network will have a static IP.</p><p>In simple terms, you will connect your cloud function to a VPC Connector. This VPC will somehow be configured to tell the cloud function to pass the traffic using the static outbound IP from the VPC.</p><p>Okay, so now that we know what to do, let’s first set up our VPC and then will link the same to our function. (I am assuming you already have <a href="https://cloud.google.com/sdk/gcloud">gcloud cli</a> setup and have a running firebase account)</p><h3><strong>Step 1 : Find the name of your VPC network</strong></h3><p>Run this command in terminal to check the networks list</p><pre>gcloud compute networks list</pre><p>You should see an output like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*ME27oYPiUlNPelWph15BaA.png" /><figcaption>Output in terminal</figcaption></figure><p>Here you can check the name of your VPC network. Most of the times, it will be default.</p><h3>Step 2 : Create a new Cloud Router</h3><p>Run the following code to create a new router</p><pre>gcloud compute routers create ROUTER_NAME \<br>  --network=NETWORK_NAME \<br>  --region=REGION</pre><p>Here, replace :</p><ul><li>ROUTER_NAME with the name of the new Cloud Router.</li><li>NETWORK_NAME with the name of the VPC network found in<strong> Step 1</strong></li><li>REGION with the region name. It is better to use the same region where your functions will be/are hosted.</li></ul><h3>Step 3 : Reserve a static IP address</h3><p>Run the following command in terminal</p><pre>gcloud compute addresses create ORIGIN_IP_NAME --region=REGION</pre><p>In the command above, replace:</p><ul><li>ORIGIN_IP_NAME with the name you want to assign to the IP address resource.</li><li>REGION with the region that will run the Cloud NAT router. Ideally the same region as your Cloud Functions to minimize latency and network costs.</li></ul><h3>4. Create a Cloud NAT gateway configuration on this router:</h3><p>Run the following command</p><pre>gcloud compute routers nats create NAT_NAME \<br>  --router=ROUTER_NAME \<br>  --region=REGION \<br>  --nat-all-subnet-ip-ranges \<br>  --nat-external-ip-pool=ORIGIN_IP_NAME</pre><p>In the command above, replace:</p><ul><li>NAT_NAME with a name for the Cloud NAT gateway resource you want to create.</li><li>ROUTER_NAME with the name of your Cloud Router.</li><li>REGION with the region in which you want to create a NAT gateway.</li><li>ORIGIN_IP_NAME with the name of the reserved IP address resource you created in the previous step.</li></ul><p>This is needed to route the traffic originating from the VPC network using the static IP address you created in <strong>Step 3</strong></p><h3>Step 5 : Create the VPC connector</h3><p>Run the following commands to create the connector</p><pre>gcloud services enable vpcaccess.googleapis.com<br><br>gcloud compute networks vpc-access connectors create CONNECTOR_NAME \<br>    --network VPC_NETWORK \<br>    --region REGION \<br>    --range IP_RANGE</pre><p>Replace the following:</p><ul><li>CONNECTOR_NAME: a name for your connector</li><li>VPC_NETWORK: the VPC network name found in <strong>Step 1</strong></li><li>REGION: a region for your connector; this must match the region of your serverless service</li><li>IP_RANGE: This IP range must not overlap with any existing IP address reservations in your VPC network. For example, 10.8.0.0/28 works in most new projects. <em>The value supplied is the network in CIDR notation (10.8.0.0/28).</em></li></ul><p>To verify wether your connector is ready or not, run the following command</p><pre>gcloud compute networks vpc-access connectors describe<br>CONNECTOR_NAME \<br>--region REGION</pre><p>Replace the following:</p><ul><li>CONNECTOR_NAME: the name of your connector; this is the name that you specified in the previous step</li><li>REGION: the region of your connector; this is the region that you specified in the previous step</li></ul><p>The output should contain the line state: READY.</p><h3>Step 6 : Link the function and deploy 🚀</h3><p>Now we are all set. All we need to do now is create the required function and link it to the VPC Connector. I have attached a dummy function code below which prints the outbound IP address. Have a look.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RsjNA30c-O5JZAk97mbpMQ.jpeg" /><figcaption>Code Image created on codeimg.io</figcaption></figure><p>While running this function, you will see the outbound IP Address will always be a static IP Address which was set in <strong>Step 3. </strong>Keep in mind to replace the CONNECTOR_NAME in the code with your actual VPC Connector name set in <strong>Step 5</strong> above.</p><p>Voila! All done now. Now you can have the ease of having a static IP within the comfort of firebase cloud functions. Enjoy!</p><h3><strong>🐞 Debugging</strong></h3><ul><li>To cross check the IP Address created in step 3, run the following command. This will show a list of all IP addresses and with their names against it.</li></ul><pre>gcloud compute addresses list</pre><ul><li>If you have already created a function and are now re-deploying the function after linking it to the VPC Connector, sometimes it would not reflect properly. It is majorly because the functions trigger line does not get updated when a function is re-deployed. <br>Either create a new function, or delete the existing one and then re-deploy again. To check wether your function is linked to your vpc connector or not, hop on to console.cloud.google.com. Navigate to your deployed cloud function and open it’s details tab. You should see your VPC Connector name in the network settings section. <br>Or open this link after replacing it with your project and function name : <a href="https://console.cloud.google.com/functions/details/us-central1/sendCustomSMS?project=eazypg-3dcb6&amp;tab=details">https://console.cloud.google.com/functions/details/&lt;REGION&gt;/&lt;FUNCTION-NAME&gt;?project=&lt;PROJECT-NAME&gt;&amp;tab=details</a><br>Replace : <br>- REGION with your project’s region<br>- FUNCTION-NAME with your function name<br>- PROJECT-NAME with your firebase project name</li></ul><p>You should see something like this 👇</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/1*i3lA03LcqfIcaaJUHOouDQ.png" /></figure><p>Some useful resources for further clarifications :</p><p><a href="https://cloud.google.com/run/docs/configuring/static-outbound-ip">https://cloud.google.com/run/docs/configuring/static-outbound-ip</a></p><p><a href="https://cloud.google.com/functions/docs/networking/network-settings">Configuring network settings | Cloud Functions Documentation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8086bbbdbbfe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[5-day or 6-day work policy?]]></title>
            <link>https://medium.com/eazypg/5-day-or-6-day-work-policy-3108d0f71c5?source=rss-2caeffbb7cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/3108d0f71c5</guid>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[work-culture-in-start-up]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[work]]></category>
            <category><![CDATA[work-life-balance]]></category>
            <dc:creator><![CDATA[Nimit Jain]]></dc:creator>
            <pubDate>Fri, 11 Jun 2021 19:40:54 GMT</pubDate>
            <atom:updated>2023-03-30T11:20:41.553Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>What should be your company’s policy or where should you work!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5rXejhcKLorK7JiXnQH-tg.jpeg" /><figcaption>Image by <a href="https://unsplash.com/@homajob">Scott Graham</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><p>Before we dig in, a bit about myself. I am <a href="http://twitter.com/techienimi/">Nimit</a> and I handle all things tech at <a href="http://www.rentok.com/">RentOk</a>, an early stage startup. So all the views and learnings I share are biased for the early stage teams and my understanding. These are my personal views and not general statements. Now that I can blame the shortcomings to my bias, let’s dig in 😂</p><p>I have been building my startup actively from my 4th year in college since 2018. Since then we have released multiple products on Android, iOS, web and PWAs (almost every domain). I also had the luxury to work closely with inexperienced but super-charged freshers (think 12th drop outs to 1st year undergrads) to people who had 5+ years of working experience. I have also worked in a 7 day (working every waking hour) environment to a no questions asked leave policy environment. Phew!</p><h3>Does working day policy really matter?</h3><p>After thinking and reflecting on this for a long, and having discussed this with the good folks at <a href="https://firstcheque.vc/investments/">firstcheque portfolio</a> I realized that there is no template for this. It really depends on the <strong>stage of your company</strong> and your <strong>company’s culture</strong>. All that matters initially is to test your hypothesis, get your product to your users, iterate and scale. So the working day policy is essentially a function of the stage of your company (<em>and the average age of team members</em> ).</p><blockquote>Generally people at the age of 28+ have more responsibilities other than work than people in college.</blockquote><p>So then it only makes sense to think about this with respect to different stages of a company.</p><h3>Different stages of a startup</h3><ol><li><strong>Super Early stage (pre-release) 🧑‍💻</strong></li></ol><p>If your startup is super early (haven’t even released the product), you need to work as fast as you can, if not 7 days a week. You need to release your product, reach to your users and see what’s working and what’s not. Initially, you need to also do things that won’t really scale (not following the ideal way to build stuff) because as a startup you don’t really have much time and money to experiment. To do this, if you have to work 7 days a week, maybe you should.</p><h4>Wait what! What about mental health? 🧠</h4><p>Okay, I get it. 7 days a week. It’s not practical (or maybe it is). But this is definitely not sustainable. Yes. I’m with you.</p><p>Understand that in the early days of a startup, you won’t work because someone would force you to, or for other reasons. You would work because you will be driven to build this thing and at the end you would feel really happy, deep inside once it is released.</p><p>If you are working at a super early stage startup for any other reason and putting in impractical amount of time, do yourself a favor and leave. Because complications and work will only arise.</p><p><strong>2. Early stage — post seed funding 🌱</strong></p><p>Okay, now that the app is released, and you see there is some initial growth, some traction and the <strong>seed round funding</strong> is done. Reward your employees as generously as you can, give them a break and maybe go on a trip together.</p><p>I believe after having some initial traction and visibility of what is working and what to build, your product should <strong>ideally have a roadmap for 3-6 months ahead</strong>. Working days should still be 6 days at least because there will still be a lot of processes to make, lots of feedbacks coming in, lots of revisits to the roadmap up and down. <strong>Working 6 days will give you enough time for execution</strong>. Again, one thing that really helps here is to have teammates with whom working is really fun. So be very careful in building your initial team right after your funding. You don’t want people shouting at each other or blaming others if work is not done.</p><p>Your only goal at this stage is to reach PMF (product market fit) or maybe raise enough money (8–10 M). Raising enough money buys you extra time but if your market is already crowded, you would still want to execute as soon as possible!</p><p><strong>3. Post PMF — Growth stage</strong> 📈</p><p>Once you attain PMF you should <strong>change you working policy entirely</strong>. Now is the time when things have stabilized and you won’t go out of business if you’re 1–2 months late in your timeline. To succeed in a stabilized way, you need to focus on the welfare of your employees. Having a 5 day work policy here really helps and motivates your employees. It helps them to refresh and recharge every week. Also this will be the time when you’ll be ideally a 40+ member team and the average age of the team tends to go above 28. (people who also have other responsibilities)</p><h3>What do I think?</h3><p>I think once you attain PMF (or raise enough money so that you have enough time) work policy should not be a 4 or 5 or 6 day. You should ideally switch to a <strong>dynamic work-based working policy</strong>. Have your roadmap ready for next 6–12 months. Divide them into sprints and let your employees divide them into several weeks and execute. When people have enough visibility on what to do, they can finish a 6 day work in 4–5 days with enough focus so as to enjoy weekend. While working 6 days there are many breaks in between or being in meetings/office just because they have to. If you know what has to be done this week and the week ahead along with the master plan, your teammates would be able to manage time in a much better way. Getting two days per week off, or planning their holidays themselves also helps them recharge.</p><p>The bigger question here is that <strong>can you have a 6–12 month roadway planned</strong>. I have often seen and observed people who do not have clarity of what’s (exactly) coming next want their employees 6 days in office so that they can have on-the-spot meeting and plan out work. These days are long gone, and your people will start leaving if this keeps on happening.</p><h3>But what if they complete less work per week?</h3><p>First of all, if you have carefully built your team and everyone is aligned properly and motivated by the company’s master plan, <strong>why would someone want to kill time and work less?</strong> Maybe think about your team building skills eh!</p><p>Secondly, it’s still your company. If you think someone is underperforming, have a one-on-one and convey this properly. Discuss if they are facing any difficulty and how can they increase their productivity. If it still does not improve, you can always hire someone better.</p><p>I believe you cannot have a better output from people when they are working just because they <strong>have</strong> to work in the given timeframe and <strong>you are roaming around to see them</strong>. This just gives us a false sense of control, as leaders. They should be motivated and driven by the work they are doing. Otherwise, good luck building your company!</p><h3>Few traps to avoid</h3><ul><li>In the early days, I <strong>used to optimize so that each and every member in my team has enough work at hand and is not sitting idle</strong>. This is wrong on multiple levels. You don’t need a bunch of guys who are working 24x7. Innovation does not come that way. By doing so, the work was often not production ready and my teammates were exhausted by doing not so important work. Do not repeat the mistake I did 🙏<br>Better spend time planning the roadmap and discuss priority with the team. Ask the team to pickup themselves and if someone has no work, let them use this time to get refreshed.</li><li>Be in a lookout for people who just tend to showoff working long hours than the output of work. Eventually you want people who value the output and not how much hours they put in. Get rid of such people as soon as you can.</li><li><strong>Do not misunderstand availability for execution</strong>. Most think working 6 days will help them execute faster. It’s not true. Execution depends equally on the planning. If you have clearly laid out plan for next 6 months, you might be able to execute faster. Otherwise you’ll have people working 6 days a week but most work won’t be going to production (for one reason or other). But laying out the exact plan is rare. Often there are changes to the plan. Having someone available for 6 days a week help you plan these things. Let there be a balance between work and planning. Remember, what you want is consistency not speed. Increase the speed later when things are consistent.</li><li>There are some startups who need their engineers (or other teams) available all 7 days. Would suggest them to have two teams or set of people who rotate working 7 days a week and there should be added incentive for people working 7 days.</li></ul><p>Here’s a poll I did on twitter a while ago. Although the dataset is too small and it can be biased as well, but wth.</p><h3>Nimit Jn on Twitter: &quot;What do you think is the speed of engg teams when working 5 days/wk vs 6 days/wk. / Twitter&quot;</h3><p>What do you think is the speed of engg teams when working 5 days/wk vs 6 days/wk.</p><h3>Personal thoughts</h3><p>Culture might be the most complicated thing if I ever have to explain. It just has too many dimensions. But in short, here what I mean by culture is that as a team, if you respect your employees, value their working extra hours even if results are not achieved, treat them with respect and value their decisions. They would happily work extra hours. Just because you as a founder work 16 hours 7 days a week, do not outrightly expect them to do the same. Be grateful if anyone is working extra hours no matter what.</p><p>For someone joining an early stage team, do not join just because you want to earn big or have maximum freedom in working or do not like hierarchy etc. Those are some real BS reasons (and sound funny to me when someone says so) The one and only reason to join an early stage team is because you <strong>believe in the mission</strong> and you <strong>want to create an impact in the given domain</strong>.</p><p>Also I have intentionally missed out a few dimensions, one of which is where execution is slow in 5 day policy even when no one is killing time because the team cannot communicate properly. Working 6 days and coming to office helps in communication (atleast). This becomes important at growth stage startups, but I don’t think I have the experience to comment on it just yet.</p><p>On a closing note, here’s an article from <a href="https://twitter.com/naval">Naval Ravikant</a> which is a real gem 💎Give it a read!</p><p><a href="https://nav.al/work-hard">Work As Hard As You Can</a></p><p>If you liked it, press on that clap button for 5 seconds ;) If you do not agree to this, that’s totally fine. Send me your thoughts on <a href="http://twitter.com/techienimi">twitter</a>. Would love to chat ☕️</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3108d0f71c5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/eazypg/5-day-or-6-day-work-policy-3108d0f71c5">5-day or 6-day work policy?</a> was originally published in <a href="https://medium.com/eazypg">RentOk</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to connect to EC2 instance when keypair file is lost]]></title>
            <link>https://medium.com/@scorpion.nimit/how-to-connect-to-ec2-instance-when-keypair-file-is-lost-2c5c6b9035a5?source=rss-2caeffbb7cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c5c6b9035a5</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[lost]]></category>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[ssh]]></category>
            <category><![CDATA[keypair]]></category>
            <dc:creator><![CDATA[Nimit Jain]]></dc:creator>
            <pubDate>Fri, 31 Jul 2020 04:31:00 GMT</pubDate>
            <atom:updated>2020-07-31T04:31:00.998Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s face it guys, there are times when we forget our passwords or lose some secret files. There should be some failsafe to recover the file, or to create a new one. Similarly, when you create a new EC2 instance, a keypair file is linked to the instance. You can either create a new keypair file or link an existing one. Keypair file is like a password file for accessing the server which can be downloaded only once, while creating the instance, so it needs to be preserved carefully.</p><p>Now what happens when the keypair file is lost. Well. You can still access your server, you just need to replace the existing keypair file with a new keypair file linked to another instance.</p><p>Let’s see how it’s done.</p><p>Let’s assume, we have two instances by the name of Server-with-key and Server-without-key.</p><ol><li>Stop the Server-without-key instance.</li><li>Check the volume linked to server-without-key instance in the Volumes section inside AWS EC2 console. Select this volume, click on Actions and Detach this volume.</li><li>After this volume is detached, again click on Actions and click on Attach.</li><li>Attach this volume to Server-with-key instance. Now this volume will be linked to the instance whose keypair file is with us.</li><li>Now connect to Server-with-key instance via SSH using the existing keypair file.</li><li>After successful connection, write these command to copy the existing authorized_keys from server-with-key instance to the volume of server-without-key.</li></ol><pre># 1. To get root access<br>sudo su -</pre><pre># 2. Create the mount point<br>mkdir -p /var/recovery-disk</pre><pre># 3. Mount the recovery disk<br>mount -o nouuid /dev/xvdf2 /var/recovery-disk<br># Here we use &quot;-o nouuid&quot; to avoid uuid while naming folders.</pre><pre># 4. Copy over the authorized keys<br>cp /home/ec2-user/.ssh/authorized_keys  /var/recovery-disk/home/ec2-user/.ssh/authorized_keys</pre><pre># 5. Unmount the disk<br>umount /var/recovery-disk</pre><blockquote>There’s a catch in 4th step. If you have EC2 instance not on core linux but on some other something over linux (let’s say you have installed Wordpress client on EC2 instance from AWS Marketplace using Bitnami etc.), in that case, the location of authorized_keys might be different.</blockquote><p>In case of instances installed using <strong>Bitnami from AWS Marketplace</strong>, the location of authorized_keys might be</p><pre>/bitnami/ec2-user/.ssh/authorized_keys</pre><p>Okay, now that we have copied the authorized_keys from server-with-key instance to volume of server-without-key instance, let’s attach this volume to original server-without-key instance and try connecting with keypair file of server-with-key instance.</p><p>Let’s go.</p><ol><li>Stop the server-with-key instance.</li><li>Locate the volume with instance ID of server-without-key instance in Volume section, and detach it.</li><li>Attach this volume to server-without-key instance.</li><li>Start the server-without-key instance.</li></ol><p>All right, now it’s time to see some action. 🤞</p><p>Connect the server-without-key instance with SSH using keypair file of server-with-key instance.</p><pre>chmod 400 &lt;location-path-of-keypair-file&gt;<br>ssh -i &quot;&lt;name-of-keypair-file&gt;&quot; &lt;linux-user&gt;@&lt;public-DNS-of-instance&gt;</pre><p>Voila! It’s now connected. 🥳</p><p>In case of any typos or other issues, reach out to me at <a href="https://twitter.com/techienimi">https://twitter.com/techienimi</a>. Would be glad to help you out.</p><p>Cheers ☕</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c5c6b9035a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handle multiple firebase projects from within the same cloud function.]]></title>
            <link>https://medium.com/@scorpion.nimit/handle-multiple-firebase-projects-from-within-the-same-cloud-function-a9120eedcc45?source=rss-2caeffbb7cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/a9120eedcc45</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[cloud-functions]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Nimit Jain]]></dc:creator>
            <pubDate>Tue, 28 Jul 2020 15:43:59 GMT</pubDate>
            <atom:updated>2020-07-28T15:43:59.266Z</atom:updated>
            <content:encoded><![CDATA[<h3>Handle multiple firebase projects from same cloud function.</h3><p>Firebase cloud functions are really good and fast to be used as backend functions. But testing such functions while it is live becomes a challenge. Hence in these scenario one needs a different project where those functions can be tested thoroughly without affecting the live project. This is somewhat similar to having a different project/server for development and another project/server for production (live users).</p><p>By default, whenever any firebase function is initialized there are a set of environment variables that are initialized along with it. One of them contains the firebase project id for the project. We are going to use that ahead. To know more about default environment variables with firebase function, <a href="https://firebase.google.com/docs/functions/config-env#automatically_populated_environment_variables">click here</a>.</p><p>Let’s assume we have two firebase projects and their IDs are <strong>test-c40c7</strong> and <strong>live-be0f0</strong>. You can find the project-id written in the bottom of the project card in firebase or extract it from the firebase console link : https://console.firebase.google.com/project/<strong>&lt;projectId&gt;</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PDEGOqdTh-KOez7x-OsKwA.png" /><figcaption>Firebase console image showing two projects</figcaption></figure><p>Let’s initialize a firebase function by the name of <strong>multiProjectTest</strong>. I am assuming you have setup firebase CLI to initialize firebase functions. If not, <a href="https://firebase.google.com/docs/functions/get-started#set-up-node.js-and-the-firebase-cli">head over here</a>.</p><p>It is a good practice to keep our function inside the folder by the same name. So make a folder by the name of <strong>multiProjectTest </strong>and initialize firebase function.</p><pre>firebase init</pre><p>You may choose either of the two projects as we are going to handle both functions. For now, i am going ahead with test project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hNqK03RbJSt0G3_FaP6ypg.png" /><figcaption>Choosing test project while initializing firebase function</figcaption></figure><p>Now that the function is initialized, let’s code.</p><p>We are going to make a simple https function that logs and returns the existing firebase project ID.</p><p>Remember the default environment variable we talked about earlier, one of them is <strong>GCLOUD_PROJECT </strong>which gives the <strong>firebase project id</strong> in which that function instance is running.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hNX_97WElZvIjPqLrPtOVA.png" /><figcaption>Code to handle multi project initialization using environment variable</figcaption></figure><p>Now, the config values such as database URL, storage bucket URL, firebase function Endpoint can be initialized in their specific if else block mentioned above. You can also use <strong>FIREBASE_CONFIG </strong>environment variable to fetch default config variables of the project such as database URL, storage bucket and project ID. But i usually have more than one database in one project, so i do it manually instead of fetching from <strong>FIREBASE_CONFIG. </strong>You may proceed as you like.</p><p>Let’s proceed with the function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ghNhdbtPdReEEV9EetPyZg.png" /><figcaption>Function to return current firebase project</figcaption></figure><p>Now the function is complete. Let’s deploy it on both the projects. Run this code in CLI to deploy the function in specific project</p><pre>firebase deploy --project &lt;project-id&gt;</pre><p>So, to deploy the function in test project, run this command in the terminal from the folder where the function is initialized.</p><pre>firebase deploy --project test-c40c7</pre><p>Select No or Type N if the firebase CLI asks you to proceed with deletion of rest projects.</p><p>You should see a screen like this once the function is deployed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RcDG574WQJldePVFjM91lQ.png" /><figcaption>Function deployed from firebase CLI</figcaption></figure><p>Observe the function URL given by the CLI right before Deploy complete declaration. That is the endpoint of our HTTPS function.</p><p>Similarly let’s deploy the function in live project.</p><pre>firebase deploy --project live-be0f0</pre><p>After the function is deployed in both projects , you should have the endpoints like these for the <strong>multiProjectTest </strong>function.</p><p><strong>Test Project Endpoint</strong></p><pre>https://us-central1-test-c40c7.cloudfunctions.net/multiProjectTest</pre><p><strong>Live Project Endpoint</strong></p><pre>https://us-central1-live-be0f0.cloudfunctions.net/multiProjectTest</pre><p>You will have different endpoints linked to your own firebase project ID. General structure for the link is</p><pre>https://us-central1-&lt;project-id&gt;.cloudfunctions.net/&lt;function-name&gt;</pre><p>Now let’s make post call to these endpoints to check their response. The function linked to Test project should return that it is running in test project and similarly Live project should return it is running in live project.</p><p>To make a simple CURL Post call to our endpoint, write this code in your terminal.</p><pre>curl -X POST https://us-central1-test-c40c7.cloudfunctions.net/multiProjectTest</pre><p>Since we need not send any data in our POST call, we can directly call our endpoint without sending any data argument. To know more about curl calls, <a href="https://gist.github.com/subfuzion/08c5d85437d5d4f00e58">head over here</a>.</p><p>Let’s check the response from the function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zDhcvhxrpi54rqMMFZzEXw.png" /><figcaption>Function response from Test project function</figcaption></figure><p>Voila! It returns that the function is running in test-c40c7 project, as expected.</p><p>Let’s test for live project by making this curl call.</p><pre>curl -X POST https://us-central1-live-be0f0.cloudfunctions.net/multiProjectTest</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ehgMErn-K_1rghh3awuBxw.png" /><figcaption>Function response from Live Project</figcaption></figure><p>Perfect. It shows that this function is running in live project.</p><p>One can handle all the operations according to different projects in the same function. There is no need to create multiple functions having same business logic for different projects.</p><p>We at <a href="https://eazypg.in/">EazyPG</a>, use this mostly for keeping our development functions and production functions different. In the development functions, all the API keys initialized are test API keys, be it for payment gateways, messaging service, analytics etc., so as to keep the testing data separate and not linked to the services of live project. Only after the function has been completely tested in the development environment, it is deployed to the production environment, where all the Live API keys and other Live config are enabled itself. This gives freedom in testing the function without worrying about the production server and saves a lot of time writing the same redundant code every time a function is moved from development to production.</p><p>Hope you enjoyed it. Let me know, if you use multiple projects from same function for a different use case.</p><p>In case of any typos or other issues, reach out to me at <a href="https://twitter.com/techienimi">https://twitter.com/techienimi</a>. Would be glad to help you out.</p><p>Cheers ☕</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9120eedcc45" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>