Terraform Commands
terraform plan:
If want to see the difference between current and desired state and We don’t want to do terraform apply command. For that We can actually execute Terraform Plan command which is like a preview, same as what Terraform apply command gives We but without the actual applying it.
terraform apply -auto-approve:
Now if We are executing Terraform apply command, however We don’t want to always confirm the execution or wait for terraform to calculate all the changes and then confirm the change. We just want to automatically approve any change We can pass in auto a proof flag and this will basically apply immediately without waiting for your confirmation.
terraform destroy:
Now what if I wanted to destroy my infrastructure and everything that I have created using this configuration file, I wanted to clean everything up for that. We can execute terraform destroy command and if we do not provide any targets as a parameter, it will basically find our Terraform configuration file. It will go through all the resources and destroy.
Or basically just remove all the resources one by one in the correct order.
Now we get a preview of. What gets destroyed? we can confirm this action and terraform will clean everything up. Now this command is handy and can be used if We want to revert exactly what We have created with configuration file.
Terraform State:
Terraform keeps a track of the current state and reads the desired state from the configuration file . Every time we do terraform apply or destroy in the outputs, we’ll have the refreshing state messages .So for the resources that are available in the configuration file terraform refreshes the state and then it calculates in the background.
How does Terraform know what the current state of the resources defined in configuration file? If We look in the Terraform folder that we have our main.tf file in. Two additional files have been generated by Terraform and these are called terraform.tfstate and terraform.tfstate.backup.

So, terraform.tfstate is a Json file that gives us a list of resources in their current state and terraform generates these files on the first time we execute Terraform apply command .
Terraform goes to AWS because that’s our provider, it connects to our AWS account, it executes whatever we define in the configuration state and then it stores the current state.
In this Terraform State file and whenever we make changes and reapply these changes, this Terraform file gets updated. So basically, this is like a state store for terraform to get that up-to-date information.
The backup file is the previous state for the resources.
Once do apply command , Terraform State file We see the empty array of resources has been filled in with new information and each element inside these resources array is basically one configuration resource and component we have defined in configuration file.
All four are stored in the state and in the backup we have again the previous state which was empty resources.
if we have hundreds of resources created in our account, it’s going to be Pretty difficult. Especially if we’re just interested in some of the attributes and we don’t want to see the whole object with all the attributes, Terraform has commands so that we can access the information inside the state.


we can list All the resources that are in the state. we have again 4 resources, we have the resource type and resource name as well as whether it’s coming from a data.

We can also show. A specific resource, for example this one right here. And all this configuration to that specific resource in the output . we have a bunch of other commands like pulling the current state from AWS as well as making changes in the state which most of the time we shouldn’t directly interact and update the state or update this file ourselves manually. It should be done by Terraform itself because of applying our configuration.
Output
Another interesting feature of Terraform is it allows us to output a set of attributes and their values of the resources we just created. we are creating resources with just a handful of attributes and the rest of the attributes get generated by AWS and We saw that we can see those generated attribute values once we create the resources.
Either in the Terraform State file or using Terraform State show subcommand for a specific resource .
Another way to output a specific attribute of a resource we just created and the resource that we have defined ourselves is using output values. So, inside the configuration file we can define what value we want terraform to output at the end of applying our configuration from one of the resources.
if we want to check which attributes are available for each resource. We can do a terraform plan. Which will show us a preview of Terraforms planned actions, including the attributes that either we set or AWS will set for that resource, and We can choose one of the attributes from here to be displayed. In our case, we’re choosing ID of the subnet .
We can’t have multiple values here even from the same exact resource. So, one output value for each attribute we want to output.

Do apply then we have outputs additionally and this gives us our output values.
