(gentle music)
So now that you have your Terraform binary installed
and ready to go
but you don't really know where to go from here
or what exactly what commands to use and whatnot.
You can use the Terraform built-in documentation
or Man Pages as a quick reference guide
to what commands to use.
You can get to that in three different ways.
You can just do a Terraform command by itself,
which will give you a list of your commands.
You can also do a terraform -h for help,
or you can do a terraform --help.
Either of those will bring up this nice list of commands
that you can use.
Has them separated with your main commands up here,
and then all your other commands.
You can also do a Terraform and then subcommand,
and then use the help, -h or the --help.
To then pull up a list of subcommands to use
with the subcommands.
So let's go over a few of these commands here,
the ones I like to use a lot.
Usually when I first start off with
my Terraform configuration,
I wanna make sure that everything is formatted correctly.
So I use this fmt command first,
once I write my Terraform configuration.
What this does is it'll reformat your configuration
in the standard style.
So it'll make sure that spacing
and everything else is correct and proper,
and you'll just run Terraform and then space fmt.
And if it comes back blank,
that means your configuration files
within your working directory are correctly formatted.
And if it does format a file,
it'll let you know which file it touched.
Then after I used the format command,
then I jump over here to our main commands.
You'll wanna initialize your working directory,
which this will prepare the directory for what you need.
It'll look at your configuration files
and it'll determine which providers
and modules it needs to pull down
from the registry to allow your configuration
to work properly.
Once you've initialized the directory,
it's good to run a Terraform validate.
A good thing to note here is
that you cannot run the validate command
before you run the init command, it will not work.
So you must initialize the working directory first
and then you can run the validation.
Your validation will catch things like syntax error,
version errors, stuff like that.
It's a really good thing to do
before you run your plan or apply.
Next is always good to run your plan
to see what it's gonna actually do.
It is a nice dry run of your apply plan.
You can even use one of the subcommands with a plan
to actually output your plan to apply it later.
And then of course, you have your apply command,
which is one of the commands you're gonna use the most.
This is where it will deploy
or apply your configuration to a provider.
Then, obviously the destroy command,
which will destroy all of your infrastructure
or when used with the target flag,
just a single or individual resources
within your infrastructure.
Some other commands
that I like to use here are your output command.
If you put together a good output variable file,
you're able to use this command to use those defined outputs
to display certain information,
like say, if you were deploying EC2 instances,
you can output tag names or instance names,
or instance ID or the IP of the instance
and so on and so forth.
So you can gather some really good information
that can be used later on to make it simple to look up.
Also, if you're working as a team, it's nice
so people can come behind you and use that output command
to figure out certain things and get up to speed.
Some other good ones to use, the Terraform show command
that will show your current state of a saved plan.
It collects a good amount of information
about your infrastructure that you've deployed.
It's a good way to check and see,
like say, if you had an EC2 instance
or a VM or something
that was deployed in your configuration,
it'll show you things like the state that it's in.
Like if it's up and it's ready,
or if it's being terminated or so on and so forth,
and you can gather information like IP address,
stuff like that, and just general useful information.
Another good way
to check your work is using the state command,
which you can find out information,
like if you use the Terraform show state
and then the subcommand list,
it'll give you a list of the resources
that are being managed by your configuration.
So if you didn't need all that extra information,
the state list command is a nice way
to give a nice consolidated list of resources,
you're managing with your configuration.
And then obviously the version,
and you'll use that quite a bit
to check your Terraform version,
especially if you have any version conflicts,
sometimes providers only work
with certain versions of Terraform.
So you have to figure defining those versions
within your configuration.
Sometimes, they can get a little sticky
so, yeah, you'll tend to use
that version command here and there.
Just like I said before,
you can use the help command with subcommands as well.
So like I said before,
the state command has quite a few subcommands.
So let's go ahead and look at that.
We'll do a terraform state
and then use the -h.
And now, it'll give you some more information
about the state command and then its list of subcommands.
So here's the list one that I was talking about,
where it lists of resources in the state.
If you are going from a local Terraform instance
to say you wanted to start backing up your state
in a remote backend,
you would use this state move command
to move your state file to that backend.
You'll probably run into situations
where you'll need to use that command a few times,
just like the Terraform show command.
There's a state show command,
which shows a resource in the state.
You can remove instances from a state
by using the rm command and the push and pull,
which is used mostly with remote state.
So those are just some of the popular commands,
kind of in a nutshell, stuff I like to use.
This is a very handy way to quickly look up commands
when you're not really 100% percent sure,
either how to use it or know what it does.
It's definitely a useful tool when using Terraform,
especially when getting started.
So I highly suggest you take a look at it
when you get a chance.
(gentle music)