<?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 Mirza Joldic on Medium]]></title>
        <description><![CDATA[Stories by Mirza Joldic on Medium]]></description>
        <link>https://medium.com/@purplecones?source=rss-3255da111cf9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6UvSWnBPp-m2IZUwh5Cj9w.jpeg</url>
            <title>Stories by Mirza Joldic on Medium</title>
            <link>https://medium.com/@purplecones?source=rss-3255da111cf9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 12:31:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@purplecones/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[Playing with CSS Grid Layout]]></title>
            <link>https://medium.com/@purplecones/playing-with-css-grid-layout-a75836098370?source=rss-3255da111cf9------2</link>
            <guid isPermaLink="false">https://medium.com/p/a75836098370</guid>
            <category><![CDATA[layout]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[grid-layout]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[grid]]></category>
            <dc:creator><![CDATA[Mirza Joldic]]></dc:creator>
            <pubDate>Sun, 26 Mar 2017 21:11:41 GMT</pubDate>
            <atom:updated>2017-03-28T13:47:25.835Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*8xM-wxiBuNnhYIG6vFem9Q.png" /></figure><p>I had the chance to see <a href="https://medium.com/u/d21301ee82b1">Jen Simmons</a> present at the <a href="https://medium.com/u/ade87cdd122e">General Assembly</a> in NYC. The workshop was titled <a href="https://generalassemb.ly/education/how-css-grid-is-reinventing-webpage-design"><strong>How CSS Grid is Reinventing Webpage Design</strong></a> but it was much more than that. Jen did an amazing job at setting the stage for CSS. She talked about how we got to where we are today and why CSS grid came into existence. It was also super awesome to have <a href="http://fantasai.inkedblade.net/">Elika Etemad</a> there who is one of the main authors of CSS Grid Layout.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*phV0oLsKV_qVjFVv5lY1vw.png" /><figcaption><a href="https://speakerdeck.com/jensimmons/designing-with-grid-uie">source</a></figcaption></figure><p>CSS Grid came out of the need to reduce the complexity of designing the layout of a web page. Sure, you can probably create any layout for your site using tables, floats and more recently Flexbox but those tools were not intended to be used that way. Flexbox was designed for efficient space distribution of elements across one direction of the page. People have embraced its efficiency and figured out ways to use it for their page layout because there was no better solution available. Well as of March 28, 2017 CSS Grid is fully supported by Firefox, Chrome, Safari (iOS as well), and Opera. It won’t be long until it is supported by every browser with the exception of Internet Explorer. More on that later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*pQM1GBUFjnZyZeOBHDZEsA.png" /><figcaption>Browser support as of March 28, 2017 | <a href="http://caniuse.com/#feat=css-grid">source</a></figcaption></figure><h3><strong>Intro to CSS Grid Layout</strong></h3><p>While learning more about CSS Grid, I thought it would be fun to create a tool to easily explore how different grid properties affect the overall grid layout. I named it appropriately as <a href="https://www.cssgridplayground.com/"><strong>CSS Grid Playground</strong></a><strong> 🙂.</strong></p><p>Brace yourself, the gifs are coming…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*EW174m7OIIpJwMHkrTW3Xg.gif" /></figure><p>CSS Grids behave similar to Flexbox in that there is a Parent-Child relationship like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*uGxt9x_1DWMVZ_nsuOY6_w.png" /></figure><p>So the first step we need to do is define the property display:grid; on our parent element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*AqB3eAY2xMYNN2Aa9fyZkg.gif" /></figure><p>Nothing happened. That’s okay because we’re not done yet. What did happen was that behind the scenes our parent div is now a <a href="https://www.w3.org/TR/css3-grid-layout/#grid-model"><em>Grid Container</em></a><em> </em>which means that all of its <strong>direct children</strong> are <a href="https://www.w3.org/TR/css3-grid-layout/#grid-items"><em>Grid Items</em></a><em>.</em></p><p>Next let’s define a 3 column grid with grid-template-columns: 1fr 1fr 1fr; .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/713/1*kguIr9J3trpOaHfIcoVXTg.gif" /></figure><p>Now we see something happening. It looks like our colorful block shrunk but it’s hard to tell exactly how it got divided. Adding a grid-gap: 5px; will make the divisions more prominent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*ZZ18YHsExSd2WHQA6UJcUg.gif" /></figure><p>Ok, so now we see that we have a collection of 7 div elements that are evenly divided into 3 columns. You can think of the 1fr as a fractional unit. By defining all 3 columns as 1fr , the browser will evenly distribute the space between all columns. Let’s see what result we get with 1fr 2fr 1fr .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*QJZ8nT3u5zwQQhFNddtSbA.gif" /></figure><p>Now, the second column is twice as long as the first and last column. Using fr units makes it really easy to define your grid dimensions.</p><p>Alright, so we’re getting closer to something usable. Let’s make the top row be as wide as possible. It can be used to hold a top navigational menu for example. To do this we want to add grid-column: 1 / 4; to the first grid item.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*R9PLfnqZz_SsX-fHUNhsHg.gif" /></figure><p>The 1 / 4 may look cryptic but lets break it down. The first number is the starting position and the second number is the ending position. The positions are the <a href="https://www.w3.org/TR/css3-grid-layout/#grid-line"><em>Grid Lines</em></a><em> </em>associated with your grid. In this example, our grid has four grid lines going from left to right. So what we’re saying here is that we want our grid item to start at grid line one and end at grid line four.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/477/1*wfW76MLzA00Rm6G_ZJ8MkA.png" /><figcaption><a href="https://www.w3.org/TR/css3-grid-layout/#grid-concepts">source</a></figcaption></figure><p>Next let’s make a left sidebar thats 300pxtall. We also want it to take up positions 2 / 3 vertically. We can do this with grid-row: 2 / 4 .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*ZHgbeKQUwABTdeVJVjDhdQ.gif" /></figure><p>Next let’s make the content area span 2 grid lines down and 1 grid line right.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*OSOvE92PWq68ODNhXPjyqg.gif" /></figure><p>Notice how we used the keyword span in this example. What we are saying with grid-column: 2 / span 1; is, start at grid line 2, and span only 1 grid line over. In the previous step we specifically indicated the start / end grid lines.</p><p>Next let’s make a footer that also spans horizontally across the entire grid. At the same time, let’s remove 2 grid items while we’re at it. We don’t need them anymore.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*-xGafylr4fPrZzimHSKBGw.gif" /></figure><p>Let’s take care of the last grid item by aligning it towards the bottom.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*7dzAyph_xKDny3yd6y5FYQ.gif" /></figure><p>To align the grid item to the bottom we used align-self: end; . The other options you have here are start , center , and stretch which was the default behavior if no specific height was given.</p><p>Ok, so here we have a relatively common layout and it was really easy to set up. Just for fun, let’s give the content body a height and width, and center it vertically and horizontally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*fqsRXTypVdjSuxTp5hjClw.gif" /></figure><p>Here we used justify-self: center; to horizontally center the grid item.</p><p>There is a lot more to CSS Grid than covered here. For more detailed examples, I highly encourage <a href="https://medium.com/u/d21301ee82b1">Jen Simmons</a> article on <a href="http://jensimmons.com/post/feb-27-2017/learn-css-grid"><strong>Learning CSS Grid</strong></a>. There she outlines a wide ranges of resources on learning more about CSS Grid Layout.</p><p><strong>Internet Explorer Support</strong></p><p>Believe it or not, the <a href="https://www.w3.org/TR/2011/WD-css3-grid-layout-20110407/">original spec</a> for CSS Grids was implemented as far back as IE 10. Many new features have been added to the spec since then however Microsoft has no plans to update the Grid implementation in IE 10 and IE 11. What this means is that if majority of your users are on these browsers, ie. enterprise intranet, then you should carefully consider which CSS Grid features you include in your design. For guidelines on creating Grid Layouts in IE 10 and IE 11, check these <a href="https://msdn.microsoft.com/en-us/library/hh673533(v=vs.85).aspx">Microsoft docs out</a>.</p><p>Lastly, Edge is one of the browsers that still does not support the latest CSS Grids spec so I encourage you to <a href="https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/6514853-update-css-grid">make it a priority</a> for Microsoft.</p><p>If you found this useful, recommend it by clicking the 💚 so that others may find it as well.</p><p>Thanks ✌️</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a75836098370" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[‘meteor deploy’ is back!]]></title>
            <link>https://medium.com/@purplecones/meteor-deploy-is-back-475377a24ed?source=rss-3255da111cf9------2</link>
            <guid isPermaLink="false">https://medium.com/p/475377a24ed</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[now]]></category>
            <category><![CDATA[meteor]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[deployment]]></category>
            <dc:creator><![CDATA[Mirza Joldic]]></dc:creator>
            <pubDate>Thu, 26 Jan 2017 18:26:26 GMT</pubDate>
            <atom:updated>2017-01-26T18:48:45.783Z</atom:updated>
            <content:encoded><![CDATA[<p>Once upon a time, there was this magical thing called meteor deploy. It was real simple and allowed you to share your Meteor app with the world. Those of you who remember when MDG <a href="https://forums.meteor.com/t/meteor-com-free-hosting-ends-march-25-2016/19308">took away the free hosting solution</a> can probably relate and say that things have not been the same in the Meteor community since. Many demo apps went offline never to be seen again. The free hosting solution that came with Meteor was one of the best things that attracted me to Meteor. It allowed me to quickly build, show my demo to a client, get feedback, and repeat the loop until the product was ready to graduate to the next level. It was also very friendly towards beginners and abstracted away many deployment complexities.</p><p><a href="https://medium.com/@justinkrup">Justin</a> and I are hoping to bring back the one-command deployment days with <a href="https://github.com/mazlix/meteor-now">meteor-now</a> and we hope everyone starts using it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*Z0-cWHR_op_9T8B1HgU6tg.gif" /></figure><p>In my <a href="https://medium.com/@purplecones/deploying-a-meteor-app-for-free-using-zeit-now-c183329057c9">introductory post</a>, I introduced meteor-now, the idea behind it, and a little about how it works. One crucial thing that was missing, that we were working hard on, is a true meteor deploy replacement where you don’t even have to provision your own MongoDB. That was our goal after the initial release of meteor-now and I think we were <a href="https://github.com/mazlix/meteor-now/pull/21">successful in implementing that in our latest release</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*b668SWg_hleJtKYwlPxzKw.png" /><figcaption>meteor-now deployment with <strong>external MongoDB</strong></figcaption></figure><p>After some crazy ideas that now seem completely unfeasible, we decided to try and bundle MongoDB in the same Docker image that the Meteor app is running on. Justin was able to put together a Dockerfile that successfully bundles Meteor as well as MongoDB together. When the Docker container is ready, MongoDB as well as the Meteor app are started together using <a href="http://supervisord.org/">supervisord</a>. From Meteor’s perspective, it is connecting to a local MongoDB instance just like when you run meteor on your development machine. This works out perfectly since every free deployment will be self contained and there is no crazy MongoDB scaling to worry about. This is mainly why MDG pulled the plug on meteor deploy. It was costing them a lot of money to maintain it. meteor-now offers a very similar experience for free thanks to <a href="https://zeit.co/now#pricing">ZEIT’s Open Source Software plan</a>. To deploy Meteor with an included MongoDB instance, just run meteor-now in your project folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*K1eQJx6d3UDQJSv3JHMbdw.png" /><figcaption>meteor-now deployment with <strong>included MongoDB</strong></figcaption></figure><p>The only caveat to this is that if your Meteor app starts to gain traction, the now service will automatically scale your deployment by creating new containers. Well if MongoDB is part of the container, things are going to get weird since now you’re going to have multiple databases. Which container are you connected to?</p><p>I have done some a small tests using <a href="https://loadimpact.com/">Load Impact</a> and did not observe any weird data anomalies but you should keep that in mind. Either way, I think this is a small trade off. If you see your app getting popular, it’s worth looking into getting a dedicated MongoDB instance from <a href="https://mlab.com/">mLab</a> or <a href="https://www.compose.com/">Compose</a> and you won’t run into these issues.</p><p>I hope this article gave you a good idea of how meteor-now is able to bring back the meteor deploy goodness. Please feel free to reach out to me or Justin, or just post an issue at the <a href="https://github.com/mazlix">meteor-now repo</a>.</p><p>Cheers</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=475377a24ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Super Simple and Free Meteor Deployments using ZEIT ▲now]]></title>
            <link>https://medium.com/@purplecones/deploying-a-meteor-app-for-free-using-zeit-now-c183329057c9?source=rss-3255da111cf9------2</link>
            <guid isPermaLink="false">https://medium.com/p/c183329057c9</guid>
            <category><![CDATA[meteor]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[now]]></category>
            <dc:creator><![CDATA[Mirza Joldic]]></dc:creator>
            <pubDate>Tue, 17 Jan 2017 15:25:42 GMT</pubDate>
            <atom:updated>2017-01-24T03:37:11.899Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4KZR-o9F8juCKa0nEMdydw.png" /></figure><p>Ever since the Meteor Development Group dropped their free hosting support, things haven’t been the same anymore in the Meteor community. The days when you can simply write meteor deploy and magically see your app get deployed and available to the world for free were gone. Until now 🤘.</p><p>A company called <a href="https://zeit.co/">ZEIT</a> has developed a <a href="https://zeit.co/now">hosting and deployment service</a> that allows developers to deploy any static site or any app (Node, PHP, <a href="https://zeit.co/blog/cobol-now">COBOL</a>, etc.) for free. Simply writing the command nowin your project folder and the tool will deploy the app and give you a unique URL for the deployment. They call it immutable deployments. Genius!</p><p>I’ve been using nowfor personal React/Node projects for a few months now but never for Meteor app. Well, during one of our weekly <a href="https://www.meetup.com/Meteor-NY/">Meteor Office Hours</a> in NYC, <a href="https://github.com/mazlix">Justin</a> and <a href="https://github.com/purplecones">myself</a> were answering questions on the options for deploying a Meteor app. I was always talking about nowand Justin was always tinkering with Docker. After some trial and error, we got a working Meteor app deployed using now. This gave us the idea for writing a small wrapper tool on top of the now CLI that specifically deploys Meteor apps. We’re calling it meteor-now.</p><p>In this article, we’re going to deploy a standard Meteor app from scratch.</p><p>To get started, download and install ZEIT’s now tool along with our meteor-now package. You will be using now CLI tool to manage your Meteor deployments. We recommend it!</p><pre>$ npm install -g now meteor-now</pre><p>In your terminal, type:</p><pre>$ now --login<br>&gt; Enter your email: &lt;your email&gt;<br>&gt; Please follow the link sent to &lt;your email&gt; to log in.<br>&gt; Verify that the provided security code in the email matches Pragmatic Manta Ray.<br><br>✔ Confirmed email address!<br><br>&gt; Logged in successfully. Token saved in ~/.now.json</pre><p>Enter your email address and click on the link in the email you receive. That’s it for getting set up. This is a one time step if you have never used now before.</p><p>After that, let’s simply create a new meteor app and install the deps:</p><pre>$ meteor create meteor-test<br>$ cd meteor-test<br>$ npm install</pre><p>Back in the old meteor deploy days, MDG was kind enough to provide a free MongoDB instance for your Meteor app and automatically link your app to it. Unfortunately, we have to replicate this step as well. Lucky for us, there are some free hosting solutions out there that we can use including <a href="https://mlab.com/plans/">mLabs sandbox free tier</a>. This is perfect for demos and MVP’s.</p><p>We’re not going to go into how to set up MongoDB, but you will need to get the connection string for your MongoDB instance before you can deploy. Otherwise, your Meteor app will not work.</p><p>In order to deploy, Meteor usually needs two environment variables, MONGO_URL and ROOT_URL. However, in our case since its the barebones Meteor app, there is no backend so just ROOT_URL is fine. To have those variables be available to your Meteor app, pass them as arguments with the -e flag to meteor-now. This follows the exact format as described <a href="https://zeit.co/blog/environment-variables-secrets">here</a>.</p><pre>$ meteor-now -e ROOT_URL=http://meteor-test.com</pre><p>What happens under the hood is that the Meteor app will be bundled using meteor build. Then we prepare a Dockerfile that has the appropriate instructions and steps to run our Meteor app. From there, now takes over by uploading, building, and deploying the docker container for our app.</p><p>After a minute or so, your Meteor app should be deployed and ready. Check the console output for a unique link to your app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*luUSP3bQBIiLv5xTIesCLg.png" /></figure><p>Every time you deploy, now will generate a unique URL for your deployment. And as a bonus, every deployment comes with full SSL and HTTP/2 support!</p><p><a href="https://meteor-test-msrbsvslpz.now.sh/">https://meteor-test-msrbsvslpz.now.sh/</a></p><p>You can use now alias to change the URL to something more memorable. Check out this <a href="https://zeit.co/blog/now-alias">blog post</a> on how to do that.</p><p>That’s it! This is a very early project. From idea to product it took us a weekend. There is a lot more to do and improve. We would love <a href="https://github.com/mazlix/meteor-now">contributions</a> if you are interesting in helping out.</p><p>Hope you enjoy using it!</p><p><em>Edit: Simplified and removed unnecessary steps</em>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c183329057c9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Serverless Environment Variables]]></title>
            <link>https://medium.com/@purplecones/serverless-environment-variables-4ec818f67388?source=rss-3255da111cf9------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ec818f67388</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[serverless]]></category>
            <dc:creator><![CDATA[Mirza Joldic]]></dc:creator>
            <pubDate>Fri, 30 Dec 2016 08:16:24 GMT</pubDate>
            <atom:updated>2016-12-30T19:51:56.040Z</atom:updated>
            <content:encoded><![CDATA[<p>Environment variables are now fully supported in AWS Lambda and the <a href="https://serverless.com/">Serverless Framework</a>. We’re going to see how to use them in our projects.</p><p>First up, make sure you are running the latest serverless CLI package. At the time of this writing, the lastest is v1.4.0.</p><pre>$ serverless -v<br>1.4.0</pre><p>Let’s create a folder for our project.</p><pre>$ mkdir serverless-environment-variables<br>$ cd serverless-environment-variables</pre><p>At the core of the Serverless Framework is the <em>serverless.yml</em> file which outlines your entire service. In our case, we will have a small handler for demo purposes:</p><pre># serverless.yml<br>service: serverless-environment-variables</pre><pre>provider:<br>  name: aws<br>  runtime: nodejs4.3</pre><pre>functions:<br>  hello:<br>    handler: handler.hello</pre><p>Here we see the name of our service, which provider and runtime we want to use. Currently, only AWS is supported but I believe Google, Azure, and IBM support is in the works.</p><p>We only have one function for this demo and we call it <em>hello</em>. The <em>handler</em> property simply indicates where the code for that function is. In this case it is in a file called <em>handler.js</em> which exports a function called <em>hello</em>. Let’s write <em>handler.js</em> now.</p><pre>// handler.js<br>&#39;use strict&#39;;</pre><pre>module.exports.hello = (event, context, callback) =&gt; {<br>  const response = {<br>    foo: &#39;bar&#39;,<br>  };</pre><pre>  callback(null, response);<br>};</pre><p>Here, all we are doing is returning a response with the property <em>foo</em> and value <em>bar</em>.</p><p>This should be all you need to run your service.</p><pre>$ serverless invoke local -f hello</pre><p>should yield</p><pre>{<br>    &quot;foo&quot;: &quot;bar&quot;<br>}</pre><p>Good. Now we have a working Serverless service. Let’s add those environment variables in there. Open the <em>serverless.yml</em> file and add the bolded line.</p><pre># serverless.yml<br>service: serverless-environment-variables</pre><pre>provider:<br>  name: aws<br>  runtime: nodejs4.3<br><strong>  environment:<br>    MY_VAR: abc</strong></pre><pre>functions:<br>  hello:<br>    handler: handler.hello</pre><p>In your <em>handler.js</em>, add the new environment variables to the response.</p><pre>// handler.js<br>&#39;use strict&#39;;</pre><pre>module.exports.hello = (event, context, callback) =&gt; {<br>  const response = {<br>    foo: &#39;bar&#39;,<br>    <strong>MY_VAR: process.env.MY_VAR,</strong><br>  };</pre><pre>  callback(null, response);<br>};</pre><p>Running the function again yields:</p><pre>$ serverless invoke local -f hello<br>{<br>    &quot;foo&quot;: &quot;bar&quot;,<br>    &quot;MY_VAR&quot;: &quot;abc&quot;<br>}</pre><p>Nice!</p><p>Adding environment variables in the <em>serverless.yml</em> file is okay for non-sensitive information. Remember, you are commiting the <em>serverless.yml</em> file in your repo so no sensitive information should be present there. Next, we’ll explore another way for us to add sensitive information such as db connections for example.</p><p>Let’s create a new file called <em>serverless.env.yml. </em>This file will hold our sensitive variables.</p><pre># serverless.env.yml<br>dev:<br>  SECRET_VAR: &#39;secret in dev&#39;</pre><p>Here we are creating a top level property <em>dev</em> and a variable within it. Now we need a way to reference this file in our main <em>serverless.yml</em> file.</p><pre># serverless.yml<br>service: serverless-environment-variables</pre><pre>provider:<br>  name: aws<br>  runtime: nodejs4.3<br>  environment:<br>    MY_VAR: abc<br><strong>    SECRET_VAR: ${file(./serverless.env.yml):dev.SECRET_VAR</strong>}</pre><pre>functions:<br>  hello:<br>    handler: handler.hello</pre><p>What happens here is that our secret variable will be a reference to the <em>serverless.env.yml</em> file which will not be commited to the repo. As a result, it may contain sensitive data like the connection string to our database.</p><p>Don’t forget to add the new variable to your <em>handler.js</em> file.</p><pre>// handler.js<br>&#39;use strict&#39;;</pre><pre>module.exports.hello = (event, context, callback) =&gt; {<br>  const response = {<br>    foo: &#39;bar&#39;,<br>    MY_VAR: process.env.MY_VAR,<br>    <strong>SECRET_VAR: process.env.SECRET_VAR,</strong><br>  };</pre><pre>  callback(null, response);<br>};</pre><p>Running the function again should produce:</p><pre>$ sls invoke local -f hello<br>{<br>    &quot;foo&quot;: &quot;bar&quot;,<br>    &quot;MY_VAR&quot;: &quot;abc&quot;,<br>    &quot;SECRET_VAR&quot;: &quot;secret in dev&quot;<br>}</pre><p>And there’s the secret variable available inside your function. Obviously <strong>you should not</strong> return this in your response in a real scenario.</p><p>Lastly, you can have environment variables for each deployment stage such as <strong>dev</strong> and <strong>prd</strong>. Let’s add a <strong>prd</strong> property to the <em>serverless.env.yml</em> file.</p><pre># serverless.env.yml<br>dev:<br>  SECRET_VAR: &#39;secret in dev&#39;</pre><pre><strong>prd:<br>  SECRET_VAR: &#39;secret in prd&#39;</strong></pre><p>Now we need to tell Serverless to dynamically pick the correct variable based on the stage we set when we run or deploy the function. We need to make one small change in the <em>serverless.yml</em> file to accomplish this.</p><pre># serverless.yml<br>service: serverless-environment-variables</pre><pre>provider:<br>  name: aws<br>  runtime: nodejs4.3<br>  environment:<br>    MY_VAR: abc<br><strong>    </strong>SECRET_VAR: ${file(./serverless.env.yml):<strong>${opt:stage}</strong>.SECRET_VAR}</pre><pre>functions:<br>  hello:<br>    handler: handler.hello<br></pre><p>We replaced dev with ${opt:stage} which will get interpreted and replaced with the stage name we choose. Now when we run with the -s flag, we can specify the stage and get the correct variable for that stage.</p><pre>$ sls invoke local -f hello <strong>-s dev</strong><br>{<br>    &quot;foo&quot;: &quot;bar&quot;,<br>    &quot;MY_VAR&quot;: &quot;abc&quot;,<br>    &quot;SECRET_VAR&quot;: &quot;secret in dev&quot;<br>}</pre><pre>$ sls invoke local -f hello <strong>-s prd</strong><br>{<br>    &quot;foo&quot;: &quot;bar&quot;,<br>    &quot;MY_VAR&quot;: &quot;abc&quot;,<br>    &quot;SECRET_VAR&quot;: &quot;secret in prd&quot;<br>}</pre><p>I hope this was helpful.</p><p>You can find all the code mentioned here <a href="https://github.com/purplecones/serverless-environment-variables">https://github.com/purplecones/serverless-environment-variables</a> for reference.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ec818f67388" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>