[MUSIC]
>> You're watching "Thrashing Code."
I've only had one cup of coffee,
so I'm going a little slow today.
First things first, here's a summary in the GitHub repo.
I'll blow that up a little bit,
in the GitHub repo Terrazura.
So, first thing, we wanna set up infrastructure
as code using Terraform,
open up iTerm,
and go ahead and pull that repo down somewhere locally
to work with it.
And now, I'm gonna open up Visual Studio code.
First thing we wanna do is we need the Azure provider,
which is gonna be provider.
At least, I hope we're gonna go with 2.20.0.
In Azure, you have a base resource
that you almost always need,
which is the Azure resource groups.
So, I wanna go ahead and just create a resource group.
All right. So, with a provider now,
I can pop open my terminal.
All right, and I can Terraform in it.
We got it, we got it running.
And let's, and hit that again, and then Terraform.
I didn't really need to do that.
We're already initialized, I just need to do Terraform plan.
Here we go.
Okay. So, it's got the resource group,
and then I'm gonna make sure that I have my provider
connection stuff already set up and actually apply that.
Terraform apply.
It looks like it's working,
navigate out to Azure portal.
Next please, create the resource group.
And it should pop up here in all resources in just a second.
It Looks like it's working.
There we go.
So, anyway, all right,
got the resource group added.
And in that resource group,
I'm gonna add the resource of azureerm_postgresql_server.
Like so, okay.
And we're gonna call it our terrazuraserver.
Hey, notice how it auto-completed
based on some other arbitrary stuff in there.
It's like it tried to be smart, but it wasn't smart,
it just auto completed based on other junk
I've typed in the file.
So, we're gonna switch to IntelliJ here in just a second
and use the HCL plugin in that,
because it introspects more effectively.
Terrazura, we got it open in IntelliJ
There's these state files,
which it shows greyed out or blued out
based on this theme.
And let's see.
Let's go into settings and actually get IntelliJ
set up a little better for the stream here.
There we go, that's more readable.
Add properties and required elements.
Okay. This is something that Visual Studio code
does not have.
Let me bounce this up to the appropriate size,
so you can see.
So, basically, I just clicked here on this resource
and I hit on Mac, it's ALT + RETURN,
on Windows, it'd be like, I think ALT + ENTER
or something like that,
and it gives you a little shortcut,
it says, "Add properties."
You notice it added it with an appropriate two spaces
based on my convention too, which is nice.
And we'll start following a good workflow practice here
as we do this, and we'll only be doing Terraform applies.
There we go, it's creating.
So, I'm gonna go up here though,
and we'll start making some of these edits.
Auto-complete, 'cause I have variables in there.
I'm hitting CTRL + Space.
Come on, space, there it goes.
Auto completes, edit.
I had to get it started there, probably, hasn't finished.
There we go.
So, everything's destroyed, these things are set now.
I'm gonna put the server up here.
So, this is the var.server, was the last one.
Yeah, the database.
We haven't added the database yet,
so we'll do that in a minute.
Okay. The next thing I need to do then,
hit the...
Yeah, we need to initialize this variables and set those.
The way I like to do that,
is to create a batch script,
to do each of the specific things that I like to do.
And we're gonna start with Terraform apply.
So, Terraform apply is gonna look like this,
and then I'm gonna go ahead and set it to auto-approve.
And whenever I run this it'll automatically apply,
and I want it to type yes, when I do Terraform apply.
So, now, this is a script way
to have that variable inserted into the script
at time of execution of the script call.
Okay. So, when I run this file,
it insert the environment variable into this file.
And then let's go ahead and add a database name.
And then, let's go ahead and add our API port.
We're gonna do API port instead of the 8080.
And that I think is all of our variables
that we were gonna set.
All right. So, these two things are set in my batch RC,
or my batch profile, or my Hasura file.
There's the Postgres username
and Postgres password like that.
Let's see sassyPassword, Postgres, blah, blah, blah,
and it'll take these environment variables
and insert those into this.
All right. So, we got those bits done.
So, now, we're pretty much set.
I'm gonna go ahead and put these two variables back.
So, even though they're not used, they aren't set,
so it won't cause an error.
The other thing that we need to do here,
is we need to chmod, 700 to make it executable.
Let's do that one.
Also, if you list it, you'll see them
color coded differently.
Usually, depending on your CLI,
they'll color code differently.
Terraform apply, and I'm gonna execute that Terraform apply.
Though, it's gonna take those variables,
insert those into there and execute this,
and we should get that whole database back.
The next thing that I want to add now,
is gonna be the actual database, I believe.
So, let's get the database added.
Resource, Azurerm_postgres_firewall.
What should we call it?
We'll call it the terrazuredb.
Then we're gonna have a resource group,
that it needs to go in.
We want the server, and that server, and then the name.
So, we're all set and we're ready
to now set up the firewall rule.
Start and end IP address, okay, that's these.
All right. This database is gonna be created
inside of the server,
and we should be all set there.
And I'll show you what I'm gonna use it for.
It's kind of like a trick in some ways,
is I'm gonna have it concatenate together,
the path we need to go to in the browser with the API port,
so that we can navigate to the Hasura console interface
to do our next stages of things.
But it'll get us started without having
to go back into the Azure portal anymore.
The apply happening.
And you should just see it zip through now,
or we'll see it zip through,
because there is no apply really to happen.
Almost got everything done here in our Terraform.
So, we have a repeatable deployable environment.
The next thing that we need now though,
is the API server.
So, this is not an entirely difficult part.
And in that resource, azurerm.
And then now, okay, it's telling me,
I have some required things, ALT + ENTER again,
and I'm gonna add the properties, boom!
It added them for me.
All right, resource group name,
it's gonna let that run.
So, let's grab that path,
and let's see where we get, huh?
Boom!
Oh, yeah, here we go!
Bam!
Yup, API server.
So, we're basically done.
We have a deployed, Hasura console API server out there.
Let's get in here and let's do something real quick.
So, we have these basic default schemas,
and, you know the database has to be there
if we create a schema, right?
Obviously.
So, let's create one.
Say, create, and we're gonna call this Terrazura.
One more, one more thing, right?
Let's create a table, and boom!
There we go, there's the table.
We can even...
Let's do this.
Let's, boom!
Look at that?
It is in our database.
So, now, we know we have a database,
It is connected to the Hasura GraphQL API server.
It has data in it, it has a table in it,
it has a schema that has that table in it.
So, bam!
We are live and working.
And keep in mind though,
it does destroy the database every single time.
So, if you put stuff in the database,
it won't be there when you go back to it,
doing the infrastructure.
Its infrastructure as code,
but I'm gonna show you how to work with the console
against your Hasura API GraphQL server
without having the console turned on,
on the actual live API GraphQL server.
That's the next trick.
So, I turned it off via the environment variable,
it is destroying the group.
So, since it destroyed the group,
it's probably gonna destroy everything.
So, that was a very disruptive change, I bet.
So, let's see here.
So now, if I take this,
and I try to go out to the site,
I'm gonna get zilch, right?
Yup. Not available.
It just gives me that dead end, okay.
But remember this path, this path is still important,
we're gonna keep it right there.
I'm just gonna type Hasura in it.
Yes, that's right, only those two things.
Now, if I click up here, it'll refresh it.
There it is.
So, there's my migration directory.
I have some files in here and have a config.yml
Here we go.
Yeah, I did it in the wrong spot,
I didn't know where it's adding it.
That's pointed at the red direction.
So, now, when we use the Hasura CLI commands
to do our migrations,
which we will do here in just a minute,
we'll actually be pointed at the live Azure deployment
of the Hasura server.
So, let's get that done, right?
False, I want that live.
Back the terminal,
then we're gonna go with Terraform apply.
All right. And then that'll get us to the next stage.
And now, if I go into migrations,
and get started with that part.
Boom, there we go!
let's get this other junk in here,
but I wanna go over to data.
And as I use this interface,
it's going to save all the changes I made.
So, first things first,
I'm gonna navigate to Terrazura,
and I'm gonna actually delete this schema.
Let's take a look in IntelliJ.
And note, migrations now has a migration in it.
Okay, there we go.
So, we've got three tables; activity, format, and schema,
and those need connected in some way.
They need correlated other things in the sources
that we would add into the database, right?
As we collect these sources of data and use dbdiagrams,
a little tool I like to use.
I'm gonna do this, and then I'm gonna click on...
Look, there is one of the schemas I have,
as you can see.
So, this is the stuff we just added. Whoops!
And then we have formatter, activity,
and then we have the schema,
and then the source have some notes about it,
possibly in a particular note jot.
But I'm gonna used this as a reference map, okay?
All right. So, we'll get to do connection now.
And this is gonna have an ID.
UID, generate random UID, add that.
So now though, we can go into activity,
and if we go look at the migrations,
you'll see the migrations are here, but each of these steps,
should be added specifically as a migration
in a particular order and everything.
So now, if we go back here, there we go.
So, the constraint is added
to connection table, foreign key connection ID,
schema connection ID, foreign key,
and you see references there too,
but it actually connects to.
All right. So now, relationships.
Let's add this one, save, and add this one, save.
And I believe that'll do it.
Yup, there we go.
So now, I'll scroll back up to relationships and add that.
When we go back out to our GraphQL,
we have all of these different ways we can query.
So, okay. We have our database out there.
We have a database schema
that appointed to out on dbdiagram.
So, this is the state of our system,
but let's say we Terraform destroyed now,
and we nuked all this stuff.
That would be scary if we did all this work,
and weren't able to redeploy it easily.
Now, the easiest way to get those back
is to do Hasura migrate.
But what we're gonna do now is nuke everything,
and get everything rebuilt with Metadata.
Terraform destroy.
I want to destroy everything.
So, that's what's happening, destroying everything.
The database is gonna be gone,
the database server is gonna be gone,
the resource group's gonna be gone,
the firewall setting is gonna be gone.
The database schema,
and tables, relationships,
all of that work we did is gonna be gone.
And then we're gonna re-add all this stuff,
get everything pushed back using migrations,
Metadata, and Terraform against Azure.
All right, running, running, running, running.
Any minute now, we are gonna have a destroyed database.
And we can complete this stream,
by having all of this stuff rolled together.
Bam, redeployed and usable.
All right, so it's destroyed.
Tests. So, we're gonna Terraform apply.
So, bounce back over here and let's apply.
This should apply, and get everything rebuilt
from the infrastructure perspective specifically, right?
As soon as that's done,
this will probably take two or three minutes.
Like it normally does to create a database,
database is getting set up
with seven days worth of database backups.
And this part has taken about a minute and then it's done.
And here is our location,
and we can do Hasura migrate, apply.
And let's all that run.
Now, if we refresh this,
it's dead, but I'm gonna do cancel.
Okay, it worked.
All right. We are looking good.
We're looking good.
And all this stuff here,
yup, all this is back,
but there's the API server, there's the server in Azure.
Okay. We set up infrastructure using Terraform,
then we built out our initial database schema.
Whew! All this stuff, all this stuff. (coughs)
So, anyway, all in all,
we have knocked out a ton of stuff today.
We just covered a fair bit of GraphQL.
Thanks to Microsoft for sponsoring the stream today.
Appreciate that.
Peace out, y'all.