<?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>Forem: Daniel Huerta</title>
    <description>The latest articles on Forem by Daniel Huerta (@danihuerta).</description>
    <link>https://forem.com/danihuerta</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%2F519117%2F53ba3632-13e0-4621-9c43-e75e10a8148b.jpg</url>
      <title>Forem: Daniel Huerta</title>
      <link>https://forem.com/danihuerta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/danihuerta"/>
    <language>en</language>
    <item>
      <title>Terraform Associate Certification: Conditional Expression &amp; Local values</title>
      <dc:creator>Daniel Huerta</dc:creator>
      <pubDate>Tue, 24 Aug 2021 16:02:09 +0000</pubDate>
      <link>https://forem.com/danihuerta/terraform-associate-certification-conditional-expression-local-values-3e9p</link>
      <guid>https://forem.com/danihuerta/terraform-associate-certification-conditional-expression-local-values-3e9p</guid>
      <description>&lt;h2&gt;
  
  
  Conditional Expression
&lt;/h2&gt;

&lt;p&gt;If you have programmed in other languages such as C or Javascript you may know the &lt;em&gt;ternary operator&lt;/em&gt;. Well, in Terraform you can use it as well and it is nothing but a conditional expression that uses the value of a bool expression to select one of two values.&lt;/p&gt;

&lt;p&gt;The syntax of it is as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;condition ? True_value : False_value&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If condition is true, then the returned value will be True_value, otherwise will be False_value.&lt;/p&gt;

&lt;p&gt;Let's see an example of this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwfsoqyoxnyt1td4mao2i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwfsoqyoxnyt1td4mao2i.png" alt="Screen Shot 2021-08-24 at 10.47.55"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the amount of aws_instances will be created according to the value of the bool variable named &lt;em&gt;istest&lt;/em&gt;. As you can see in the image, this value is set to false so only one &lt;em&gt;aws_instance.prod will be created&lt;/em&gt; 😉.&lt;/p&gt;

&lt;h2&gt;
  
  
  Local Values
&lt;/h2&gt;

&lt;p&gt;A local value assigns a name to an expression, allowing it to be used multiple times within a module without repeating it.&lt;br&gt;
Let's see an example to understand this better, in the next image you will see that I created a set of tags within a &lt;strong&gt;locals block&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgqiwjp9gw6zkwvulxibj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgqiwjp9gw6zkwvulxibj.png" alt="Screen Shot 2021-08-24 at 10.57.19"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm using these tags inside two different resource blocks but I'm not typing every tag on it, that's the advantage of the local values. &lt;/p&gt;

&lt;h3&gt;
  
  
  Local values support for expression
&lt;/h3&gt;

&lt;p&gt;Local values can be used for multiple different use-cases like having a conditional expression.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feipg07kk3ipwy8f5t635.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feipg07kk3ipwy8f5t635.png" alt="Screen Shot 2021-08-24 at 10.58.47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Use local values only in moderation, in situations where a single value or result is used in many places and that value is likely to be changed in future&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>cloud</category>
      <category>aws</category>
      <category>devops</category>
    </item>
    <item>
      <title>Terraform Associate Certification: Count &amp; Count index</title>
      <dc:creator>Daniel Huerta</dc:creator>
      <pubDate>Mon, 23 Aug 2021 20:30:28 +0000</pubDate>
      <link>https://forem.com/danihuerta/terraform-associate-certification-count-count-index-2aan</link>
      <guid>https://forem.com/danihuerta/terraform-associate-certification-count-count-index-2aan</guid>
      <description>&lt;p&gt;Let's assume, you need to create two EC2 instances in AWS. One of the common approaches is to define two separate resource blocks for &lt;em&gt;aws_instance&lt;/em&gt; such as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D0mnnkvx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in4i742ag3wmh925vxst.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D0mnnkvx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in4i742ag3wmh925vxst.png" alt="Screen Shot 2021-08-23 at 15.14.30"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Two EC2 instances is not a real problem, but what if we need to create more than that, maybe 10 instances?! 😖 It doesn't seem to be really cool right? &lt;/p&gt;

&lt;p&gt;Well, in Terraform there is something named &lt;em&gt;Count Parameter&lt;/em&gt; and it can simplify configurations and let you scale resources by simply incrementing a number. &lt;/p&gt;

&lt;h2&gt;
  
  
  Count parameter
&lt;/h2&gt;

&lt;p&gt;With it, we can simply specify the count value and the resource can be scaled accordingly, for example, let's create 3 EC2 instances with this parameter:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--biC86WuC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lh9ci1b465he3990nmql.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--biC86WuC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lh9ci1b465he3990nmql.png" alt="Screen Shot 2021-08-23 at 15.19.08"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The instances will be ordered as a list, we will be able to access to them using the position of it, for example &lt;em&gt;instance-1[0]&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Count index
&lt;/h2&gt;

&lt;p&gt;In resource blocks where count is set, an additional count object is available in expressions, so you can modify the configuration of each instance.&lt;/p&gt;

&lt;p&gt;This object has one attribute: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Count index&lt;/em&gt;: the distinct index number (starting with 0) corresponding to this instance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
With the below code, terraform will create 5 IAM users. But the problem is that all will have the same name:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LuthG9Ai--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghr0gcpw4cp878ynln3v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LuthG9Ai--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghr0gcpw4cp878ynln3v.png" alt="Screen Shot 2021-08-23 at 15.22.14"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Count.index&lt;/em&gt; allows to fetch the index of &lt;strong&gt;each iteration&lt;/strong&gt; in the loop:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TSkZw9rM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/028knv7shew7j7bousmc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TSkZw9rM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/028knv7shew7j7bousmc.png" alt="Screen Shot 2021-08-23 at 15.22.48"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Closure
&lt;/h2&gt;

&lt;p&gt;Now that you know how to scale resources using this parameter, you can combine it with other TF things such as variables.&lt;/p&gt;

&lt;p&gt;We can take advantage of the &lt;em&gt;count.index&lt;/em&gt; to use it in a different way:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x9sMcojY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8962pd7ias8xr1u8jg3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x9sMcojY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8962pd7ias8xr1u8jg3.png" alt="Screen Shot 2021-08-23 at 15.23.18"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example we are setting the name of each IAM user according to the position of the variable named &lt;em&gt;elb_names&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Any comments are welcome, remember we are here to help each other 😃💙.&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>devops</category>
      <category>cloud</category>
      <category>aws</category>
    </item>
    <item>
      <title>Terraform Associate Certification: Variables</title>
      <dc:creator>Daniel Huerta</dc:creator>
      <pubDate>Mon, 23 Aug 2021 17:51:35 +0000</pubDate>
      <link>https://forem.com/danihuerta/terraform-associate-certification-variables-37ig</link>
      <guid>https://forem.com/danihuerta/terraform-associate-certification-variables-37ig</guid>
      <description>&lt;p&gt;When using Infrastructure as Code there can be situations in which we need to use the same value multiple times and in different locations, but what if our configuration has changed and we need to modify that value. It can be a little tedious to do, depending upon the number of times that value appears.&lt;/p&gt;

&lt;p&gt;Well, in that situations we can use &lt;strong&gt;variables&lt;/strong&gt; to have a central source from which we can import the values from 😎.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use them
&lt;/h2&gt;

&lt;p&gt;To use variables we have to create a file in which we will place them. As standard, the file must be named &lt;em&gt;variables.tf&lt;/em&gt;.&lt;br&gt;
In that file we need to specify the &lt;em&gt;name of the variable&lt;/em&gt;, &lt;em&gt;type&lt;/em&gt; (optional) and the &lt;em&gt;default value&lt;/em&gt; (in case another value is not specified, TF will take the default one).&lt;/p&gt;

&lt;p&gt;The syntax is as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxh8vquxttpb85e029kw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxh8vquxttpb85e029kw.png" alt="Screen Shot 2021-08-23 at 12.00.18"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we have declared and defined our variable, we can use it with the next syntax:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7rco6vumwgm2p4dh2fu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7rco6vumwgm2p4dh2fu.png" alt="Screen Shot 2021-08-23 at 12.01.18"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We just need to type &lt;strong&gt;&lt;em&gt;var.&lt;/em&gt;&lt;/strong&gt; followed by the name of the variable, in this case &lt;em&gt;instance_type&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiple approaches to Variable assignment
&lt;/h2&gt;

&lt;p&gt;Variables in Terraform can be assigned values in multiple ways, some of these include:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Default Variables
&lt;/h3&gt;

&lt;p&gt;If we have a file named &lt;em&gt;variables.tf&lt;/em&gt; with the variables defined and a default value within it, Terraform will take it in the execution as we did it in the previous example.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If there is not a default value set to that file, when we run Terraform plan it will prompt to us to set a value to the variable&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. From a file
&lt;/h3&gt;

&lt;p&gt;As &lt;em&gt;best practices&lt;/em&gt;, it is recommended to have two files to use variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variables.tf: in which we need to declare the variables, type and the default value&lt;/li&gt;
&lt;li&gt;Terraform.tfvars: in which we just need to set the value to the variable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So for instance, we can have something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faxxtjoyks1gjs4tf9zpk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faxxtjoyks1gjs4tf9zpk.png" alt="Screen Shot 2021-08-23 at 12.12.54"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is important to mention that the &lt;em&gt;terraform.tfvars&lt;/em&gt; file must not be included in a repository, so it has to be added in a &lt;em&gt;.gitignore&lt;/em&gt; file.&lt;/p&gt;

&lt;p&gt;Also, this file must be named as this, otherwise we need to specify the name of the file in the CLI:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;terraform plan -var-file="custom.tfvars&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Command line flags
&lt;/h3&gt;

&lt;p&gt;If not explicit value is specified in the terraform.tfvars file, it will take the default one.&lt;br&gt;
We can specify an specific value to variable using something like this in the CLI:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;terraform plan -var="var_name=new_value"&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Environment variables
&lt;/h3&gt;

&lt;p&gt;In case you don't have a terraform.tfvars defined but you have a variables.tf, we can set the value of the variable using environment variables, we just need to export them with the next structure:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export TF_VAR_name-of-the-variable=value-of-the-variable&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And when we execute a terraform plan for example, TF will take the value of the exported variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variables data types
&lt;/h2&gt;

&lt;p&gt;The type argument in a variable block allows us to restrict the type of value that will be accepted as the value for a variable.&lt;/p&gt;

&lt;p&gt;If no type constraint is set then a value of any type is accepted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data types:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;String&lt;/strong&gt;: sequence of unicode characters representing some text&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Number&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Collection types&lt;/strong&gt;: allows group multiple values of the same type into a single value. The element type is specified as argument:

&lt;ul&gt;
&lt;li&gt;list(...): sequential list of values identified by their position starting with 0, for example ["hello", "world"] in the case of a list(string).&lt;/li&gt;
&lt;li&gt;map(...): a group of values identified by named labels like {name = "Dan", lastname = "Hdz"} in the case of map(string).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Structural types&lt;/strong&gt;: allows group multiple values of distinct type into a single value:

&lt;ul&gt;
&lt;li&gt;Object(...): a group of values identified by a key name, it is necessary to specify a schema as argument to indicate the type of values that it is going to store like: &lt;em&gt;object({name=string, age=number})&lt;/em&gt; and it will match a value with the same structure such as &lt;em&gt;{name = "Karla", age = 24}&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;Tuple(...): A sequence of values. The schema for this must be like tuple &lt;em&gt;([string, number, bool])&lt;/em&gt; and a value will match if follows that structure such as ["Mike", 23, true]&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Data from maps and list variables
&lt;/h2&gt;

&lt;p&gt;There can be situations in which we have variables defined as map or list and we just need one of those values inside of them.&lt;br&gt;
For example, suppose the next scenario:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fid1zplufud0frytxa0ar.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fid1zplufud0frytxa0ar.png" alt="Screen Shot 2021-08-23 at 12.43.42"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the instance type is going to take the corresponding value for the &lt;em&gt;"us-west-2"&lt;/em&gt; key (t2.nano).&lt;br&gt;
If we will use a variable type as List, we have to select the position of the element we want like: &lt;em&gt;instance_type = var.list[0]&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Closure
&lt;/h2&gt;

&lt;p&gt;Now you are able to use variables in your Terraform configuration files, remember that are different ways to use them, you'll choose which to use depending of the situation 😉.&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>cloud</category>
      <category>aws</category>
      <category>devops</category>
    </item>
    <item>
      <title>Terraform Associate Certification: Terraform Import</title>
      <dc:creator>Daniel Huerta</dc:creator>
      <pubDate>Mon, 19 Jul 2021 18:25:08 +0000</pubDate>
      <link>https://forem.com/danihuerta/getting-started-with-terraform-terraform-import-53do</link>
      <guid>https://forem.com/danihuerta/getting-started-with-terraform-terraform-import-53do</guid>
      <description>&lt;p&gt;Even when we are using an &lt;em&gt;Infrastructure as Code&lt;/em&gt; tool such as &lt;strong&gt;Terraform&lt;/strong&gt; can be a member of a team (including us 😅) that will ignore all this workflow and will create &lt;strong&gt;manually&lt;/strong&gt; some resources directly in the cloud provider. It is unnecessary to say that it can be a really headache for the team, mainly because all the required changes for that resource need to be manually as well and ... that's why Terraform exists!! 🤯.&lt;/p&gt;

&lt;p&gt;So, in this scenario there's a solution in TF and it is the &lt;strong&gt;import&lt;/strong&gt; command 😎. &lt;/p&gt;

&lt;h2&gt;
  
  
  Terraform Import
&lt;/h2&gt;

&lt;p&gt;This is a simple command that will help us in this tricky situations, it is going to (as its name says) import &lt;em&gt;existing&lt;/em&gt; infrastructure to be managed by Terraform from now on.&lt;/p&gt;

&lt;p&gt;But be careful, there is an important thing to mention here, &lt;strong&gt;&lt;em&gt;Terraform will not create the configuration file of the resource that was created manually&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Because of this, it is necessary to first write manually a resource configuration block for the resource (with the same properties as the one that is already created), to which the imported object will be linked.&lt;/p&gt;

&lt;p&gt;So let's do an example with an AWS EC2 instance:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a file with the provider configuration (in this case AWS).&lt;/li&gt;
&lt;li&gt;Create a file in which we need to configure the resource that is already created in the Cloud with the same properties. &lt;/li&gt;
&lt;li&gt;Run &lt;em&gt;terraform import  &lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  EC2 instance manually
&lt;/h3&gt;

&lt;p&gt;Let's start creating the EC2 instance manually with some basic configuration. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T73CUvLf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1v1w44toqn7ww7b0sxa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T73CUvLf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1v1w44toqn7ww7b0sxa.png" alt="Screen Shot 2021-07-19 at 12.58.19"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here it is important to review all the important properties that this resource has because they need to be the same as the ones that we are goin to set in Terraform to be able to match both resources.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating configuration file
&lt;/h3&gt;

&lt;p&gt;See that just some attributes are required to mapped both resources:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gnflbhyC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/akr2vjjwlkxr21lz4bjk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gnflbhyC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/akr2vjjwlkxr21lz4bjk.png" alt="Screen Shot 2021-07-19 at 13.11.23"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we have created the configuration file for the resources let's run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;terraform import aws_instance.myec2 i-0c598199bf2771676&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Change the name of the resources and the id according to yours&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now the resource that was created manually has been mapped to the local one that we set with Terraform, it means that if we change something in the file, the change will be reflected in AWS.&lt;/p&gt;

&lt;p&gt;Let's change the Name tag from &lt;em&gt;manual&lt;/em&gt; to &lt;em&gt;manual2&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qlx86J4N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iiuhxrz6pv7fi2l5hy5v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qlx86J4N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iiuhxrz6pv7fi2l5hy5v.png" alt="Screen Shot 2021-07-19 at 13.15.25"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even when we run the plan command we can see that any resource will be created but just modified (it means that there's already an existing created resource with that configuration).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;terraform plan&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;terraform apply -auto-approve&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then, we can see that the changes were reflected in AWS:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ui05Vqhm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbqbo1ry970t69r4d8lm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ui05Vqhm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbqbo1ry970t69r4d8lm.png" alt="Screen Shot 2021-07-19 at 13.17.31"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can manage that resource using Terraform 😉. &lt;/p&gt;

&lt;h2&gt;
  
  
  Closure
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;import&lt;/strong&gt; command is very useful, these situations where a resource is created manually occurs very frequently 😅 fortunately exists this solution so don't hesitate to use it when it happens to you.&lt;/p&gt;

&lt;p&gt;Oh! And just one last thing, Terraform will include the feature of creating the &lt;strong&gt;configuration file&lt;/strong&gt; when we run the &lt;em&gt;import command&lt;/em&gt; in future releases, so this tricky part will be just for a few moment 😉💙.&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>aws</category>
      <category>devops</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Terraform Associate Certification: Remote Backend</title>
      <dc:creator>Daniel Huerta</dc:creator>
      <pubDate>Fri, 16 Jul 2021 21:22:15 +0000</pubDate>
      <link>https://forem.com/danihuerta/getting-started-with-terraform-remote-backend-4092</link>
      <guid>https://forem.com/danihuerta/getting-started-with-terraform-remote-backend-4092</guid>
      <description>&lt;p&gt;In the previous post we talked about how does Terraform manages the resources in the cloud using the local &lt;a href="https://dev.to/danihuerta/getting-started-with-terraform-state-file-1l2f"&gt;state&lt;/a&gt; file, however in the real world we will have to collaborate with other members of the team, it is to say that not only one person is going to modify the Terraform files.&lt;/p&gt;

&lt;p&gt;In this scenario, does the entire team have to modify  the &lt;em&gt;local state file&lt;/em&gt; manually in their machine to be all synchronized? 🤔.&lt;/p&gt;

&lt;p&gt;Well, it does not sound so practical. When we work in a collaborative workflow it is necessary to use a &lt;strong&gt;remote backend&lt;/strong&gt; which is the same as a &lt;strong&gt;remote state file&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Remote Backend
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;Remote Backend&lt;/em&gt; is the resource in Terraform that will allows us to store the &lt;a href="https://dev.to/danihuerta/getting-started-with-terraform-state-file-1l2f"&gt;state file&lt;/a&gt; in a remote location, not locally as it occurs in TF by default.&lt;/p&gt;

&lt;p&gt;Doing this, all the team members that are working with the Terraform files will be able to do it in a synchronized way, there will be a unique shared state file for the project placed in a remote location which can be an AWS S3 Bucket, Consul, AzureRM, etc. In this post we are going to use an AWS S3 Bucket as our remote backend.&lt;/p&gt;

&lt;h2&gt;
  
  
  S3 Backend
&lt;/h2&gt;

&lt;p&gt;If any backend is specified in the code, the state will be stored locally. Keeping that in mind, let's define our first remote backend 👨🏽‍💻.&lt;/p&gt;

&lt;p&gt;To keep our code in a good structure, we need to create a file named &lt;strong&gt;backend.tf&lt;/strong&gt; (you can name it as whatever you want, it is just to follow the standard). Inside of it we are going to use the next code and then configure it with the command &lt;em&gt;terraform init&lt;/em&gt; in the terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r4UswvuM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/siqqno6srkb7e72sgusy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r4UswvuM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/siqqno6srkb7e72sgusy.png" alt="Screen Shot 2021-07-16 at 16.09.37"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note that the AWS S3 Bucket needs to be created in the cloud before running "terraform init", otherwise it will show an error because there will not be a bucket to store the remote state file.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Once the code has been applied, you will see that inside of the &lt;em&gt;.terraform&lt;/em&gt; folder there is a &lt;em&gt;terraform.tfstate&lt;/em&gt; file. However, the information about the created resources are not stored there, it only specifies that the &lt;strong&gt;backend&lt;/strong&gt; is an S3 Bucket.&lt;/p&gt;

&lt;h2&gt;
  
  
  State Locking
&lt;/h2&gt;

&lt;p&gt;Now we are able to work in the same Terraform infrastructure file in a collaborative way, however, what if while I'm modifying a specific resource in the code, another member of the team modify the same part as well? What changes are the ones that will remain? 😣.&lt;/p&gt;

&lt;p&gt;This is a common problem when a team works in the same code, the solution of it is the &lt;strong&gt;state locking&lt;/strong&gt;. As its name says, it is going to block the state during the &lt;em&gt;applying operations&lt;/em&gt; such as &lt;em&gt;terraform plan&lt;/em&gt; or &lt;em&gt;terraform apply&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Once we have added the state locking (which is also included by default when we use the local state but not in a remote backend) if a user is running a &lt;em&gt;terraform apply&lt;/em&gt; command for example, and another one tries to do it as well at the same time, TF will show him an error specifying that the state is currently locked by the changes of a different user, avoiding inconveniences in the code.&lt;/p&gt;

&lt;p&gt;To create the state locking with the S3 Bucket, it is necessary to create a &lt;strong&gt;DynamoDB&lt;/strong&gt; and add &lt;em&gt;LockID&lt;/em&gt; as primary key:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jRlMg8aj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0imwo71do6u70k1hbc2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jRlMg8aj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0imwo71do6u70k1hbc2.png" alt="Screen Shot 2021-07-16 at 16.13.18"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's specify to the backend that we are going to use a DynamoDB for our State Locking. It is very simple, we just need to add the &lt;em&gt;dynamodb_table&lt;/em&gt; tag:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z5MDEL19--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ih8y733y1ollfmbnto0x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z5MDEL19--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ih8y733y1ollfmbnto0x.png" alt="Screen Shot 2021-07-16 at 16.16.09"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To apply this configuration let's run the command &lt;em&gt;terraform init&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now we can work with the TF files within the team with the best practices 😉.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closure
&lt;/h2&gt;

&lt;p&gt;This topic can be a little tricky to understand, mainly because there are many resources that can be used as &lt;strong&gt;Remote Backends&lt;/strong&gt;, however, you don't have to be an expert implementing all of them. In my case, AWS is the CP that I mostly use, that's why I'm using its resources with Terraform.&lt;/p&gt;

&lt;p&gt;If you have any doubt or comments about it let's discuss it, remember we are here to help each other 😉💙.&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>aws</category>
      <category>devops</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Terraform Associate Certification: State file</title>
      <dc:creator>Daniel Huerta</dc:creator>
      <pubDate>Mon, 12 Jul 2021 19:41:25 +0000</pubDate>
      <link>https://forem.com/danihuerta/getting-started-with-terraform-state-file-1l2f</link>
      <guid>https://forem.com/danihuerta/getting-started-with-terraform-state-file-1l2f</guid>
      <description>&lt;p&gt;If you have started using Terraform as your &lt;em&gt;Infrastructure as Code&lt;/em&gt; tool you might wonder how does TF know which resources need to be created, deleted, or modified when you run your code. Well, let me tell you that it is not a magic stuff, the agent behind this is the &lt;strong&gt;state file&lt;/strong&gt; 😉.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terraform apply
&lt;/h2&gt;

&lt;p&gt;Every time we deploy our Infrastructure with &lt;em&gt;terraform apply&lt;/em&gt;, a file named &lt;em&gt;terraform.tfstate&lt;/em&gt; is created, it includes all the information about the resources that are currently deployed in the cloud. &lt;/p&gt;

&lt;p&gt;You can navigate on it and you will see a key with the name of &lt;strong&gt;resources&lt;/strong&gt;, it is an array which lists all the elements that were created with the configuration file (after doing &lt;em&gt;terraform apply&lt;/em&gt;). &lt;/p&gt;

&lt;p&gt;If you create an EC2 instance in AWS, your tfstate file will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5tzx0w4lmg8tsxba0ihl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5tzx0w4lmg8tsxba0ihl.png" alt="carbon (2)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To understand better how this works, it is important to talk about the &lt;strong&gt;&lt;em&gt;Current and Desired State&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Current State
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;terraform.tfstate&lt;/em&gt; file is the responsible to store the current state of the infrastructure, it is to say all the information about the resources that are currently deployed in your Cloud Provider (after the &lt;em&gt;terraform apply&lt;/em&gt; command has been run).&lt;/p&gt;

&lt;h2&gt;
  
  
  Desired State
&lt;/h2&gt;

&lt;p&gt;All the code that is set in the main.tf file corresponds to the desired state, on it you specify what resources you expect to be created in the Cloud, and here is where the magic occurs 😉.&lt;/p&gt;

&lt;p&gt;Terraform is going to &lt;strong&gt;compare&lt;/strong&gt; both states (current and desired one), if there is an existing difference between both, it will make the changes that are necessary so that the current state is equal to the desired state.&lt;br&gt;
TF shows the result of the comparison when we execute the &lt;em&gt;terraform plan&lt;/em&gt; command in the terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Refreshing the state
&lt;/h2&gt;

&lt;p&gt;But, what if we modify a resource directly in the Cloud Provider interface not using Terraform apply (AWS Console for instance), let's say that we changed the EC2 instance type from t2.micro to t2.large, how is our &lt;strong&gt;local&lt;/strong&gt; &lt;em&gt;terraform.tfstate&lt;/em&gt; be modified to match with those &lt;em&gt;remote&lt;/em&gt; changes? 🤔.&lt;/p&gt;

&lt;p&gt;Well, Terraform has the solution for that. We just need to run the command &lt;strong&gt;terraform refresh&lt;/strong&gt; and all the configuration that is currently running in the cloud provider will be updated in the local &lt;em&gt;terraform.tfstate&lt;/em&gt; file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Closure
&lt;/h3&gt;

&lt;p&gt;Now that you understand the &lt;strong&gt;state&lt;/strong&gt; concepts in Terraform, we can start talking about how to manage it in a collaborative workflow, let's talk about the &lt;strong&gt;remote state&lt;/strong&gt; in a different post.&lt;/p&gt;

&lt;p&gt;Thanks for reading, any comments or suggestions are welcome. We are here to help each other 😉💙.&lt;/p&gt;

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