<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohsiur Rahman</title>
    <description>The latest articles on DEV Community by Mohsiur Rahman (@mohsiur).</description>
    <link>https://dev.to/mohsiur</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F657790%2F19e37911-4df8-411b-a9af-c78b04e7a09b.jpeg</url>
      <title>DEV Community: Mohsiur Rahman</title>
      <link>https://dev.to/mohsiur</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohsiur"/>
    <language>en</language>
    <item>
      <title>Terraform + yaml = ❤️</title>
      <dc:creator>Mohsiur Rahman</dc:creator>
      <pubDate>Mon, 28 Jun 2021 14:41:37 +0000</pubDate>
      <link>https://dev.to/mohsiur/terraform-yaml-256l</link>
      <guid>https://dev.to/mohsiur/terraform-yaml-256l</guid>
      <description>&lt;p&gt;Managing infrastructure on the cloud these days can be done in numerous ways, tools like Terraform, Pulumi, Cloudformation, and CDK have propelled the area of Infrastructure as Code(IaC) into new heights. While most of these tools require some basic programming knowledge or even learning new languages, like Terraform HCL, the true power lies in building a platform using these tools.&lt;/p&gt;

&lt;p&gt;When building infrastructure, as infrastructure developers we tend to create it in ways where the author can create a new infrastructure easily, but not the remainder of the organization. This usually happens when as engineers we think the tool is built only for "us", however this can be avoided by thinking about &lt;a href="https://www.thoughtworks.com/insights/articles/infrastructure-as-product"&gt;infrastructure as a product&lt;/a&gt; where our customer is the engineering team as a whole, this helps in ensuring anyone can build new infrastructure easily without prior knowledge about the tools being used.&lt;/p&gt;

&lt;p&gt;This was one of the goals for my team at  &lt;a href="https://accounts.welcomesoftware.com/signup?utm_source=welcome&amp;amp;utm_medium=internal&amp;amp;utm_campaign=welcomigos&amp;amp;utm_term=signup&amp;amp;utm_content=18"&gt;Welcome&lt;/a&gt;  (formerly Newscred) when I first joined. Prior to me joining, the team had built an in house solution using &lt;code&gt;aws cli&lt;/code&gt; and &lt;code&gt;boto3&lt;/code&gt;, while this worked it was missing key features like state management, allowing quick disaster recovery, and maintainability. We decided to use terraform as our tool in the end and in this article I will be covering how we used Terraform and yaml files to be in sync with our in house solutions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Yaml?
&lt;/h2&gt;

&lt;p&gt;Our in-house solution maintains major resources like security groups and iam roles using yaml for configuration as code. &lt;/p&gt;

&lt;p&gt;My initial thoughts to migrate was to use Terraform with &lt;code&gt;.tfvars&lt;/code&gt; files to maintain our configurations. However, as soon as we wrote our initial configurations, there was a large unreadability issue and an increasing complexity of duplicate configurations stored in different formats, to ensure we keep a single source of truth we made the choice of implementing our existing pattern of using &lt;code&gt;yaml&lt;/code&gt; configurations for our migration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The implementation was written using terraform version &lt;code&gt;0.14.6&lt;/code&gt;, and has not been tested on &lt;code&gt;1.0.0&lt;/code&gt; yet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The following implementation uses a lot of advanced tools of terraform like &lt;code&gt;for_each&lt;/code&gt;, &lt;code&gt;lookup&lt;/code&gt; and &lt;code&gt;flatten&lt;/code&gt;. It is highly recommended to go through the  &lt;a href="https://www.terraform.io/docs/language/resources/index.html"&gt;docs&lt;/a&gt;  to get a better understanding of how it works. &lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up the yaml files
&lt;/h3&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Reading the Yaml files
&lt;/h3&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;yamldecode&lt;/code&gt; -  helps in formatting your yaml file into a map object that terraform can read from.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;flatten&lt;/code&gt; -  helps in restructuring nested maps into a more readable map that is easier to access by terraform functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating all resources
&lt;/h3&gt;

&lt;p&gt;Based on the configurations above, we can now create &lt;code&gt;n&lt;/code&gt; sqs queues just by adding new configurations in the &lt;code&gt;yaml&lt;/code&gt; file. The following file helps in doing that, using &lt;code&gt;for_each&lt;/code&gt; &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for queue in local.sqs_standard_queues : queue.name =&amp;gt; queue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above statement iterates through our list of flattened queues and maps them to a key value pair. In our scenario the key is the name of the queue, and the value is the map object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"production-example-queue-dlq":   {
    "access_policy": "basic"
    "dlq": null
    "name": "production-example-queue-dlq"
    "type": "standard"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;for_each&lt;/code&gt; -  Iterate through each key in the map generated above and creates a resource as shown below in the plan.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws_sqs_queue.sqs_standard_queues["production-example-queue-dlq"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The above statement is also how we need to reference the queue in a different resource&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;if&lt;/code&gt; - Helps to condense the list based on meeting the criteria if the key dlq exists or not.&lt;br&gt;
&lt;code&gt;each.value.*&lt;/code&gt; - each references to the &lt;code&gt;key&lt;/code&gt;. &lt;code&gt;value&lt;/code&gt; references to the value of the key and the &lt;code&gt;*&lt;/code&gt; can be any of the keys that we set in our locals.&lt;/p&gt;
&lt;h3&gt;
  
  
  Debugging Tips
&lt;/h3&gt;

&lt;p&gt;Terraform has a lot of useful functions, but sometimes it becomes hard to debug situations with complex maps. In order to debug you can use the terraform console . This helps in calling your local resources and seeing the &lt;code&gt;map&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example in order to debug the above example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform console
&amp;gt; local.sqs_queues #prints out the yaml file decoded
&amp;gt; local.sqs_standard_queues #prints out the flattened object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Using only terraform limits us to writing configurations in &lt;code&gt;.tfvars&lt;/code&gt; files to abstract away complexity from our infrastructure users, which in turn introduces a burden on our users to  understand how terraforms language works. By leveraging yaml for configuration as code, as our user interface, we empower our infrastructure users to easily  create new resources and stacks using a language they are already familiar with.&lt;/p&gt;

&lt;p&gt;This will allow the larger engineering team to bring up services quickly and with less wait times. We have already implemented this for our standalone AWS services successfully, and are currently in the process of migrating our more complex stacks like EKS clusters using Terraform + Yaml.&lt;/p&gt;

&lt;p&gt;Special mention to  &lt;a href="https://github.com/pratikjoy7"&gt;Pratik Saha&lt;/a&gt;  who had figured out how to convert yaml files into terraform objects.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>terraform</category>
      <category>aws</category>
      <category>yaml</category>
    </item>
  </channel>
</rss>
