[MUSIC PLAYING]
KATIE MCLAUGHLIN: So Martin, you remember how we
were talking about Terraform?
MARTIN OMANDER: Yeah, Katie.
It made me really want to try it out.
KATIE MCLAUGHLIN: Well, how about we try out some Terraform
provisioning right now?
MARTIN OMANDER: Sounds great.
[MUSIC PLAYING]
KATIE MCLAUGHLIN: So Martin, I can
see you've got a new project set up.
And you've configured billing, yes?
MARTIN OMANDER: Yeah, that's right.
I have here Martin's Project, and a bunch of numbers.
Yeah.
KATIE MCLAUGHLIN: Yeah.
So if you open up the source code I
gave you over in GitHub--
MARTIN OMANDER: All right.
There it is.
KATIE MCLAUGHLIN: Yeah.
And if you go down to the README,
there's all the instructions you need to get it working.
MARTIN OMANDER: Oh, excellent.
My cat photo identification service.
All right.
Billing enabled, launch Cloud Shell.
KATIE MCLAUGHLIN: Yeah.
So you can run all this in Cloud Shell, because it already
has gcloud locally and it also has Terraform
locally installed, as well.
And you can get to the Cloud Shell
by clicking that little icon in the Google Cloud Platform
Console.
So if you go into the README, there
will be a git clone command that you can clone the source
code into your Cloud Shell.
MARTIN OMANDER: Oh, I love it when it's simple.
All right.
Let's see.
Copy that.
Paste that git clone in there.
KATIE MCLAUGHLIN: Mhm.
MARTIN OMANDER: OK.
KATIE MCLAUGHLIN: And then you want
to go into the expeditions directory, and then
the terraform directory.
MARTIN OMANDER: OK.
Very good.
All right.
I'm there.
KATIE MCLAUGHLIN: Yeah.
And so from here, there are three commands--
gcloud builds submit to create the container,
and then two commands to terraform.
MARTIN OMANDER: All right. gcloud builds submit.
Oh, I had said project.
Let's do that.
And my project ID is up here.
Authorize that.
All right.
The build finished.
KATIE MCLAUGHLIN: Yeah.
And now it's just two terraform commands--
first, terraform init.
MARTIN OMANDER: terraform init.
Very good.
KATIE MCLAUGHLIN: And now terraform apply.
MARTIN OMANDER: terraform apply.
Project ID-- I have that in my copy buffer.
Well, do I want to perform these actions?
What should I say here?
KATIE MCLAUGHLIN: Y-E-S for yes.
MARTIN OMANDER: Aha.
Yes, let's do it.
And it's done!
Looks like it's a service URL here?
What's that?
KATIE MCLAUGHLIN: That's your running service.
MARTIN OMANDER: Oh, cool.
Should I click it?
KATIE MCLAUGHLIN: Yeah.
MARTIN OMANDER: All right.
Let's see.
KATIE MCLAUGHLIN: Cats.
MARTIN OMANDER: Oh, look.
[LAUGHS] I have cats.
And we have some image labels here?
Where do these labels come from?
KATIE MCLAUGHLIN: So would you like
to know what this service does?
MARTIN OMANDER: Yeah, please explain.
KATIE MCLAUGHLIN: So if you go back to the GitHub repo,
I have got a architecture diagram listed on the README
there, right at the end.
MARTIN OMANDER: Aha, architectural diagram.
Nice.
KATIE MCLAUGHLIN: So this is what's going on here.
What we've done is, as we described in last week's video,
we had to create a container first.
So we used Cloud Build to create a container that
then stored that container in Container Registry.
And then we used Terraform.
We have, or I already created, a whole bunch
of manifests that would do all the steps required
by Terraform--
was going and creating a whole bunch
of IAM permissions, service enablement, and the like.
It then created a Cloud Run bucket and a whole bunch
of sample cats that we saw.
It then created a function, and then it created a service.
What we have is a service that goes
through all the images in a bucket,
and for each of those images, sends them off
to Cloud Functions and asks Cloud Functions to talk
to the Vision API to get information about each image.
In this case, it's a cat, and we're
asking it to describe the image.
So it's coming back with labels like cats, whiskers,
and the like.
Those results then come back to Cloud Run,
and then it displays all the results in that lovely web
page.
MARTIN OMANDER: Nice.
So these four boxes were all automatically deployed
with that one terraform apply command?
KATIE MCLAUGHLIN: Yep.
Easy, isn't it?
MARTIN OMANDER: Yeah.
So they are all in here somewhere in my project now?
KATIE MCLAUGHLIN: Yeah.
So if you want to go to Cloud Run,
you can see the service has been created.
MARTIN OMANDER: Yeah.
Let's see.
All right.
I see the cats service here.
KATIE MCLAUGHLIN: So you can see the service has been created.
Because we did all this in Terraform,
the Console is having to start from cold.
MARTIN OMANDER: Oh, I see.
KATIE MCLAUGHLIN: Yeah.
MARTIN OMANDER: Ah, yeah.
I clicked on Revisions.
I see there's one revision here.
It's getting 100% of the traffic.
This is looking good.
KATIE MCLAUGHLIN: Yeah.
We also created the Cloud Run function,
which if you go over to Functions,
you can see that there.
MARTIN OMANDER: All right.
Cloud Functions.
Let's have a look.
All right, so I'm in Functions.
Ah, we have a function called processing here.
Aha.
So here I can see the source code of that function that
was part of the project.
Very nice.
KATIE MCLAUGHLIN: And then the last major thing we did
was create the Cloud Storage bucket and pre-populate it.
MARTIN OMANDER: Oh, right.
That's those cat pictures we saw a minute ago, right?
KATIE MCLAUGHLIN: Yeah.
MARTIN OMANDER: All right.
We're in Cloud Storage.
KATIE MCLAUGHLIN: The bucket I created
is the one that has media on the end.
MARTIN OMANDER: Oh, OK.
Let's have a look.
Mhm, and it looks like some cat pictures.
KATIE MCLAUGHLIN: Yeah.
So what I did in this case, Martin,
is I created all these elements in Terraform.
But I also pre-populated the bucket with some sample cats.
That way, you can see that your service is working.
What we can do now is we can delete those cats.
Because we created them in Terraform,
we can recreate them again.
So what we can do is remove those cats
and get Terraform to recreate them for us.
MARTIN OMANDER: Oh, cool.
Let's try it.
Delete them.
All right.
Feels a little scary, but I trust you, Katie.
KATIE MCLAUGHLIN: In this case, Terraform
created this sample data, so it can recreate this sample data.
Please do not go deleting everything from your buckets.
MARTIN OMANDER: [LAUGHS] And--
KATIE MCLAUGHLIN: If you reload the service--
MARTIN OMANDER: --how do I get them back with Terraform?
KATIE MCLAUGHLIN: Reload the service first,
just to show that the cats have gone away.
MARTIN OMANDER: Oh, right.
OK, so here was the cat pictures.
Oh, I have no cats.
That's so sad.
KATIE MCLAUGHLIN: I have no cats.
So if you go back to Cloud Shell,
--the last command we ran was terraform apply.
So if you want to run that again--
and maybe make the window a little bit
bigger, because we're going to start looking at some
of the Terraform output now.
MARTIN OMANDER: Oh, OK.
Cool.
There we go.
Let's see if I have the project--
yeah-- project ID in my copy buffer.
KATIE MCLAUGHLIN: So last time, you
may have noticed we were creating 18 elements.
This time, we're only creating a smaller set of elements.
Terraform has identified that our desired
state of the project requires us to recreate
these three images and some other smaller bits of metadata.
But--
MARTIN OMANDER: Ah.
I see.
KATIE MCLAUGHLIN: --you can type Y-E-S again
and get all the cats back.
MARTIN OMANDER: Oh, yes.
Let's get the-- ooh, that was quick.
All right.
So if I hit the service over here now,
we should hopefully see some cats?
KATIE MCLAUGHLIN: Mhm.
MARTIN OMANDER: Yeah.
The cats are back.
Oh, I love this, how if anything went awry with the service,
I could just easily recreate it.
KATIE MCLAUGHLIN: That's one of the powers of Terraform.
Now, there are some limitations to how much
Terraform can save you from misconfigured projects.
But this is an example of how Terraform can recreate some
of these resources for you.
MARTIN OMANDER: Ah, right.
Cool.
I have to keep on entering the project ID here.
That's a little annoying.
Can we do something about that?
KATIE MCLAUGHLIN: We can.
So what we'll do is we'll do two things at once here.
We'll get you to reapply terraform again,
and we'll also get you to define that variable.
So if you go terraform apply, but before you hit Enter,
go space, -var space, project= and then your project name.
MARTIN OMANDER: Ah, I see.
Let's do that.
Now, this is a working application here.
Is it safe to run terraform apply again when everything
is working as it should?
KATIE MCLAUGHLIN: Yep.
In theory, Terraform will not see any changes,
or any changes that it does see are little tiny differences
in metadata or something that it doesn't control.
But whenever you run terraform apply,
you should just check what's going to be applied.
And then if you're comfortable with those changes, type Y-E-S.
MARTIN OMANDER: Y-E-S. Let's do it.
Boom.
That was quick.
KATIE MCLAUGHLIN: Would you like to destroy everything now?
MARTIN OMANDER: [GASPS] Destroy?
It sounds dangerous, but we did get the cat pictures
back before.
OK, I'll trust you.
Let's do it.
KATIE MCLAUGHLIN: Disclaimer-- in this case,
we are using a brand-new separate project that we
created with Terraform, all the elements
we created with Terraform.
So now we're going to get Terraform
to undo all the resource creation that it applied.
So if you're ready, Martin, if you want to type terraform
destroy.
MARTIN OMANDER: Oh, my gosh.
terraform destroy.
Like so?
KATIE MCLAUGHLIN: Yep.
And you're going to have to enter in your project, as well.
MARTIN OMANDER: Ah, OK.
Should we do this--
whoops-- -var project=--
there it is.
All right.
Let's do it.
18 things will be destroyed.
Oh, my gosh.
KATIE MCLAUGHLIN: We created 18 things,
now we're going to destroy 18 things.
So if you're ready, Martin, Y-E-S, and Terraform
will remove everything that it created.
MARTIN OMANDER: Oh, my.
All right, so it's destroyed.
Now if I hit this, the application of the web page--
oh, my gosh.
It's not even I have no cats.
It's literally a 404.
The URL doesn't exist, eh?
KATIE MCLAUGHLIN: We destroyed everything.
MARTIN OMANDER: Wow, OK.
How do I get it back?
KATIE MCLAUGHLIN: terraform apply.
MARTIN OMANDER: Oh, really?
OK, let's do that.
All right.
18 things will be added, it says.
OK, let's do it.
All right.
The terraform apply is all done.
KATIE MCLAUGHLIN: Yeah, and your cats should now be back.
MARTIN OMANDER: Oh, let's see here.
So, OK, before, I got a 404 error,
because the service had been removed.
Ah, the cats are back.
I'm relieved.
That was really cool, Katie.
KATIE MCLAUGHLIN: Did you want to take a look
at what the Terraform manifests look like, while we're here?
MARTIN OMANDER: Ah, yes.
What's the magic that makes all of this happen?
KATIE MCLAUGHLIN: No magic, just the careful application
of configuration files.
MARTIN OMANDER: [LAUGHS] Very good.
Let's have a look.
KATIE MCLAUGHLIN: You can go to GitHub,
or we can use the Cloud Shell Editor.
MARTIN OMANDER: All right, I'm going to GitHub.
I'll go up in the directory here.
Ah, .tf-- does that have anything to do with it?
KATIE MCLAUGHLIN: Yep, .tf is for Terraform manifest.
So there are a couple of manifests in here.
The three main ones that are standard names for Terraform
manifests are the main, which is the main file, variables
for variables, and then outputs for outputs.
Each of the other manifests that I
have there for this demonstration
are just a logical separation of each
of the different components-- the function for functions,
the service for service, media for media, et cetera.
Those folders that I've got in there
are also just the source code.
So if you have a look at the function,
you'll end up with the requirements.txt
and the application file that we saw earlier in Cloud Functions.
So that's just the function code.
Same for the service code.
But it's those Terraform manifests that define--
MARTIN OMANDER: Ah, the service is the Cloud Run service?
KATIE MCLAUGHLIN: Yeah.
Yeah.
MARTIN OMANDER: Aha.
Cool.
KATIE MCLAUGHLIN: But it's those manifests
that define what we want our project state to be.
MARTIN OMANDER: Oh, I see.
They list the services, and the buckets,
and everything that's in these .tf files.
KATIE MCLAUGHLIN: Yeah.
MARTIN OMANDER: Cool.
So, Katie, I have one more question for you.
When I was running this terraform apply,
it always asked me to input yes, Y-E-S,
to actually make it happen.
Can I automate that away, as well?
KATIE MCLAUGHLIN: Yes, but.
You can automate it away.
You can auto-approve, but when you are running Terraform
locally, you really should be checking what you're doing.
For automation pipelines and continuous deployment,
you can't have a human type Y-E-S.
So there are ways Terraform can do that.
But when you're learning, make sure that you actually
go back and read what Terraform is going to do, especially
if you start destroying stuff, just to make sure
that you're in the right project,
that you're manipulating the resources that you expect.
MARTIN OMANDER: Ah, cool.
Got it.
Well, I can't wait to write some Terraform manifests
and replace these long README files I've
been sending to my customer.
This is very exciting stuff.
Thank you for showing me, Katie.
KATIE MCLAUGHLIN: Absolutely no worries, Martin.
MARTIN OMANDER: All right.
Thank you for watching.
And if you have any questions or any suggestions for new topics
we should bring up in these episodes,
please enter them in the comments below.
Thank you.
Bye.
[MUSIC PLAYING]