5 Basic Terraform Commands You Should Know | by Tate Galbraith | May, 2022

Once I was first launched to Terraform an entire new world of prospects opened up. Cloud assets may now be managed similar to code. I had been bitten by the IaC bug and was consuming the Kool-Support. Lastly, small incremental modifications to situations or networks could possibly be documented and deployed in commits.

Constructing large service architectures could possibly be completed on the drop of terraform apply.

Quick ahead a couple of years later and Terraform continues to be offering a easy, efficient option to provision assets with code. I nonetheless use it virtually each single day, however have come throughout a couple of situations and instructions which are useful for on a regular basis Terraforming.

Working with Terraform will be daunting when you’re deploying a number of sophisticated companies or if the state begins to float aside. On this article, we’ll have a look at some useful options to frequent Terraform issues and find out how to get probably the most out of Terraform for day by day use.

This one might sound thoughts numbingly easy, however I’ve run into an invalid configuration about one million instances now. Performing easy config validation earlier than you begin working dearer instructions like plan or apply can prevent a little bit of time.

All you need to do is execute the next out of your root Terraform listing:

terraform validate

You may as well construct this into your current CI/CD pipelines to have configurations validated earlier than they get executed. The validate command may even settle for the -json flag to supply machine-readable output. That is useful for consumption by Jenkins or another automation platform.

In case your Terraform repository has began to develop fairly massive it may be troublesome to plan or apply in several separated phases. Generally it’s possible you’ll want to solely apply particular assets, or possibly you’re nonetheless engaged on a selected module and don’t need it evaluated for deployment simply but.

In circumstances like these, it’d make sense to carry out a focused deployment. It will restrict the assets that Terraform appears at when evaluating a brand new plan. Let’s say you may have two completely different situations:


By default, Terraform will consider all assets. When you solely wish to run a deployment in opposition to a selected useful resource or set of assets then you possibly can goal the useful resource/s by their path.

Execute the next command to run a focused plan or apply in opposition to the second occasion:

terraform plan -target=aws_instance.instance_02

It will solely consider modifications to aws_instance.instance_02 and ignore all different modifications. If you wish to add extra assets to focus on, you possibly can merely go extra -target flags to plan.

Utilizing the -target flag is taken into account “distinctive use”, which implies it isn’t really useful for constant, on a regular basis utility. I’d advocate utilizing this solely if you’re in a pinch or have managed to get Terraform into a very nasty state.

You may learn extra about useful resource focusing on here.

Infrastructure isn’t good. There are at all times skeletons within the closet. Generally these skeletons have been constructed a variety of years in the past earlier than Terraform existed or was carried out. When you’ve got some legacy assets created “by hand” in your cloud supplier’s console worry not, as a result of you possibly can simply import them.

Let’s assume you may have an outdated AWS EC2 occasion working, nevertheless it wasn’t deployed by Terraform. So as to handle this occasion, you’ll must import it into state:

terraform import aws_instance.identify i-1234567890

After you import the occasion into state, you’ll additionally wish to add a definition for it in your TF information. When you don’t, Terraform might imagine you wish to delete the occasion as a substitute. Normally, the quickest manner is to run a terraform plan with out including any new definition and evaluation the definition that Terraform thinks it ought to take away. You may then use this as a template for what so as to add.

As soon as the definition is added and state matches up now you can make modifications to it, reference it from different assets and rather more.

Learn extra about importing current assets here.

Developing with descriptive names isn’t at all times straightforward. Generally you don’t know what one thing will actually be used for till in a while. When you’ve got some poorly named assets you’ll have already tried to easily rename them within the configuration. Sadly, that doesn’t work. Terraform will assume you need a completely different useful resource altogether and attempt to spin one up for you.

So as to “rename” a useful resource (particularly when its referenced by different assets) you need to carry out a “state transfer”:

terraform state mv <old_path> <new_path>

What this command does is take one useful resource path and rename it to a different one within the state file. Keep in mind that the trail contains the useful resource kind. So to be able to transfer an AWS occasion named “check” to “test2” you’ll execute one thing like this:

terraform state mv aws_instance.check aws_instance.test2

Whereas making a number of handbook edits to state isn’t suggested, I’ve used this quite a few instances and barely bumped into points renaming assets. Simply watch out when making modifications to essential assets that many others rely on.

Take a look at extra in regards to the state command here.

Everybody has skilled this in some unspecified time in the future of their Terraform journey. A useful resource deployment goes awry. Your web connection drops. Your cloud supplier doesn’t settle for the configuration. A deprecated choice not works. Generally assets can get into a foul state and turn out to be “half deployed”. They could be lacking some configuration, or not even be practical in any respect.

If you wish to redeploy these assets with out eradicating them, Terraform features a built-in mechanism for dealing with this. You may taint a selected useful resource. What this implies is you’re basically setting a flag on this useful resource to be destroyed and recreated:

terraform taint aws_instance.instance_02

Operating this command will taint the aws_instance.instance_02 useful resource till you difficulty the untaint command in opposition to it. It will trigger the subsequent deployment to destroy and rebuild this occasion.

You don’t must remark out assets or fear about copying and pasting. Simply taint the useful resource by path, redeploy and proceed enterprise as ordinary.

Learn extra about tainting assets here.

More Posts