- Okay, so now we're gonna take a look at Terraform itself
as a tool, now that we have it installed.
So Terraform, as I mentioned before,
is a command line interface tool, or CLI.
And it has a number of sub-commands.
You can see we've got 10 or 15 sub-commands here.
Only three that you use on a regular basis.
And those are, in order, init, okay?
So the description here is
initialize a Terraform working directory.
That's not terribly helpful.
What init does is it downloads Terraform providers
or makes sure that they already exist, okay?
And we're gonna get much deeper
into what providers are later on
but I can tell you right now
that those are the interface to individual cloud services.
For example, there's an AWS provider,
there's a Google provider,
there are providers for things
that may not seem like cloud at all,
like there's a GitHub provider.
There's a G-Suite provider.
So the providers are very, very important.
So init does two things.
It downloads or ensures that
the providers referenced in the config are available
and then it also ensures
that any modules referenced are available.
And we're gonna get into what modules are here
in a few lessons.
You don't have to worry about that right now.
But those are basically pieces of code that either you wrote
or that it downloads from the internet.
So init must be run on any Terraform directory
before you can proceed to the next step, which is plan.
Okay?
So what plan does,
if you'll remember a few slides ago,
we showed the config and the state being compared.
That's what plan does.
Plan prepares the configuration
so what you're telling Terraform
that you want the state of the world to be
with what Terraform thinks the state of the world is.
And then the output from that step is a detailed plan
of exactly everything that Terraform is going to change
in order to get there.
So that plan will display in the console
all the changes that it's going to make.
And if you tell it to, it will also create a plan file.
And the plan file is used for the next step, which is apply.
Okay?
Apply simply tells Terraform to make the state of the world,
reflect the configuration that you've given it.
And you can either give it a plan file
or it will run the plan step for you
and then you'll have to confirm that you want to run it.
It's always best practice to run plan with an output file.
And we'll be using these steps
throughout the entirety of this training.
So in addition to those,
Terraform has lots of other commands.
We've got about 12 other commands here.
Destroy is also one that you may use on a regular basis.
So what destroy does is tells Terraform
to take that state file,
so all the resources that Terraform has created,
and destroy them.
So this is common in development environments,
but you have to be careful with it, obviously,
because it can also destroy your production database.
Fmt, or format, you can see the description,
rewrite config files to canonical format.
So what that does, and this is somewhat common
in tools that are written in go,
is that rather than allowing everybody to have an opinion
about what the formatting of the files should be,
Terraform just goes ahead and tells you.
So you can run this format command
against your Terraform config files.
And it will do things like convert tabs to spaces
and align the indentations and things like that.
So things that aren't necessarily syntactically incorrect
it will change for you.
So it's a good practice to run the format command
before you do something like
check your code in to get up for example.
Okay get is rarely used.
This simply downloads and installs modules
for the configuration.
That's something that's normally handled by init.
Graph, we'll talk about graph later,
but it will display the dependency tree of the resources.
And when we talk about the config language,
we're gonna talk about dependencies quite a bit.
Import is an important one.
This one is used if you have resources
that have been created without using Terraform,
so if you already have a whole AWS environment
and you would like to begin managing that in Terraform,
you'd use the import command.
And we'll talk about how to do that later.
Unfortunately, it's pretty tedious.
There's not a good way from within Terraform itself
to sort of import in an entire infrastructure.
So it's sort of one piece at a time.
Outputs, we'll talk about outputs later,
but this will simply display on the screen
any outputs that are defined
in your Terraform configuration.
Providers, we talked about Providers a little bit,
but this command also rarely used,
but it will tell you sort of the dependency tree
of the providers.
Refresh will update the local state file
against real resources.
So if you have some drift in there,
you may be able to detect that with the refresh command.
The show command will simply display some information
about your state or your plan file.
Taint is an interesting one.
So taint will mark a resource as needing to be refreshed
within the configuration.
This one needs to be used occasionally.
It's somewhat rare.
I would say that it's sort of an advanced use case.
I do use it regularly, but it's usually a sign
that there's something wrong with your configuration
that needs to be addressed otherwise.
Untaint, I've never used untaint.
I guess if you've inadvertently tainted something,
you can untaint it.
Validate is similar to a lint or just a syntax check.
So validate will run against your config file
and just simply tell you whether the syntax itself is valid.
So it's a good idea to run this
as part of your something like a get pre-commit hook,
for example, so you don't check typos in it.
We'll talk about this a little bit
when we get into the continuous deployment section.
Version you already saw.
This simply tells you
what version of Terraform you're using.
And then workspace we'll get into
during the collaboration section.
But a workspace in Terraform is essentially
an alternative version of the state.
So you can do things like run
different stages of development using the same config files.
So you can have a dev staging and production workspace,
for example, is a pretty common use for that.
And then there are some other commands here
that are less commonly used.
The 0.12upgrade command,
in between Terraform 0.11 and 0.12,
the configuration language was changed very significantly.
So the 0.12 upgrade commands will attempt to take
a earlier version of your config
and convert it to something that's compatible with 0.12.
This debug command will basically turn on
verbose output of your commands.
There's other ways of doing that
using log settings and so forth.
Force unlock is used when the state file becomes locked.
When you're doing plan or apply
or some of these other commands.
They will enforce a lock on the state file
so that somebody else doesn't work on it at the same time.
And sometimes if Terraform crashes,
or if you cancel it in certain ways,
that state can be locked infinitely.
So sometimes you need to go in and force unlock it
if something has crashed or something has gone wrong.
Sometimes that locked file will become permanently locked.
And then the state command,
well there's a push command
and you can see that they've called it obsolete.
So that's one unused.
But the state command is used for
interacting with the state file.
Occasionally, especially during development,
you may need to manually delete
something from the state file.
Or if somebody has gone into another management tool
and deleted a resource, for example,
like they've gone into the AWS console
and they've deleted something
and didn't realize that it was managed by Terraform,
you may need to go into the state file
using this state command and remove that resource.
We'll talk about that
in the debugging and troubleshooting section.