<?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 Vish on Medium]]></title>
        <description><![CDATA[Stories by Vish on Medium]]></description>
        <link>https://medium.com/@vishal-m-bhalla?source=rss-e83d4fc8213b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*OGZeSPgb8Zh6Kt7FcGhJMw.jpeg</url>
            <title>Stories by Vish on Medium</title>
            <link>https://medium.com/@vishal-m-bhalla?source=rss-e83d4fc8213b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 03:46:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vishal-m-bhalla/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[Make your DevOps life a little easier]]></title>
            <link>https://vishal-m-bhalla.medium.com/make-your-devops-life-a-little-easier-c75f74dc5459?source=rss-e83d4fc8213b------2</link>
            <guid isPermaLink="false">https://medium.com/p/c75f74dc5459</guid>
            <category><![CDATA[zsh]]></category>
            <category><![CDATA[stern]]></category>
            <category><![CDATA[virtualenv]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[oh-my-zsh]]></category>
            <dc:creator><![CDATA[Vish]]></dc:creator>
            <pubDate>Wed, 08 Dec 2021 20:39:25 GMT</pubDate>
            <atom:updated>2021-12-08T20:39:25.193Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tL7wNuNlnblB5p7w" /><figcaption>Photo by <a href="https://unsplash.com/@hnhmarketing?utm_source=medium&amp;utm_medium=referral">Hunter Haley</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Further to <a href="https://medium.com/airwalk/3-tools-to-make-your-terraform-life-easier-47c2fd078800">this article</a> written by my colleague James at Airwalk Reply, I thought I’d highlight some lesser known <strong>free</strong> software tools that have helped me over the years working with infrastructure as code.</p><p>All these tools can be used on any Linux distro and Mac OS X.</p><p>First off…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/398/1*D3e9UwCA9QWvaGXvoiJUXA.png" /></figure><p>OK, so this is not a lesser known tool…nor is it a tool :-) Worth mentioning though, as I often come across people who haven’t heard of it. It’s the base that makes everything easier, quicker and cool.</p><p>It’s an excellent framework for managing <a href="https://en.wikipedia.org/wiki/Z_shell">Zsh shell</a> configuration. It supports plugins and themes that just make terminal/CLI work a lot smoother. My core tools at the time of writing are Terraform, AWS CLI, Python with virtualenv and Kubernetes based things (docker, kubectl, helm etc.).</p><p>Once <a href="https://ohmyz.sh/#install">installed</a>, you can specify the <a href="https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins">plugins</a> and <a href="https://github.com/ohmyzsh/ohmyzsh/wiki/Themes">theme</a> you want in your ~/.zshrc file. Here’s what I use:</p><pre>ZSH_THEME=&quot;agnoster&quot;</pre><pre>plugins=(<br>  git<br>  aws<br>  pip<br>  python<br>  terraform<br>  virtualenv<br>  virtualenvwrapper<br>  docker<br>  kubectl<br>  helm<br>  minikube<br>  zsh-autosuggestions<br>)</pre><p>Be careful how many plugins you use. It can start to slow your terminal down if there are too many. And if you use the <a href="https://github.com/agnoster/agnoster-zsh-theme">agnoster</a> theme (and why wouldn’t you? Just look at it…it’s beautiful!) you may need to install some fonts on your machine and configure your terminal app (I use <a href="https://iterm2.com/">iTerm2</a> on my Mac):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OmRZxQz8tZi2QW6SF6SS1Q.png" /></figure><p>Now that you have a Zsh shell that looks pretty, you can play around with what you can do with your chosen plugins. The main advantages are command line completion and your command prompt, which of course can be customized to your heart&#39;s content. I like to keep mine reasonably simple.</p><p>It’s very handy having some shortcuts like asp to switch between AWS CLI profiles (if you have some set up) and using double tab to show all the options available for some of the commands you have plugins for. A quick video below to show some examples and how the command prompt changes depending on your setup:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*PlxI_tYjTpFdfJ3wXxgX5g.gif" /></figure><p>If you decide to use it, then consider showing them some love by <a href="https://shop.planetargon.com/collections/oh-my-zsh">buying their merch</a>.</p><h3>Virtualenv Wrapper</h3><p>It’s basically <a href="https://virtualenvwrapper.readthedocs.io/en/latest/install.html">Python virtual environment for lazy people</a>. After installation, just set the following in your user profile somewhere:</p><pre>export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3<br>export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv<br>export WORKON_HOME=~/.virtualenvs<br>source /usr/local/bin/virtualenvwrapper.sh</pre><p>Then you can use the following commands to create, use and destroy Python virtual environments:</p><p>Create a virtual environment:</p><pre>mkvirtualenv my-project-venv</pre><p>List all virtual environments:</p><pre>lsvirtualenv</pre><p>Use a venv or switch to it:</p><pre>workon my-project-venv</pre><p>Come out of the virtual environment:</p><pre>deactivate</pre><p>Delete a virtual environment</p><pre>rmvirtualenv my-project-venv</pre><p>And if you have oh my ZSH installed, and the virtualenv plugin, command auto completion should work, and you can see what else you can do by referring to the <a href="https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/virtualenvwrapper">plugin documentation</a>.</p><h3>Stern</h3><p>As described on their <a href="https://github.com/stern/stern">github readme page</a>:</p><p><em>Stern allows you to </em><em>tail multiple pods on Kubernetes and multiple containers within the pod. Each result is color coded for quicker debugging.</em></p><p>Instead of me trying to explain how it works, this short video says it all:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F-2L1GHTBteY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D-2L1GHTBteY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-2L1GHTBteY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b9de11d49ab83d9ccd7009983539807/href">https://medium.com/media/1b9de11d49ab83d9ccd7009983539807/href</a></iframe><h3>Terraform-docs</h3><p><a href="https://github.com/terraform-docs/terraform-docs">Terraform-docs</a> is a powerful utility to generate documentation from Terraform modules in various output formats.</p><p>It is very well <a href="https://terraform-docs.io/user-guide/introduction/">documented</a> and is a tool that helps me make my Terraform modules have great readmes. Give it a go!</p><p>Make sure all your Terraform variables are well formed with meaningful descriptions, and types. After installation, run:</p><pre>terraform-docs markdown .</pre><p>You’ll see some lovely markdown output to your terminal. That should obviously be redirected to a readme file or wherever you wish.</p><p><a href="https://github.com/AirWalk-Digital/terraform-aws-codebuild-deploy-tf#inputs">Here’s an example.</a></p><p>If you decide to use Terraform-docs, you should look to incorporate it into your automated workflow somehow. Either into your code development workflow for your Terraform modules (perhaps a pre or post commit hook) or into your CI/CD pipeline if that makes more sense.</p><p>I first came across this tool when working with <a href="https://github.com/cloudposse/build-harness">Cloud Posse’s build-harness</a>. They do some very cool things with Terraform, so check out their stuff.</p><p><strong>If you have any other suggestions or pointers, leave a comment below.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c75f74dc5459" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cron in a Pod]]></title>
            <link>https://vishal-m-bhalla.medium.com/cron-in-a-pod-6fdf6676c623?source=rss-e83d4fc8213b------2</link>
            <guid isPermaLink="false">https://medium.com/p/6fdf6676c623</guid>
            <category><![CDATA[pods]]></category>
            <category><![CDATA[cron]]></category>
            <category><![CDATA[shell-script]]></category>
            <category><![CDATA[environment-variables]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Vish]]></dc:creator>
            <pubDate>Thu, 18 Mar 2021 10:16:55 GMT</pubDate>
            <atom:updated>2021-03-18T10:19:40.871Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oZ5PTpMBCeoscrzu" /><figcaption>Photo by <a href="https://unsplash.com/@aronvisuals?utm_source=medium&amp;utm_medium=referral">Aron Visuals</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>On a recent project, I came across a problem where I needed to run a cron job inside a pod.</p><p>The cron job had to connect to a database to do its thing and all the information required for the script was already available in the pod’s environment variables.</p><p>Now I know there is a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/">Kubernetes CronJob resource</a> that can be used, but this quick blog post is based on the use case of just a normal Linux cron job, inside a Kubernetes pod, that requires access to environment variables.</p><p>All the code is available here if required:</p><p><a href="https://github.com/vishbhalla/cron-in-a-pod">https://github.com/vishbhalla/cron-in-a-pod</a></p><p>Let’s run through a simple example. Below is noddy script that echoes the contents of an environment variable imaginatively called $MY_ENV_VARIABLE:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8830caf8b29f34ee58d7dbd1e0a55b40/href">https://medium.com/media/8830caf8b29f34ee58d7dbd1e0a55b40/href</a></iframe><p>Below is a cron line that can be installed in Linux in order to run that script and output the results to a log file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c35216a0cf5e321f8d5f97bbf5efa6e3/href">https://medium.com/media/c35216a0cf5e321f8d5f97bbf5efa6e3/href</a></iframe><p>Now here is a Dockerfile that runs Ubuntu, copies the above files over, installs the cron job to run the script, and then tails the log file created by the cron job:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1943ebfea8d4751b6af828d5400ee67f/href">https://medium.com/media/1943ebfea8d4751b6af828d5400ee67f/href</a></iframe><p>I’m working locally with <a href="https://minikube.sigs.k8s.io/docs/">minikube</a>, so I can build via:</p><pre># eval $(minikube docker-env)<br># docker build . -t echo_env</pre><p>Lastly, here is a Kubernetes manifest that simply starts a pod with the above docker image, setting the value of our wonderfully named environment variable:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68addae5860b5130936a0d60dda42c6a/href">https://medium.com/media/68addae5860b5130936a0d60dda42c6a/href</a></iframe><p>So we can now apply the manifest via:</p><pre># kubectl apply -f ./pod.yaml</pre><p>And see the the output of the cron job every minute via:</p><pre># kubectl logs echo-env -f</pre><p>As you can see:</p><pre>* Starting periodic command scheduler cron<br>…done.<br>MY_ENV_VARIABLE is empty!</pre><p>Why? Because Cron is designed to run in its own fresh shell, with limited environment variables. So we need to find a way of grabbing them from the pod’s environment, and making them available to the cron job.</p><p>One way to achieve this is to have the pod spit out all the environment variables into a file and make them available upon start up. Take a look at the ENTRYPOINT in the below Dockerfile:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/074f86819cd46dba389c3b6a831085fc/href">https://medium.com/media/074f86819cd46dba389c3b6a831085fc/href</a></iframe><p>I’ve excluded the environment variable LS_COLORS above simply because there are some special characters in it, and I didn’t need access it, so safer to just remove it.</p><p>Now we can edit the cron job to execute this file before running the script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6282f819e47e6ae0fb38f4f784a5b3dd/href">https://medium.com/media/6282f819e47e6ae0fb38f4f784a5b3dd/href</a></iframe><p>So after building our new docker image, and spinning up a new, updated pod, you should now see the variable being correctly reported in the logs:</p><pre>* Starting periodic command scheduler cron<br>…done.<br>Hello from the environment</pre><p>With this being a Ubuntu image, the better way to obtain the environment variables would be to stick them into /etc/environment via something like:</p><pre># env &gt; /etc/environment</pre><p>But I chose the approach I did as it should work with any Linux variant.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6fdf6676c623" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using the AWS Developer Tools to Deploy Terraform]]></title>
            <link>https://vishal-m-bhalla.medium.com/using-the-aws-developer-tools-to-deploy-terraform-259e71486b5b?source=rss-e83d4fc8213b------2</link>
            <guid isPermaLink="false">https://medium.com/p/259e71486b5b</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[infrastructure-as-code]]></category>
            <category><![CDATA[codebuild]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[codepipeline]]></category>
            <dc:creator><![CDATA[Vish]]></dc:creator>
            <pubDate>Fri, 02 Aug 2019 14:03:14 GMT</pubDate>
            <atom:updated>2019-08-02T14:03:14.062Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k7s8qSFVdU3kkVI1tpFqyw.jpeg" /></figure><p>AWS has a bunch of software tools to allow developers to store, build and deploy code. Being a DevOps engineer I wanted to explore how these tools could be used to deploy an <strong>infrastructure as code</strong> project.</p><p>The <a href="https://aws.amazon.com/products/developer-tools/">AWS Developer Tools</a> (CodeCommit, CodePipeline, CodeBuild and CodeDeploy) are obviously designed to work well with <a href="https://aws.amazon.com/cloudformation/">CloudFormation</a>, but I wanted to explore how one would go about deploying <a href="https://www.terraform.io/">Terraform</a> instead.</p><p>My rough plan was to setup a Terraform project in GitHub (not CodeCommit, as all our other code was already in GitHub. I also wanted the challenge of getting the code from an external repo) and then attempt to deploy that using the AWS developer tools.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*DqldXuoO53SQrT137Op4IQ.png" /></figure><p>Along my journey, I wanted to use the latest versions of everything in order to keep this write up relevant for as long as possible and I also wanted to use the opportunity as a learning exercise. So I decided to take my first foray into <a href="https://www.hashicorp.com/blog/announcing-terraform-0-12">Terraform v0.12</a>.</p><p>To start with, I created <a href="https://github.com/vishbhalla/terraform-aws-hello-world-lambda">this simple Terraform project</a> that just implements a hello world Lambda in Python, along with the required IAM role, policy and permissions. I decided to set up environment vars files for my specific deployment and stored the state in S3. So that means executing my Terraform would need to be done in a specific way. Your mileage may vary. I tested it to make sure it behaved as expected, by running it locally from my laptop.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/caf4c81d1ca37c683b29a7aea8f3143d/href">https://medium.com/media/caf4c81d1ca37c683b29a7aea8f3143d/href</a></iframe><p>The next step was to create a <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html">CodeBuild buildspec</a> file in the root of the repo that will be used to instruct CodeBuild later:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30490cbd954906457c214776cbdc0a71/href">https://medium.com/media/30490cbd954906457c214776cbdc0a71/href</a></iframe><p>The buildspec has 3 phases:</p><ul><li><a href="https://github.com/vishbhalla/terraform-aws-hello-world-lambda/blob/master/buildspec.yml#L4">Install</a>: This is where we download and install Terraform on the image that CodeBuild uses to do its work. Due to wanting to use the latest Ubuntu standard image (version 2.0) that is available with CodeBuild, I had to specify a run-time section as version 2.0 <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/sample-runtime-versions.html">mandates that a runtime-versions section must exist</a> in the buildspec. I just chose python but it isn’t actually used for anything here. CodeBuild also allows to use your own custom images, so you could create your own Docker image with Terraform already baked in, hosted in ECR or elsewhere.</li><li><a href="https://github.com/vishbhalla/terraform-aws-hello-world-lambda/blob/master/buildspec.yml#L12">Build</a>: In a traditional code building process, this is where you would…well…build the software :-) But as we are trying to deploy Terraform here, this is where we will run our Terraform commands in order to deploy our Lambda. So effectively this is the deploy. Hence why we don’t need to use AWS CodeDeploy.</li><li><a href="https://github.com/vishbhalla/terraform-aws-hello-world-lambda/blob/master/buildspec.yml#L18">Post_build</a>: I didn’t really need this section, but I just stuck a completion message in there just for the sake of having something in there.</li></ul><p>CodeBuild makes <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html">environment variables</a> available for you to use. You can see in buildspec,yaml I used $<em>CODEBUILD_SRC_DIR</em> to cd into the root of the source code directory. I also used some custom environment variables such as ${<em>TF_VERSION</em>} that I later setup in the CodeBuild project.</p><p>Next I had to create a CodeBuild project that would checkout the Terraform/Lambda project and execute what’s in the its buildspec.yaml file and a CodePipeline pipeline that triggers the CodeBuild job upon detecting changes in the repo.</p><p>After going through the AWS documentation I managed to create a CodeBuild project with a <a href="https://help.github.com/en/articles/git-automation-with-oauth-tokens">GitHub OAuth token</a> and some environment variables specifying things like the Terraform version to use when executing the hello-world project. In order to keep the GitHub token secure, I added it as a parameter in <a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html">AWS SSM Parameter Store</a> so it could be pulled out from there.</p><p>I then looked at how I could automate the building of such a project, which I achieved with Terraform. After which I cleaned everything up and actually ended up creating a <a href="https://github.com/AirWalk-Digital/terraform-aws-codebuild-deploy-tf">reusable Terraform module</a>. The diagram below depicts the components created by the module, including the IAM resources required:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*ZgnOHYeo17o8KR0oVA80xA.png" /></figure><p>This module can be used as shown in the <a href="https://github.com/AirWalk-Digital/terraform-aws-codebuild-deploy-tf/blob/master/README.md#usage">usage section in the readme</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38611448b98da69936c6d8bf163beb76/href">https://medium.com/media/38611448b98da69936c6d8bf163beb76/href</a></iframe><p>It basically just requires some details about the source repository, some IAM policies and allows you to set some environment variables for use in your buildspec file. The IAM policies of course could be existing AWS ones or be created elsewhere e.g. in the Terraform calling the module. Suffice to say, they should be locked down as much as possible to follow <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege">least privilege principles</a>.</p><p>The module isn’t as polished and flexible as it could be. You may want to adapt and fine-tune it to meet your particular needs. For example, I would probably want a manual approval step before deployment to a production environment, so you could perhaps integrate SNS for this purpose. I just decided to make what I had at the time into a reusable module because it was simple enough to do…pull requests, are of course, welcome.</p><p>As mentioned earlier, I had no use for CodeDeploy, as the build process basically does our deployment for us. So there are no references to CodeDeploy in the Terraform.</p><p>So what we end up with here is a Terraform module that allows you to spin up a CodePipeline pipeline that triggers CodeBuild to run Terraform when something is committed to master in a given git project (our hello-world Lambda Terraform repo).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*F6Sp1-4Wt0-vFAh-CASJvg.jpeg" /></figure><p><a href="https://github.com/vishbhalla/terraform-aws-codebuild-deploy-tf-example">Here is an example project</a> I setup that uses this module to deploy the hello-world Lambda Terraform project.</p><p>An example plan output can be seen <a href="https://github.com/vishbhalla/terraform-aws-codebuild-deploy-tf-example/blob/master/plan.txt">here</a>.</p><p>In order to run builds manually you can simply click the Release Change button in the Pipeline:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YYD68QjlAHgG86UhC4sQ4Q.png" /></figure><p>If you have some issues in the Terraform deployment, you can set the <a href="https://www.terraform.io/docs/internals/debugging.html">TF_LOG</a> environment variable as required in CodeBuild which will enable you to see enhanced log messages from Terraform. This can either be done in Terraform (see module <a href="https://github.com/AirWalk-Digital/terraform-aws-codebuild-deploy-tf/blob/master/README.md">README</a> or the <a href="https://github.com/vishbhalla/terraform-aws-codebuild-deploy-tf-example/blob/master/env_vars/sandbox3.tfvars#L51-L54">example project</a>) or the AWS console (CodeBuild -&gt; Build Projects -&gt; Project -&gt; Edit -&gt; Environment -&gt; Additional Configuration):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*CCTnsZ-vAzLidQ2fPiyHEw.png" /></figure><p>Below is an example of the logs shown during a deployment:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*qhwlukp2LIj9PKOWSBUJXQ.png" /></figure><p><strong>What about Terraform destroy?</strong></p><p>An issue I found I had when looking to tear everything down was that the Lambda Terraform project was not destroyed when destroying the parent Terraform project (that sets up CodePipeline/CodeBuild). So in order to have the ability to destroy a target project, I used another environment variable for the Terraform action to perform (apply or destroy). Again, as this was just an experiment, it was the easy option. I’m sure there are more elegant solutions available. I did consider creating 2 pipelines per project with one being for destroying it, but it just didn’t seem like a sensible solution.</p><p>So in order to destroy everything, I set the CodeBuild environment variable $<em>TF_ACTION</em> to be <em>destroy</em> and then ran the pipeline, which deleted the Lambda and all the IAM resources associated with it. And then from my laptop I ran terraform destroy on the example project to destroy all the CodeBuild/CodePipeline resources.</p><p><strong>What about rollback?</strong></p><p>Currently, there is <a href="https://forums.aws.amazon.com/thread.jspa?messageID=851807&amp;tstart=0">no provision in CodePipeline to roll back</a> or release a previous version. There <a href="https://docs.aws.amazon.com/codedeploy/latest/userguide/deployments-rollback-and-redeploy.html">is in CodeDeploy</a>, but we have not used that service here. Terraform itself doesn’t actually do roll-backs as a design feature. It stores the current state of the environment in <a href="https://www.terraform.io/docs/state/">state files</a>. So the fix would be to roll forward if a successful <em>terraform plan</em> was to fail on <em>terraform apply</em>.</p><p><strong>What’s missing?</strong></p><p>Testing. I have not included any tests with my code, but they could be added as another stage in CodePipeline. You may have to install any software you use for testing (e.g. <a href="https://github.com/gruntwork-io/terratest">Terratest</a>) on the image CodeBuild uses.</p><p><strong>Closing comments</strong></p><p>In conclusion, I’ve managed to achieve my aim, but the final solution didn’t feel very <em>slick </em>(well, not in the way I decided to utilise the services anyway). The AWS developer tools are capable, and flexible enough to cater for a variety of deployment types but probably better suited to traditional software builds and deployments.</p><p>Without CodeDeploy in the mix, visually, the tools don’t offer a good snapshot view of the environment. E.g. traditionally I would want to tag something in Git and then view in my CI/CD tool where that tag was deployed, or see what version my production environment is currently on etc. That isn’t really possible with just CodePipeline/CodeBuild here.</p><p>If you are restricted to using only AWS tools for Terraform deployment, then hopefully this article has highlighted some possibilities. The costs for using the <a href="https://aws.amazon.com/codepipeline/pricing/">CodeBuild</a> and <a href="https://aws.amazon.com/codebuild/pricing/">CodePipeline</a> are very cheap, and may work out much less than perhaps spinning up an EC2 instance running your CI/CD tool of choice. And they do integrate well with the other AWS services. These being serverless services is great and not having to worry about networking is always a plus (unless of course you are running in very secure AWS environments and require the use of proxies to access the internet).</p><p>So it is possible. But as of today, I would probably use something else for automated deployments for Terraform. If and when the AWS developer tools mature a little, then perhaps I will revisit this then.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=259e71486b5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>