{"id":6034,"date":"2017-10-03T05:47:13","date_gmt":"2017-10-03T05:47:13","guid":{"rendered":"https:\/\/blog.cloudthat.com\/?p=6034"},"modified":"2024-06-25T11:11:41","modified_gmt":"2024-06-25T11:11:41","slug":"terraform-is-it-flexible-than-cloudformation","status":"publish","type":"blog","link":"https:\/\/www.cloudthat.com\/resources\/blog\/terraform-is-it-flexible-than-cloudformation","title":{"rendered":"Terraform. Is it flexible than CloudFormation?"},"content":{"rendered":"<p>In the time when there is a lot of focus on Infrastructure and Operations automation, won\u2019t it be amazing if I say the whole <strong>Infrastructure<\/strong> you need can be deployed by just defining a piece of <strong>code? <\/strong>Tools like CloudFormation on AWS and Terraform are major players in the Infrastructure as a Code space. But which of these tools suit your requirement and which is the best among them? By end of this post you would be able to infer answers to this question or would be able to get enough pointers to continue your research.<\/p>\n<p>Terraform is a tool by HashiCorp used for building, changing and versioning infrastructure. Before starting the execution, Terraform generates an execution plan elaborating what actions it will take to reach the goal. The whole infrastructure is controlled by Command-line Interface. Terraform can generate and execute incremental plans to update the existing infrastructure to newly described state. In .tfstate the whole infrastructure is defined as JSON that can be easily used for documenting the infrastructure. The infrastructure managed by Terraform are compute instances, storage and networking, DNS and many more.<\/p>\n<p>Why Terraform will be a tycoon of the IaC tool market?<\/p>\n<ol>\n<li>If we use CloudFormation, then the impact will be restricted to AWS only. People working with multi-cloud need to define and redefine for each cloud provider, but if we use Terraform we can define it once and use across multiple cloud providers. Therefore, we can say that Terraform is one stop solution for IaC tool.<\/li>\n<li>Configuration tools like Puppet and Chef, install and manage software on existing servers, whereas Terraform has a different way of dealing, it provisions the server itself.<\/li>\n<li>Other tools can mutate the whole infrastructure resulting in breakage of the services running on it. Suppose, you make a change of version of a software on a node, then it starts producing a different changelog over a span of time, whereas in Terraform this issue does not take place.<\/li>\n<li>The rollback mechanism of Terraform works in this manner; if a resource is created and fails provisioning, Terraform marks the resource as \u201ctainted\u201d and in the next execution plan, it will remove the tainted resources and execute again unlike CloudFormation rolling back completely.<\/li>\n<li>In CloudFormation, when we want to launch more instances we need to write the definition for each instance, whereas in Terraform this is not the case i.e. all we need to mention is the number of instances required.<\/li>\n<\/ol>\n<p><strong><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/aws-instance.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6036\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/aws-instance.png\" alt=\"aws instace\" width=\"356\" height=\"106\" \/><\/a><\/strong><\/p>\n<p>This launches 10 ec2 instances.<strong><br \/>\n<\/strong><\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/aws-instances.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6037\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/aws-instances.png\" alt=\"aws instances\" width=\"610\" height=\"160\" \/><\/a><\/p>\n<p>But, when we change it to 3 (we specify only what is our requirement, need not calculate) instances, it kills 7 instances.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/aws-instance-count.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6038\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/aws-instance-count.png\" alt=\"aws instance count\" width=\"340\" height=\"102\" \/><\/a><\/p>\n<p>And the instances get destroyed (it kills last 7 instances) and 3 remains, unlike other tools<\/p>\n<p>What is so cool about Terraform?<\/p>\n<ol>\n<li>We can write the desired code either in a txt format or JSON format. If the code is supposed to run programmatically, then it is recommended to use JSON format.<\/li>\n<li>You can visualize the plan before you execute. This may be helpful in eliminating the undesired steps (catching up bugs). If we run \u201cTerraform plan\u201d, we can visualize our plan like the below image:<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/terraform-plan.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6039\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/terraform-plan.png\" alt=\"terraform paln\" width=\"588\" height=\"493\" \/><\/a><\/li>\n<li>You can generate graph for all your resources that are in use which will give an easy idea about the architecture of the infrastructure you are working in, that too by just adding a simple command \u201c$Terraform graph | dot -Tpng &gt; graph.png\u201d. Here is an example how the graph will look:<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/terraform-graph.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6042\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/terraform-graph.png\" alt=\"terraform graph\" width=\"700\" height=\"233\" \/><\/a><br \/>\nThis will ease your knowing about the dependencies too and this will also help in applying changes to the infrastructure.<\/li>\n<li>Suppose, you plan to make few changes and you write the changelog.tf accordingly, then all you need to do is run it by giving the command \u201cTerraform apply\u201d and changes will take place. How Terraform maps the changes to which resource? All this is taken care by Terraform state when we give the command \u201cTerraform apply\u201d. It also builds a file named \u201cterafform.tfstate\u201d. This file contains a custom JSON format that records a mapping from our definition to the actual one.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/terafform-tfstate.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6043\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/terafform-tfstate.png\" alt=\"terraform tfstate\" width=\"595\" height=\"46\" \/><\/a><\/li>\n<li>When you use Terraform you need not bother to write the Terraform template twice i.e. once in Development and again in Production. All you need to do is define this Template module once and run anywhere by just calling the module.<\/li>\n<li>When you want to abort your entire infrastructure, all you need to do is give a command \u201cTerraform destroy\u201d. This will terminate all your created resources and you need not worry about manually terminating them.<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/terraform-plan1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6044\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/terraform-plan1.png\" alt=\"\" width=\"588\" height=\"493\" \/><\/a><\/li>\n<\/ol>\n<p>\u201cAll that glitters is not gold\u201d. With so many advantages Terraform also has certain shortcomings with it.<\/p>\n<ol>\n<li>Firstly, Terraform has no automatic rollback like CloudFormation. So, at the time of need we need to manually find out what went wrong or we need to rollback to the previous version of the code and re-deploy.<\/li>\n<li>Secondly, whenever Terraform apply takes place it just compares the .tfstate and .tf files and creates the resources which are not present. If .tfstate file is deleted, then it will recreate the resources, so constantly sharing of this .tfstate file is required.<\/li>\n<li>Thirdly, it does not support conditional statements and looping, therefore we use tricks to overcome it. In the above example, when we launched similar kind of instances we mentioned the number of instance as count = \u201cvalue\u201d.<\/li>\n<\/ol>\n<p>Seems as far as ease of accessibility is concerned, Terraform has won the battle over CloudFormation.<\/p>\n<p>I hope this article gave you some weighted pointers to opt for the best IaC tool for your infrastructure. Please feel free to post your views in the comment section below, I will be happy to discuss.<\/p>\n<p>To know more about our training services, visit <strong><a href=\"http:\/\/www.cloudthat.in?utm_source=blog-website&amp;utm_medium=text-link&amp;utm_content=terraform-is-it-flexible-than-cloudformation&amp;utm_campaign=training-website\">www.cloudthat.in<\/a><\/strong> and for consulting services, visit <strong><a href=\"http:\/\/www.cloudthat.com?utm_source=blog-website&amp;utm_medium=text-link&amp;utm_content=terraform-is-it-flexible-than-cloudformation&amp;utm_campaign=consulting-website\">www.cloudthat.com<\/a><\/strong><\/p>\n","protected":false},"author":219,"featured_media":6078,"parent":0,"comment_status":"open","ping_status":"open","template":"","blog_category":[3606],"user_email":"prarthitm@cloudthat.com","published_by":"324","primary-authors":"","secondary-authors":"","acf":[],"_links":{"self":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/6034"}],"collection":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/users\/219"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/comments?post=6034"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/6034\/revisions"}],"predecessor-version":[{"id":45900,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/6034\/revisions\/45900"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/media?parent=6034"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog_category?post=6034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}