<?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 Lich Nguyen on Medium]]></title>
        <description><![CDATA[Stories by Lich Nguyen on Medium]]></description>
        <link>https://medium.com/@lichnguyen?source=rss-934352b0023c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*WHkLw_FugL21IfpwBTzT0A.jpeg</url>
            <title>Stories by Lich Nguyen on Medium</title>
            <link>https://medium.com/@lichnguyen?source=rss-934352b0023c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 18:08:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lichnguyen/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[Hidden risks when using Web3 (Part 1)]]></title>
            <link>https://medium.com/@lichnguyen/hidden-risks-when-using-web3-part-1-3adbdbd8c396?source=rss-934352b0023c------2</link>
            <guid isPermaLink="false">https://medium.com/p/3adbdbd8c396</guid>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[web3]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Lich Nguyen]]></dc:creator>
            <pubDate>Thu, 31 Mar 2022 03:14:04 GMT</pubDate>
            <atom:updated>2022-06-05T03:26:20.851Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5lRbKpxNIqQmF7fK.png" /></figure><p>There are always risks for user when interacting with Defi, NFT or Web3 platform. And it is not always smart contract risk but also the system/cloud security risk. As mentioned on a <a href="https://www.linkedin.com/posts/activity-6915136563475595264-A0BM?utm_source=linkedin_share&amp;utm_medium=member_desktop_web">LinkedIn</a> post, those security risks are not being addressed seriously at the moment.</p><p>In part 1 of this series, we will talk about how easily a “hacker” can trick user to interact with his malicious smart contract.</p><blockquote><strong>TL;DR:</strong> <br>For user: Every Defi apps require approval to use of fund in the Wallet, meaning <strong>allows its smart contract to spend our token without permission</strong>. Take extra 1 minute to carefully verify the contract address, set Custom Spend Limit, and avoid project that we don’t trust.</blockquote><blockquote>For platform builder: invest in cloud security too.</blockquote><h3>For user: approve spending, but don’t blindly trust</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gK7G8ChGggR8p4Sl.jpg" /><figcaption>Image not mine</figcaption></figure><p>For every Web3 platform, we need to approve the smart contract to spend the token in our wallet. Most of us click Confirm here without a second thought. But we must understand that <strong>this allow the smart contract to withdraw the fund in our wallet in the future without any other approval. </strong>Yeah, it’s that dangerous.</p><blockquote>Risk: If the smart contract is exploited, we might lose our fund.</blockquote><p>This risk is minimal if we use the trusted brand such as Uniswap since their contract are properly audited and being monitored it all the time. But since now we we know what this step does, we should avoid approve a brand new, unknown Defi platform, or at least, take an extra precaution and set the Custom Spend Limit.</p><h3>Are smart contract risk the only risks?</h3><p>Unless we’re savvy users that use Terminal to sign transaction, normal user when interact with smart contract, still need to use an web UI, web application as below diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zem5Y6bHsQ2vQv220LzRXg.png" /><figcaption>Image is mine</figcaption></figure><p>Since it is part of the interaction, this Web Application becomes another point of failure. Without proper security, all it takes is someone gain access and update the Web Application to point to a malicious smart contract.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ESD7XDlCQj_ndTKsuWmbJg.png" /></figure><p>And the worst thing, most of the user couldn’t detect this until it’s too late. It could be a disaster, users lose money, the platform reputation goes to 0, business going down.</p><p><strong>How easy above scenario could happen? Without proper cloud security, anyone from the team with intention can do it.</strong></p><p>Did it happens before? Yes, from <a href="https://www.theverge.com/2021/12/2/22814849/badgerdao-defi-120-million-hack-bitcoin-ethereum">BadgerDao hack</a>:</p><blockquote>While the investigation is still ongoing, members of the Badger team have told users that they believe the issue came from someone inserting a malicious script in the UI of their website. For any users who interacted with the site when the script was active, it would intercept Web3 transactions and insert a request to transfer the victim’s tokens to the attacker’s chosen address.</blockquote><h3>Self protection</h3><p>For user, to self protect our self, always check the smart contract address before approving. It is inconvenient, but it only take extra 1 minute, and it can save us from losing all our saving.</p><h3>For the industry</h3><p>Common wallet app such as Metamask should have a way to warn user about unknown smart contract. Or display the smart contract address in a friendly manner, leveraging on a DNS system such as ENS? For example uniswap.eth is much easier to verify than 0xdaclkja832lkasdfzxvblahblahblha.</p><h3>For the builder</h3><p>If platform can spend 100,000$ to get multiple security firms to audit smart contract, spend equally amount or at least half of that to get a security expert.</p><h3>Conclusion</h3><p>Ultimately, user still need a web interface to interact with the smart contract. And with that, we still have single point of failure. It is a ticking bomb that can cause business failure and users millions or even billions $.</p><p>So, invest in cloud security too, and do it early, for our user and our business.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3adbdbd8c396" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Quick tutorial: Grant AWS SSO user access to EKS cluster]]></title>
            <link>https://medium.com/@lichnguyen/quick-tutorial-grant-aws-sso-user-access-to-eks-cluster-d0ffae6d3327?source=rss-934352b0023c------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0ffae6d3327</guid>
            <category><![CDATA[rbac]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[sso]]></category>
            <dc:creator><![CDATA[Lich Nguyen]]></dc:creator>
            <pubDate>Wed, 16 Feb 2022 02:18:16 GMT</pubDate>
            <atom:updated>2022-06-05T02:55:24.481Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ky_ZfE9wAeeolJje" /></figure><p>AWS SSO login is more secured compare to username/password, also no more access key management headache.</p><p>But, how do we grant AWS SSO user access to EKS cluster via aws-authconfigmap? There’re a 2 ways to do this:</p><ul><li>Create another role, allow AWS SSO roles to assume and update kube-config to use this new role.</li><li>Assign the AWS SSO roles directly in aws-auth</li></ul><p>The first way is standard and we can refer to the AWS tutorial <a href="https://aws.amazon.com/blogs/opensource/integrating-ldap-ad-users-kubernetes-rbac-aws-iam-authenticator-project/">here</a>.</p><p>For the second way, we need to find out the ARN of the SSO user.</p><p>First, login and use AWS CLI to identify ourselves:</p><pre>aws sts get-caller-identity</pre><p>and got result:</p><pre>{<br>    &quot;UserId&quot;: &quot;AROAYG7HX5NXJTXQFAPEM:example@example.io&quot;,<br>    &quot;Account&quot;: &quot;782533642732&quot;,<br>    &quot;Arn&quot;: &quot;arn:aws:sts::782533642732:<a href="/service/mailto:assumed-role/AWSReservedSSO_AdministratorAccess_68dc3124e3d0e3ad/lich.nguyen@cybereight.io">assumed-role/AWSReservedSSO_AdministratorAccess_2468dc31e3d0e3ad/</a>example@example.io&quot;<br>}</pre><p>From the result, we currently assume this role: <a href="/service/mailto:assumed-role/AWSReservedSSO_AdministratorAccess_68dc3124e3d0e3ad/lich.nguyen@cybereight.io">AWSReservedSSO_AdministratorAccess_2468dc31e3d0e3ad</a></p><p>Let’s go to AWS IAM console to get its arn, result:</p><pre>arn:aws:iam::782533642732:role/aws-reserved/sso.amazonaws.com/region/<a href="/service/mailto:assumed-role/AWSReservedSSO_AdministratorAccess_68dc3124e3d0e3ad/lich.nguyen@cybereight.io">AWSReservedSSO_AdministratorAccess_2468dc31e3d0e3ad</a></pre><p>Final step, remove: aws-reserved/sso.amazonaws.com/region . Below is the final result that can be used with aws-auth:</p><pre>arn:aws:iam::782533642732:role/<a href="/service/mailto:assumed-role/AWSReservedSSO_AdministratorAccess_68dc3124e3d0e3ad/lich.nguyen@cybereight.io">AWSReservedSSO_AdministratorAccess_2468dc31e3d0e3ad</a></pre><p>Try this, 100% work!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0ffae6d3327" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using terraform.workspace with Terraform Cloud]]></title>
            <link>https://medium.com/@lichnguyen/using-terraform-workspace-with-terraform-cloud-993c31c1f8bc?source=rss-934352b0023c------2</link>
            <guid isPermaLink="false">https://medium.com/p/993c31c1f8bc</guid>
            <category><![CDATA[terraform-cloud]]></category>
            <category><![CDATA[terraform-workspace]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[Lich Nguyen]]></dc:creator>
            <pubDate>Sun, 24 May 2020 05:54:01 GMT</pubDate>
            <atom:updated>2020-06-05T07:56:27.376Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uaVd4nhQq2RwUuf-.png" /><figcaption>Terraform Cloud Workflow</figcaption></figure><p>Have you ever tried Terraform Cloud before? Have you tried all the integration, collaboration features nicely setup on Terraform Cloud? Have you ever been so eager running all your configuration in Terraform Cloud and then suddenly realized it is not reading variable from your desired terraform.workspace?</p><p>Let’s see what we can do to fix this.</p><h3>Using terraform.workspace to setup multi environment</h3><p><em>If you already know how to use </em><em>terraform.workspace to switch between multi environment on the same configuration, then you can skip this section.</em></p><p>When working with Terraform, we usually want to use the same configuration but with different variables for each environment (dev, test, prod). There are many technique to archive that, one of them is leverage on terraform.workspace</p><p>To use terraform.workspace, first, we create the workspace corresponding to our environments. For example here I can create 3 workspace: dev, test and prod using the command: terraform workspace new dev .</p><p>I can switch between workspace using the command: terraform workspace select dev/test/prod</p><p>Also, the Terraform state of each workspace is separated and managed by the workspace select command.</p><p>Now, we can define resource as below example, that will switch to use the variables corresponding to current environment workspace</p><pre># Variables files<br>---- variables.tf ------<br>variable name {<br>    description = &quot;Environment&quot;<br>    type        = map(string)<br>    default = {<br>        dev = &quot;dev&quot;<br>        stg = &quot;stg&quot;<br>        prod = &quot;prod&quot;<br>    }<br>}</pre><pre># AWS ECS cluster configuration file<br>---- ecs.tf ------<br>module &quot;ecs&quot; {<br>  source  = &quot;../modules/ecs/&quot;<br><br>  name = var.name[terraform.workspace]<br>}</pre><p>As you can see, the variable name is a map of key and value type, with the key is the env we defined with terraform.workspace. Or in other word, we are mapping the variables with terraform.workspace</p><p>This is a very smart way to use different variables for each environment and not repeating a lot of code.</p><h3>Use terraform.workspace with Terraform Cloud</h3><p><a href="http://app.terraform.io/">Terraform Cloud</a> is a cloud version of Terraform Enterprise, which promote collaborative IAC (Infrastructure-As-Code), make it easier for multi teams to work on Terraform.</p><blockquote>The workspace concept on TF Cloud is not the same as local terraform.workspace.</blockquote><blockquote>Locally, configuration codes are organized into directories, each directory has configuration, variables and state file. TF Cloud doesn’t have a directory structure, instead, those configuration, variables and state file are stored in a workspace, or you can say a cloud directory.</blockquote><blockquote>In local environment, terraform.workspace is just a way to switch state file.</blockquote><p>In TF Cloud, we would like to have one workspace per environment per configuration. We shall organize the ECS example above into 3 workspace: ecs-dev, ecs-stg, ecs-prod</p><p><strong>How do we match local workspace with TF Cloud workspace?</strong></p><p>How can I configure that, locally, when I am inside ecs configuration, terrafom.workspace=dev, I would work with ecs-dev on the cloud? This is archive-able using <a href="https://www.terraform.io/docs/backends/types/remote.html#prefix">workspace prefix</a> in backend config.</p><pre>terraform {<br>  backend &quot;remote&quot; {<br>    hostname     = &quot;app.terraform.io&quot;<br>    organization = &quot;my-org&quot;<br><br>    workspaces {<br>      prefix = &quot;ecs-&quot;<br>    }<br>  }<br>}</pre><p>With this, Terraform will always know which remote workspace you are working with by combining the prefix with the local terraform.workspace</p><p>Terraform Cloud run Plan/Apply on its own machine, which is great to ensure consistency and a single source of truth for all the Plan/Apply. However, because of this, we can’t set terraform.workspace before each run, thus it is always set to default value default. This is not what we want.</p><p>We have to modify our code a bit, to make it compatible with both running locally and on TF Cloud.</p><pre>variable &quot;TFC_WORKSPACE_NAME&quot; {<br>  type = string<br>  default = &quot;&quot;<br>}<br><br>locals {<br>  # If your backend is not Terraform Cloud, the value is ${terraform.workspace}<br>  # otherwise the value retrieved is that of the TFC_WORKSPACE_NAME with trimprefix<br>  workspace = var.TFC_WORKSPACE_NAME != &quot;&quot; ? trimprefix(&quot;${var.TFC_WORKSPACE_NAME}&quot;, &quot;ecs-&quot;) : &quot;${terraform.workspace}&quot;<br>}</pre><ul><li>TFC_WORKSPACE_NAME is a special variable that auto set by TF Cloud, it is the current TF Cloud workspace.</li><li>If I run locally, use terraform.workspace</li><li>Else in the cloud, use TFC_WORKSPACE_NAME without the prefix ecs</li></ul><p>In configuration code, switch to use local.workspace instead:</p><pre>module &quot;ecs&quot; {<br>  source  = &quot;../modules/ecs/&quot;<br><br>  name = var.name[local.workspace]<br>}</pre><p>This is done.</p><p>Now you can have the best of both world, use Terraform Cloud in remote execution mode, and still able to switch between terraform.workspace on your local environment without any issue.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=993c31c1f8bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Feedback loops and DevOps (Part 2)]]></title>
            <link>https://medium.com/@lichnguyen/feedback-loops-and-devops-part-2-9d1907dedd13?source=rss-934352b0023c------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d1907dedd13</guid>
            <dc:creator><![CDATA[Lich Nguyen]]></dc:creator>
            <pubDate>Thu, 07 May 2020 13:29:36 GMT</pubDate>
            <atom:updated>2020-05-09T02:47:16.374Z</atom:updated>
            <content:encoded><![CDATA[<p>In <a href="https://medium.com/@lichnguyen/feedback-loops-and-devops-part-1-791b92936b36">part 1</a>, I have talked two types of feedback loops: <strong>reinforcement and balancing loops.</strong></p><p>In part 2, we will continue with how to recognize and use them in DevOps context.</p><h3>But first…What is DevOps?</h3><p>The question with many answers:</p><ul><li>To developers, DevOps is tools and automation.</li><li>To management, DevOps is a methodology.</li><li>To recruiter, a DevOps job title could attract more applicants.</li><li>To DevOps engineer, DevOps should not be a job title, role or position.</li></ul><p>So what is it? In my opinion, they are all correct.</p><p>DevOps is very much a methodology that leverage on tools, processes and culture<strong> that change the way Developers and Operations teams work</strong>. Technical skills are common, but not many can grasp the methodology.</p><p>In this part 2, we will focus on “change”. We will leverage on our understanding of feedback loops in previous chapter to <strong>detect loops, amplify the good feedback loop and recognize when things is going the other way</strong>.</p><h3>Build positive feedback loops</h3><p>Automation tools are great way to build reinforcement feedback loops.</p><p>Obvious example is, once you start automate tasks and build tools so useful for developers, they can never go back to the old way again. People start approaching you to automate more tasks, build more tools so their life can be easier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/321/1*UFZCFZGFhprbozLzLQKP9Q.png" /><figcaption>Postive DevOps loop</figcaption></figure><blockquote><strong>The more convenient and effective of DevOps are to people, the more team/people want DevOps to involve in their works. Which make DevOps even more effective to more people.</strong></blockquote><p>And soon, everyone will be doing DevOps, it’s that easy you might think?</p><h3>Feedback can be negative</h3><p>From <a href="https://medium.com/@lichnguyen/feedback-loops-and-devops-part-1-791b92936b36">part 1,</a> we knew that reinforcement loop can also be a negative loop. Unfortunately, it might be obvious for us to recognize a positive loop (because we are proud of our magic and want a pat on our back), it is more difficult to recognize a negative loop.</p><p>Sometimes, it could start with positive feedback and slowly turn into negative. One example is <strong>Alerting</strong>.</p><p>When we first started, we collect metrics and setting up alerts from certain systems. We start seeing result as we can detect and prevent problems on those systems earlier. This lead to more system owners want to be onboard on the effective “monitoring and alerting train”. We now have a positive feedback loop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*NtakF4yAW9r8D8wvPXWs2g.png" /><figcaption>Alert positive loop</figcaption></figure><p>The alerts are sent everywhere, on separate different channels, to all different teams. Just as you think that now you have total visibility of your systems, you might actually have less. Why?</p><ul><li>There might be lot of noise than signal. Noises are those alerts that require no action from any body.</li><li>The more noise we are received, the more we ignoring it.</li><li>As in the classic <a href="https://en.wikipedia.org/wiki/The_Boy_Who_Cried_Wolf">The Boy Who Cried Wolf,</a> we eventually ignore the alert totally, checking the alert only when we want to, until disaster happens.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*UuyBLWt9fqE9fmp9Gkglxw.png" /><figcaption>Positive turns to negative loop</figcaption></figure><blockquote><strong>Be careful all the loops around you, as similar as positive loop, once people are in the negative feedback loop, it will be very difficult to go back.</strong></blockquote><h3>Recognize and deal with balancing loops</h3><p>Lots of time, things will not go our way. Especially in bigger organization where teams structure is complex and each has different focus.</p><p>One classic example is the software development vs project deadline. We all want nice things, our code should pass the code styling check with 100% unit test coverage and more. Unfortunately, we also have a day to deliver the project. The closer to the deadline, the less developers would have time to write tests or follow all the checks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/312/1*p2rAZLAD0YqjG2sIJOF6sQ.png" /><figcaption>A balancing loop</figcaption></figure><p>Our target changes from having proper DevOps pipeline to anything to meet deadline. All the proper tools and processes become obstacles. The closer to the deadline, the more they are skipped. We now have a balancing feedback loop.</p><p>The balancing loop does happen all the time, and it is the most difficult problem to solve because it is related to organization structure, business goals and people culture. This is something a DevOps engineer alone won’t be able to solve. Management need to step in, evaluate trade off between deadline focus or customer experience focus.</p><h3>Conclusion</h3><p>We now understand that automation, CI/CD pipeline or any tools alone can only help us so far. We must be able to recognize the feedback loops surrounding us, amplify the positive while be aware of the negative.</p><p>As an DevOps practitioner, we should strive more than the engineering aspect(tools and automation), but also trying to look at the people aspect(organization structure and focus). That is why doing DevOps is hard, it is not a role but an abstract ideas that hard to grasp, and need everyone to understand for it to works. It requires a lot of patient.</p><blockquote>To be able to do DevOps, the whole organization would have to move toward it.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d1907dedd13" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Feedback loops and DevOps (Part 1)]]></title>
            <link>https://medium.com/@lichnguyen/feedback-loops-and-devops-part-1-791b92936b36?source=rss-934352b0023c------2</link>
            <guid isPermaLink="false">https://medium.com/p/791b92936b36</guid>
            <category><![CDATA[growth]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[systems-thinking]]></category>
            <dc:creator><![CDATA[Lich Nguyen]]></dc:creator>
            <pubDate>Sat, 05 Oct 2019 05:06:36 GMT</pubDate>
            <atom:updated>2020-05-06T12:27:06.667Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*W5I4NLcZPQb1iEcv.png" /></figure><h3>What is a feedback loop?</h3><blockquote>If A causes B, can B also causes A?</blockquote><p>A feedback loop is created when the response from an event, in turn, affects the event itself. Or in other word, A causes B, then B also causes A.</p><p>The classic example of the feedback loop is the saving account with interest. The more money you have in the saving account, the more interest it produces, which generate more interest to the account and so on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/281/1*zX5OgEsXDMZvEBb0MUzkTg.png" /><figcaption>Feedback loops of a saving account</figcaption></figure><h3>Reinforcement loop</h3><p>The above example is called <strong>reinforcement loop. </strong>When you start looking around, these loops are everywhere in our daily life.</p><p>The more you play soccer, the better you are at it, which make you want to play it even more. More people are on a social media platform such as Facebook, the more people want to join. These are <strong>positive feedback loops</strong>.</p><p>In contrast, there are <strong>negative feedback loops</strong>. The less you interested in playing soccer, the worse you are every time you do it, make you even less interested in doing it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/301/1*CXxvmRCXDL8R1x6aOfnIow.jpeg" /><figcaption>Negative feedback loop</figcaption></figure><h3>Balancing feedback loops</h3><p>Not all loop are reinforcement loop, there are loops which the response in return balance the event, those are called <strong>balancing feedback loop</strong>. A balancing loop happens usually because there is a target.</p><p>One example is how air conditioner works with a target temperature. The more cold air it produces, the cooler the room is. The cooler the room is, the less cold air it needs to produce. Eventually it can stop once the room temperature reaches the target.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/301/1*c5ADGnVX3S2vy3oi3PeBrg.jpeg" /><figcaption>Air conditioner balancing loops</figcaption></figure><p>Balancing loops are also everywhere. People will eat more if they drop below their target weight, and try to eat less if they are above.</p><h3>Summary</h3><p>Now, we have been introduced to the two kind of feedback loops: <strong>reinforcement and balancing loops</strong>. If we start looking, they are everywhere in our lives, invisibly affect our actions and habits without us being aware of. By understanding it, we can start using it to change not only ourselves, but in DevOps context, the culture of a whole team and organization.</p><p>We will continue explore this idea in part 2 of the series (to be continued)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=791b92936b36" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gitlab CI — Merge request build with AWS Lambda]]></title>
            <link>https://medium.com/@lichnguyen/gitlab-ci-merge-request-build-with-aws-lambda-ae45cdc62e3d?source=rss-934352b0023c------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae45cdc62e3d</guid>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[gitlab-ci]]></category>
            <category><![CDATA[pull-request]]></category>
            <category><![CDATA[lambda]]></category>
            <dc:creator><![CDATA[Lich Nguyen]]></dc:creator>
            <pubDate>Thu, 27 Dec 2018 15:05:49 GMT</pubDate>
            <atom:updated>2019-05-24T02:27:30.621Z</atom:updated>
            <content:encoded><![CDATA[<h3>Gitlab CI — Building Merge Request with AWS Lambda</h3><p><strong>Updated May 2019:</strong> Gitlab now allow you to run pipeline for Merge Request, so you don’t have to use this method anymore. Refer to Gitlab Documentation <a href="https://docs.gitlab.com/ee/ci/merge_request_pipelines/">here</a>.</p><h3>TL;DR</h3><p>This post will guide you through the steps to use AWS Lambda to trigger a CI Pipeline for a merge request build on Gitlab.</p><h3><strong>What is MR Build?</strong></h3><p>Merge Request(MR) build is an additional steps in CI/CD Pipeline that happens at Merge Request, or Pull Request in Github term..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*Cn_9cTLYq9vMzF1ui_R5kg.png" /><figcaption>Gitlab CI Pipeline with MR Build</figcaption></figure><blockquote>With MR Build, instead of running test after the merge, it actually merges the source branch to the target branch within CI environment, and run tests on the merged code.</blockquote><h3>Why do we need MR Build?</h3><p>Imagine you are the maintainer of a repo and with the responsibility of approving MR. While each feature branch can pass their own tests, how can you be sure that the code, if merged, will also pass the test and not mess up with your main branches such as release or master? While Gitlab has the feature of undo a MR, worst thing can happen when someone already forked from that bad commit. One solution is that you can fetch the repo to your local computer, merge locally there and run the test locally. But will it be better if the CI can do this automatically?</p><blockquote>The MR Build makes sure that the merged code passes all the test before the MR is approved.</blockquote><h3>Why do we use AWS Lambda?</h3><p>MR build is an advanced feature that other CIs such as TeamCity, Travis, CircleCI offer. Inconveniently, Gitlab CI doesn’t come with it by default.</p><p>Gitlab CI, however, offers <a href="https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#merge-request-events">webhook</a> for MR events and <a href="https://docs.gitlab.com/ee/ci/triggers/">API</a> to trigger pipeline. If trigger using the webhook, the pipeline will just purely run follow the flow in your .gitlab-ci.yaml without any info about the source/target branch or what is the state of the MR (open, closed, modified). There is no way (as I know) to pass the Webhook JSON object to the pipeline directly.</p><blockquote>As recently working a lot with Lambda, first thing came into my mind was to use Lambda as a webhook receiver, extract useful information and used it to trigger the pipeline</blockquote><h3>How to setup?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*SAE47QsMsfWXvQmjihRfpA.png" /><figcaption>Using Lambda for Gitlab MR Build</figcaption></figure><h4>1. Setup the trigger</h4><p>Go to your Gitlab Project page, Settings — CI/CD — Pipeline triggers.</p><p>Click on Add trigger and <strong>note down the token</strong>. This is the authentication token to trigger the Gitlab CI Pipeline.</p><h4><strong>2. Setup the Lambda function</strong></h4><p><strong>What this function does?</strong></p><ul><li>A webhook receiver</li><li>Process the MR event and extract information</li><li>Trigger the pipeline above if needed</li></ul><p><strong>Writing our Lambda function</strong></p><p>The <a href="https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#merge-request-events">MR event message</a> contains all the information about the MR. We would need to extract:</p><ul><li>Project ID</li><li>Source branch</li><li>Target branch</li><li>MR Status</li><li>Changes in MR</li><li>X-Gitlab-Token for authentication to trigger the pipeline. This is the same token we created above in step 1. In the later part, we will cover on how to pass this token to Lambda.</li></ul><p>First, we extract all the information needed.</p><pre>  const token = event.headers[&#39;X-Gitlab-Token&#39;];<br><br>  const body = JSON.parse(event.body);<br>  console.log(body); // Cloudwatch Debug Message<br>  <br>  const project_id = body.project.id;<br>  const merge_request = body.object_attributes;<br>  const source_branch = merge_request.source_branch;<br>  const target_branch = merge_request.target_branch;<br>  const changes = body.changes;</pre><p>Then, we check for condition to trigger MR pipeline. In my case, I only want to trigger MR build if the target branch is a main branch, the MR is opened and there is code changes.</p><pre>if ( [&#39;master&#39;,&#39;develop&#39;].includes(target_branch) &amp;&amp;<br>        merge_request.state == &#39;opened&#39; &amp;&amp;<br>        typeof changes.total_time_spent !== `undefined`<br>      ) {<br>...<br>}</pre><p>The Gitlab web-hook body doesn’t explicitly have a key to indicate there is any code changed, but I found that every time there is, the body will contain the key total_time_spent. It is a bit hackish at the moment, we will update the code once there is better indicator to be used, please leave a comment below if you found a better way.</p><p>Next, once above condition is met, we would need to trigger the pipeline together with necessary information. We want to pass the source branch via ref , TARGET_BRANCH and the token.</p><pre>// Construct Gitlab pipeline trigger formData &amp; url<br>    const formData = {<br>      &#39;token&#39;: token,<br>      &#39;ref&#39;: source_branch,<br>      &#39;variables[TARGET_BRANCH]&#39;: target_branch<br>    };<br>    const url = &#39;https://gitlab.com/api/v4/projects/&#39; + project_id + &#39;/trigger/pipeline&#39;;</pre><p>Once the formData and url are constructed, we can start trigger the pipeline.</p><pre>request.post({url: url, form: formData}, function optionalCallback(err,res,body) {<br>      if (err) {<br>        return console.error(err);<br>        callback(null, {<br>          statusCode: 403,<br>          body: JSON.stringify({<br>            message: &#39;Error triggering pipeline: &#39; + err<br>          })<br>        });<br>      }<br>      console.log(body);  // Cloudwatch debug message<br>      callback(null, {<br>        statusCode: 200,<br>        body: JSON.stringify({<br>          message: &#39;Pipeline trigger successful.&#39;<br>        })<br>      });<br>    });</pre><p>Our complete Lambda function as below:</p><pre>&#39;use strict&#39;;<br><br>const request = require(&#39;request&#39;);<br><br>module.exports.<a href="https://z5s9rqt72b.execute-api.ap-southeast-1.amazonaws.com/dev/mrevent">glmr</a> = (event, context, callback) =&gt; {<br>  const token = event.headers[&#39;X-Gitlab-Token&#39;];<br><br>  const body = JSON.parse(event.body);<br>  console.log(body); // Cloudwatch Debug Message</pre><pre>const project_id = body.project.id;<br>  const merge_request = body.object_attributes;<br>  const source_branch = merge_request.source_branch;<br>  const target_branch = merge_request.target_branch;<br>  const changes = body.changes;<br><br>  /* Only process MR to master/develop branch<br>  /  and state is open.<br>  /  To avoid rerun the pipeline when MR<br>  /  has non code related changes, we check for<br>  /  changes.total_time_spent<br>  */<br>  if ( [&#39;master&#39;,&#39;develop&#39;].includes(target_branch) &amp;&amp;<br>        merge_request.state == &#39;opened&#39; &amp;&amp;<br>        typeof changes.total_time_spent !== `undefined`<br>      ) {<br>    // Construct Gitlab pipeline trigger formData &amp; url<br>    const formData = {<br>      &#39;token&#39;: token,<br>      &#39;ref&#39;: source_branch,<br>      &#39;variables[TARGET_BRANCH]&#39;: target_branch<br>    };<br>    const url = &#39;https://gitlab.com/api/v4/projects/&#39; + project_id + &#39;/trigger/pipeline&#39;;<br><br>    // Cloudwatch debug message<br>    console.log(&#39;Trigger URL: &#39; + url + &#39;\nTrigger Formdata: &#39;);<br>    console.log(formData);<br><br>    request.post({url: url, form: formData}, function optionalCallback(err,res,body) {<br>      if (err) {<br>        return console.error(err);<br>        callback(null, {<br>          statusCode: 403,<br>          body: JSON.stringify({<br>            message: &#39;Error triggering pipeline: &#39; + err<br>          })<br>        });<br>      }<br>      console.log(body);  // Cloudwatch debug message<br>      callback(null, {<br>        statusCode: 200,<br>        body: JSON.stringify({<br>          message: &#39;Pipeline trigger successful.&#39;<br>        })<br>      });<br>    });<br><br>  } else {<br>      callback(null, {<br>        statusCode: 403,<br>        body: JSON.stringify({<br>          message: &#39;Target branch not master/develop or MR is closed.&#39;<br>        })<br>      });<br>  }<br>};</pre><p>Next, we need to setup an API endpoint so the Gitlab web-hook can call our Lambda function.</p><p><strong>Setup the API Endpoint</strong></p><p>In this setup, I used the <a href="https://serverless.com/framework/docs/providers/aws/guide/intro/">Serverless framework</a>. Using Serverless framework, I can focus on writing the Lambda function and let the framework do all the setup job. Once deployed, you will receive a API endpoint similar to this:</p><p><a href="https://z5s9rqt72b.execute-api.ap-southeast-1.amazonaws.com/dev/mrevent">https://z0x1rim91a.execute-api.us-west-2.amazonaws.com/glmr</a></p><p>We will use Gitlab Webhook to trigger this endpoint when there’s a MR.</p><h4><strong>3. Setup Gitlab Webhook for MR event</strong></h4><p>Go to your Gitlab Project page, Settings — Integration. Setup a web-hook for Merge Request event using the Lambda URL in step 2 and the token we created in step 1.</p><p>You can trigger a test event to check if our Lambda function is actually working. The Lambda function log can be found in Cloudwatch.</p><h4>4. Setup your MR job in .gitlab-ci.yml</h4><p>I won’t cover in detail on how to set up your merge request build job in .gitlab-ci.yml within this post.</p><p>Generally, based on the source/target variables received from Lambda, I trigger a local code merge within CI environment, and run test on this merged build.</p><h3>Result</h3><p>You will be able to see immediate new job triggered inside your MR:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*vO8WrzvjRRfOpPnKOyXDIQ.png" /><figcaption>See the extra step?</figcaption></figure><p>The pipeline in MR will looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*Cn_9cTLYq9vMzF1ui_R5kg.png" /><figcaption>Gitlab CI Pipeline with MR Build</figcaption></figure><h3>Conclusion</h3><p>With a simple Lambda function, we have been able to create Merge Request build with Gitlab CI. With MR Build, now we are able to detect problems early and allow our main branches to be updated with much higher confidence .</p><p><em>P/S: While the post doesn’t cover security aspect of your Lambda API endpoint, as the endpoint is public in the Internet, it is recommended to implement some kind of authorization along with it.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae45cdc62e3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setup AWS Lambda permission to query AWS Elasticsearch and send notification to AWS SNS]]></title>
            <link>https://medium.com/@lichnguyen/setup-aws-lambda-permission-to-query-aws-elasticsearch-and-send-notification-to-aws-sns-126ed2181b84?source=rss-934352b0023c------2</link>
            <guid isPermaLink="false">https://medium.com/p/126ed2181b84</guid>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[aws-sns]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[vpc]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Lich Nguyen]]></dc:creator>
            <pubDate>Sun, 25 Nov 2018 14:18:26 GMT</pubDate>
            <atom:updated>2018-12-27T13:34:22.125Z</atom:updated>
            <content:encoded><![CDATA[<h3>Permission for AWS Lambda in VPC</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wzWUZ05IBI5eEWF3uBAoUA.jpeg" /><figcaption>Lambda or Half-Life?</figcaption></figure><p>Recently, I had the opportunity to use AWS Lambda to automate a lot of trivial tasks.</p><p>One day, I was hitting roadblock when trying to configure a Lambda function to query AWS Elasticsearch which is inside a VPC, then send notification to SNS.</p><p>We will break the problem into two parts:</p><ul><li>Lambda cannot access resources within your VPC</li><li>Once Lambda run inside a VPC, it cannot send notification to AWS SNS</li></ul><h3>Allow Lambda to access resource within your VPC</h3><p>By default, Lambda will run in AWS managed resources, in AWS managed VPC and communicate to your resource via the Internet.</p><p>To access private resources within a VPC, Lambda needs a VPC network endpoint. Thus, it needs permission to create a Elastic Network Interface (ENI) in your VPC.</p><p>From official AWS documentation:</p><p><a href="https://docs.aws.amazon.com/lambda/latest/dg/vpc.html">https://docs.aws.amazon.com/lambda/latest/dg/vpc.html</a></p><blockquote>The Lambda function execution role must have permissions to create, describe and delete ENIs. AWS Lambda provides a permissions policy, AWSLambdaVPCAccessExecutionRole, with permissions for the necessary EC2 actions (ec2:CreateNetworkInterface, ec2:DescribeNetworkInterfaces, and ec2:DeleteNetworkInterface) that you can use when creating a role</blockquote><p>Simply add these permissions into the Lambda IAM roles policy as below:</p><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: [<br>                &quot;ec2:CreateNetworkInterface&quot;,<br>                &quot;ec2:DescribeNetworkInterfaces&quot;,<br>                &quot;ec2:DeleteNetworkInterface&quot;<br>            ],<br>            &quot;Resource&quot;: &quot;*&quot;<br>        }<br>    ]<br>}</pre><p>From there, AWS will handle all the ENI creation/deletion tasks. Pretty much that’s all for Lambda to access the VPC resources.</p><h3>Create VPC Endpoint for Lambda to send notification to AWS SNS</h3><p>However, when trigger inside a VPC, the Lambda function cannot send notification to AWS SNS.</p><p>Why does it happen? As we know, AWS SNS is also a AWS managed service in separate AWS managed VPCs. To send data to SNS, we need some kind of network link to it.</p><p>Firstly, give Lambda permission to publish to AWS SNS topics.</p><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Sid&quot;: &quot;VisualEditor0&quot;,<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: [<br>                &quot;sns:Publish&quot;,<br>                &quot;sns:Subscribe&quot;<br>            ],<br>            &quot;Resource&quot;: &quot;arn:aws:sns:*:*:*&quot;<br>        }<br>    ]<br>}</pre><p>Secondly, we need to setup a network link to AWS SNS. How do we do this? AWS has a very detailed documentation on this:</p><p><a href="https://docs.aws.amazon.com/vpc/latest/userguide/vpc-endpoints.html">AWS PrivateLink concepts</a></p><blockquote>A VPC endpoint enables you to privately connect your VPC to supported AWS services and VPC endpoint services powered by PrivateLink without requiring an internet gateway, NAT device, VPN connection, or AWS Direct Connect connection. Instances in your VPC do not require public IP addresses to communicate with resources in the service. Traffic between your VPC and the other service does not leave the Amazon network.</blockquote><p>Go to VPC — VPC Endpoints — and Create Endpoint for your VPC.</p><p>Next, give Lambda permission to access the VPC Endpoint:</p><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;:[{<br>    &quot;Effect&quot;:&quot;Allow&quot;,<br>    &quot;Action&quot;:&quot;ec2:*VpcEndpoint*&quot;,<br>    &quot;Resource&quot;:&quot;*&quot;<br>    }<br>  ]<br>}</pre><p>That’s pretty much it. Now go ahead and see if it’s working now.</p><p>Let me know in the comment if you have any trouble setting this up.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=126ed2181b84" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>