<?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 Richard Miles on Medium]]></title>
        <description><![CDATA[Stories by Richard Miles on Medium]]></description>
        <link>https://medium.com/@richlloydmiles?source=rss-5abaf1f426b8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Zie6tUxZc0TVDddiPMUZTQ.png</url>
            <title>Stories by Richard Miles on Medium</title>
            <link>https://medium.com/@richlloydmiles?source=rss-5abaf1f426b8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:10:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@richlloydmiles/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to run serverless step functions locally]]></title>
            <link>https://richlloydmiles.medium.com/how-to-run-serverless-step-functions-offline-26b7b994d2b5?source=rss-5abaf1f426b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/26b7b994d2b5</guid>
            <category><![CDATA[low-code]]></category>
            <category><![CDATA[step-functions]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[serverless]]></category>
            <dc:creator><![CDATA[Richard Miles]]></dc:creator>
            <pubDate>Mon, 06 Sep 2021 21:13:57 GMT</pubDate>
            <atom:updated>2021-09-06T21:14:54.441Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JavkJ8ygbPaXrJeyADDoQw.jpeg" /><figcaption><a href="https://unsplash.com/photos/gDlpMyInsak?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditShareLink">Wim Arys on Unsplash</a></figcaption></figure><p>In the following article we will be looking at how to set up serverless step functions locally. As well as look at some state machine flow examples.</p><p>TLDR; code examples can be found here— <a href="https://github.com/richlloydmiles/serverless-step-functions-local-example">https://github.com/richlloydmiles/serverless-step-functions-local-example</a></p><p>I’ll assume that you went ahead and clicked on the TLDR link — however, if this still doesn’t help give you all of the information about how step functions work locally, continue reading.</p><h3>Getting Setup</h3><p>I’m going to assume that you have cloned the repo above and run the commands to get yourself up and running, namely: npm install and npm run start</p><p>You should see something like this in your terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WnHq27ZIa-nxJKGrFkTsDQ.png" /></figure><p>By default the serverless framework will look at our serverless.yml file to give us some instructions about how the serverless app should run.</p><p>The default serverless.yml file has some basic serverless configuration entries including plugin and function definitions, but where it really gets interesting is the stepFunctions: section.</p><p>Here you will see a couple of things:</p><ul><li>stateMachines: — this is the key where we define a list of state machines (step functions are run in the context of a state machine). In our example we have defined a single stateMachine called WaitMachine . Which in itself will have its own set of definitions and will specify step functions to run.</li><li>The Comment entry is pretty self explanatory.</li><li>The StartAt is similarly self-explanatory with the exception that the value of this needs to reference a state found in the States: definition, (if not blindingly obvious, this is the first state (or step function) that will be executed in our state machine by default).</li><li>States: Are a list of states that have key / value pairs based on their functions (these are what could be considered our “step functions”).</li></ul><p>There are various types of states.</p><h3>Task State</h3><ul><li>When referencing a <strong>Task </strong>type, the state needs to contain an ARN for that resource in the Resource definition (usually a lambda function task). By default, serverless version 2.5 and up supports the Fn::GetAtt shorthand references for resources within your serverless configuration.</li></ul><blockquote><strong>Side note:</strong> when running your state machine in the real world (e.g. not locally), this is all you should need, however, the severless-step-functions-local plugin requires that your specify these <strong>Task</strong> resource arn’s in the custom configuration, so that it knows what to reference when executing your state machine locally.</blockquote><blockquote>You will see this in the TaskResourceMapping definition in the stepFunctionsLocal custom configuration section. e.g.</blockquote><pre>TaskResourceMapping:</pre><pre>  FirstState: arn:aws:lambda:us-east-1:101010101010:function:hello</pre><pre>  FinalState: arn:aws:lambda:us-east-1:101010101010:function:world</pre><blockquote>The region (us-east-1) and AWS account ID (101010101010) are local values that are irrelevant when using this in the real world.</blockquote><p>As we continue to go through our States: definition in the stepFunctions: section of the serverless.yml you will notice a Next: wait_using_secondor End: true. This tells our state machine whether or not to continue to the next step (usually based on some condition), or to end the execution of the state machine.</p><h3>Wait State</h3><p>The next thing you’ll notice is that the wait_using_seconds state does not specify a resource arn, this is because it is a baked in state whose purpose is to wait for a specified amount of time (in this case 10 seconds), before moving onto the next state (FinalState) or ending. When you execute this particular example locally you should see ‘<em>hello</em>’ being logged to the console, followed by ‘<em>world</em>’ logged 10 seconds later.</p><h3>Parallel State</h3><p>If you load up (or replace the serverless.yml file with) the serverless-parallel.yml file you will notice the addition of the Type: Parallel state (FirstState). This state also does not specify a resource arn(much like the wait state), but does have a definition for Branches: . This allows us to run multiple independant states (or step functions) at the same time, that can have completely independent flows that branch off at this point. In our case they are just running two parallel task states second_task and FinalState.</p><h3>Choice State</h3><p>Finally, if you look at the serverless-choice.yml file you will see a new type of state called Choice , this allows us to conditionally run step functions based on a set of conditions, which in our case is an object key returned by the response in the FirstState lambda function calledfoo,which conditionally runs the next step based off its value (whether it is equal to 1 or 2 ) This can be found in the hello function contained in handler.js .</p><pre>module.exports.hello = async event =&gt; {</pre><pre>  console.log(&#39;hello&#39;)</pre><pre>  return { foo: 1 }</pre><pre>}</pre><p>There are a few more Types of state functions that can be used found in the offical aws documentation— <a href="https://docs.aws.amazon.com/step-functions/latest/dg/concepts-states.html">https://docs.aws.amazon.com/step-functions/latest/dg/concepts-states.html</a></p><p>with an infite amount of combinitions in which they can be used.</p><h3>Executing our state machine</h3><p>The final piece in the puzzle would be how we go about actually triggering the execution of our state machine.</p><p>In our case the startSF function in handler.js is where this happens:</p><pre>const AWS = require(&#39;aws-sdk&#39;)</pre><pre>module.exports.startSF = (event, context, callback) =&gt; {</pre><pre>  const stepFunctions = new AWS.StepFunctions({ endpoint: &#39;http://localhost:8083&#39; })</pre><pre>  const stateMachineArn =     process.env.OFFLINE_STEP_FUNCTIONS_ARN_WaitMachine</pre><pre>  const params = {</pre><pre>    stateMachineArn</pre><pre>  }</pre><pre>  return stepFunctions.startExecution(params).promise().then(() =&gt; {</pre><pre>    callback(null, `Your state machine ${stateMachineArn} executed   successfully`)</pre><pre>  }).catch(error =&gt; {</pre><pre>    callback(error.message);</pre><pre>  })</pre><pre>}</pre><p>Let’s go through the main parts of this.</p><pre>const stepFunctions = new AWS.StepFunctions({ endpoint: &#39;http://localhost:8083&#39; })</pre><p>http://localhost:8083 is where the underlying state machine will be run locally. If you were running this in the real world you would obviously run this in a different way (no using your local port)</p><p>Next, the environment variable</p><pre>OFFLINE_STEP_FUNCTIONS_ARN_WaitMachine</pre><p>is automatically added to the context of our local serverless execution. With a base value of:</p><pre>OFFLINE_STEP_FUNCTIONS_ARN_</pre><p>and the suffix specifying the name of the state machine that we want to start executing:</p><pre>WaitMachine</pre><p>we then execute our state machine, calling it with:</p><pre>stepFunctions.startExecution</pre><p>In order to actually call this lambda function</p><pre>startSF()</pre><p>we need to invoke it in some way, in our case we have chosen to invoke it via an http endpoint within our existing serverless context:</p><pre>startSF:</pre><pre>  handler: handler.startSF</pre><pre>  events:</pre><pre>  - http:</pre><pre>    path: hello</pre><pre>    method: GET</pre><p>You should be able to fire this by hitting the following in your web browser or postman:</p><pre>http://localhost:3000/local-dev/hello</pre><h3>Error handling</h3><p>If you get the following error when trying to start your offline server (which I suspect you will be after making any changes to the serverless state machine definitions):</p><pre>StateMachineAlreadyExists: State Machine Already Exists: &#39;arn:aws:states:us-east-1:101010101010:stateMachine:WaitMachine&#39;</pre><p>You can delete your state machine manually by running the following after stopping the serverless offline server.:</p><pre>npm run delete</pre><p>You should be able to run npm run start successfully afterwards.</p><h3>Conclusion</h3><p>In my opinion step functions are the future of AWS serverless infrustucture, especially with the rising popularity of the low-code no code approaches - (<a href="https://aws.amazon.com/blogs/aws/new-aws-step-functions-workflow-studio-a-low-code-visual-tool-for-building-state-machines/">https://aws.amazon.com/blogs/aws/new-aws-step-functions-workflow-studio-a-low-code-visual-tool-for-building-state-machines/</a>. Having a low overhead way to test this is almost a nessesity, making the use of local step functions an integral part of the serverless journey.</p><p>That’s all folks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26b7b994d2b5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An Alternative to Integration Testing With Jest and Sequelize Transactions]]></title>
            <link>https://richlloydmiles.medium.com/an-alternative-to-integration-testing-with-jest-and-sequelize-transactions-7a9b703a562c?source=rss-5abaf1f426b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a9b703a562c</guid>
            <category><![CDATA[sequelize]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[jest]]></category>
            <dc:creator><![CDATA[Richard Miles]]></dc:creator>
            <pubDate>Thu, 03 Dec 2020 08:54:06 GMT</pubDate>
            <atom:updated>2020-12-05T00:25:04.003Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v-BxPh-SyBy6SI5y8lAn3A.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@testalizeme?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Testalize</a> on <a href="https://unsplash.com/s/photos/testing-code?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>An integration test is one that is written to cover multiple modules working as a group. For the most part this would mean testing that our code works as expected when making real queries to a database (e.g not mocking database connections and function calls — in the case of unit tests).</p><h3>The problem</h3><p>The usual approach I have taken in the past for integration testing is to create a test database that I migrate and seed during the lifecycle of each running test. This is probably fine for the first couple of tests, but becomes exponetially slower to run as the number of database migrations and seeds needed grows.</p><p>A shortcut that I have taken in the past is to have an initial seed that works for all tests. Although this does speed up the time it takes to run the tests, it fails to isolate each test result from the rest of the test data, which means more often then not, you will constantly be updating tests and seed data to get new tests to run as expected. The worst part about all of this is that is almost impossible to actually know what the state of your database is going to be when a particular test is run, especially for tests that are run later in the test lifecycle.</p><p>Before we talk about the solution, we should probably talk a bit about transactions in <a href="https://sequelize.org/">sequelize</a>.</p><h3>Database transactions</h3><p>If you didn’t know, a transaction is basically a way in which we can defer writing to our database permintantly, but still visualise the effects any changes to the database would have, without actually commiting them. There are a couple of good reasons why we would want to use transactions for pretty much everything database query related, but the main one being that we can group individual queries together, so that if one of the queries fail - all of them are rolled back. This is super useful when the effects of different queries rely on each other e.g:</p><pre>const someFunction = async () =&gt; {<br>    const transaction = await sequelize.transaction();<br>    try {<br>        await sequelize.query(&quot;INSERT INTO Persons (FirstName,LastName) VALUES (&#39;John&#39;,&#39;Smith&#39;)&quot;, { transaction });<br>        const result = await sequelize.query(&quot;SELECT * FROM Persons&quot;, { transaction })<br>        await transaction.commit();<br>        return result<br>    } catch (error) {<br>        await transaction.rollback();<br>        throw new Error(error)<br>    }<br>}</pre><p>In the above example we are adding a new Person to the Persons table and then querying all of the records in Persons. The <strong>select</strong> statement in this case relies on the success of the <strong>insert </strong>and visa versa, and so if anything fails, then everything is rolled back, and nothing is actually added to the database.</p><h3>The proposal</h3><p>In light of the fact that we can still get a snapshot of our database even if the changes haven’t been commited perminently, we see there is an oppertunity to test the results of our queries without actually commiting them perminantly to the database and then rolling them back after every test.</p><p>What we would need for this is a <strong>single</strong> seed test database that includes everything we need to run our tests, as well as a way to make sure we always rollback our commits.</p><h3>Mocking the commit function</h3><p>One way we can do this is by mocking our transaction’s commit function as follows:</p><pre>// someModule.test.js<br>const { sequelize } = require(&#39;./db&#39;)</pre><pre>jest.spyOn(sequelize, &#39;transaction&#39;);</pre><pre>beforeEach(() =&gt; {<br>    jest.clearAllMocks()<br>})</pre><pre>describe(&#39;db connection&#39;, () =&gt; {<br>    test(&#39;test db connection&#39;, async () =&gt; {<br>        const transaction = await sequelize.transaction();<br>        await sequelize.query(&quot;INSERT INTO Persons (FirstName,LastName) VALUES (&#39;Robert&#39;,&#39;Hope&#39;)&quot;, { transaction });<br>        transaction.commit = transaction.connection.rollback<br>        sequelize.transaction.mockImplementation(() =&gt; Promise.resolve(transaction));<br>        console.log(await someFunction())<br>    })<br>})</pre><p>Let’s break this down a bit:</p><ul><li>Firstly we are importing our sequelize connection instance</li><li>Next we want to spy on the transaction method of the sequelize instance (this is a way to help partially mock specific methods)</li><li>Then we want to make sure our mocks are reset after every test is run, mainly because we want a new mock transaction for each test</li><li>Within the test itself we want to create an actual transaction (line 12)</li><li>We can use this transaction to make any additional database calls within our test (line 13) (which will also be rolled back), before the function being tested is run. This will all conviently be added to the same single transction</li><li>We then want to reassign the commit method on this transaction to be the connection.rollback method (this is just the usual rollback method)</li><li>Finally we want to create a mock of our transaction method in our tested function which instead uses the transaction we have just created (with the reassigned commit method).</li><li>Now everytime we call the commit method within our tested function it will rollback the results, while we can still visualise the queries effects with in the database.</li></ul><h3>Conclusion</h3><p>Using this process means that we can test the actual integrated effects of our queries on the database, without changing it in anyway for every single test.</p><p>This not only speeds up the time it takes to run our integration tests, but I believe could be a viable solution to replace traditional database integration tests all together.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a9b703a562c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Calculate the distance between two points on Earth using Javascript]]></title>
            <link>https://richlloydmiles.medium.com/calculate-the-distance-between-two-points-on-earth-using-javascript-38e12c9a0f52?source=rss-5abaf1f426b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/38e12c9a0f52</guid>
            <category><![CDATA[trigonometry]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Richard Miles]]></dc:creator>
            <pubDate>Tue, 10 Mar 2020 06:22:18 GMT</pubDate>
            <atom:updated>2020-03-10T06:22:18.847Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vVsHrNKKmHyPmJNtXZc5nA.jpeg" /></figure><h3>The distance on a plane</h3><p>Given the X and Y position on a flat plane we can calculate the distance between two points using the <a href="https://en.wikipedia.org/wiki/Pythagorean_theorem">Pythagorean theorem</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jDMQTWe8YFiqghnBtpUMug.png" /></figure><p>Which visualised as an equation looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qHZ1Nv0monsiLYRUMuNtUg.png" /></figure><p>Let’s turn this into some code:</p><p>Firstly, javascript provides us with a <a href="https://www.w3schools.com/js/js_math.asp">Math object</a>, which is a core utility library.</p><p>Let’s start with the inner most part and work our way out:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*uZcIT6aTD0t9q8Rj1LuEaA.png" /></figure><p>What we actually need here is a difference calculation, which as we know will be the absolute value of the result. e.g:</p><p>The difference between 5 and 20 is 15, we can calculate this with</p><pre>abs(20–5) = 15 or abs(5-20) = 15</pre><p>Next we need to square the result with the <a href="https://www.w3schools.com/jsref/jsref_pow.asp">Math.pow</a> function:</p><pre>Math.pow(result, 2);</pre><p>Then we need to <strong>add</strong> the x and y resulting equations and <strong>square root</strong> the result using the <a href="https://www.w3schools.com/jsref/jsref_sqrt.asp">Math.sqrt</a>. The entire equation in some pseudo Javascript would look at follows:</p><pre>Math.sqrt(Math.pow(Math.abs(x1 - x2)) + Math.pow(Math.abs(y1 - y2)))</pre><p>This is straight forward enough on a flat plane, however, this becomes quite inaccurate when plotting this on the earth which is an essentially a sphere (oblate spheroid).</p><h3>The distance on a sphere</h3><p>In order to do this more accurately we need <strong>two</strong> additional equations.</p><p>First, we need a way to calculate the angle between the two points from the centre of the sphere, followed by an equation to calculate the distance between two points given the central angle (the result of the former equation). Fortunately we have mathematical formulas to do both.</p><p>Let’s begin with calculating the angle between two points from the centre (also known as the central subtended angle).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9r05LMmAVQK75jn73toLZg.png" /><figcaption><a href="https://en.wikipedia.org/wiki/Great-circle_distance">https://en.wikipedia.org/wiki/Great-circle_distance</a></figcaption></figure><ul><li><strong>Δ (delta)</strong>representing the<strong> </strong>difference</li><li><strong>σ </strong>being the central subtended angle and the result we are looking for</li><li><strong>arcos </strong>or the inverse cosine (cos to the power of -1) is represented by<strong> </strong><a href="https://www.w3schools.com/jsref/jsref_acos.asp">Math.acos</a> in Javascript</li><li><strong>Φ</strong> representing the latitude values and<strong> λ</strong> the longitude.</li></ul><p>Lets brake this equation down into some pseudo code as we did before:</p><p>Before we begin, we realise that we need to deal with <a href="https://en.wikipedia.org/wiki/Radian">radians</a> as opposed to degrees, as this is a far more accurate way to measure angles, and more importantly, Javascript trigonometry does not work in degrees, but radians.</p><p>Because of this we will need the following conversion functions:</p><pre>const degreesToRadians = degrees =&gt; degrees * (Math.PI / 180)<br>const radiansToDegrees = radians =&gt; radians * (180 / Math.PI)</pre><p>Then all we need to do is put the rest of the equation into a function making use of Javascript’s Math trigonometry utilities.</p><p>The function will look something like this:</p><pre>const centralSubtendedAngle = (locationX, locationY) =&gt; {<br>  const locationXLatRadians = degreesToRadians(locationX.latitude)<br>  const locationYLatRadians = degreesToRadians(locationY.latitude)</pre><pre>return radiansToDegrees(<br>    Math.acos(<br>      Math.sin(locationXLatRadians) * Math.sin(locationYLatRadians) +<br>        Math.cos(locationXLatRadians) *<br>          Math.cos(locationYLatRadians) *<br>          Math.cos(<br>            degreesToRadians(<br>              Math.abs(locationX.longitude - locationY.longitude)<br>            )<br>       )<br>    )<br>  )<br>}</pre><p>The next thing we need to calculate is the <strong>distance </strong>of the arc the <strong>centralSubtendedAngle —</strong> this is known as the “Great circle distance”</p><p>This can be described as followed:</p><pre>2 * PI * {radius of the earth} * ({central subtended angle} / 360)</pre><p>The earth is roughly considered to have a radius of 6371kms</p><p>We can then turn this into a piece of Javascript:</p><pre>const earthRadius = 6371</pre><pre>const greatCircleDistance = angle =&gt; 2 * Math.PI * earthRadius * (angle / 360)</pre><pre>const distanceBetweenLocations = (locationX, locationY) =&gt;<br>  greatCircleDistance(centralSubtendedAngle(locationX, locationY))</pre><p>Calling <strong>distanceBetweenLocations </strong>is as follows:</p><pre>const NewYork = {latitude: 40.7128, longitude: 74.0060}<br>const Sydney = {latitude: -33.8688, longitude: -151.2093}</pre><pre>console.log(distanceBetweenLocations(NewYork, Sydney))</pre><p>A final note here — because of the distortion of the earth this method is not 100% accurate, however it is usually only off by less than 0.05%</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38e12c9a0f52" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mocking in PHPUnit]]></title>
            <link>https://medium.com/nona-web/mocking-in-phpunit-802f91112645?source=rss-5abaf1f426b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/802f91112645</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Richard Miles]]></dc:creator>
            <pubDate>Mon, 19 Aug 2019 14:45:27 GMT</pubDate>
            <atom:updated>2019-08-19T14:45:27.768Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dh41JNtqX9421L27HEayrQ.jpeg" /></figure><h4>A practical guide to mocking in PHPUnit</h4><h3><strong>First, what is a mock?</strong></h3><p>A mock is a piece of dummy code that helps your tests run in a way that isolates specific functionality.</p><blockquote>e.g. we can mock out a function to return values in a way that makes a related piece of functionality either pass or fail a test.</blockquote><p>An example of a mocked function would be a mimicked response from a database call with dummy data that matches what we would expect the database call returned data to look like.</p><p>This is useful in unit testing as we don’t actually want to call a database, but rather test a specific piece of functionality that would interact with an expected response from a database.</p><h3>When should we use mocking?</h3><p>For starters, some common elements that you might want to mock include:</p><ul><li>Database queries</li><li>3rd party libraries</li><li>http requests</li><li>Anything tightly coupled with the underlying hardware of the operating system (e.g. Date time related or reading and writing to a file on disk)</li><li>Functions with unpredictable results (random numbers)</li></ul><p>The common thread here is that we don’t necessarily want to test anything that is outside of the scope of the individual functions we write. However, it’s worth mentioning that sometimes you do want to test code that has been integrated with one of these “external” services, making sure that it does in fact work as expected. This falls into the realm of an <a href="https://en.wikipedia.org/wiki/Integration_testing"><strong>integration test</strong></a><strong>. </strong>An integration test is basically asserting that multiple key components work together as expected, e.g. your code works as expected using actual database calls.</p><p>Here I have chosen to focus specifically on<strong> unit testing, </strong><a href="https://medium.com/nona-web/an-introduction-to-assert-with-phpunit-dbdec566a6de">which follows on from this intro to assert with PHP Unit</a> — focusing on testing a single “unit” of functionality.</p><h3>Jumping into the code</h3><p>For our example I am going to create a “person” class that can do specific person related things, one of those things is display the greeting message “Hello World”.</p><pre>&lt;?php</pre><pre>class Person<br>{<br>    public function greeting()<br>    {<br>        return &#39;Hello World&#39;;<br>    }<br>}</pre><p>Good stuff, so now we can create a new person and tell them to greet the world.</p><pre>$person = new Person();<br>echo $person-&gt;greeting();</pre><p>We can easily transform this into a basic test assertion:</p><pre>require __DIR__ . &#39;/Person.php&#39;;</pre><pre>use PHPUnit\Framework\TestCase;</pre><pre>class MockPerson extends TestCase<br>{<br>    public function test_greeting()<br>    {<br>        $test = new Person();<br>        $this-&gt;assertEquals(&#39;Hello World&#39;, $test-&gt;greeting());<br>    }<br>}</pre><p>Now I want to be able to query a list of users from a database and depending on the ID passed into my <strong>greeting </strong>function, return the corresponding name of that person in the database.</p><p>The database class would probably look something like this:</p><pre>&lt;?php</pre><pre>&lt;?php</pre><pre>class Database<br>{<br>    public function getPersonByID($id)<br>    {<br>        // do some stuff in the db to get a person by their ID<br>        return sql(&quot;select * from person where id = $id limit 1;&quot;)[0];<br>    }<br>}</pre><p>We can now pull this into our person class and use it:</p><pre>&lt;?php</pre><pre>class Person<br>{<br>    public $db = null;</pre><pre>function __construct($db) <br>    {<br>        $this-&gt;db = $db;<br>    }</pre><pre>public function greeting($id)<br>    {<br>        $friend = $this-&gt;db-&gt;getPersonByID($id);<br>        $friendName = $friend-&gt;name;<br>        return &quot;Hello $friendName&quot;;<br>    }<br>}</pre><pre>require __DIR__ . &#39;/Database.php&#39;;</pre><pre>$db = new Database();<br>$person = new Person($db);</pre><pre>// in our database we have a person with an ID of 2 and a name of &quot;Bob&quot;</pre><pre>echo $person-&gt;greeting(2);</pre><pre>// result should be &quot;Hello Bob&quot;</pre><p>We don’t actually want to test that <strong>getPersonByID </strong>returns a result from our database, but instead that person-&gt;greeting(2) gives us a result that we expect.</p><p>In theory, our test code would look like this (<strong>NB: this is an example of bad code</strong>):</p><pre>&lt;?php</pre><pre>require __DIR__ . &#39;/Person.php&#39;;<br>require __DIR__ . &#39;/Database.php&#39;;</pre><pre>use PHPUnit\Framework\TestCase;</pre><pre>class MockPerson extends TestCase<br>{<br>    public function test_greeting()<br>    {<br>        $db = new Database();<br>        $test = new Person($db);<br>        $this-&gt;assertEquals(&#39;Hello Bob&#39;, $test-&gt;greeting(2));<br>    }<br>}</pre><p>To avoid actually making a database query we can mock our database class within our test.</p><pre>&lt;?php<br>require __DIR__ . &#39;/Database.php&#39;;<br>require __DIR__ . &#39;/Person.php&#39;;</pre><pre>use PHPUnit\Framework\TestCase;</pre><pre>class MockTest extends TestCase<br>{<br>    public function test_greeting()<br>    {<br>        $dbMock = $this-&gt;getMockBuilder(Database::class)<br>            -&gt;setMethods([&#39;getPersonByID&#39;])<br>            -&gt;getMock();</pre><pre>$mockPerson = new stdClass();</pre><pre>$mockPerson-&gt;name = &#39;Bob&#39;;<br>        $dbMock-&gt;method(&#39;getPersonByID&#39;)-&gt;willReturn($mockPerson);</pre><pre>$test = new Person($dbMock);<br>        $this-&gt;assertEquals(&#39;Hello Bob&#39;, $test-&gt;greeting(2));<br>    }<br>}</pre><p>Let’s go through this one section at a time to see what’s going on:</p><p>Firstly we want to build a mock of the Database class that is exactly the same except we are telling the MockBuilder that we are going to get setting the getPersonByID later on ourselves.</p><p>$dbMock now becomes our $db</p><pre>$dbMock = $this-&gt;getMockBuilder(Database::class)<br>            -&gt;setMethods([&#39;getPersonByID&#39;])<br>            -&gt;getMock();</pre><p>Next we create a “fake” person object returned from the db that is just a standard class type with a name attribute of “Bob”:</p><pre>$mockPerson = new stdClass();</pre><pre>$mockPerson-&gt;name = &#39;Bob&#39;;</pre><p>Next we tell the newly created mock class to return our “fake” person whenever the getPersonByID function is called, instead of calling the real function that exists on the Database class:</p><pre>$dbMock-&gt;method(&#39;getPersonByID&#39;)-&gt;willReturn($mockPerson);</pre><p>Next we pass our mocked Database instance into our Person constructor as opposed to a real one:</p><pre>$test = new Person($dbMock);</pre><p>Finally we call our greeting function which will in turn call our mocked Database instance during its execution.</p><pre>$this-&gt;assertEquals(&#39;Hello Bob&#39;, $test-&gt;greeting(2));</pre><p>Mocking out unrelated or external functionality is a great way to isolate <strong>only</strong> the code that you want to test.</p><p>The above example is quite simple in nature, and mocking can become complicated as a project grows. It takes a fair amount of intuition to learn the best way to implement mocking when writing tests, but this will become easier the more you practice.</p><p>I would be keen to hear any other thoughts or answer any questions you may have on the subject in the comments below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=802f91112645" width="1" height="1" alt=""><hr><p><a href="https://medium.com/nona-web/mocking-in-phpunit-802f91112645">Mocking in PHPUnit</a> was originally published in <a href="https://medium.com/nona-web">Nona Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An introduction to assert with PHPUnit]]></title>
            <link>https://medium.com/nona-web/an-introduction-to-assert-with-phpunit-dbdec566a6de?source=rss-5abaf1f426b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbdec566a6de</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Richard Miles]]></dc:creator>
            <pubDate>Wed, 07 Aug 2019 15:24:57 GMT</pubDate>
            <atom:updated>2019-08-07T15:24:57.839Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SCVzCLjeJAKapIVpdwXW5A.jpeg" /></figure><p>This article serves as the first of a series on unit testing in PHPUnit aimed at providing a fundamental understanding of unit tests at a basic level. To do this I’ll be going through an example with PHPUnit. First I’ll be showing you how to setup PHPUnit and introducing assertions (a core part of tests). Then I’ll dive in to how and why we write unit tests.</p><h3>A brief setup of PHPUnit</h3><ol><li>First install PHPUnit (I will assume you have php ~7 installed on your system)</li></ol><p><a href="https://phpunit.de/getting-started/phpunit-7.html">Getting Started with Version 7 of PHPUnit - The PHP Testing Framework</a></p><p>2. Next, verify that it has been installed: run the following in your terminal.</p><pre>phpunit --v</pre><p>3. Create a new file called HelloWorld.test.php in a folder on your machine and populate it with the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/696898c3666a985844d43639e7062265/href">https://medium.com/media/696898c3666a985844d43639e7062265/href</a></iframe><p>Here we are creating a testable class that extends the PHPUnit base TestCase class.</p><p>4. Now, let’s create some configuration in the form of a phpunit.xml file with the following contents:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b69b7a934c2b92a4405a4083bdf17542/href">https://medium.com/media/b69b7a934c2b92a4405a4083bdf17542/href</a></iframe><p>This will allow us to define which files will be tested when we run phpunit and add a brief description as to what can be found in this particular test suite.</p><p>4. Next, run phpunit in your terminal</p><p>You should see that the tests ran, but with a warning (as we haven’t written any tests yet). At this point we can start writing our tests.</p><pre>WARNINGS!<br>Tests: 1, Assertions: 0, Warnings: 1.</pre><p>Before we start looking at assertions let’s take a look at <strong>why</strong> and <strong>how</strong> we write unit tests.</p><h4><strong>Why do we write unit tests?</strong></h4><p>We write <strong>unit tests</strong> to make sure that isolated pieces of functionality (functions) work <strong>as we expect </strong>outside of the context of the rest of the application. The way in which we write our tests should also affect the way we write our code. What you should start to notice over time is that you start writing functions differently and in a way that can be better tested. This usually amounts to purer functions (with little to no side-effects) being written. If your focus is already on writing pure functions then you have already won half the battle!</p><p>If our tests pass, and assuming we have written them correctly, we can be confident that our code will work as expected. This also means that we need to write sufficient tests to cover all possible scenarios, including some which might be out of the ordinary (fringe cases). We ideally also want to account for failures and expectant error handling , I will be touching on both of these later on in the series.</p><h4><strong>How do we go about doing this?</strong></h4><p>Traditionally the <strong>how</strong> in unit tests relates to “Test Driven Development” (TDD), to write tests that <strong>drive</strong> the rest of the code. This means a few things for us:</p><ol><li>We usually write our tests before we write the code that will <strong>eventually</strong> pass the test written for it</li><li>We never write more code (or tests) than needed</li><li>Our tests need to be specific and inclusive enough to cover the requirements of our app, which includes all realistic good and bad scenarios</li></ol><p>Let’s write a basic test to make sure that our app does what we expect it to do.</p><p>We will start by writing a test that is going to fail, because it does not do what we expect it to do, we will then write just enough code to make that test pass.</p><h4><strong>Our first assertion</strong></h4><p>We can assert (confirm) that something is as expected, by writing a test assertion.</p><p>Lets replace the current contents of our HelloWorld.test.php file with the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51cf85e554375972e7115858ba194dc6/href">https://medium.com/media/51cf85e554375972e7115858ba194dc6/href</a></iframe><p>In line 6 we are creating a function with a name that describes what that function is testing, which in this case when translated into english equates to ‘test helloWorld returns value as expected’. Something important to note is that in PHPUnit the test will not run out-of-the-box unless the function name is prefixed with the word ‘test’. Some people prefer to use comments to describe what a test is doing, however, I am not going to get into the semantics of writing descriptive testing patterns in this article.</p><p>In line 7 we are <strong>asserting</strong> that whatever is returned from a function named HelloWorld equates to ‘hello’. This is our test assertion.</p><p>Lets run our test to see if it passed by once again running: phpunit</p><p>Oops … it looks like it failed.</p><pre>There was 1 error:</pre><pre>1) HelloWorldTest::test_helloWorld_returns_value_as_expected<br>Error: Call to undefined function helloWorld()</pre><pre>/phpunit/HelloWorld.test.php:7</pre><pre>ERRORS!<br>Tests: 1, Assertions: 0, Errors: 1.</pre><p>Well of course it failed,</p><ol><li>No function with the name <em>helloWorld</em> exists</li><li>The return value of this non existent function is also not equal to ‘hello’</li></ol><p>Thankfully, we expected our assertion (‘assertEquals`) should fail. If our test passed at this point, then we should suspect that something went wrong.</p><p>Let’s define a function called helloWorld at the top of the same file which returns the value ‘hello’ and then run the test again ( phpunit in your terminal).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56647a1080fe8e0dc712287cc3c32f5d/href">https://medium.com/media/56647a1080fe8e0dc712287cc3c32f5d/href</a></iframe><pre>.                                                                   1 / 1 (100%)</pre><pre>Time: 79 ms, Memory: 8.00MB</pre><pre>OK (1 test, 1 assertion)</pre><p>Eureka, our test passed!</p><p>Great news, we have covered basic assertions and written our first passing test in PHPUnit!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbdec566a6de" width="1" height="1" alt=""><hr><p><a href="https://medium.com/nona-web/an-introduction-to-assert-with-phpunit-dbdec566a6de">An introduction to assert with PHPUnit</a> was originally published in <a href="https://medium.com/nona-web">Nona Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cleaning up commit history with Git rebase]]></title>
            <link>https://medium.com/nona-web/cleaning-up-commit-history-with-git-rebase-61dc41d2829c?source=rss-5abaf1f426b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/61dc41d2829c</guid>
            <category><![CDATA[technical]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Richard Miles]]></dc:creator>
            <pubDate>Tue, 08 Jan 2019 07:38:31 GMT</pubDate>
            <atom:updated>2019-01-15T17:23:46.124Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s7CqYQYbK44Kq2NClcVp_A.jpeg" /></figure><h3>Cleaning up commit history with git rebase</h3><p>Have you ever seen a commit history something like this?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aGvxGGYir-dKTPayUrWB8w.png" /></figure><p><em>(please excuse the colour scheme)</em></p><p>Well I have, and it sucks…</p><p>Reading bottom to top: We started off “okay” with the obligatory initial commit commit. Followed by the feat and fix prefix touches which are cute, however, by the time we get to the deploy code to staging section I can see the frustration starting to unfold. 10 commits into our project and already there is no real consistency . It’s fine if your code doesn’t work the way you want first time, and yes you might get frustrated when it doesn’t (which is also normal) — but we should really not be exhibiting this in our Git commit history.</p><p><strong>How do we go about avoiding this?</strong></p><p>We could just avoid making any mistakes ever, or we could be really creative with our commit messages to mask the reality that the current branch is a disaster that may or may not be fixed at some point. Both of these situations are not ideal.</p><p>There is a third (and much better) option. We can change history, purely for the sake of clarity in our commit messages.</p><h3>Why change history when you can just alter the present to suit you?</h3><p>There is one main reason, clarity, which takes the form of grouping features in a logical way as well as writing better, clearer more consistent commit messages.</p><p><strong>Why do we care about clarity?</strong></p><ul><li>Isolated features can be rolled out more easily as related commits are grouped together.</li><li>Control over a commit history, coupled with the more traditional use of git rebase — rebasing specific commits onto other branches (a topic that I wont be getting into right now, more information can be found <a href="https://git-scm.com/book/en/v2/Git-Branching-Rebasing">here</a>), when combined, are powerful tools to have in our development artillery.</li><li>Less work when describing a pull request. This really makes your life and that of your reviewer easier in the long run, when you have a clear additional reference to what was done when in a pull request in the form of clear commit messages.</li><li>Establishes good habits when building features: when prioritising clean commit messages we start adding features in a more consistent fashion, grouping pieces of relevant code together by default (as we know that if we don’t, we may need a rebase) and ultimately only using git rebase when necessary. I have found that since starting this approach to my workflow I rebase less and less, because my commit messages are clearer to start with.</li></ul><h3><strong>A practical example of rebasing</strong></h3><p>For this entire section I am going to assume you have Git installed on your local terminal / cmd as well as have an understanding of some basic Git commands such as adding / committing / pushing, as well as making file edits in your command line editor (usually VIM).</p><p><strong>Some important points before diving into rebasing</strong></p><p><strong>Team members’ buy in</strong></p><p>It’s really important that your team understands you will be rebasing in a project, as caution needs to be taken by them when working with your branches.</p><p>Before team members merge or branch off anything that you are currently working on, they should confirm with you that you aren’t currently in the middle of a rebase. Alternatively, If you push a rebased branch to a remote branch that another team member has previously pushed to containing <strong>new</strong> changes that you don’t have locally, your team member’s changes will likely be lost. Because of this, I usually keep my own branches separate and only push a rebased branch if I know that I am the only person working on it. I can always create temporary branches that can be merged back into feature branches at a later stage if needed. This approach can be summarised as the “Golden Rule” of rebasing.</p><p><strong>Keeping track of what has and hasn’t been done</strong></p><p>I would suggest using some sort of Kanban system such as Jira, Trello or Github Projects with tasks/cards that map to features or pull requests when working on feature branches, mainly to track what has and hasn’t been done.You ideally don’t want to find yourself in a situation where important work has been lost due to rebasing, so having another method outside of Git to track your changes (even if at a high level) can be helpful. Alternatively if you intentionally need to revert work for the purpose of refactoring, it is important to keep track of any changes outside of version control, as Git does not keep a concise historical record of rebasing.</p><h3><strong>Cleaning up descriptions</strong></h3><p>In the problematic commit log screenshot seen previously in this article we notice there are 10 commits in total. Depending on our situation I may want to group these commits differently, which means rewording, squashing or dropping a commit completely.</p><p>All three will require us to run the following in our terminal:</p><pre>git rebase -i HEAD~9</pre><p>Breaking this command down:</p><p>git rebase — tells our terminal we are running Git with the rebase command</p><p>-i — tells git rebase to run in interactive mode (VIM)</p><p>HEAD~9 — we want the ability to rebase the last 9 commits (we don’t want to change our initial commit message).</p><p>After running this we see a file open in VIM (or whatever your default command line editor is) that lists our commits as well as some options that we can choose to apply to each of these commits (the default option being pick — meaning that we pick this commit as is).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_3ByFhykR7fdIYrBTlxwPw.png" /></figure><p>I am only going to be covering the drop, reword and fixup options in this article.</p><p><strong>drop</strong></p><p>For the sake of example, I have decided that the final commit FFS is not needed as I discovered that I hope this one works actually fixed the problem but I didn’t realise it at the time, therefore I am going to drop the FFS commit.</p><p>We can do this by replacing pick next to FFS with drop and then save and exit the file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*sdGTc-8zGL4g9o57mY2ktw.png" /></figure><p>If we have a look at our Git log now (git log ) you will see that both the commit message and any code affiliated with FFS no longer exists,. Congratulations, we have changed history.</p><p><strong>fixup and rewording</strong></p><p>I like to use fixup when I want to group a few commits together under a single commit, as either they are all doing the same thing, or I can reword a group of features in a better way that makes more sense under a single commit message.</p><p>Lets go back into our interactive rebase mode by running</p><pre>git rebase -i HEAD~8</pre><p>(notice now we only have 8 commits now, as we removed FFS from history)</p><p>I have taken the liberty of doing some rebase file changes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DaTB3i-GThZMnITiDJpdug.png" /></figure><p>Lines 1 and 2 make sense to be their own commit messages, and they were clear enough to understand and intentionally added to describe the project’s state.</p><p>Lines 3 and 4 were mistakes, so I have decided to run fixup on them. This essentially squashes the code into the closest previous commit and removes their commit messages, which in this case is feat: added api endpoint for adding blogpost (the squash command is similar to fixup except it keeps a record of the squashed commit message).</p><p>I am merging lines 6, 7 and 8 into a single commit called deployment with the fixup command.</p><p>I don’t like the wording of line 5, so I am going to reword this to something that encapsulates this commit (as well as all of the later commits that are being squashed into this one) in a better way. Let’s save and exit the file, which will bring up a new editor instance, where it will ask us what we want to <strong>reword</strong> the commit message of line 5 to. If we wanted to do rewording on more than one commit in our interactive rebase, then we would be prompted multiple times. Update the commit message then save and close VIM.</p><p>Lets run git log again to see everything that we expected did indeed happen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tm9K_6StFP-fFF3SviRHjA.png" /></figure><p>Hooray! it looks like our commits and messages have been cleaned up.</p><blockquote>NB: When pushing back to a remote git branch you will need to use the force flag (git push origin master <strong>-f</strong>) to tell git that you are forcing a rebase to happen. Git does not like us doing this without acknowledging that we know what we are doing . Please use with caution as if any problems around rebasing are going to occur (especially related to more than one person working on the same branch), then this is the step where it will likely happen.</blockquote><h3><strong>Conclusion</strong></h3><p>Rebase is a is a powerful tool that should be used with caution but can be used effectively in the situation described in this article. If you are thinking of using this method of rebasing in your own projects you should consider consulting the official documentation — <a href="https://git-scm.com/book/en/v2/Git-Branching-Rebasing">https://git-scm.com/book/en/v2/Git-Branching-Rebasing</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61dc41d2829c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/nona-web/cleaning-up-commit-history-with-git-rebase-61dc41d2829c">Cleaning up commit history with Git rebase</a> was originally published in <a href="https://medium.com/nona-web">Nona Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AWS Parameter Store vs Environment Variables]]></title>
            <link>https://medium.com/nona-web/aws-parameter-store-vs-environment-variables-d86a5f175a74?source=rss-5abaf1f426b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/d86a5f175a74</guid>
            <category><![CDATA[environment-variables]]></category>
            <category><![CDATA[technical]]></category>
            <category><![CDATA[parameter-store]]></category>
            <category><![CDATA[km]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Richard Miles]]></dc:creator>
            <pubDate>Tue, 01 Jan 2019 19:24:05 GMT</pubDate>
            <atom:updated>2019-01-14T07:37:36.627Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rOTNSzATFDGZIZhUeSCL1g.jpeg" /></figure><p>In this article I will be looking at if and when the AWS Parameter Store should be used to replace environment variables within the AWS infrastructure. I will not be looking at what each of these are or how to set them up in any in-depth way but rather a comparison between the two.</p><h3><strong>A case for environment variables</strong></h3><p><strong>Easy to setup</strong></p><p>It is pretty simple to get setup with environment variables. Node, for example, has a dotenv module that can be installed via npm with a single command:</p><pre>npm install dotenv</pre><p>We need to make sure that dotenv has an entry point somewhere in our script, usually via a require statement -</p><pre>require(&#39;dotenv&#39;).config()</pre><p>From here, all we need to do is add our enviroment variables to a .env file placed in the root folder of the project.</p><p>You can find more information on the dotenv module here:</p><p><a href="https://www.npmjs.com/package/dotenv">dotenv</a></p><p>It should be noted that the Parameter Store does not initially share the same aspects in ease of setup — if you have never worked with the Parameter Store before, the setup process can seem quite laborious with a few things to consider:</p><ul><li>You need access to an AWS account</li><li>You need know how to navigate the AWS dashboard — specifically the SSM section.</li><li>Sensitive/Secure parameters should be encrypted with KMS — which in itself requires some additional setup (<a href="https://aws.amazon.com/kms/">https://aws.amazon.com/kms/</a>)</li><li>Depending on your needs, you may need configuration knowledge of other AWS services such as IAM and SSM order to get the Parameter Store to work correctly.</li><li>All parameters are hosted on the cloud requiring IAM programatic access, which means an uninterrupted connection is required (it should be noted that bespoke local setups can be created with local variables that can be used in place of Parameter Store variables depending on the situation— I would in fact encourage this kind of approach).</li></ul><p><strong>Easy to update during development, deployment and testing</strong></p><p>Environment variables can easily be updated via the aforementioned .env file and dotenv module. Variations of this file can also be created and utilised to suit each relevant environment. Importing our enviroment variables in a testing scenario works in much the same way (importing env vars from the relevant dotenv file).</p><p>For deploying to a staging or production server, all we would do is use an alternate version of the .env file. This can easily be done by ignoring the existing .env file in your version control system locally (usually git) and creating a new copy on each stage / server instance.</p><p>Environment variables also integrate nicely with Continuous Integration systems (CI). Circle CI, for example, has a dedicated section for managing environment variables where they can be added at a project build level and updated in a single place when ready for deployment —</p><p><a href="https://circleci.com/docs/2.0/env-vars/">Using Environment Variables - CircleCI</a></p><p>From a Parameter Store perspective, it is language / framework agnostic, meaning all setup will either need to be done manually using an AWS SDK with programatic access to the Parameter Store service, or similarly through a 3rd party provider (such as an npm module). While AWS and its services are the benchmark for security standards in the cloud computing domain, custom modules that you might want to develop or utilise may have security vulnerabilities due to malice or oversight — bearing in mind that there are industry accepted modules for this that are maintained and endorsed by trusted entities such as AWS themselves.</p><p>From a deployment / testing perspective the Parameter Store also comes with a unique set of challenges, as although there are suggested approaches, how and when you choose to interact with the Parameter Store is entirely up to you.</p><p><strong>Enviroment Variables are free to use</strong></p><p>Although AWS Parameter Store does not include additional charges (<a href="https://aws.amazon.com/systems-manager/pricing/">https://aws.amazon.com/systems-manager/pricing</a>), Amazon’s pricing structure for related services such as KMS (<a href="https://aws.amazon.com/kms/pricing/">https://aws.amazon.com/kms/pricing</a>) will start to incur additional costs based off of usage. On the other hand, using environment variables is free.</p><h3><strong>So why replace environment variables? : a case for the Parameter Store</strong></h3><p>Up to this point the vanilla environment variable solution seems to be edging the Parameter Store in the race for dominance in the stage/secure variable arena. While the Parameter Store does seem to create more challenges than it solves at this point, there are a few things, however, that the Parameter Store undisputedly does better than environment variables:</p><p><strong>Parameter Store variables can be shared across multiple projects</strong></p><p>The best setup I have found for sharing environment variables across projects is to use an automated deployment process that fetches environment variables from a file contained in a shared entity, such as a S3 bucket which plugs into the project configuration as needed (usually done through a script that is run from a CI service). From previous experience, this is the most semantically viable option (please let me know if you have experience with any better options). This is unfortunately a tedious exercise initially and ultimately not something you would want to maintain manually in the long run, which is mainly because any mistakes or oversights along the way when setting up or maintaining it could lead to problems.</p><p>Everything that we can hand over to an AWS service to automate, we should and this is where the Parameter Store shines. Sharing KMS keys and Parameter Store variables across projects comes out-of-the-box.</p><p>I want to take step back and look at AWS from a holistic perspective. Amazon has done a great job at managing every aspect of cloud computing, and has a plethora of development teams dedicated to specific services that you and I will never be able to replicate. This ultimately means that once you have bought into the Amazon ‘experience’ you should make use of <strong>all</strong> of the services designed to work together under the banner of AWS cloud infrastructure. Although it has its issues (such as you and I now being completely locked into AWS as a service provider), it is ultimately easier to offload everything you can to them, as it is one less thing you need to worry about — even if it costs a little extra .</p><p><strong>Parameter Store can use access control</strong></p><p>Having specific control over user access makes the IAM service one of AWS’s greatest attributes, especially when it comes to dealing with potentially sensitive information such as API keys or passwords. The concept of controlling access to environment variables within the vanilla sense (e.g. using the dotenv approach) is just not an option (unless you are willing to develop your own bespoke solution — or move outside of the realm of ‘best practices’).</p><p><strong>Parameter Store values are easily updated</strong></p><p>In order to update any values in your Parameter Store all you need is appropriate access to your AWS dashboard (or the CLI), meaning that even non-technical team members can update values with a bit of AWS dashboard experience.</p><p>On the environment variable side of the argument updating environment variables becomes problematic as <strong>usually</strong> only skilled team members who have access to server update permissions will be able to access and update these. Additionally this opens up your project to a layer of vulnerability, as logging into a server and updating core project files on the fly (even on an automated setup) can lead to issues.</p><p><strong>KMS can encrypt Parameter Store values easily</strong></p><p>Although the initial setup of encrypting values in the Parameter Store might seem a bit daunting, once you get used to it, it is quite simple and makes a lot of sense — even from a non technical perspective. This is also a great layer of security that is added, out-of-the-box.</p><p>It is possible to use encryption for environment variables that you may not want to store in plain text, but this can be difficult to setup and maintain manually.</p><p><strong>Parameter Store variables can be organised</strong></p><p>Production and Staging parameters are managed in a single place when using the Parameter Store, which includes sorting and filtering parameters and even adding descriptions to clarify their purpose.</p><p>From an environment variable perspective you just don’t get out-of-the-box variable organisation. Having a few environment variables might be easy enough to manage, but this becomes problematic when there are too many variables to manage manually. It may be possible to organise environment variables into different files and folders, but there really isn’t a viable out-of-the-box solution for this that doesn’t seem to complicate things unnecessarily.</p><p>Ultimately, the decision around which of the two solutions to use should be determined by the complexity and scope of the project, taking into account the factors mentioned above.</p><p><strong>Conclusion</strong></p><p>There are other factors to take into consideration when choosing whether to use the Parameter Store or environment variables to manage your stage / secure parameters, but hopefully in this article we have covered the important ones.</p><p>If you have any other factors that you feel are worth consideration I would like to hear your thoughts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d86a5f175a74" width="1" height="1" alt=""><hr><p><a href="https://medium.com/nona-web/aws-parameter-store-vs-environment-variables-d86a5f175a74">AWS Parameter Store vs Environment Variables</a> was originally published in <a href="https://medium.com/nona-web">Nona Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>