(upbeat music)
Terraform is such a great tool
to manage multiple cloud providers.
Whether it be GCP, Azure, or AWS,
Terraform has you covered.
In fact, one of Terraform's best features
is the extensibility
that allows it to be used with multiple providers.
Now, covering all three providers is too much in this video,
so in this video,
we'll look at how easy it is to make changes
to an existing Terraform configuration
and apply those changes to resources in AWS.
(upbeat music) (keyboard clicking)
When it comes to popularity, both Terraform and AWS
are at the top of their prospective list,
so it comes as no surprise how well they work together.
Infrastructure is continuously changing and evolving.
Terraform can help you manage those changes
in your AWS environment.
As you make changes to your Terraform configuration,
Terraform will build an execution plan
that will only modify what is necessary
to reach your desired state.
Let's take a look at how to make changes
to your infrastructure.
(upbeat music) (keyboard clicking)
Here we go, guys.
I am using VS Code in this demo,
so I can easily show you our Terraform configuration,
as well as run the code all from the same window.
So here is the Terraform configuration that we are using.
We are declaring our providers.
Here we're using our AWS provider.
Source we're using is our hashicorp/aws provider
from the provider registry.
We're specifying a version of the provider.
And down here, we're declaring our version of Terraform
we would like to run.
It's basically anything greater than or equal to 0.14.9.
Here we're setting our provider-specific values.
So we're declaring our region as us-east-1.
In our profile, we're just using default.
And then down here at our resource block,
we are deploying an EC2 instance
using this AMI here.
And we want a t3.medium instance.
We are setting our Name with our tags
called long_live_the_bat.
Let's go over here to our AWS Console.
Here we're in our EC2 instance dashboard.
As you can see, we have long_live_the_bat.
That's our EC2 instance that we created, t3.medium.
So now, we'll go back over here to our code,
we want to modify this and add to it.
What we're gonna do
is we're going to change our instance type
from a t3.medium to a t3.micro,
and we want to add more app servers.
This is also one of the reasons
why we are lowering the instance type
because we are going from one app server to three
to help balance the load.
We're gonna accomplish this by using the count function.
And then we're going to specify the name.
We're gonna come down here to the tags
and we're going to use an expression
so we can add the number designation
to our naming convention,
as well as set a tag labeling these
as our development app servers.
So the first thing we want to do is we'll come down here,
change the instance type from medium to micro.
Before our AMI, I am going to add our function.
So I'm gonna use the count function,
and we're gonna do =3.
Next, we're gonna come down here to our tag
and we're going to use an expression
to add the number of the app server to our name.
So we're gonna put a space in between the bat
and the double quote.
We're gonna use the dollar sign, squiggly bracket,
and do a count.index, end squiggly bracket,
and then our ending double quote.
Then after this line, we're going to drop down a line,
and then we're going to specify our environment.
So we're gonna do an Environment
equals, and then in double quotes, we're gonna do Dev.
So we're gonna go ahead and save our configuration.
Now down here in our terminal,
first, we're going to check our format of our file.
We'll do a terraform
fmt,
which now has correctly formatted our configuration file.
Now if you haven't already initialized
your working directory, you can do so now.
It's not gonna hurt anything.
If it's already initialized,
you can go into the next command,
which is terraform validate,
which will validate our configuration.
But like I said, you must have already run
the terraform init command to do so,
or it will come up with an error.
We successfully validated our configuration.
Go ahead and clear my screen.
We're gonna actually expand our terminal here
so we can actually see this.
Next, we're gonna do a dry run of our deployment
to see what exactly is being changed.
So we're gonna do a terraform plan.
You can see here,
it says plans to add two resources, change one.
Scroll up far enough and it will show you
changes that are being made in yellow
and the additions in green.
So now that we're satisfied with that,
we can now do our terraform apply.
Type in yes to confirm our changes.
I'll give it a few seconds to run
and make our changes.
And just like that, our apply has completed,
and it's added two resources, changed one resource.
We can check our work by doing a terraform show.
As you can see, it's added other app servers.
They're in a running state.
Instance type is t3.micro.
Same thing with the server here.
Here's our third server.
Also, we can do our terraform
state list command,
which will show the resources
that are being managed by our configuration.
And if we hop over to our console, do a quick refresh,
we went from one to three EC2 instances.
Notice how we now have a
number designation next to each one of our names
and that our instance types are t3.micros now.
(upbeat music) (keyboard clicking)
So now that we have our three instances,
I'm gonna demonstrate now how easy it is to get rid of those
and destroy our infrastructure
that we just deployed.
Go ahead and clear my screen.
So say we are done with our development environment
and we wanna blow it all away.
We can do that by just doing one simple command.
We can do a terraform destroy,
which will delete all of our infrastructure.
If we go ahead and hit Enter,
you can see here that it is destroying
each one of our app server instances here
and removing all its dependencies
and all the infrastructure associated with all three
that were built using this configuration.
Now it hasn't destroyed anything yet,
so it gives you an opportunity to back out if you want to.
Here I'm gonna go ahead and type in yes to destroy it.
And just like that, our infrastructure has been destroyed.
It destroyed three resources.
So now if we do a terraform show,
it should come up blank, which it does.
And now if we do a terraform
state list,
it should also come up blank.
We can also head over to our AWS Console.
And if we do a refresh here on our instances,
as you can see, our instances have been terminated
and are no longer there.
So just as quickly as we deployed our resources
to our AWS environment with one single command,
we could blow them all away.
And there we go.
We successfully added and removed resources
in our AWS environment using Terraform.
Terraform gives what you need to get the job done with ease.
As you can see,
Terraform simplifies managing your AWS infrastructure,
being able to add and remove resources
and not having to worry about the dependency,
removes one of the biggest headaches
from managing cloud infrastructure.
If you enjoyed this episode,
let me know by liking this episode.
If you really want to go deeper with Terraform,
check out my Terraform course on A Cloud Guru,
but for now, keep being awesome, Gurus.
(upbeat music)