Terraform Basics 101: Part 3
Terraform commands
To check if our syntax is right, use terraform validate
terraform fmt file makes the .tf file much more suitable to read
terraform show
command shows current state of the resources. We can also use flag json to check them in json format
terraform providers
command list the providers we have
Also to import the providers plugins that has been downloaded, we can use terraform providers mirror <target_location>
In this way, our targetted location will have the provider’s plugins copied.
Using terraform output
, we can print values of the output
Using refresh command , we can let terraform check the terraform statefiles and look for changes
terraform graph
command gives a visual presentation of dependencies
Pass this one through graphviz
and we can open the graph.svg file
Mutable vs Immutable infrastructure
Assume that we have 3 servers with nginx v1.17 running on them
How to update them ? We can manually update each one
This approach is called “In-place” approach as the resources are still same but the version changed. But there are drawbacks like users might not avail the servers for some time.
To prevent that, we can create new servers with updated versions and delete the older one
Then we create again another one with new version and remove old one
So, in the same way, we finally create all 3 new servers with latest versions
If we face any issue,
we can also rollback to the earlier versions
For example, assume that this is our main.tf file and we have already created a pets.txt file with it
Now, let’s update the file and apply changes.
Here you can see 1 destroyed meaning 1 file got destroyed and 1 file got added meaning our desired file with changes got created.
Here the older file was deleted first and the new one was created.
If we want the updated one to be created first and then the older to be deleted or, don’t want to delete the resource at all, we have to modify lifecycle rules.
For example,
here the new one will be created first and then the old one will be deleted
Again, if we don’t want to delete the older one, use this one.
Also we can ignore changes. But how? Let’s create an IAM instance with tag “projectA-webserver”
If changes are made in any of the things like changing the tag to ProjectB-webserver, terraform will look this and update the existing IAM instance.
but if we want to ignore the changes made, we can add that within lifecycle
This way, if the tag is changed, the change will be ignored.
Data Sources
Assume that we have a pets.txt file created using terraform
Now using shellscript we have created another text file (dogs.txt) there
The file contain “Dogs are awesome!”
What if we want to make this file a source for us and use it as our content for the pets.txt file? Yes, we can do that.
We have to create a data block and mention the local_file (as both are using same path and file type) then filename and update the content for pets.txt
In this way , our resource pets.txt was able to use dog.txt as source
Meta Arguments
Assume that, we want to create 3 copies of our pet.txt files . To do that we need to set this count=3
But here is an issue. As we didn’t set other file names, we can just see 1 file
To solve this issue, we can set 3 file names in the variables.tf file and update the filename in main.tf
Now, once we apply the changes, we can see 3 files created
If we mistakenly remove the pets.txt file and apply changes, what should happen?
It should only delete pets.txt file, right? But not so
Terraform replaces dogs.txt, cats.txt and destroys remaining one. Why so?
Because when we removed the pets.txt file, dogs.txt has now become index 0 and cats.txt has become index 1.
So, new element in index 0 and 1. Also, no element in index 2.
If we want to add more files? We can manually set the count value or set a function to return the number of files
We can also use for_each and each.value for filename. Note: We can use for_each with sets and maps.
Here we therefore made changes to the type of file which was set to string by default. It works.
Or, we don’t need to make changes in the variables.tf file rather make change to the main.tf. We used toset() to convert to set.
Here we go!
If we now delete any filename from variables.txt , only that file gets deleted. Other files remain as it is
It’s because the resources are no longer saved in the list format. Rather they are saved in the map format.
Earlier when we used count, the files were saved as list but now, in the maps format.
Version constraints
When we initialized the terraform file, it installed the latest provider plugins
What if we don’t want to have a different version? We just need to create a terraform block and mention like this
Now if we initialize the terraform, we can see our desired version 1.4.0 is installed
We can also set different rules in the version option like these:
etc.