<?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 Philipp on Medium]]></title>
        <description><![CDATA[Stories by Philipp on Medium]]></description>
        <link>https://medium.com/@pentacent?source=rss-524783b33278------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*liF5ajtAG7OQErv1Px6eVQ.jpeg</url>
            <title>Stories by Philipp on Medium</title>
            <link>https://medium.com/@pentacent?source=rss-524783b33278------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 03:21:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pentacent/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[Failing Big with Elixir and LiveView]]></title>
            <link>https://pentacent.medium.com/failing-big-with-elixir-and-liveview-ce32f6574986?source=rss-524783b33278------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce32f6574986</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[phoenix]]></category>
            <category><![CDATA[failure]]></category>
            <dc:creator><![CDATA[Philipp]]></dc:creator>
            <pubDate>Tue, 01 Jun 2021 12:02:21 GMT</pubDate>
            <atom:updated>2021-06-01T12:02:21.670Z</atom:updated>
            <content:encoded><![CDATA[<h3>A post-mortem of why a political party in Germany had to delay their convention by an hour.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JH1O4_cLUADXuCSZwnU8sA.jpeg" /></figure><p>Phoenix LiveView is, without a doubt, one of the greatest web development innovations in recent years. In this article, I will explore how I created one of the world’s first production deployments with LiveView — and how I caused a lot of trouble while trying to improve it.</p><p>If you’re already familiar with LiveView, feel free to skip to the following two paragraphs. For everyone else, here’s here’s a quick explanation of what LiveView is:</p><p>LiveView is part of Elixir’s <a href="https://www.phoenixframework.org/">Phoenix web framework</a> and lets you build interactive applications without needing to write your own front-end JavaScript. It does that by keeping the application state on the server. When the application state changes, it automatically pushes an update to the user’s browser via Websockets. At the same time, it can also send user events (such as button clicks or form submits) back to the server. And all that, automagically, without the need to worry about front-end JavaScript.</p><p>For me, LiveView has meant unparalleled speed when developing web applications. What’s more, I find that writing all code in Elixir really reduces the mental load that usually comes from juggling a backend language with JavaScript for the frontend. And so it’s no surprise that the Ruby and PHP communities have come up with their own spins of the idea, <a href="https://docs.stimulusreflex.com/">StimulusReflex</a> and <a href="https://laravel-livewire.com/">Livewire</a>.</p><h3>It All Started In Prague — And Bulgaria</h3><p>Back in summer 2019, LiveView was still in its early stages. Chris McCord had <a href="https://dockyard.com/blog/2018/12/12/phoenix-liveview-interactive-real-time-apps-no-need-to-write-javascript">announced it</a> only a few months earlier and the first version had just been released in August. After having seen Chris’s <a href="https://www.youtube.com/watch?v=8xJzHq8ru0M">keynote on LiveView</a> at ElixirConf EU in Prague, Czechia, I was excited to give it a shot.</p><blockquote><em>The event ended up being a great success for what was likely one of the first LiveView production deployments in the world.</em></blockquote><p>Fortunately, an opportunity to put this new technology to work had just presented itself: <a href="https://www.volteuropa.org/">Volt Europa</a>, a transnational and pan-European progressive party, had just been officially incorporated and they were looking to organize their first big General Assembly. With its members spread out across the European continent, being online had been part of Volt’s DNA from the start. This is why it was quickly agreed that its first convention would be held as hybrid event. A number of members would gather in Sofia, Bulgaria — but an even larger number would follow the event from home, participating digitally.</p><h3>The Architecture of an Online Party Convention</h3><p>And so, after just two or three weeks of hacking, Volt’s online assembly tool, <em>Tusker</em>, was born. It allowed the convention chairs to share messages and votes with participants, and participants to take part in votes. The architecture of the tool was rather straightforward:</p><ol><li>All information about the state of the event is held in a long-running GenServer process.</li><li>Admins can publish votes and messages to the GenServer process.</li><li>Participants poll the GenServer for updates every second.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jv5XxQgCaNT01djM9FVwvg.png" /><figcaption>The original architecture of the online convention tool.</figcaption></figure><p><strong>The event in Sofia ended up being a great success for what was likely one of the first LiveView production deployments in the world!</strong></p><p>As the COVID-19 pandemic hit, other political parties all across the globe were forced to completely rethink their party conventions. Many parties had no previous experience with online assemblies and were left struggling to improvise new solutions. But Volt had <em>Tusker</em>, and local chapters all over Europe had already started using it for their own assemblies.</p><p><strong>Everything was great — except for one problem: The party kept growing, and thus the number of participants in these events kept growing, too.</strong></p><h3>Disaster Strikes</h3><p>With growing numbers of participants, it became necessary to optimize the application architecture. The frequent polling intervals of the first iteration ended up maxing out all eight CPU cores of a t3a.2xlarge AWS EC2 instance. This was not yet a major problem because the Erlang Virtual Machine with its own scheduler ensures that applications still run smoothly under these conditions. But it was clearly not future-proof anymore.</p><p>So I decided to switch from constant polling to a <a href="https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">Pub/Sub model</a>. This is also quite easy to do with Elixir and Phoenix: Phoenix comes with its own easy-to-use <a href="https://hexdocs.pm/phoenix_pubsub/Phoenix.PubSub.html">PubSub</a> module.</p><p>Instead of querying the event state at regular intervals, participants were now subscribed to the live_event/:id and live_event/:id/:participant_id topics on which they’d receive messages about the event as a whole and their own status in it. In addition, admin users would be subscribed to a live_event/:id/admin topic on which they’d receive additional information such as updates to the list of participants.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lmTCuaxez0BukKkizMUAZQ.png" /><figcaption>The updated application architecture with Phoenix.PubSub</figcaption></figure><p>The code for this in the LiveView controller looks something like this:</p><pre>def mount(_params, %{id: id}, socket) do<br>  if socket.connected? do<br>    Phoenix.PubSub.subscribe(MyPubSub, &quot;live_event/#{id}&quot;)<br>  end<br>  # ...<br>  {:ok, socket}<br>end</pre><pre>def handle_info({:votes_updated, votes}, socket) do<br>  {:noreply, assign(:votes, votes)<br>end</pre><p>My local tests went well, everything was blazingly fast. And CPU use seemed to be greatly reduced. Success! Next, I deployed the updated platform for organizing a couple of small-scale local events for Volt in the Netherlands. That, too, went without a hitch.</p><blockquote><em>It was like watching a trainwreck … until the inevitable out-of-memory crash.</em></blockquote><p>So all seemed well and I was confident to use the new version for a much bigger event: A three-day convention packed with votes and almost 3,000 eligible members in Germany. <strong>It was going to go smoothly with the server happily humming along at greatly reduced CPU usage levels. Or so I thought.</strong></p><p>The national convention of Volt in Germany was set to begin on Friday, May 28th, at 16:00 CEST. I eagerly watched the admin panel as hundreds of participants were logging into the platform.</p><p>But after just two minutes: 502 - Bad Gateway: The application had crashed. Panicked, I checked the logs - and could find no obvious reason for a crash. So I restarted the server, this time watching resource usage on htop. It was like watching a trainwreck: As soon as the server was up again, RAM usage immediately started climbing, and climbing … until the inevitable out-of-memory crash.</p><p>A third attempt also brought, unsurprisingly, no improvement.</p><p>Luckily, I was able to roll back to an older version, new invitation links were sent out, and with a mere one-hour delay, the three-day convention could eventually begin.</p><h3>Investigation Time</h3><p>Now it was time to investigate. What had gone wrong?</p><p>I fired up the Erlang <a href="https://erlang.org/doc/apps/observer/observer_ug.html">Observer</a> and took a look at the <a href="https://erlang.org/doc/apps/observer/crashdump_ug.html">Crashdump Viewer</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*Ups7ejmiFJUjJkiWLiiPew.png" /><figcaption>The problem: Processes.</figcaption></figure><p>It didn’t take me too long to figure out that moving from polling to Pub/Sub is what had caused the excessive memory consumption.</p><p>Whenever a user logged into the platform, the GenServer would broadcast a complete list of updated participants to all admin users:</p><pre>PubSub.broadcast(<br>  MyPubSub,<br>  &quot;live_event/#{id}/admin&quot;,<br>  {:participants_updated, participants}<br>)</pre><p>The LiveView controller process would then receive these messages, set the @participants assign and render an updated view:</p><pre>def handle_info({:participants_updated, participants}, socket) do<br>  { :noreply,<br>    assign(socket, :participants, participants) }<br>end</pre><p>My theory was this: <strong>With dozens of these updates happening per second as participants were joining the convention, messages were piling up in the inbox of the LiveView admin controller processes faster than they could be handled.</strong> With several admin users online, this would quickly lead to excessive memory consumption, filling up all 32 GB of my instance, eventually causing an out-of-memory crash.</p><p>To test this theory, I set up an event on my dev machine, opened an admin panel, and started broadcasting 5,000 updates of the participants list. <strong>My laptop crashed, the theory had been confirmed!</strong></p><h3>Fixing the issue</h3><p>Now it was time to fix the issue.</p><p>My first attempt at solving the issue was to try and handle incoming messages faster at the LiveView controller process level. In order to do so, I tried no longer directly setting the @participants assign each time the :participants_updated message hit. Instead I tried setting a different assign that wasn’t used in the HTML template and thus causing no delay for re-rendering. I then wanted the LiveView process to occasionally check if this other assign had been modified and, if so, also update @participants.</p><p>This appeared to work fine. I broadcast 5,000 updates and my laptop didn’t crash. However, when I then tried to use the admin panel in my browser, I noticed that the tab had frozen. As it turns out: Even if an assign is not used in the HTML template, LiveView will still send an (empty) update to the browser. With thousands of updates coming in at the same time, neither Firefox nor Chromium stood a chance.</p><p>And besides, this solution still came with the inherent risk of the LiveView controller process’s inbox filling up.</p><p>So ultimately, I settled on another solution: Throttling the broadcast frequency at the GenServer level. Instead of broadcasting the participant list as soon as a change occurred, I implemented a mechanism to do so at most once every second.</p><p>This can be achieved via a function that gets called in the GenServer process at a given interval:</p><pre>@process_participants_interval 1000</pre><pre>@impl true<br>def init(state) do<br>  schedule_process_participants()<br>  # ...<br>  {:ok, state}<br>end</pre><pre>@impl true<br>defp schedule_process_participants() do<br>  Process.send_after(<br>    self(),<br>    :process_participants,<br>    @process_participants_interval<br>  )<br>end</pre><pre>@impl true<br>def handle_info(:process_participants, state) do<br>  schedule_process_participants()<br>  {:noreply, process_participants(state)}<br>end</pre><pre>defp process_participants(state = %{participants_stale: true}) do<br>  Phoenix.PubSub.broadcast(<br>    MyPubSub,<br>    &quot;live_event/#{state.id}/admin&quot;,<br>    :participants_updated<br>  )</pre><pre>  %{state | participants_stale: false}<br>end</pre><pre>defp process_participants(state), do: state</pre><p>And since the participants list is no longer included in each broadcast, the LiveView controller process now fetches it when an update occurs:</p><pre>def handle_info(:participants_updated, socket) do<br>  {:noreply, fetch_participants(socket)}<br>end</pre><h3>Learnings and Best Practices</h3><p>The learnings from this somewhat disastrous experience can be summarized in three points:</p><ol><li>Avoid large payloads in Phoenix.PubSub if possible</li><li>Throttle PubSub events at the sender level to avoid clogged process inboxes</li><li>Using assign/3 in LiveView always causes an update via Websocket, even if no changes were made</li></ol><p><strong>Will I keep using LiveView for applications such as this one? Absolutely!</strong> Even though this is an example how things can go wrong if you’re not careful, LiveView is an amazing technology and we wouldn’t have been able to include thousands of members in political decision-making processes without it.</p><p>Did you enjoy this article? Let me know by <a href="https://pentacent.us14.list-manage.com/subscribe/post?u=055bc6f21f05a4681d45317fb&amp;id=88303977c5">subscribing to future posts</a>!</p><p>You aren’t the type for newsletters? Just <a href="https://twitter.com/pentacent_hq?ref_src=twsrc%5Etfw">follow @pentacent_hq on Twitter</a> instead.</p><p>If you have any questions or if you’re curious how Phoenix and LiveView could benefit your organization, <a href="https://pentacent.com/contact">let me know!</a></p><p><a href="https://pentacent.com/contact">Pentacent</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce32f6574986" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dynamic SQL Queries With Elixir]]></title>
            <link>https://pentacent.medium.com/dynamic-sql-queries-with-elixir-56232184a9ce?source=rss-524783b33278------2</link>
            <guid isPermaLink="false">https://medium.com/p/56232184a9ce</guid>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[ecto]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Philipp]]></dc:creator>
            <pubDate>Thu, 25 Feb 2021 15:38:05 GMT</pubDate>
            <atom:updated>2021-02-25T15:38:05.061Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building a MongoDB-style Query API for Postgres with Ecto</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dzDS6Oojqpqqw-jBNi35_Q.jpeg" /></figure><p>There are challenges that come up in almost every web application project. One of those challenges is finding a way to let users query your database or filter a given dataset.</p><p>Again and again you’ll have to build a UI or an API in which users can compose queries and specify sorting options.</p><p>Depending on your use-case, this can quickly become a complex endeavour. How do you even design such a querying API? And more importantly: How do compose those queries in a secure fashion?</p><p>This article will introduce you to a little-known feature of Ecto and show you how to use it to build a querying API in the style of MongoDB.</p><h3>A Quick Word on Designing a Querying API</h3><p>There are many ways to design a querying API. In fact, there are so many ways that it can easily become an issue of <a href="https://en.wikipedia.org/wiki/Law_of_triviality">bike-shedding</a>.</p><p>In recent years, <a href="https://graphql.org/">GraphQL</a> has become a popular choice for many. There is good support for GraphQL in Elixir with <a href="https://github.com/absinthe-graphql/absinthe">Absinthe</a>, but GraphQL requires special tooling and this can actually make things more complex than they need to be.</p><p>Another opption is to build a RESTful API with filter support. But there is no commonly accepted standard for this and dealing with more complex filter conditions can quickly become cumbersome. So more bike-shedding lies ahead down this road — best avoid it!</p><p>Fortunately, there is no need to reinvent the wheel here. The folks behind <a href="https://www.mongodb.com/">MongoDB</a> have come up with a querying standard based on JSON: <a href="https://docs.mongodb.com/manual/tutorial/query-documents/">Query Documents</a>.</p><p>Here’s an example from the MongoDB documentation:</p><pre>db.inventory.find( { $or: [ { status: &quot;A&quot; }, { qty: { $lt: 30 } } ] } )</pre><p>This is quite easy to understand. We’re looking for inventory entries that have either the status &quot;A&quot; or a quantity of less than 30.</p><p>This JSON representations is well-suited for even the most complex of queries, it is extendable, and doesn’t require dedicated tools. All we need is a JSON parser.</p><p>And while it was originally designed for MongoDB’s noSQL database, in this article I will show you how to implement it in Elixir with Ecto for Postgres (or MariaDB/MySQL).</p><h3>Dynamic Queries with Ecto</h3><p>You probably already known that Ecto queries are composable. Simple queries on a small number of fields can be implemented very easily like this:</p><pre>filter = %{&quot;artist&quot; =&gt; &quot;ABBA&quot;, &quot;year&quot; =&gt; 1974}</pre><pre>def filter(filter) do<br>  from(s in Songs)<br>  |&gt; maybe_filter_artist(filter)<br>  |&gt; maybe_filter_year(filter)<br>  |&gt; Repo.all()<br>end</pre><pre>defp maybe_filter_artist(query, %{&quot;artist&quot; =&gt; artist}),<br>  do: where(query, [s], s.artist == ^artist)</pre><pre>defp maybe_filter_artist(query, _), do: query</pre><pre>defp maybe_filter_year(query, %{&quot;year&quot; =&gt; year}),<br>  do: where(query, [s], s.year == ^year)</pre><pre>defp maybe_filter_year(query, _), do: query</pre><h3>field/2</h3><p>The example above is already a decent solution in some use cases.</p><p>But as you add more fields, it starts getting very verbose. Fortunately, there’s a way to make it easier: Ecto.Query’s field/2 function. It allows us to use fields without hard-coding their name as we write the query.</p><p>With the help of this function, we can simplify the code from the example above and make it much more flexible.</p><pre>filter = %{<br>  &quot;artist&quot; =&gt; &quot;ABBA&quot;,<br>  &quot;year&quot; =&gt; 1974,<br>  &quot;language&quot; =&gt; &quot;English&quot;<br>}</pre><pre>def filter(filter) do<br>  from(s in Songs)<br>  |&gt; maybe_filter(&quot;artist&quot;, filter[&quot;artist&quot;])<br>  |&gt; maybe_filter(&quot;year&quot;, filter[&quot;year&quot;])<br>  |&gt; maybe_filter(&quot;language&quot;, filter[&quot;language&quot;])<br>  |&gt; Repo.all()<br>end</pre><pre>@allowed_fields ~w{artist year language}<br>defp maybe_filter(query, _field, nil),<br>  do: query</pre><pre>defp maybe_filter(query, field, value)<br>     when field in @allowed_fields do<br>  field = to_existing_atom(field)</pre><pre>  query<br>  |&gt; where([s], field(s, ^field) == ^value)<br>end</pre><h3>dynamic/3</h3><p>Let’s say we want to fetch all songs that were either released by ABBA in 1974 <em>or</em> written in Swedish. Our MongoDB-style filter would look something like this:</p><pre>filter = %{<br>    &quot;$or&quot; =&gt; [<br>        %{&quot;artist&quot; =&gt; &quot;ABBA&quot;, &quot;year&quot; =&gt; 1974},<br>        %{&quot;language&quot; =&gt; &quot;Swedish&quot;}<br>    ]<br>}</pre><p>Building a more complex query with OR and NOT conditions isn’t easily possible when chaining Ecto.Query.where/3 calls.</p><p>This is where the next cool feature of Ecto comes in: Ecto.Query.dynamic/3. Instead of directly operating on the Ecto.Query struct like where does, this function lets you build a fully dynamic query independently.</p><p>This is how dynamic works in principle:</p><pre>condition1 = dynamic([s], s.artist == &quot;ABBA&quot; and s.year == 1974)<br>condition2 = dynamic([s], s.language == &quot;Swedish&quot;)<br>or_condition = dynamic([s], ^condition1 or ^condition2)</pre><pre>from(s in Songs)<br>|&gt; where([s], ^or_condition)<br>|&gt; Repo.all()</pre><p>This example of using dynamic/3 is, ironically, very static.</p><p>Let’s try to build our conditions dynamically from user input by combining what we’ve learned about field/2 and dynamic/3:</p><pre>filter = %{<br>    &quot;$or&quot; =&gt; [<br>        %{&quot;artist&quot; =&gt; &quot;ABBA&quot;, &quot;year&quot; =&gt; 1974},<br>        %{&quot;language&quot; =&gt; &quot;Swedish&quot;}<br>    ]<br>}</pre><pre>def filter(filter) do<br>  from(s in Songs)<br>  |&gt; filter(filter)<br>  |&gt; Repo.all()<br>end</pre><pre>defp filter(query, filter) do<br>  # The top level of the query is always an AND condition<br>  conditions = build_and(filter)<br>  from(q in query, where: ^conditions))<br>end</pre><pre># Building a group of AND-connected conditions<br>defp build_and(filter) do<br>  Enum.reduce(filter, nil, fn<br>    {k, v}, nil -&gt;<br>      build_condition(k, v)</pre><pre>    {k, v}, conditions -&gt;<br>      dynamic([c], ^build_condition(k, v) and ^conditions)<br>  end)<br>end</pre><pre># Building a group of OR-connected conditions<br>defp build_or(filter) do<br>    Enum.reduce(filter, nil, fn<br>      filter, nil -&gt;<br>        build_and(filter)</pre><pre>      filter, conditions -&gt;<br>        dynamic([c], ^build_and(filter) or ^conditions)<br>    end)<br>  end</pre><pre>@allowed_fields ~w{artist year language}<br>defp build_condition(field_or_operator, filter)</pre><pre>defp build_condition(&quot;$or&quot;, filter),<br>  do: build_or(filter)</pre><pre>defp build_condition(field, filter)<br>     when field in @allowed_fields,<br>  do: build_condition(String.to_existing_atom(field), filter)</pre><pre>defp build_condition(field, value)<br>     when is_atom(value),<br>  do: dynamic([c], field(c, ^field) == ^value)</pre><p>We now have framework for building dynamic queries that is extremely easy to extend.</p><p>Do you want to add further operators, e.g. a <em>greater than</em> or an <em>in</em> operator? Just add the following functions:</p><pre>defp build_condition(field, %{&quot;$gt&quot; =&gt; value}),<br>  do: dynamic([c], field(c, ^field) &gt; ^value)</pre><pre>defp build_condition(field, %{&quot;$in&quot; =&gt; value}) when is_list(value),<br>  do: dynamic([c], field(c, ^field) in ^value)</pre><p><strong>Learning about </strong><strong>dynamic/3 can radically simplify how you build APIs with Ecto.</strong> If you want to see a more detailed and well-commented implementation of the principle shown in this article, take a look at the querying module <a href="https://github.com/pentacent/keila/blob/2af0a2f91b0916f7d757f3c24d2955d9550cc6b3/lib/keila/contacts/query.ex">in the Keila GitHub repository</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56232184a9ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Let’s Encrypt For Your Docker App in Less Than 5 Minutes]]></title>
            <link>https://pentacent.medium.com/lets-encrypt-for-your-docker-app-in-less-than-5-minutes-24e5b38ca40b?source=rss-524783b33278------2</link>
            <guid isPermaLink="false">https://medium.com/p/24e5b38ca40b</guid>
            <category><![CDATA[traefik]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[lets-encrypt]]></category>
            <dc:creator><![CDATA[Philipp]]></dc:creator>
            <pubDate>Sat, 21 Mar 2020 14:01:01 GMT</pubDate>
            <atom:updated>2020-03-21T14:01:01.072Z</atom:updated>
            <content:encoded><![CDATA[<h4>Secure any app with free HTTPS certificates in absolutely no time with Traefik.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XHG4SjI3Zam8wOMo8e_Uhg.jpeg" /></figure><p>A while ago, I wrote an article titled <a href="https://medium.com/@pentacent/nginx-and-lets-encrypt-with-docker-in-less-than-5-minutes-b4b8a60d3a71">“Nginx and Let’s Encrypt in Less Than 5 Minutes”</a>. It quickly became my most popular post ever, with thousands of claps and hundreds of forks and stars <a href="https://github.com/wmnnd/nginx-certbot">on GitHub</a>.</p><p>My original approach included a script that needed to be run once for each deployment. It’s effective, simple — but a little clumsy. So I kept wondering: <strong>What if there is an even easier way to set up Let’s Encrypt with docker-compose?</strong></p><p>I like keeping things simple. This is why I tend to avoid unnecessary dependencies in my projects. You certainly won’t find me using packages like is-odd or the infamous left-pad. So when my colleague <a href="https://christian.sciberras.me/">Christian</a> suggested adding a dependency “just” for managing HTTPS certificates, I wasn’t a big fan. <em>Why not just use my script</em>, I wondered. But I gave it a try and never looked back.</p><p><strong>So here it is, my new best solution for securing your Docker app with an HTTPS certificate from Let’s Encrypt.</strong> This article is going to use <em>docker-compose</em> but you could use the same technique with Kubernetes or “pure” Docker.</p><blockquote><em>Before we continue, here is a short but shameless plug: I occasionally write about deployment best practices, JavaScript and Elixir. If you’d like to keep up, </em><a href="http://eepurl.com/c-ECJT"><em>please sign up to my 100% spam-free newsletter</em></a><em>.</em></blockquote><h3>Traefik</h3><h4>The Unix Principle</h4><p><em>Do One Thing and Do It Well.</em> This is one of the main tenants of <a href="https://en.wikipedia.org/wiki/Unix_philosophy">Unix philosophy</a>. Following this principle, let’s ask the question: what do we need when we want to “<em>Add Let’s Encrypt to a Docker App</em>”? We want to <strong>request and renew HTTPS certificates</strong>, we want to <strong>terminate TLS (HTTPS) connections </strong>and, finally, <strong>pass the connection on to our containerized application</strong>. You might argue that this is actually four different things, but for our purposes, it’s the combination of these features that matters. <strong>Fortunately, there is a tool that implements this very set of features: </strong><a href="http://traefik.io/"><strong>Traefik</strong></a><strong>.</strong></p><blockquote>Traefik (pronounced <em>traffic</em>) is a modern HTTP reverse proxy and load balancer that […] configures itself automatically and dynamically.</blockquote><blockquote>– <a href="https://github.com/containous/traefik/#overview">The Traefik Docs</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*Hl3OGno7atEBb5-wLUhefw.png" /><figcaption>Traefik is written in Go and its logo is <a href="https://github.com/containous/traefik/#credits">inspired by the Go Gopher</a>.</figcaption></figure><p>You probably think of Nginx or Apache when they hear the term <em>reverse proxy</em>. Many modern web frameworks come with their own HTTP server (e.g. Express in Node.js, Puma in Ruby, Plug in Elixir, or Tomcat in Java). But more often than not, that server isn’t exposed directly to your users. Instead, a <em>reverse proxy</em> terminates TLS connections and then passes on simple HTTP requests to your app.</p><p>This means, Traefik can take the role of Nginx if you’re running an application with a web server. But Traefik can also be a reverse proxy <em>for</em> Nginx, taking care of routing and HTTPS.</p><h4>Automatic configuration</h4><p>Now to the interesting part: The docs say <strong><em>Traefik</em> <em>configures itself automatically</em>.</strong> How is that even possible? The answer is <a href="https://docs.docker.com/config/labels-custom-metadata/">Docker Object Labels</a>.</p><p>Every Docker container can be assigned one or several <em>labels</em>. Once you’ve got Traefik running, making a service available on a specific host name is as simple as assigning it the following label: traefik.http.routers.my-app.rule=Host(`example.com`).</p><p>Traefik processes all Docker labels in real time. This makes sure your routes are always configured according to the containers you have running. And since Traefik supports Let’s Encrypt by default, your container will receive an HTTPS certificate almost instantly.</p><h3>Setting up Let’s Encrypt Certificates For Your Docker App With Traefik</h3><h4>Setting Up Traefik</h4><p>Before we get started, we need to create the Traefik config files. Traefik distinguishes between two types of configuration: <em>static configuration </em>is loaded at startup and describes the Traefik instance. <em>dynamic configuration</em> is loaded at runtime and describes services and middlewares.</p><p>Start by creating a folder name traefik that will contain our configuration.</p><p>Here’s what we want to configure in the static configuration, traefik/traefik.toml:</p><ul><li>An entry point http on port 80</li><li>An entry point https at port 443</li><li>A certificate resolver lets-encrypt to automatically retrieve certificates from Let’s Encrypt via the ACME protocol</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f50b26ad50a333a31d8d63e806f2613/href">https://medium.com/media/1f50b26ad50a333a31d8d63e806f2613/href</a></iframe><blockquote>Note: Setting <em>providers.docker.exposedByDefault</em> to <em>false</em> makes sure we don’t accidentally expose containers and need to explicitly enable Traefik for each container we want to use it with.</blockquote><p>Make sure to insert a working email address in the configuration to get notified by Let’s Encrypt whenever there is a problem with your certificate.</p><p>In order to enforce HTTPS for our domains, we now need to add a router and a middleware to our dynamic configuration, traefik/dynamic/force-https.toml:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c42fd4146915893a8c5c86af98db31b/href">https://medium.com/media/7c42fd4146915893a8c5c86af98db31b/href</a></iframe><p>Next, we need an empty file in which Traefik can store retrieved HTTPS certificates. Make sure to set restrictive rw------- permissions or else Traefik refuses to use the file.</p><pre>touch traefik/acme.json<br>chmod 600 traefik/acme.json</pre><p>Finally, let’s create a docker-compose.yml file that starts Traefik:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/628303b0aea84a34143e05c9eaa9bf89/href">https://medium.com/media/628303b0aea84a34143e05c9eaa9bf89/href</a></iframe><h4>Configuring Your Container(s)</h4><p>Now we’re ready to launch our own application! Add your app to docker-compose. Keep in mind that you’ll have to replace “my-app” with the actual name of your application and “example.com” with your actual domain.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f6160d4a71054e5ccac8b5e737ac17d/href">https://medium.com/media/6f6160d4a71054e5ccac8b5e737ac17d/href</a></iframe><blockquote><strong>Pro tip:</strong> If you need multiple domains for one container, you can use comma-separated values (e.g. Host(`foo.bar`, `fizz.buzz`)). And in case you need <em>a lot of domains</em>, you can use \ to break the expression into multiple lines.</blockquote><h4>Done!</h4><p>Are we done already? Yes, we are!</p><p>As soon as you run docker-compose up, Traefik and your application will start. Traefik will then immediately get a Let’s Encrypt certificate for your domain and <strong>you’ll be online, secured with HTTPS, in seconds</strong>. If only life could always be so easy.</p><blockquote><strong>Did you enjoy this article?</strong></blockquote><blockquote><em>I occasionally write about deployment best practices, JavaScript and Elixir. If you’d like to keep up, </em><a href="http://eepurl.com/c-ECJT"><em>sign up to my 100% spam-free newsletter</em></a><em>.<br>You can also follow me on Twitter </em><a href="https://www.twitter.com/pentacent_hq"><em>@pentacent_hq</em></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24e5b38ca40b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nginx and Let’s Encrypt with Docker in Less Than 5 Minutes]]></title>
            <link>https://pentacent.medium.com/nginx-and-lets-encrypt-with-docker-in-less-than-5-minutes-b4b8a60d3a71?source=rss-524783b33278------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4b8a60d3a71</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[ssl]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[docker-compose]]></category>
            <dc:creator><![CDATA[Philipp]]></dc:creator>
            <pubDate>Fri, 28 Sep 2018 14:01:01 GMT</pubDate>
            <atom:updated>2018-12-19T09:29:57.644Z</atom:updated>
            <content:encoded><![CDATA[<h4>Getting Nginx to run with Let’s Encrypt in a docker-compose environment is trickier than you’d think …</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p--Z0OzI08m6zLVVJdIjBg.jpeg" /></figure><p>The other day, I wanted to <em>quickly</em> launch an nginx server with Let’s Encrypt certificates. I expected the task to be easy and straightforward. Turns out: I was wrong, it took a significant amount of time and it’s quite a bit more complicated.</p><p>Of course, in the grand scheme of things, it <em>is</em> pretty straightforward. But there are a couple of details you need to be aware of. The goal of this guide is to help you build a docker-compose setup that runs nginx in one container and a service for obtaining and renewing HTTPS certificates in another. <strong>Whether you’re using nginx as a proxy for your web app or just for serving static files, this guide is for you.</strong></p><blockquote>TL;DR: The full code from this guide is <a href="https://github.com/wmnnd/nginx-certbot">available on GitHub</a>.</blockquote><h4>Quick Reminder: What is docker-compose?</h4><p><a href="https://docs.docker.com/compose/"><em>docker-compose</em></a> is a tool for defining containers and running them. It’s a great choice when you have multiple interdependent containers but you don’t need a full-blown container cluster like Kubernetes.</p><p>The <a href="https://docs.docker.com/compose/overview/">official documentation</a> puts it like this:</p><blockquote>With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration</blockquote><p>This guide requires docker-compose. If you don’t have it yet, take a look at the <a href="https://docs.docker.com/compose/install/#install-compose">installation instructions</a> and get it.<br><em>Hint: If you’re installing docker-compose on CoreOS, it needs to go into </em><em>/opt/bin instead of </em><em>/usr/local/bin.</em></p><h4>The Setup</h4><p>Official images of <a href="https://hub.docker.com/_/nginx/">nginx</a> and an automated build of <a href="https://hub.docker.com/r/certbot/certbot/">certbot</a>, the EFF’s tool for obtaining Let’s Encrypt certificates, are available in the Docker library.</p><p>Let’s begin with a basic docker-compose.yml configuration file that defines containers for both images:</p><pre>version: &#39;3&#39;<br>services:<br>  nginx:<br>    image: nginx:1.15-alpine<br>    ports:<br>      - &quot;80:80&quot;<br>      - &quot;443:443&quot;<br>    volumes:<br>      - ./data/nginx:/etc/nginx/conf.d<br>  certbot:<br>    image: certbot/certbot</pre><p>Here is a simple nginx configuration that redirects all requests to HTTPS. The second server definition sets up a proxy to <em>example.org</em> for demonstration purposes. This is where you would add your own configuration for proxying requests to your app or serving local files.</p><p>Save this file as data/nginx/app.conf alongside docker-compose.yml. <strong>Change </strong><strong>example.org in both occurrences of </strong><strong>server_name to your domain name.</strong></p><pre>server {<br>    listen 80;<br>    server_name example.org;</pre><pre>    location / {<br>        return 301 <a href="https://$host$request_uri;/">https://$host$request_uri;</a><br>    }    <br>}</pre><pre>server {<br>    listen 443 ssl;<br>    server_name example.org;<br>    <br>    location / {<br>        proxy_pass <a href="https://wikipedia.org;/">http://example.org;</a> #for demo purposes<br>    }<br>}</pre><p>If you would try to run docker-compose up now, nginx would fail to start because there is no certificate. We need to make some adjustments.</p><h4>Linking up nginx and certbot</h4><p>Let’s Encrypt performs domain validation by requesting a well-known URL from a domain. If it receives a certain response (the “challenge”), the domain is considered validated. This is similar to how Google Search Console establishes ownership of a website. The response data is provided by certbot, so we need a way for the nginx container to serve files from certbot.</p><p>First of all, we need two shared Docker volumes. One for the validation challenges, the other for the actual certificates.</p><p>Add this to the volumes list of the nginx section in docker-compose.yml.</p><pre>- ./data/certbot/conf:/etc/letsencrypt<br>- ./data/certbot/www:/var/www/certbot</pre><p>And this is the counterpart that needs to go in the certbot section:</p><pre>volumes:<br>  - ./data/certbot/conf:/etc/letsencrypt<br>  - ./data/certbot/www:/var/www/certbot</pre><p>Now we can make nginx serve the challenge files from certbot! Add this to the first (port 80) section of our nginx configuration (data/nginx/app.conf):</p><pre>location /.well-known/acme-challenge/ {<br>    root /var/www/certbot;<br>}</pre><p>After that, we need to reference the HTTPS certificates. Add the soon-to-be-created certificate and its private key to the second server section (port 443). <strong>Make sure to once again replace </strong><strong>example.org with your domain name.</strong></p><pre>ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem;<br>ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem;</pre><p>And while we’re at it: The folks at Let’s Encrypt maintain best-practice HTTPS configurations for nginx. Let’s also add them to our config file. This will score you a straight A in the <a href="https://www.ssllabs.com/index.html">SSL Labs test</a>!</p><pre>include /etc/letsencrypt/options-ssl-nginx.conf;<br>ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;</pre><h4>The Chicken or the Egg?</h4><p>Now for the tricky part. We need nginx to perform the Let’s Encrypt validation But nginx won’t start if the certificates are missing.</p><p>So what do we do? Create a dummy certificate, start nginx, delete the dummy and request the real certificates.<br>Luckily, you don’t have to do all this manually, I have created a convenient script for this.</p><p>Download the script to your working directory as init-letsencrypt.sh:</p><pre>curl -L <a href="https://raw.githubusercontent.com/wmnnd/nginx-certbot/master/init-letsencrypt.sh">https://raw.githubusercontent.com/wmnnd/nginx-certbot/master/init-letsencrypt.sh</a> &gt; init-letsencrypt.sh</pre><p><strong>Edit the script to add in your domain(s) and your email address.</strong> If you’ve changed the directories of the shared Docker volumes, make sure you also adjust the data_path variable as well.</p><p>Then run chmod +x init-letsencrypt.sh and sudo ./init-letsencrypt.sh.</p><h4>Automatic Certificate Renewal</h4><p>Last but not least, we need to make sure our certificate is renewed when it’s about to expire. The certbot image doesn’t do that automatically but we can change that!</p><p>Add the following to the certbot section of docker-compose.yml:</p><pre>entrypoint: &quot;/bin/sh -c &#39;trap exit TERM; while :; do certbot renew; sleep 12h &amp; wait $${!}; done;&#39;&quot;</pre><p>This will check if your certificate is up for renewal every 12 hours as recommended by Let’s Encrypt.</p><p>In the nginx section, you need to make sure that nginx reloads the newly obtained certificates:</p><pre>command: &quot;/bin/sh -c &#39;while :; do sleep 6h &amp; wait $${!}; nginx -s reload; done &amp; nginx -g \&quot;daemon off;\&quot;&#39;&quot;</pre><p>This makes nginx reload its configuration (and certificates) every six hours in the background and launches nginx in the foreground.</p><h4>Docker-compose Me Up!</h4><p>Everything is in place now. The initial certificates have been obtained and our containers are ready to launch. Simply run docker-compose up and enjoy your HTTPS-secured website or app.</p><p>Did you find this guide helpful? <strong><em>Make sure to subscribe to my newsletter so you don’t miss the next article with useful deployment tips!</em></strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F5b327c%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F5b327c%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252F5b327c%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/84ae85849a6e173ebfce5042b3f678c7/href">https://medium.com/media/84ae85849a6e173ebfce5042b3f678c7/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4b8a60d3a71" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build A Great Status Page in 15 Minutes With No Budget]]></title>
            <link>https://medium.com/hackernoon/build-a-great-status-page-in-15-minutes-with-no-budget-98257f67aef1?source=rss-524783b33278------2</link>
            <guid isPermaLink="false">https://medium.com/p/98257f67aef1</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[serverless]]></category>
            <dc:creator><![CDATA[Philipp]]></dc:creator>
            <pubDate>Fri, 13 Apr 2018 12:01:01 GMT</pubDate>
            <atom:updated>2019-05-22T08:36:28.475Z</atom:updated>
            <content:encoded><![CDATA[<h4>Here is one lesson I had to learn the hard way: <strong>Everyone needs a status page.</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OramvJChXw9QsbfSrq1bTQ.jpeg" /><figcaption>Senior Lamb Data Analyst. (Original photo by <a href="https://stocksnap.io/author/46145">Robin Vet</a> on <a href="https://stocksnap.io/photo/CX48VQ5EU7">Stocksnap</a>)</figcaption></figure><p>Yesterday my app went down. My PaaS provider had decided to update their platform late that evening. This caused an obscure bug which kept my Docker containers from spinning up again. The consequence was devastating: My <a href="https://hackernoon.com/tagged/app">app</a> was offline for hours. I had no way of fixing a bug over which I had no control.</p><p><strong>Worse yet: I had no good way of letting my users know what was going on.</strong> I could only email everyone and apologize in person.</p><h3>Why Monitoring Is Not Enough</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*MKA7mgKSS7qU557zwTKKcA.png" /><figcaption>Here’s an email you don’t want to get at 11:24 PM</figcaption></figure><p>Of course I had set up automatic monitoring of my app’s web page and API endpoints. But it’s not enough if <em>you</em> know that your server is down — unless you’re the only one using it! If anyone else is depending on your service, <strong>you need to have a communication channel <em>when</em> (not if!) something goes wrong.</strong></p><p>A status page can be an important element in your relationship with your users. It helps you achieve the following goals:</p><ol><li><strong>Transparency</strong><br>Be open about what’s gone wrong and what you’re doing to fix it</li><li><strong>Fast crisis response</strong><br>Publish information and updates about service interruptions right as they happen</li><li><strong>Building user trust</strong><br>Being transparent and responsive is the best way to show your users that they can trust you. They see that they can rely on your service even when things go bad.</li></ol><h3>Why Set Up Your Own Page</h3><p>There are plenty of status-page-as-a-service providers around. One prominent example is <a href="https://www.statuspage.io/">Atlassian Statuspage</a> which powers the status pages of sites like <a href="http://www.vimeostatus.com/">Vimeo</a>, <a href="https://status.dropbox.com/">Dropbox</a> or <a href="https://reddit.statuspage.io/">Reddit</a>. Similarly well-known is <a href="https://status.io/">Status.io</a> which is behind the status pages of <a href="https://status.twitterstat.us/">Twitter</a>, <a href="https://status.docker.com/">Docker</a> and <a href="https://letsencrypt.status.io/">Let’s Encrypt</a>. Other commercial alternatives include <a href="https://www.sorryapp.com/">Sorry</a>, <a href="https://statusy.co/">Statusy</a> and <a href="https://www.adminlabs.com/">AdminLabs</a>.</p><p>None of them come cheap; Statuspage’s smallest plan (which doesn’t include SMS notifications) comes in at USD 348/year; Status.io’s Basic plan (again, no SMS notifications, and this time even without HTTPS encryption) is available for no less than USD 948/year. <strong>I’m sure this pricing makes perfect sense for VC-backed Silicon Valley companies — but not for bootstrapped startups or freelancers like myself.</strong> This is why I decided to set up my own status page.</p><h3>What Do We Want?</h3><p>There are really three boxes that a good status page needs to check:</p><ol><li>☐ <strong>Automatic updates from monitoring services</strong><br>The status page needs to be able to display monitoring and/or performance data from external sources</li><li>☐ <strong>Communication of current status and planned maintenance</strong><br>The main purpose of a status page: Communicating the availability of your services to your users</li><li>☐ <strong>It needs to look good</strong><br>Well, obviously!</li></ol><h3>The Solution?</h3><p>Fortunately, there is an open-source (Apache-2.0-licensed) status page that fulfills all my requirements: <a href="http://lambstatus.github.io/">LambStatus</a>. <strong>LambStatus is a status page you can install on AWS with one click</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bW61b4mm-DCi_TYc0k9j6w.png" /><figcaption><a href="https://lambstatus.github.io/">The LambStatus website</a></figcaption></figure><p>Even though its name evokes cuddly, woolly sheep babies, LambStatus is actually named for Amazon’s <a href="https://en.wikipedia.org/wiki/Serverless_computing">serverless</a> platform <a href="https://aws.amazon.com/lambda/">AWS Lambda</a>. LambStatus is essentially a bunch of node.js functions, glued together by some <a href="https://hackernoon.com/tagged/aws">AWS</a> configuration. The functions are stored in AWS Lambda and only executed on request. This means: Other than a few additional resources (such as a handful of files on S3), <strong>there are almost no operating costs for running LambStatus.</strong> How low are the operating costs? Here’s an estimate from the LambStatus website:</p><blockquote>A status page only occasionally gets huge traffic. The system takes $1 per 30,000 visitors and almost $0 if no visitors.</blockquote><p>Let’s recap our requirements:</p><h4>☑ <strong>Automatic updates from monitoring services</strong></h4><p>LambStatus can display data from AWS CloudWatch which automatically monitors many AWS resources. It can also receive data from <a href="https://lambstatus.github.io/integrate-with-other-monitoring-systems">external monitoring services</a> via an API.</p><p>On top of that, you can also set up your own monitoring for custom HTTP(S) endpoints. I’ve written an guide about that here:</p><p><a href="https://medium.com/@pentacent/serverless-monitoring-of-apps-websites-with-aws-lambda-5431e6713a66">Serverless Monitoring of Apps &amp; Websites with AWS Lambda</a></p><h4>☑ <strong>Communication of current status and planned maintenance</strong></h4><p>With LambStatus, your users can subscribe to incidents via RSS or email. You can announce planned maintenances and provide updates for incidents as they happen.</p><h4>☑ <strong>It needs to look good</strong></h4><p>You can take a look at the minimalist beauty of LambStatus on the <a href="https://demo-status.lambstatus.org/">demo status page</a> and the <a href="https://demo-admin.lambstatus.org/">demo admin page</a>. Or just take it from this screenshot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*SrvWiXoMs01tHoS_UKT0TQ.png" /><figcaption>Clean, clear, minimalist. What more can you want from a status page?</figcaption></figure><h3>Getting Started with LambStatus</h3><p>I’m planning to write a step-by-step guide with some personal tips for setting up LambStatus. <strong><em>Make sure to subscribe to my newsletter so you don’t miss the next article!</em></strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F5b327c%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F5b327c%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252F5b327c%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/84ae85849a6e173ebfce5042b3f678c7/href">https://medium.com/media/84ae85849a6e173ebfce5042b3f678c7/href</a></iframe><p>If you don’t want to wait: Getting started with LambStatus is really simple. The only think you need is an AWS account. Thanks to LambStatus’ CloudFormation template, you can set it up in a matter of only a few clicks. Take a look at the official <a href="https://lambstatus.github.io/get-started">Getting Started guide</a>!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98257f67aef1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/build-a-great-status-page-in-15-minutes-with-no-budget-98257f67aef1">Build A Great Status Page in 15 Minutes With No Budget</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Serverless Monitoring of Apps & Websites with AWS Lambda]]></title>
            <link>https://pentacent.medium.com/serverless-monitoring-of-apps-websites-with-aws-lambda-5431e6713a66?source=rss-524783b33278------2</link>
            <guid isPermaLink="false">https://medium.com/p/5431e6713a66</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Philipp]]></dc:creator>
            <pubDate>Mon, 02 Apr 2018 13:01:01 GMT</pubDate>
            <atom:updated>2018-04-02T13:45:09.194Z</atom:updated>
            <content:encoded><![CDATA[<h4>Could this be the easiest, cheapest, most reliable way to monitor websites and API endpoints?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6ooGqu0WxnTifQ4GRs4F9A.jpeg" /><figcaption>A traveler watching monitoring stats on AWS CloudWatch. (Original photo by <a href="https://unsplash.com/photos/78A265wPiO4?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">David Marcu</a> on <a href="https://unsplash.com/search/photos/watchtower?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>)</figcaption></figure><p>When you run a website, a web app or some other kind of service on the Internet, it is always a good idea to keep an eye on its availability and performance. Obviously, you don’t want to manually check in every hour to see if everything is still up and running. <strong>Let’s automate this!</strong></p><p><strong>This guide is going to walk you through setting up monitoring for one or several HTTP(S) endpoints</strong>. We are going to do this using two AWS services: <em>AWS Lambda</em> and <em>AWS CloudWatch</em>. Lambda is Amazon’s <a href="https://en.wikipedia.org/wiki/Serverless_computing">serverless computing</a> offer which allows you to perform one-off executions of small functions, and pay only for actual execution time. CloudWatch is a service originally designed for monitoring EC2 instances. Instead, we’re going to use it to keep logs for our custom HTTP(S) endpoints which don’t need to be hosted on AWS.</p><p><strong>Thanks to this setup, monitoring multiple endpoints is super-reliable and costs next-to-nothing.</strong></p><h4>Creating an IAM Role</h4><p>Before we can get started, we need to set up an <em>IAM Role</em>. IAM Roles are AWS’s way of declaring permissions for accessing other services and resources.</p><p>In order to create the new role, go to your AWS dashboard and select the <em>IAM</em> service. Then select “Roles” in the left-hand menu and click on the “Create role” button. Next you need to select which service is going to use this role. In our case, this is going to be <em>Lambda</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C7HXFjYcPGvetFFDCiAvCg.png" /></figure><p>After confirming your selection by clicking on “Next: Permissions”, we have to create a new IAM Policy. Click on “Create Policy” and then select “CloudWatch” as the service you want to access. Then, select “PutMetricData” from the “Write” permissions and click on “Review policy”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4npp112cPqVf0YPEKQ3t5A.png" /></figure><p>In the next screen, you can give give the policy a name (e. g. “CloudWatch-putMetricDataPermission”) and confirm everything by clicking on “Create Policy”.</p><p>Now back to the tab where you were creating the new role. Click on the “Refresh” button, then find and select the newly created policy in the list. Once that’s done, click on “Next: Review”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0FUVerSn_pBMW5s2W0raCg.png" /></figure><p>Finally, enter a role name (e.g. “watchtower-role”) and complete the process with a click on “Create Role”.</p><h4>Creating the Lambda Function</h4><p>Now we’re ready to set up the monitoring script. Navigate to the AWS Lambda service from your AWS dashboard and click on “Create a function”. Enter a function name (e. g. “Watchtower-ping”), select the Node.js runtime and pick the IAM role you’ve just created. Confirm by clicking on “Create function”.</p><p>Now you should see the function configuration. Scroll down to see the text editor. You could create your own monitoring script — which would probably take you an hour or two. Alternatively, you can go with the one I’ve prepared for this guide! Simply get the contents of index.js from the <a href="https://github.com/wmnnd/lambda-watchtower">lambda-watchtower</a> GitHub repository and paste them into the text editor of the Lambda configuration page.</p><p>Let’s make sure everything is working according to plan. AWS Lambda allows us to try our function with a test event. In order to do that, click on the “Test” button and create a new test case with the parameters of your choice. If you’re using lambda-watchtower, your test event could look something like the following snippet. For more information about configuring lambda-watchtower, check out the <a href="https://github.com/wmnnd/lambda-watchtower/blob/master/README.md">documentation</a>.</p><pre>{<br>  &quot;targets&quot;: [<br>    {<br>      &quot;url&quot;: &quot;<a href="https://www.example.com/">https://www.example.com</a>&quot;,<br>      &quot;name&quot;: &quot;example.com&quot;<br>    }<br>  ],<br>  &quot;timeout&quot;: 5000,<br>  &quot;namespace&quot;: &quot;Watchtower&quot;<br>}</pre><p>Give your test case a name and confirm the test parameters by clicking on “Create”. Now, all you need to do is click on “Save” before you can click on “Test” once again. This will run the newly created function with the test event you’ve just created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*It77FZQsQK_QghYPNLh01w.png" /></figure><p>If everything went according to plan, you should see no errors and the execution statistics from the script.</p><h4>Configuring a CloudWatch Rule</h4><p>Now that the Lambda script has been set up, you only need to make sure it gets run every five minutes (or any other interval you prefer). This is possible with <em>CloudWatch Rules</em>. Go to the CloudWatch service from your AWS dashboard and select “Rules” from the left-hand menu. Click on “Create rule” and select “Schedule” as your event source. You can now choose the execution interval for the monitoring script. Running the script every five minutes is usually a good choice; shorter intervals might incur additional fees.</p><p>Next, click on “Add target” and select the Lambda function you’ve created earlier. Click on “Configure input” and select “Constant (JSON text)”. Paste your script parameters in the input field below. Confirm everything by clicking on “Configure details”. Type in a fitting name (e. g. “watchtower-rule”) and finalize everything with a click on “Create rule”.</p><h4>Accessing the Logs</h4><p>Now that you’ve successfully set up monitoring with Lambda and CloudWatch, it’s time to take a look at the data that is being stored. On the CloudWatch service, select “Metrics” from the navigation on the left. Then click on the namespace you’ve chosen for the logs (e.g. “Watchtower”). In the case of lambda-watchtower, you’ll see the names of your endpoints and by clicking on one of them you’ll see the detailed statistics that were recorded. <strong>If you’ve only just set up the monitoring script, the logs are going to contain only one or two data points. But soon, it should look something like this:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wrr43ZYQ4qGazbrAyF_SLQ.png" /></figure><p>That’s it! You now have a fully functional HTTP(S) monitoring service running with AWS Lambda and CloudWatch.</p><p><strong>If you have any questions or suggestions for improvements, please let me know!</strong> Pull requests for lambda-watchtower are also welcome.</p><p><em>In this series I write about monitoring applications and web services with AWS. The next segment will be about setting up email and SMS notifications when something goes wrong. </em><strong><em>Make sure to subscribe to my newsletter so you don’t miss any future updates!</em></strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F5b327c%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F5b327c%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252F5b327c%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/84ae85849a6e173ebfce5042b3f678c7/href">https://medium.com/media/84ae85849a6e173ebfce5042b3f678c7/href</a></iframe><h3>Credit Where Credit Is Due</h3><p>Thanks to <a href="https://twitter.com/slashdotpeter">Péter Martón</a> for his article on <a href="https://blog.risingstack.com/measuring-http-timings-node-js/">Measuring HTTP Timings with Node.js</a> which is a great explanation of the event sequence and significance in Node’s http(s) module and helped me tremendously in creating lambda-watchtower.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5431e6713a66" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Remote Code Execution Vulnerability in Electron Apps]]></title>
            <link>https://medium.com/hackernoon/remote-code-execution-vulnerability-in-electron-apps-b21fbdc162c?source=rss-524783b33278------2</link>
            <guid isPermaLink="false">https://medium.com/p/b21fbdc162c</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[electron]]></category>
            <category><![CDATA[software]]></category>
            <dc:creator><![CDATA[Philipp]]></dc:creator>
            <pubDate>Thu, 25 Jan 2018 15:29:22 GMT</pubDate>
            <atom:updated>2019-05-22T08:36:28.910Z</atom:updated>
            <content:encoded><![CDATA[<h4>Here is what Developers and Users need to know about Electron’s latest security issue.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N2-NnysZ8JATyUOz95FahQ.jpeg" /><figcaption>A glowing electron in front of a gloomy sky with random binary numbers is never a good sign … Original photograph by <a href="https://pixabay.com/en/nebula-apocalypse-disaster-end-time-2273069/">code404</a> (<a href="https://creativecommons.org/choose/zero/">CC0</a>)</figcaption></figure><p>On Monday, the <a href="https://hackernoon.com/tagged/electron">Electron</a> team published security advisory <a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-1000006">CVE-2018–1000006</a> describing an arbitrary code execution vulnerability in Electron. Arbitary code executions — or <em>remote code executions</em> (RCE) when performed through a network — are the most severe class of <a href="https://hackernoon.com/tagged/software">software</a> vulnerabilities. They allow an attacker to run any code on the victim’s machine.</p><h3>Am I affected as a User?</h3><p>The current report affects Electron applications on Windows that use custom protocols. Custom protocols allow applications to handle links from web pages or other applications. Links on Apple’s App Store web page, for example, use URLs beginning with itmss:// so that they can be opened directly by iTunes.</p><p>If an Electron application uses custom protocols on Windows and has not been updated since Monday, it is vulnerable. You can verify which applications use custom protocols in the <a href="https://support.microsoft.com/en-us/help/4028161/windows-10-change-default-programs">Windows Settings</a>. <strong>Disable protocols for applications that have not yet been updated with a security patch or uninstall them</strong>.</p><h3>Am I affected as a Developer?</h3><p>If you have published an application with any version of Electron before 1.8.2-beta.4, 1.7.11, or 1.6.16 and you are using the app.setAsDefaultProtocolClient API on Windows, your application is vulnerable. <strong>You need to take immediate action.</strong></p><h4>What if I am affected?</h4><ol><li>Follow the <a href="https://electronjs.org/blog/protocol-handler-fix">mitigation advice</a> put out by the Electron team</li><li>Inform your users about the vulnerability and the severity of it.</li><li>Release an updated version of your application built with an updated version of Electron.</li></ol><h4>How can I secure my application in the future?</h4><p>Electron is a convenient way to create desktop applications with familiar web frameworks. Unfortunately, this ease-of-use comes at the cost of many millions of lines of code from the Chromium and Electron project — each one of them a potential security hazard. Remember: <a href="https://blogs.msdn.microsoft.com/elee/2009/03/11/source-code-is-a-liability-not-an-asset/"><strong>Source code is a liability, not an asset</strong></a> and using a vast software project like Electron in your application is a vast security liability.</p><p><strong>There is no way to ensure your application is safe from future vulnerability discoveries in Electron</strong>. This is why you need to make sure to tick every point on the following checklist:</p><ol><li>Make sure you <strong>follow the Electron team’s </strong><a href="https://electronjs.org/docs/tutorial/security"><strong>guide to creating secure Electron applications</strong></a>. Before you start worrying about vulnerabilities in Electron, make sure you are not adding own security pitfalls.</li><li><strong>Subscribe to the </strong><a href="https://github.com/electron/electron/releases.atom"><strong>Electron Release feed</strong></a>. Make sure to keep an eye out for for any releases with mentions of [SECURITY]</li><li><strong>Enable auto-updates</strong>. Only with a functioning auto-update pipeline can you react quickly to known vulnerabilities. You can follow <a href="https://github.com/electron/electron/blob/master/docs/tutorial/updates.md">Electron’s auto-update guide</a>. Alternatively, you can use a specialized service such as <a href="https://www.dblsqd.com/go/electron">DBLSQD for Electron</a> for managing your updates.</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F9ff2e1%3Fas_embed%3Dtrue&amp;url=https%3A%2F%2Fupscri.be%2F9ff2e1%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252F9ff2e1%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/d08f366177a4c47220fcbe9d5ed26274/href">https://medium.com/media/d08f366177a4c47220fcbe9d5ed26274/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b21fbdc162c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/remote-code-execution-vulnerability-in-electron-apps-b21fbdc162c">Remote Code Execution Vulnerability in Electron Apps</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Started With Elixir & Docker]]></title>
            <link>https://pentacent.medium.com/getting-started-with-elixir-docker-982e2a16213c?source=rss-524783b33278------2</link>
            <guid isPermaLink="false">https://medium.com/p/982e2a16213c</guid>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Philipp]]></dc:creator>
            <pubDate>Wed, 15 Nov 2017 13:01:01 GMT</pubDate>
            <atom:updated>2017-11-15T13:37:44.446Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn how to build Docker containers for Elixir apps with this guide.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_RoAm_k9JDhlxK4LYWLz9g.jpeg" /><figcaption>Lots of containers. (Original photograph by <a href="https://stocksnap.io/photo/PYJKVJ1ST1">Chuttersnap/StockSnap</a>. License: <a href="https://creativecommons.org/publicdomain/zero/1.0/legalcode">CC0</a>)</figcaption></figure><h3>A Quick Recap on Docker &amp; Elixir Releases</h3><p>This article is best read when you already know what Docker and Elixir Releases are. If you need to refresh your memory, check out this little introduction that (curiously) explains both concepts at the same time:</p><p><a href="https://medium.com/@pentacent/elixir-releases-docker-the-basics-explained-4c800a5b990b">Elixir Releases &amp; Docker — The Basics Explained</a></p><h3>Why use Docker with Elixir?</h3><p>I’ve previously written about <a href="https://medium.com/@pentacent/mastering-elixir-releases-with-distillery-a-pretty-complete-guide-497546f298bc">how to create Elixir Releases with Distillery</a>. Releases are great and one of the many features to love about the Elixir/Erlang ecosystem. But the extent to which they are <em>truly</em> self-contained is limited.</p><p><strong>A Release can only run on systems sufficiently similar to the build system.</strong> What does that mean? Unfortunately, that question doesn’t have a trivial answer. Most importantly, operating system and processor architecture need to match and system libraries need to be compatible. The former is why you can’t run a Release that was built on a macOS machine on Linux. The latter is why a Release that you build with Ubuntu 17.10 doesn’t work on Ubuntu 14.04: Their C standard libraries (specifically <a href="https://www.gnu.org/software/libc/">glibc</a>) are not <a href="https://en.wikipedia.org/wiki/Application_binary_interface">ABI-compatible</a>. This means that your application might work perfectly fine on one system, but crash with an obscure error message on another.</p><p>Fortunately, there is a solution to this problem and it’s <em>containers.</em></p><h3>Step-by-Step Guide</h3><h4>Installing Docker</h4><p>Before we get started, you need to have Docker installed on your development machine. Even though most of the time you’ll be running Docker in Continuous Integration (CI) and other specialized server environments, in order to get familiar with it and to follow this guide, I recommend setting it up locally.</p><p>There are multiple versions of Docker available but for our purposes Docker Community Edition (<em>Docker CE</em>) is more than sufficient. The Docker website has installation instructions for <a href="https://docs.docker.com/docker-for-windows/">Windows</a>, <a href="https://docs.docker.com/docker-for-mac/install/">macOS</a> and <a href="https://docs.docker.com/engine/installation/">Linux</a>.</p><p><strong>If you are using Linux, you might already have Docker installed but many distributions ship with outdated versions.</strong> So I recommend installing the latest version from the Docker website to make sure there are no compatibility issues.</p><h4>What’s in a Dockerfile?</h4><p>Before we can create a Docker image, we need to tell Docker what this image should look like. This is what the aptly named Dockerfile is for:</p><blockquote>A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.<br>- <a href="https://docs.docker.com/engine/reference/builder/">Docker reference documentation</a></blockquote><p>Here are the most important commands that we’ll be using to create our image:</p><ul><li>FROM initializes a new image based on an existing image. Alternatively, FROM scratch can be used.</li><li>COPY copies files from the build context (which is usually the directory in which the Dockerfile resides) into the image.</li><li>ENV sets persistent environment variables.</li><li>RUN executes one or more shell commands.</li></ul><p>Each command in a Dockerfile creates a new <em>layer</em> of the final image. I don’t want to go into the internals of Docker too much but for our purpose, it is desirable to keep the number of total layers as low as possible. If you are interested in how exactly layers work, I find that <a href="http://tuhrig.de/layering-of-docker-images/">this short article</a> gives a better explanation than the official Docker documentation</p><h4>Dockerfile Stages</h4><p>In some cases the files you deploy on a production machine will be pretty much identical to your source code repository. Languages such as PHP and Ruby are interpreted at runtime; they don’t need to be compiled before they can be executed.</p><p>Elixir, on the other hand, is a compiled language and always requires a dedicated build phase. During the build phase you need files and programs that are not necessary in order to <em>run</em> the application: Mix, Git and your source code all don’t need to be available on your production system.</p><p>This is where Docker’s multi-stage builds come in handy: <strong>You can set up environments for building your application that are not going to be part of your final deployment container.</strong></p><p>How do multi-stage builds work? Easy: Every FROM command in your Dockerfile initializes a new stage. Later stages can copy files from previous stages. The last stage becomes your final image while all other stages are discarded.</p><h4>Ready, Set …</h4><p>I have created a little Elixir application that does nothing but print out the current time. Let’s put this amazing work of software engineering into a Docker container!</p><pre>git clone <a href="https://github.com/wmnnd/elixir-docker-guide">https://github.com/wmnnd/elixir-docker-guide</a><br>cd elixir-docker-guide</pre><h4>The Build Stage</h4><p>Start by creating a file called Dockerfile in the project’s root folder.</p><p>The first step is to pick an existing image from Docker Hub for the build phase: Let’s go with Paul Schoenfelders’s awesome alpine-elixir image which ships with everything we need to compile an Elixir application.</p><pre>FROM bitwalker/alpine-elixir:1.5 as build</pre><p>Next, let’s copy our source code into the Docker container:</p><pre>COPY . .</pre><p>If you want to be more selective about what you copy from your source folder, you could also do something like this instead:</p><pre>COPY rel ./rel<br>COPY config ./config<br>COPY lib ./lib<br>COPY priv ./priv<br>COPY mix.exs .<br>COPY mix.lock .</pre><p>Being more explicit with the folders you want to copy, can be useful if you want to avoid issues with artifacts in the _build folder or previously downloaded dependencies.</p><p>Next, we want to fetch the application’s dependencies, compile it and build a Release with Distillery:</p><pre>RUN export MIX_ENV=prod &amp;&amp; \<br>    rm -Rf _build &amp;&amp; \<br>    mix deps.get &amp;&amp; \<br>    mix release</pre><p>The rm -Rf _build bit is not necessary if you are only building your image in an isolated CI environment or if you have chosen the more explicit way of copying your files mentioned above.</p><p>At this point, you can already try building an image from your Dockerfile. Call the following command and you should see the application being built by Mix and Distillery — inside a Docker container:</p><pre>docker build -t elixir-docker-guide .</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*8Iz5nDFctG4oRXJwdnYyGw.png" /><figcaption>Building our application in a Docker container. We can’t use the container yet but it’s working so far!</figcaption></figure><p>The -t parameter for docker build gives a name <em>or tag</em> to the newly created image. Like this, we can access it at a later point. We also need to specify the <em>build context</em>, i. e. the local folder from which to build the image. Since this is the current folder, we simply put . there.</p><h4>Finishing the Build Stage</h4><p>Distillery buries the .tar.gz archive of a Release deep in the _build folder. Since it is our goal to use the Release we’ve just built in our deployment image, let’s copy it to a more easily accessible location:</p><pre>RUN APP_NAME=&quot;clock&quot; &amp;&amp; \<br>    RELEASE_DIR=`ls -d _build/prod/rel/$APP_NAME/releases/*/` &amp;&amp; \<br>    mkdir /export &amp;&amp; \<br>    tar -xf &quot;$RELEASE_DIR/$APP_NAME.tar.gz&quot; -C /export</pre><p>While this snippet might seem a bit obscure at first, it’s actually pretty simple: It extracts the .tar.gz archive created by Distillery into a folder called /export .</p><h4>The Deployment Stage</h4><p>Now we initialize a second stage in our Dockerfile:</p><pre>FROM pentacent/alpine-erlang-base:latest</pre><p>The next step is to copy the compiled Release from the previous stage:</p><pre>COPY --from=build /export/ .</pre><p>It’s good practice to use a non-root user, even inside of a container. This is why we make Docker switch to the default user (which has already been created in alpine-erlang-base).</p><pre>USER default</pre><p>Finally, all we need to do is specify an ENTRYPOINT and a CMD for our image. ENTRYPOINT defines the application that is started when creating a container from our image; CMD specifies the default arguments for that application:</p><pre>ENTRYPOINT [&quot;/opt/app/bin/clock&quot;]<br>CMD [&quot;foreground&quot;]</pre><p>Let’s give it a try! Build and run the application:</p><pre>docker build -t elixir-docker-guide .<br>docker run -t elixir-docker-guide</pre><p>That wasn’t too hard, was it?</p><h3>The Full Recipe</h3><p>At this point you might want to “dockerize” your own application. For this purpose, I have created this annotated Dockerfile that you can use as a boilerplate. Just make sure to replace both instances of MY_APP_NAME with your actual application name:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76d091b6c03ac687555cf2ef6bec7204/href">https://medium.com/media/76d091b6c03ac687555cf2ef6bec7204/href</a></iframe><h3>From Here On Out</h3><h4>Phoenix</h4><p>You can also use this method for creating Docker images of Phoenix applications. Just make sure to include the steps to build and digest your assets in the build phase. With the default Brunch asset pipeline, this would look something like this:</p><pre>ENV MIX_ENV=prod<br>RUN apk update &amp;&amp; \<br>    apk add -u musl musl-dev musl-utils nodejs-npm build-base<br>RUN mix deps.get<br>RUN mix compile<br>RUN cd assets &amp;&amp; \<br>    npm install &amp;&amp; \<br>    node ./node_modules/brunch/bin/brunch b -p &amp;&amp; \<br>    cd .. &amp;&amp; \<br>    mix phx.digest<br>RUN mix release</pre><p>If you are using a more elaborate asset pipeline or if you want to take better advantage of Docker’s caching capabilities, you can even make this a dedicated Docker stage.</p><h4>Umbrella Applications</h4><p>There isn’t really anything special you need to do to build Docker images from umbrella applications. Distillery already takes care of bundling everything up nicely. If you’re using Phoenix as one of your child apps, just remember that you need to adjust the paths in the above example for building your assets.</p><h4>What About the Base Image?</h4><p>You might have noticed that I suggested using my very own pentacent/alpine-erlang-base image as the base of the production image. You could also use bitwalker/alpine-erlang but alpine-erlang-base gives you the smallest image size possible. I will write about choosing and creating a base image for Elixir deployment in a future article.</p><p><strong>I hope you enjoyed this guide! If you have any questions or suggestions, please let me know!</strong></p><p><em>This article is part of an ongoing series about developing and deploying Elixir applications to production. </em><strong><em>Make sure to sign up for my email list if you enjoyed this article.</em></strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F86f113%3Fas_embed%3Dtrue&amp;url=https%3A%2F%2Fupscri.be%2F86f113%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252F86f113%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb49c49863193cb20190f062dfe15a7c/href">https://medium.com/media/fb49c49863193cb20190f062dfe15a7c/href</a></iframe><h3>Credit Where Credit is Due</h3><p>I’d like to thank those who helped me create this guide:</p><ul><li><a href="https://bitwalker.org/">Paul Schoenfelder</a> for creating <a href="https://github.com/bitwalker/alpine-erlang/">alpine-erlang</a> on which inspired my alpine-erlang-base image and for his helpful feedback.</li><li><a href="https://github.com/cdegroot">Cees de Groot</a> and <a href="https://twitter.com/ThomasAthanas">Thomas Athanas</a> for their feedback and suggestions.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=982e2a16213c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[5 Videos from ElixirConf 2017 You Need to Watch (+Bonuses)]]></title>
            <link>https://pentacent.medium.com/5-great-videos-from-elixirconf-2017-4d985386b55b?source=rss-524783b33278------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d985386b55b</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[videos]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Philipp]]></dc:creator>
            <pubDate>Wed, 20 Sep 2017 12:01:02 GMT</pubDate>
            <atom:updated>2017-09-20T12:41:07.026Z</atom:updated>
            <content:encoded><![CDATA[<h4>From Beginner to Expert: Improve Your Elixir Skills With These Five Videos.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XNWl9BMW80_K7tIKm6s8Eg.jpeg" /><figcaption>Colorful Elixirs. (original photo CC0 by <a href="https://pixabay.com/en/neon-art-color-colorful-light-1596205/">JohnBello2015/Pixabay</a>)</figcaption></figure><p>The number of Elixir developers keeps growing steadily. And while Elixir hasn’t become the world’s number one programming language just <em>yet</em>, there is a constant stream of new developers who join the community. Naturally, this also means an ever-increasing number of participants for the two biggest annual Elixir conferences: <a href="http://www.elixirconf.eu/">ElixirConf EU</a> in spring and <a href="https://elixirconf.com/">ElixirConf US</a> in fall.</p><p>Developer conferences don’t always manage to keep their quality up to par with growing audiences. The ElixirConfs have been a brilliant exception so far, offering both approachable talks for newcomers and interesting presentations for veterans.</p><p>In this post I have picked five talks I found particularly rewarding — both for people new to Elixir and experienced “<a href="https://elixirforum.com/t/elixirists-or-alchemists/262/1">Alchemists</a>”.</p><h3>#1: Bryan Hunter: Elixir by the Bellyful</h3><p>You’ve heard that Elixir is great for concurrent, fast and reliable applications. Maybe you’ve heard about Erlang, functional programming, cheap VM processes and powerful metaprogramming. But what on earth are <em>OTP</em>, <em>Supervisors</em> and <em>GenServers</em>?</p><p>“Elixir by the Bellyful” is an amazing introduction that covers just about everything you need to know about the Elixir ecosystem. The talk is <em>packed</em> with information. Starting with a bit of Erlang history, Bryan delivers a broad sweep across all the reasons that makes developing in Elixir so enjoyable.</p><h4>Who is this talk for?</h4><p>Everyone who’s interested in Elixir and willing to invest 40 minutes. You will not regret watching this.</p><h4>About the speaker</h4><p><a href="https://twitter.com/bryan_hunter">Bryan</a> is a polyglot functional programming advocate who runs his own software consultancy, <a href="http://leanfp.com/">LeanFP,</a> in Nashville, TN.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fiswld-Rpi_g%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Diswld-Rpi_g&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fiswld-Rpi_g%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f538182b4766d2e1a5cdc5aab1e0013/href">https://medium.com/media/1f538182b4766d2e1a5cdc5aab1e0013/href</a></iframe><h3>#2 Darin Wilson: Thinking in Ecto</h3><p><a href="https://github.com/elixir-ecto/ecto">Ecto</a> is Elixir’s most popular database toolkit. Why <em>toolkit</em>, not <em>framework</em>? “Thinking in Ecto” answers this question and more:</p><ul><li>How is Ecto different from ORMs like ActiveRecord or jOOQ?</li><li>How does Ecto protect you from the infamous n+1 query problem?</li><li>Why is it great to represent database operations as data structures?</li></ul><p>This is the new go-to introduction to Ecto.</p><h4>Who is this talk for?</h4><p>Everyone who wants to use databases in Elixir. It’s also interesting for more experienced users, showing the power of <em>schemaless changesets</em>, Ecto.Multi and <em>embedded schemas</em>.</p><h4>About the speaker</h4><p><a href="https://twitter.com/darinwilson">Darin</a> has become a great advocate for Ecto in the community. Last year, he gave a talk called <a href="https://www.youtube.com/watch?v=QE7bpqpDoKo">Leveling Up With Ecto</a> — a direct predecessor to this year’s slightly more refined version. He is currently co-authoring the book “Programming in Ecto” together with Ecto core developer <a href="http://xn--meadows-jnsson-3pb/">Eric Meadows-Jönsson</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FYQxopjai0CU%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DYQxopjai0CU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FYQxopjai0CU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cd2c9d991f99a1040ce813eac342a8c/href">https://medium.com/media/0cd2c9d991f99a1040ce813eac342a8c/href</a></iframe><h3>#3 Griffin Byatt: Plugging the Security Holes in Your Phoenix Application</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FqC0vEVFUxBdh6%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fgandalf-qC0vEVFUxBdh6&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FqC0vEVFUxBdh6%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="248" frameborder="0" scrolling="no"><a href="https://medium.com/media/24cdd0e79c9e3d5d0ee8851ff8dd1ead/href">https://medium.com/media/24cdd0e79c9e3d5d0ee8851ff8dd1ead/href</a></iframe><blockquote>Is it secret? Is it safe?</blockquote><blockquote>– Gandalf, freelance security advisor</blockquote><p><a href="http://phoenixframework.org/">Phoenix</a> is productive, reliable, fast, sure. But is it secure? The reassuring answer is: <em>Mostly</em>, yes.</p><p>Out of the box, Phoenix offers good security but there are some common pitfalls you ought to be aware of. This talk gives an enlightening overview of potential security issues in Phoenix projects. Learn how to keep security in mind when writing code and configuration.</p><h4>Who is this talk for?</h4><p>Everyone who wants to deploy a Phoenix application should be aware of the potential issues that come with careless coding.</p><h4>About the speaker</h4><p><a href="https://twitter.com/griffinbyatt">Griffin</a> is a security consultant and developer, currently working for <a href="https://www.nccgroup.trust/">NCC Group</a>. He has developed a <a href="https://github.com/nccgroup/sobelow">static analysis tool for Phoenix applications</a> that can detect most of the potential issues highlighted in his talk.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fw3lKmFsmlvQ%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dw3lKmFsmlvQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fw3lKmFsmlvQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc2300f7cfec8946453916351d6e1ef8/href">https://medium.com/media/fc2300f7cfec8946453916351d6e1ef8/href</a></iframe><h3>#4 Jesse Anderson: Don’t Write Macros But Do Learn How They Work</h3><p>Metaprogramming is big in Elixir. It’s an incredibly powerful tool and lets you do things that are impossible in most other languages.</p><p>Why should you not use it then? Or should you? How do Macros even work? This talk answers these questions and more. It’s a great introduction and a good primer to start learning <a href="https://pragprog.com/book/cmelixir/metaprogramming-elixir">Metaprogramming Elixir</a>.</p><h4>Who is this talk for?</h4><p>Intermediate Elixirists who want to get a better understanding of Macros and the AST.</p><h4>About the speaker</h4><p><a href="https://twitter.com/jessejanderson">Jesse</a> has been active in the Elixir community for some time now and while this is his first ElixirConf talk, he has previously <a href="https://www.youtube.com/watch?v=4SCwubzqsVU">spoken about OTP</a> at Erlang &amp; Elixir Factory SF.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FBo48sQDb-hk%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DBo48sQDb-hk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FBo48sQDb-hk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2168cf01c7dd481a4c77b0460a1a9b66/href">https://medium.com/media/2168cf01c7dd481a4c77b0460a1a9b66/href</a></iframe><h3>#5 Andrew Bennett: Well Behaved Native Implemented Functions</h3><p>The Erlang virtual machine BEAM is great. And it’s fast. But not quite as fast as native code! This is where Native Implemented Functions (NIFs) come in: They allow integrating native code, written in languages like C, C++ or Rust, into Elixir and Erlang projects.</p><p>This talk isn’t quite a how-to for writing your own NIFs but it answers some interesting questions:</p><ul><li>What is the role of the different schedulers in BEAM?</li><li>How do you prevent native functions from blocking your application?</li><li>What are the different types of NIFs and their advantages?</li></ul><p>Even if you’re not planning to write your own NIFs anytime soon, this talk still provides great insight into some of the inner workings of the Erlang virtual machine.</p><h4>Who is this talk for?</h4><p>Experienced Elixir/Erlang developers who want to leverage the full performance of native code or get a better understanding of the BEAM platform.</p><h4>About the speaker</h4><p><a href="https://twitter.com/potatosaladx">Andrew</a> has been interested in NIFs for a while now. While researching them, he even came up with <a href="https://github.com/erlang/otp/pull/1480">a PR</a> for one of Erlang/OTP’s own built-in native functions.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FFYQcn9zcZVA%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DFYQcn9zcZVA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FFYQcn9zcZVA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c82da8973f5446ff487717e2a17012e/href">https://medium.com/media/6c82da8973f5446ff487717e2a17012e/href</a></iframe><h3>Bonus round!</h3><p>As promised in my <em>not-at-all clickbaity</em> title, I wanted to share some more videos from this year’s ElixirConf US. They aren’t quite as educational as the other videos I’ve shown so far but I really enjoyed them:</p><ul><li>The keynotes were great. <a href="https://www.youtube.com/watch?v=bd_EIWU9GzQ">Do</a> <a href="https://www.youtube.com/watch?v=Wa7_I_pc0yo">watch</a> <a href="https://www.youtube.com/watch?v=Wa7_I_pc0yo">them</a>.</li><li>Tim Mecklen <a href="https://www.youtube.com/watch?v=ARQD4BN_5ns">built an artificial pancreas for his wife</a> with Elixir and Nerves. Awesome!</li><li>James Smith <a href="https://www.youtube.com/watch?v=aLnSO6FQpHs">shows off a vehicle tracking device</a> that runs Elixir and Nerves. Is it the year of embedded Elixir yet?</li></ul><p><strong>Thanks for reading! </strong>This article is a little break from my ongoing series about developing and deploying Elixir applications to production. If you liked it, please let me know!</p><p><em>In this series I am sharing my experience of creating </em><a href="https://www.dblsqd.com/"><em>DBLSQD</em></a><em>, a release and update server written in Elixir. Check it out, there is a 60-day no-strings-attached free demo:</em></p><p><a href="https://www.dblsqd.com/">Publish &amp; Distribute Software - DBLSQD</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d985386b55b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Elixir Releases with Distillery — A (Pretty) Complete Guide]]></title>
            <link>https://medium.com/hackernoon/mastering-elixir-releases-with-distillery-a-pretty-complete-guide-497546f298bc?source=rss-524783b33278------2</link>
            <guid isPermaLink="false">https://medium.com/p/497546f298bc</guid>
            <category><![CDATA[elixir-releases]]></category>
            <category><![CDATA[distillery]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <dc:creator><![CDATA[Philipp]]></dc:creator>
            <pubDate>Tue, 12 Sep 2017 12:01:01 GMT</pubDate>
            <atom:updated>2019-05-22T08:36:29.586Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to create Elixir Releases. And some tips for Phoenix, Ecto, and umbrella applications.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gDw8nUbMrE6wM6t-SR6baQ.jpeg" /></figure><h3>A Quick Refresher and a Bit of History</h3><p>Releases have long been around in the Erlang/OTP world.</p><p>I have previously written about <a href="https://medium.com/@pentacent/elixir-releases-docker-the-basics-explained-4c800a5b990b">what Releases are and why they’re great in more detail</a>. Here is a little refresher: Releases are self-contained bundles of compiled Elixir/Erlang <a href="https://hackernoon.com/tagged/code">code</a> that can easily be deployed on <a href="https://hackernoon.com/tagged/remote">remot</a>e machines.</p><p>Naturally, most tools for creating Releases have so far come from the Erlang world. Erlang ships with systools and reltool, but their usage is rather complicated. So the community built <a href="https://github.com/erlware/relx">relx</a> as an easier-to-use alternative that has now become the de-facto standard for creating Releases from Erlang projects. Since those tools are all made to work with Erlang, they <em>can</em> work with Elixir projects but leave much to be desired.</p><p>In 2014, Paul aka <a href="http://bitwalker.org/">bitwalker</a> set out to make Elixir Releases easier and published exrm — built on top of relx with some Elixir-specific features. Due to limitations of relx, exrm was inflexible and never able to leverage the full potential of modular Elixir applications. So Paul decided to create a new Release manager, written from scratch in pure Elixir — and so <a href="https://github.com/bitwalker/distillery">Distillery</a> was born.</p><h3>Step-by-step guide</h3><p>In this section of the article, you’ll learn how to add Distillery to a project and build your first release step-by-step. Let’s go!</p><p>I have created a small application that prints out the current time every second. We can use it to build a Release:</p><pre>git clone <a href="https://github.com/wmnnd/distillery-demo.git">https://github.com/wmnnd/distillery-demo.git</a><br>cd distillery-demo</pre><h4>Installing Distillery &amp; Creating the First Release</h4><p>Before you can create your first Release, you need to install Distillery. Add {:distillery, &quot;~&gt; 1.5&quot;} to the list of dependencies in your project’s mix.exs and run mix deps.get.</p><p>Next, we initialize Distillery by calling mix release.init. This command creates a configuration file rel/config.exs. You could also manually create the file but it’s more convenient like this.</p><p>We will look at the contents of the configuration file in a bit. For now it’s enough to build our first Release. In order to do that, run mix release. Distillery will now create an output that looks something like this:</p><pre>==&gt; Assembling release..<br>==&gt; Building release clock:0.1.0 using environment dev<br>==&gt; You have set dev_mode to true, skipping archival phase<br>==&gt; Release successfully built!<br>    You can run it in one of the following ways:<br>      Interactive: _build/dev/rel/clock/bin/clock console<br>      Foreground: _build/dev/rel/clock/bin/clock foreground<br>      Daemon: _build/dev/rel/clock/bin/clock start</pre><p>Congratulations, you have just created your first Release! Run it with _build/dev/rel/clock/bin/clock foreground. As expected, the application now prints out the current time in one-second intervals. You can terminate the application by hitting Ctrl+C.</p><blockquote><strong>Note: There is currently a bug in Distillery that might crash the Release when starting it. In this case, run </strong><strong>mix clean &amp;&amp; mix compile and try running the application again.</strong></blockquote><p>In the call to start the application, you passed the foreground command at the end. This is the <em>boot command</em>. There are also other boot commands for starting an application: console launches an interactive iex session with your application and start creates a daemon process in the background.</p><h4>Release Profiles and Environments</h4><p>Let’s take a step back. You’re probably already familiar with Elixir’s build tool Mix. Mix supports multiple <em>environments</em>: dev, prod and test. Depending on the current environment, Mix can, for example, determine which dependencies or configuration files to include.</p><p>You might have noticed that when we created the Release, Distillery printed out this line: Building release clock:0.1.0 <strong>using environment dev</strong>. Distillery also has <em>environments — </em>but they are different from Mix environments. <strong>Distillery uses environments to allow for multiple configurations of the Release building process.</strong></p><p>Conveniently — or confusingly — Distillery environments are by default the same as the Mix environments. Let’s take a look at the auto-generated configuration file rel/config.exs in order to better understand how profiles work:</p><pre>use Mix.Releases.Config,<br>  default_release: :default,<br>  default_environment: Mix.env()</pre><p>We can see that default_environment is set to Mix.env(). This means that Distillery will match our Mix environment. You can override default environment with the --env switch. Here are some examples of how this could work:</p><pre>mix release<br>#Mix environment and Distillery environment are both <em>dev</em></pre><pre>MIX_ENV=prod mix release<br>#Mix environment and Distillery environment are both <em>prod</em></pre><pre>MIX_ENV=prod mix release --env=dev<br>#Mix environment is <em>prod</em>, Distillery environment is <em>dev</em></pre><p>The configuration file also specifies that default_release is :default. This is because you can actually define more than just one Release for your project. With the :default setting, Distillery automatically picks the <em>first </em>Release defined in rel/config.exs. If you want to override the default Release, you can specify this with the --name flag.</p><p><strong>The combination of an environment with a specific Release name is called a <em>profile</em>.</strong> You can pick a specific profile with the --profile=name:env flag.</p><p>In the next section we’re going to take a look at how we can use multiple environments to configure Distillery.</p><h4>Release Build Configuration</h4><p>Our Distillery configuration includes two environments (dev and prod) on top of one Release named like our project (clock):</p><pre>environment :dev do<br>  set dev_mode: true<br>  set include_erts: false<br>  set cookie: :&quot;W?cN_`G&lt;&gt;ayUI&amp;ku{&lt;$3w7J&lt;^nUBRBu[F[…]&quot;<br>end</pre><pre>environment :prod do<br>  set include_erts: true<br>  set include_src: false<br>  set cookie: &quot;^_`fz{dk|`w.n3Z%T,n=F&gt;ezazFk.1ci5}[…]&quot;<br>end</pre><pre>release :clock do<br>  set version: current_version(:clock)<br>  set applications: [<br>    :runtime_tools<br>  ]<br>end</pre><p>Here is what the options included in the configuration file do:</p><ul><li>dev_mode: true/false: If true, bytecode, assets and other files are not copied to the Release folder. Instead, Distillery creates symlinks. This makes creating the Release faster and is great for testing purposes.</li><li>include_erts: true/false: If true, Distillery includes the Erlang Runtime ERTS. This is not necessary on your development machine but recommended for making self-contained Releases.<br>If you want to deploy your Release to a machine with a different operating system or processor architecture, you can specify the path to a cross-compiled version of ERTS instead of true/false.</li><li>cookie: $STRING: Distillery can set the <a href="http://erlang.org/doc/reference_manual/distributed.html">Erlang cookie</a>. The auto-generated configuration includes random Erlang cookies. <strong>Don’t include a hard-coded cookie if you want to commit your configuration to a Git repository! </strong>Later in this post we’ll look into a better way to configure the magic cookie.</li><li>include_src: true/false: This option is available for <a href="https://twitter.com/gotbones/status/906270692970295296">backwards compatibility</a> with the Erlang Release tool relx. If true, Erlang source code from your project and dependencies is included in the Release. Elixir code is never included, so most of the time, you can ignore this option.</li><li>version: $VERSION_STRING: Sets the version of the Release. You can use the function current_version/1 to extract the version from your project’s mix.exs.</li><li>applications: [:app_name]: Here you can list additional applications that you want to include. By default, Distillery includes :runtime_tools which is part of the Erlang standard library and enables certain debugging features.</li></ul><h4>Lifecycle Scripts aka Hooks</h4><p>It is possible to add shell scripts to your Release to run at certain points of your application’s lifecycle. These scripts are also referred to as <em>hooks</em> and can be configured in your rel/config.exs. Simply add them to an env or release section like this:</p><pre>environment :prod do<br>  set pre_start_hook: &quot;rel/hooks/pre_start.sh&quot;<br>end</pre><p>Notice that the paths are relative to your project directory and not relative to the rel directory. The most interesting hook is certainly pre_start, but there is also post_start , pre/post_configure , pre/post_stop, and pre/post_upgrade. You can currently only configure one script per hook with Distillery although Erlang would theoretically allow for more.</p><h4>Umbrella Apps</h4><p>Distillery is also great when working with Umbrella apps! There are really only two things you need to pay attention to:</p><ol><li>Include the names of your child applications in the applications list of the Release configuration.</li><li>You can choose to take the Release version number from any of the child applications by using current_version(:my_child_app).</li></ol><p>As mentioned before, it’s also possible to define more than one Release in your rel/config.exs and thus specify multiple Release profiles. This can be useful if you want to have Releases that only include certain child applications from your umbrella project.</p><h3>Runtime Configuration With Releases</h3><p>Configuring Elixir applications — especially when it comes to third-party libraries, can be a tricky affair. Ecto core-developer Michał recently <a href="http://michal.muskala.eu/2017/07/30/configuring-elixir-libraries.html">voiced his concern</a> about the confusing status quo.</p><h4>The Problem with Mix Configuration</h4><p>The first thing you need to remember is that configuration in Mix configuration files is evaluated at compile-time. This is not a problem when you run your application from source code. <strong>But when Distillery builds a Release, all configuration files are evaluated at build-time.</strong></p><p>If you have anything like foo: System.get_env(&quot;BAR_VAR&quot;) in a Mix.Config file, you’re out of luck. foo will take on the value of the BAR_VAR environment variable from your build system. When you launch the application on another machine, the value won’t be updated.</p><p>Fortunately, there are ways around this. The easiest one is a little trick employed by Distillery:</p><h4>A Simple Solution: REPLACE_OS_VARS</h4><p>Distillery has a special way of injecting environment variables into Mix configuration. Simply use a string like &quot;${BAR_VAR}&quot; in your config file and you’re good to go. Distillery will replace it with the corresponding environment variable value when your application starts. All you need to do for this to work is setting the environment variable REPLACE_OS_VARS=true when launching your release. This makes it very easy to write configuration like this:</p><pre>config :my_app,<br>  foo: &quot;${BAR_VAR}&quot;</pre><p><strong>This method is great for initializing the Erlang cookie.</strong> Use set cookie: &quot;${ERLANG_COOKIE}&quot; in your rel/config.exs to set the cookie from an environment variable.</p><p>REPLACE_OS_VARS doesn’t come without problems. Most importantly, this method only works with strings. If you try something like String.to_integer(&quot;${BAR}&quot;), you’ll get an error message because the compiler evaluates the expression before Distillery gets a chance to replace the special string.</p><p>Fortunately, many libraries now support a more flexible approach to configuration.</p><h4>Ecto</h4><p>Ecto has introduced a new way of dynamically configuring application dependencies. Their solution: A callback! You can dynamically configure Ecto with the Ecto.Repo.init/2 callback.</p><p>This callback takes type and the config as arguments. We can safely ignore type (which is either :supervisor or :dry_run, depending on the context of the call). config is the configuration Keyword list from your Mix configuration. This is convenient because it allows you to set default values in your Mix configuration and then override them with environment variables.</p><p>The callback has to return {:ok, config} or :ignore. Here is a simple example with type conversion:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e65163efc81d1893623eb2621b28d8e7/href">https://medium.com/media/e65163efc81d1893623eb2621b28d8e7/href</a></iframe><p>Instead of writing your own code for parsing environment variables, you might find a third-party application such as <a href="https://github.com/vinli/atmo_ex">Atmo</a> or <a href="https://github.com/Nebo15/confex">Confex</a> useful. I haven’t tried them yet but they seem to have some nice convenience features.</p><h4>Phoenix</h4><p>Creating a Release from a Phoenix application is easy.</p><p>Before you create a production release, make sure your assets are built and digested:</p><pre>./node_modules/brunch/bin/brunch b -p<br>MIX_ENV=prod mix phx.digest</pre><p>With version 1.3, Phoenix has also adapted the Ecto’s callback-style configuration method. This makes it very easy to flexibly configure Phoenix.Endpoint:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1640d26e22ebc2441c6c18f82da41f7d/href">https://medium.com/media/1640d26e22ebc2441c6c18f82da41f7d/href</a></iframe><h3>Upgrading Releases</h3><p>No (pretty) complete guide to Elixir Releases would be (pretty) complete without mentioning one of their coolest features: Hot updates.</p><p>You probably know that Erlang and Elixir are compiled to bytecode which is then executed by the <em>BEAM virtual machine</em>. And thanks to BEAM, we can upgrade an application without ever stopping it.</p><h4>Creating an Upgrade Release</h4><p>Release upgrades internally use .appup files to determine which modules should be upgraded and how. Fortunately for us, Distillery creates them automatically when we pass it the --upgrade parameter. <strong>Make sure the previous Release is still available in your </strong><strong>/_build folder when building an upgrade. </strong>Otherwise, Distillery wouldn’t be able to determine which modules need updating.</p><h4>Creating a Production Release</h4><p>Only Releases that include ERTS can use hot upgrading. So, let’s start by creating a production Release of our clock application:</p><pre><strong>#In Terminal 1</strong><br>MIX_ENV=prod mix deps.get<br>MIX_ENV=prod mix release</pre><p>Now open up a second terminal window and launch the Release:</p><pre><strong>#In Terminal 2<br></strong>cd distillery-clock-demo<br>_build/prod/rel/clock/bin/clock foreground.</pre><p>Again, you should see the current time printed out every second.</p><h4>Creating an Upgrade Release</h4><p>Let’s create a new version of clock. Since it prints out its current version number together with the current time, all you need to do to get a different output is change the version in mix.exs. Currently it’s set to 0.1.0. Let’s change it to 0.2.0.</p><p>Then, create an upgrade Release by passing the --upgrade flag to mix.release.</p><pre><strong>#In Terminal 1</strong><br>MIX_ENV=prod mix release --upgrade</pre><h4>We’ll Do It Live!</h4><p>After the Release upgrade has been created, the Clock application in the second terminal hasn’t changed. Yet. Still in the first terminal, run the following command:</p><pre><strong>#In Terminal 1</strong><br>_build/prod/rel/clock/bin/clock upgrade 0.2.0</pre><p>In the second terminal, the Clock output will now change and start to also print out the current date. How neat!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*97Zic3IhrNIDRJ3o1eqiSw.png" /><figcaption>Updating an application wile it’s running!</figcaption></figure><h3>Are You a Master of Elixir Releases Yet?</h3><p>I hope that after reading this guide, you now have a better understanding of Elixir Releases . I certainly do after writing it!</p><p>If you think I missed an aspect of Elixir Releases that should be part of this (pretty) complete guide, please let me know and I will try to add it!</p><p><strong>This article is part of an ongoing series about developing and deploying Elixir applications to production.</strong> Upcoming articles will cover deployment strategies and building Docker images.</p><p><em>In this series I am sharing my experience from creating </em><a href="https://www.dblsqd.com/"><em>DBLSQD</em></a><em>, a release and update server written in Elixir. Check it out, there is a 60-day no-strings-attached free demo:</em></p><p><a href="https://www.dblsqd.com/">Publish &amp; Distribute Software - DBLSQD</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=497546f298bc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/mastering-elixir-releases-with-distillery-a-pretty-complete-guide-497546f298bc">Mastering Elixir Releases with Distillery — A (Pretty) Complete Guide</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>