(upbeat music)
You could provision resources in Azure manually
or with Azure Resource Manager Templates.
So why would you want to use Terraform?
Well, let me tell you why.
(keyboard clicking)
Terraform is your Swiss army knife of IaC tools.
Though you can use IaC in Azure
with the built-in tools and ARM templates,
most companies don't like putting their eggs
in just one basket.
They normally use some form of hybrid
or multi-cloud solution.
So why not use one tool that can cover them all?
And you only have to learn one configuration language.
For provisioning networking components
to deploying VMs,
Terraform can make provisioning fast,
efficient and repeatable,
and will quickly become an admin's best friend.
So enough talk,
let's start building with Terraform and Azure.
(keyboard clicking)
Hey there guys, in this demo,
we're going to use VS Code
to demonstrate how to provision infrastructure in Azure.
Up top, I have my Terraform configuration
down below I have my terminal.
I've already created my working directory,
which is Terraform.
And within that directory,
I have my main .tf file,
which is this code up here.
First thing we'll need to do is log into Azure
with our Azure CLI.
We'll do an az login,
which will pop up a web page
to put in your user information.
Once we've logged in,
you'll get this message here.
This gives you a little information about your environment.
You have your tenant ID,
your name and what user you're logged in as.
Clear my screen,
bring this down here a little bit so we can see.
In our configuration here
we are declaring our Azure provider.
On the source we're using the Hashicorp AzureRM provider
from the provider public registry
we're declaring our version,
which is greater or equal to 2.26.
And our required version of Terraform,
which is greater or equal 0.14.9.
Here we're declaring some provider specific arguments.
There is actually currently a bug
which sometimes can cause issues with provider registration.
So we are skipping that here,
here we have our first resource block.
We are creating a virtual network in Azure called Batman.
We're setting our address range,
the location of where it's creating it,
we're gonna be using central US.
And then our resource group
that we're deploying this virtual network to.
In our second resource block,
we're creating a subnet called Robin.
We're specifying the resource group
to deploy this subnet to.
We're using the virtual network from above
to deploy our subnets to,
and then our address range.
So pretty simple configuration.
So now let's go ahead and get this thing ready
to deploy to our Azure environment.
First let's check our formatting of our configuration file,
we're running Terraform fmt, for format.
And this will look through the file
and make sure everything's formatted,
everything has the correct beginning and ending brackets,
the correct syntax and so on and so forth.
If it comes up blank,
that means your configuration file was formatted properly.
If it made any changes to the formatting,
it'll specify which file it formatted,
which in this case, it's our main.tf file.
So the next thing we'll want to do
is we want to initialize our working directory.
So we'll do a Terraform init.
So if it comes up all green like this,
that means that it has successfully initialized
our working directory.
And we can move forward.
Obviously, if this didn't,
it would show an error and come up in red.
So now let's check and make sure
that our configuration is valid.
This can only be done
after your working directory has been initialized.
You can run a Terraform validate.
And if our configuration is valid,
you'll get a green success.
If it was not valid, you would get an error.
And it would tell you where the problem is
so you could fix it.
So you don't have to go hunting through your configuration.
So now that we know our configuration is valid,
we can go ahead and do a dry run of our deployment
to get an idea of what is gonna be added and changed.
We'll do a Terraform plan.
Let me actually expand my terminal
so we can see it better.
So here you can see it is adding our subnets
and our virtual network
and any other dependencies it may need to deploy this.
We got no errors and it looks like it's going to do
what we intend it to do.
So now we can go ahead and run a Terraform apply
to deploy our infrastructure.
Go ahead and hit enter.
Just like with the plan,
it shows you exactly what it intends to do.
It's going to add two resources
and then it asks you if you want to proceed.
So you type in yes,
and we'll give it a few seconds
to deploy our infrastructure.
And if it completes it successfully,
you'll get a apply complete all in green
and lets you know that it added two resources.
If we made changes,
it would show how many changes were made.
And if it was destroyed,
it would show how many resources were destroyed.
So here we just added our two. To confirm
we can do a Terraform show,
which will tell us all that was deployed.
Here we have our subnet that had deployed
and our virtual network by the name of Batman
and Robin for our subnet,
you can also see a list of resources
that our configuration is managing
by doing a Terraform state list,
which you can see it is managing our subnet
in our virtual network.
Let's go ahead and hop over here to our Azure console.
If we come over here to our virtual networks
you can see we have Batman, virtual network.
If we come down here to subnets,
we can confirm that our Robin subnet was created as well.
Now that was pretty simple.
A good example of how Terraform can save you some time.
Let's say you were asked to create a dev test
and production environment in Azure.
How would you do this?
Now, you could do it manually,
but it would take considerable amount of time.
With Terraform, you could use the same configuration code
and use some variables and deploy all three environments
in a matter of minutes.
Now say you don't need those resources
or even full environments,
manually deleting resources can be such a chore.
With all those dependencies that you must account for
depending on how large your infrastructure is,
can take time and can be quite tedious finding out
what is stopping you from removing the resource
you want to get rid of.
With Terraform though, deleting dependencies
is a much faster process,
since it uses the provider's modules.
It will delete the resources in the order that it needs to
without any hassle.
Let's take a look at this simple process.
(keyboard clicking)
And just as easy as we provisioned our resources
in our Azure environment,
we can also delete them just as quickly.
Now say, if I wanted to delete one single resource,
I could do a Terraform destroy
and then use the target flag
to specify a specific resource.
Now, if I wanted to destroy everything I deployed,
I would just use the simple Terraform destroy command,
which I will demonstrate right now.
Let me just expand my terminal.
We'll go ahead and hit enter.
And just like with the Terraform apply,
it's gonna show you everything that it's going to delete.
So it's going to delete everything that we deployed,
including any dependencies
that were involved in the deployment.
So it hasn't done anything yet,
just like with the apply,
you need to confirm it before you delete.
So I'm gonna type in yes.
And we're gonna give it a few seconds
to destroy our infrastructure.
And we get the all green
saying that are destroyed completed,
and that it destroyed two resources.
So we can confirm this just like we did
with the apply by doing a Terraform show.
And if it did truly destroy our infrastructure
that should come up blank, which it does.
Then also, if we do a Terraform
state list, this too will come up blank.
And to triple check,
we can come over here to our resources in our Azure portal
do a refresh, you can see we don't have any more subnets.
If we come over here to home and then
our virtual networks,
you can see that our Batman virtual network
is no longer there.
So nice, simple and efficient.
So just to quickly recap,
I was able to deploy my network resources in Azure
with a single configuration.
After we confirmed
that our networking resources were deployed,
we were able to use the same configuration
to also delete the resources
we just deployed with a single command.
We set up and tore down our infrastructure
in a matter of minutes, opposed to hours,
if we did it manually.
This was a nice hassle-free experience.
I don't know about you,
but I am always looking to find tools
that can make my job and life easier
and Terraform is one of those tools.
When you are managing multiple cloud
and on-prem environments,
Terraform should be the first tool you should think of
whether you use Azure, AWS, GCP, VMware and many others.
If you enjoyed this video,
give it a thumbs up.
If you have a question, let me know in the comments.
Until next time folks, keep being awesome.
(upbeat music)