>> Hey Friends, Azure Corp Terraform is
an open source infrastructure as code tool
for provisioning any infrastructure anywhere.
So it's no surprise, of course,
if you can provision a Kubernetes cluster
on AKS using Terraform.
Mishra, from HashiCorp, is here to
show me how it works on Azure Friday.
Hey friends, I'm Scott Hanselman and
it's another episode of Azure Friday.
I'm here with Mishra and you don't work for Microsoft?
>> No, I don't. I work for HashiCorp.
>> Fantastic, I like having people from
all over because it's open source software,
making great Clouds work.
HashiCorp has a bunch of different products.
>> Yeah. We make six different open source products
in different spaces like security,
provisioning space, run time space.
Yeah, so we make open source software.
They're pretty popular.
Today, we will talk about Terraform though,
kind of focus on Terraform.
>> Right, so on the deployment side,
Terraform works with AKS?
>> Yeah, it works with AKS. So Microsoft
and HashiCorp have a really good partnership.
So we have Azure provider that
enables us to provision pretty much anything in Azure,
in terms of Azure resource manager,
and then AKS is one of those resources.
>> Okay, so it's people
who already know how to use Terraform will
then feel very comfortable and
feel like they understand what's going on?
>> Yeah, exactly. So the idea is
that folks that are already using Terraform,
or they want to use Terraform,
they can just pick it up.
It's pretty simple, pretty easy to kind of pick up.
On the other side, we also have
support for Terraform in the Azure Shell as well.
So there's a Terraform binary that's dropped in.
So as soon as you open your Azure Shell,
you should have that available.
>> Well, that's a really interesting thing.
So that means if I go to shell.azure.com and I type TF,
I've got Terraform right there.
>> Yeah.
>> Without having to do anything.
>> No. Yeah, everything is configured for
you so you're good to go. So you can get started.
>> That's pretty cool. So people
right now who may be watching this,
may not be familiar with Terraform,
they already have this.
They can just learn how to use it
and they've already got it configured in their shell.
>> Yeah.
>> So you brought me some demos?
>> Yeah, so today we're going to focus
into provisioning a Kubernetes cluster
using Terraform and using Azure provider for doing this.
So here, I'll go through the configuration here.
Here, I have a simple Teraaform configuration.
The first thing you see here is the provider which is
the Azure item provider, Azure resource manager.
So we support all the APIs and every object
that the API exposes on the Microsoft side.
Then we start off with like creating a resource group.
So the idea is that,
all the compute resources in Azure,
they need to correspond to a resource group.
In this case, we recreate
a resource group using Terraform.
So this is the resource group type of resource,
and the name of the resource.
Then we focus in on the core of the example here,
this is the Kubernetes cluster resource
and that's pretty much all you need.
So all you need is this,
like these many lines of code to create
a Kubernetes cluster in Azure, and then,
>> Just as a quick point of note,
I'm noticing that you're on a Mac.
You're in Visual Studio Code.
>> Yes.
>> You're going to do TF file.
You've got IntelliSense,
>> Yes.
>> You've added an extension for TerraForm?
>> Yes. So there is an ATL parsing
on the Visual Studio side,
in the plugins I think you can just find it.
>> Very nice.
>> Yeah.
>> Makes it really easy to understand this.
>> Totally.
>> I'm assuming that these are different
variables that you're pulling out to
make this like var.cluster
names is coming from somewhere.
>> Exactly. So I'm just going to walk
through the configuration here.
>> Please.
>>The first thing you see here is the cluster name.
These are things like names and locations.
They are called attributes in Terraform.
>> Okay.
>> So that the name has a variable called cluster name.
If you go to the "Variables File" here,
we have cluster name defined and it's hardcoded for
now but you can also provide it
using the CLI if you want to.
In this case, I'm just going to hardcord it.
Then things like the resource group name and
the location is actually not defined in a variable.
It's being referenced between the two resources.
I'm referencing the Azure RM resource group
and getting the name and the location.
So in this type of
syntax in Terraform is called interpolation.
So you get into links resources together.
So now in Terraform, when it goes,
it goes ahead and creates these resources,
it'll actually create the resource group first,
then create the Kubernetes cluster
because its nodes depends on it.
You don't have to explicitly
provide that drift reference.
This is automatically done using graph theory.
>> Oh, let me see if I understand that for a second.
If you wouldn't scrolling up a little bit.
>> Yeah.
>> May we can get a full,
wide shot of me pointing at this.
So that's a declaration of something I want,
and that's the declaration of something I wan't.
>> Totally.
>> Not necessarily a
programmatically in order things
like I need one of these,
and I need one of those, and this, this,
that this, that this refers
to this, that this, that that.
>> Yeah, that's absolutely.
>> Graph theory as you said.
>> Yeah.
>> Then.
>> Creates dependency graph,.
>> Who goes first.
>> Exactly.
>> Very Cool.
>> So it'll try to provision
as many things in parallel as possible.
Yeah. So this is it, and then you provide things like
the admin username if you wanted to,
you can actually generate a
private and a public key using Terraform as well.
That's what I'm doing with the TLS resource,
and then provide things
like the agent count and things like that.
That's all defined here too.
So we need like three agents in this case,
and I'm defining that here, and that's it.
Then in terms of like the outputs,
so Terraform also has this notion of outputs.
So you can actually output things out of
the resource using the outputs syntax right here.
So what do you're going to do is, you can use
interpolation again to pull out the output.
In this case, we're pulling out the Kubernetes config,
and this is what we'll use in the demo to
connect and figure the client's side.
Then also pulling out the host name,
and I'm just like outputting some simple commands
that we can use to configure the client`s side.
>> So this chunk between configuring and figured,
this is a shell script?
>> Here, I'm just like outputting some string.
>> So that's help?
>> That's help for us.
>> I see.
>> So you can do things like this as well.
So you'll see how this all comes
together when I go through the demo.
>> I see. Thank you.
>> So now, we'll move into the term like kind of,
the first thing when you go through
that certain set of Terraform commands,
the first thing you do is,
do a Terraform in it, which initializes the alignment.
This will download all the plugins that you need,
all the modules that you might
be using and things like that.
Then the next command you're on is a Terraform plan.
Now, this is unique to Terraform.
So basically, Terraform shows
you what's going to happen before it actually happens.
So in this case, Terraform is showing
you that it's going to add four resources,
and these other four resources,
as you can see the Kubernetes cluster,
the resource group, and in this case some ssh_key.
>> So it's doing it but not doing it, it's a simulation.
>> Yes.
>> It's a what if.
>> Exactly. It's a dry run. This is
completely safe to run.
It's only a read-only operation.
It doesn't do anything. But what you can do here is
actually share this plan across the organizations.
So that can share this with operations team,
or security team to get a plus one and being like okay,
this change makes sense and you
can make predictable changes,
which is one of the goals of Terraform.
Once this is done, you can actually
go ahead and apply if you want to.
In this case, we would just run the "Apply".
>> Okay.
>> It'll go through the application process.
This takes up to 10 minutes on the Azure site to create.
So I've already pre-created a cluster for
us and we'll use that,
and this is all kicked off and
maybe we'll see it at the end.
>> I see.
>> I create it but, yeah, we'll move to the same project.
I'll just change the name.
I've pre-provisioned the resources.
>> Right, just like when you put the pie and the cake
in the stove and you press the perfect pie.
>> The cooking show, yeah.
>> Yeah.
>> That's exactly right.
>> Fantastic.
>> So I run "Plan" here,
and then this plan should tell us that everything is,
that there's no changes basically,
because you've already provisioned everything.
>> So does that mean that it is important to point out
that the Terraform has re-entrant and that I can
run it multiple times without fear of destruction.
>> Totally.
>> I am making a declaration of what I
want and if I've already got that,
it's like, "Yep, still cool."
>> Yeah.
>> We're good.
>> That's absolutely right.
Terraform also has this notion of state,
and we will go into state in depth.
But the idea is that, it tracks
all the resources throughout its lifecycle.
So from being created, to being changed,
to being destroyed, it'll kind of track
that resource across its lifecycle.
So in this case, if you don't apply here,
we'll kind of get those outputs,
the outputs that which we
saw in the previous code examples.
So here, you see the, that's a lot of it.
>> That's impressive.
>> So it's like a text. So what you get out from
the Kubernetes resource is
this Kubernetes config file in raw format,
you get the host name,
and you also get some helper commands to do things.
So let's start with the Terraform output command.
This will only filter certain things that we need to
kind of see to configure the client's side.
>> Okay.
>> So we'll kind of output the configure
variable and this is the helper text that we saw.
>> Right.
>> So now, we'll configure the client side so we can
access that Kubernetes cluster that we created.
>> Okay.
>> So the first thing we will do is
dump that raw config Kubernetes
config file into under.kube/aks.config.
This is the standard Kubernetes,
>> Is this a normal way to do things?
Is it not expected that Terraform doesn't do everything,
but it structures everything.
>> Yeah.
>> But I could take the command that you just did,
the plan and these two or
three and then put it into a shell script.
>> Yeah.
>> If you want appropriate.
>> Yeah, that's totally right. It has
local provisions and also remote provisions,
and they can trigger based on the resource.
So once that resources changed,
you can trigger the shell script again and it'll
dump out the things again if you wanted to.
>> Okay.
>> In this case, I wanted to go through
these steps so we can show that
users how we can connect them.
>> Yeah, I appreciate that.
>> So yeah, we'll dump that in,
and again if I cut this file.
This is that crazy Kubernetes configuration file,
I'll output this again.
Then I will set this kube config environment variable.
This is what kubectl and all the client side tools
used to kind of get through
the Kubernetes configuration file.
So once that's done we'll do kubectl get notes.
>> That's to be able to get nodes
using the Kube config that Terraform,
>> That we just created.
>> Got, out of AKS
and that is right now. Boom! There it is.
>> Yeah.
>> AKS file.
>> So, now we have a working cluster.
It was provisioned like a day ago
because pre-provision things.
But here like, yeah, we're able to connect directly.
Now, we're kind of take this a step further and
showcase some of the other features in Terraform.
So we also have a Kubernetes provider.
So if you go into the "Pod Directory".
Sorry, I can barely type. Oh my God, okay.
So you go to "Pod-Service-Example".
>> It's okay.
>> This is another Terraform configuration project.
Here, you see the provider instead of Azure.
If you remember from the previous example.
>> Right, that is where I am.
>> Now, it's Kubernetes provider.
So we're going to use the involved,
the Kubernetes provider and create Kubernetes namespace,
Kubernetes spot, and a service.
>> Does that allow me with,
all due respect to our friends at Kubernetes,
to hide from some of the difficult parts,
the YAML and the things that I don't want to think about?
So I'm comfortable with Terraform, I prefer Terraform.
While the YAML and the things that Kube control provide
and the JSON and
the things that RM provides in the Azure side,
I can use one language and
one tool to configure both of those things.
>> That's totally the the goal
with the Kubernetes provider that we want,
operators that are using
Terraform to provision multi-Cloud clusters,
VMs and Azure, they can use the same language.
They've learned it. They understand it.
They understand the steps of planning and applying.
So they just use that across the board.
So in this case, we'll just create the pod,
and it's just a simple ngix container,
>> Sure.
>> Which will just give us an nginx web page,
and also we'll kind of expose that as type LoadBalancer.
>> Of course.
>> That will give us an IP
that we can access on to the outside world.
Similarly, we'll go to this project,
we'll do Terraform in it,
which initializes the thing,
it downloads the Kubernetes provider,
and then run a plant,
which is very similar to the previous step.
So in this, you'll notice I'm only adding one resource.
>> Right.
>> Because I've pre-provisioned
the service and the namespace.
Because the service on the Azure side,
the LoadBalancer takes like
at least five minutes to come up.
>> Yeah.
>> So just for the time sake,
I have pre-provisioned of this.
Now, we will go ahead and apply it.
Then this will create that pod and
just start the ngix container on it,
and now, what we're going to do, we kind
of see the pod running.
So if you do get pods,
actually I have this in my history.
History is your friend. Well, if you know how to use it.
>> I'm still learning. It's been 25 years,
>> There we go.
>> I'm almost there. Here we go.
>> As you can see, the pod's ready,
and our service is ready and it has
an external IP of that,
and this is all running in Azure and you can see,
>> Boom! Look at that.
>> Welcome to nginx.
>> Yeah.
>> This is the classic example that people love to show.
>> But it's a great example because
like to the point that
I personally find RM to be a little bit overwhelming,
and I find the YAML and some of the requirements,
I spend more time getting my YAML wrong,
>> Right.
>> Than I do messing with it,
and being able to do this all in
Terraform is a very comforting thing,
and you saw how quickly we're able to put that together.
>> Totally. Yeah. I think
the partnership with Microsoft has
been really pivotal for achieving these things like that.
I'll be able to work on a preview,
be able to work on stages like
preview when AKS was in preview,
we had support for it as soon as it
became GAV release the support for it,
and we are super excited too.
>> So in our remaining few seconds,
where do people go to learn about this,
how we can use Terraform on Azure?
>> Yeah. So you can go to terraform.io
and that's like a landing page.
It has all the documentation.
You can also go to the Terraform module registry.
It has a bunch of pre-baked modules as well.
There should be a module for AKS,
for an AKS cluster if you want it too.
You can scroll through that.
>> You can use Terraform for all kinds of Azure services?
>> Yeah, everything. Everything that the azure
Resource Manager support, we kind of support it.
>> Fantastic. I am learning all
about how to automate Azure and in this case,
Azure Kubernete Service using
Terrorform today on Azure Friday.