Top 10 Terraform Commands

Top 10 Terraform Commands

·

3 min read

1. fmt

When we finish our Terraform configuration, we can make sure that everything is formatted correctly, run the fmt command first. This command reformats your configuration in the standard style, so it’ll make sure that the spacing and everything else is formatted correctly. If it comes back blank, that means the configuration files within your working directory are already correctly formatted. If it does format a file, it will let you know what file it touched.

2. init

After we use the format command, we have to initialize our working directory to prepare it for what we need.

The init command looks at your configuration files and determines which providers and modules it needs to pull down from the registry to allow your configuration to work properly.

3. validate

Once we have initialized the directory, it’s good to run the validate command before you run plan or apply. Validation will catch syntax errors, version errors, and other issues. One thing to note here is that you can’t run validate before you run the init command. You have to initialize the working directory before you can run the validation.

4. plan

Next, it’s always a good idea to do a dry run of your plan to see what it’s actually going to do. You can even use one of the subcommands with terraform plan to output your plan to apply it later.

5. apply

And then of course you have your apply command, which is one of the commands you’re going to use the most. This is the command that deploys or applies your configuration to a provider.

6. destroy

The destroy command, obviously, will destroy your infrastructure — or, when used with the target flag, individual resources within your infrastructure.

7. output

If you’ve put together a good output variable file, you can use the output command to make those defined outputs to display certain information. For example, if you’re deploying EC2 instances, you can output tag names, instance names, instance IDs, the IP of the instance, and so on. You can gather some really good information that makes it simple to look up later. And if you’re working as a team, people coming behind you can use the output command to figure things out and get up to speed.

8. show

The show command shows the current state of a saved plan, providing good information about the infrastructure you’ve deployed. For example, if you have an EC2 instance or a VM deployed in your configuration, it’ll show you the state that it’s in — if it’s up and ready or if it’s being terminated. It also provides useful information like IP addresses.

9. state

Another good way to check your work is to use the state command. If you use state and then the subcommand list, it’ll give you a consolidated list of the resources that are being managed by your configuration. If you are moving your Terraform instance, such as from a local instance to a remote backup, you would use the state mv command. And just like the show command, there’s a state show command that shows a resource in the state. You can also remove instances from a state by using the state rm command.

10. version

We will use the version command quite a bit to check our Terraform version, especially if we have any version conflicts. Sometimes providers work only with certain versions of Terraform, so if we are defining those versions within our configuration we can use the version command.

That's a wrap......