You can provision resources in AWS manually
or with CloudFormation templates.
So why would you want to use Terraform?
Well, let me tell you why.
When provisioning infrastructure resources in AWS,
it can be a time-consuming manual process,
depending on how large your infrastructure needs to be.
Most cloud providers allow for the creation and modification
if infrastructure as code.
AWS provides a service called CloudFormation,
but if you use a multi-cloud approach,
keeping up with code for provider specific tools
can be a pain.
This is where Terraform can help.
Terraform provides a common human readable language
for creating and managing infrastructure
across multiple providers,
making it your one-stop shop
for your infrastructure's code needs.
Are you ready to see just how easy it is
to provision resources in your AWS environment
with Terraform?
Let's jump in.
All right, guys, I'm going to use VS Code
to do this demonstration.
This way, we can look at our Terraform configuration
nice and easily,
and we also have the built-in terminal
that we're gonna use to run our commands.
First thing I did was I created a directory
called Terraform.
This is my working directory
where I'm gonna store my Terraform files.
If you look inside, I have a main dot TF file,
which is our main Terraform configuration,
that is this code up here.
In this code we are deploying three app servers
within our AWS environment.
We're starting our Terraform configuration here.
First thing we're doing is we are declaring
our required providers, in this case it's AWS.
So we're using the AWS provider.
The source is HashiCorp's AWS provider
in the public provider registry.
Next we're specifying a version which here we're doing 3.27.
This operator here is actually a shorthand way
of stating that we are allowing only patch releases
within a specific minor release.
So like I said, in this case, it's 3.27.
Next, we are specifying the required version of Terraform,
which is greater than or equal to 0.14.9.
Next, we are declaring the provider specific values.
Here, we're setting the AWS region as US East One.
Profile, we're just using default,
and then finally we are declaring our resource
within our resource block.
We're creating three EC2 instances.
We're using the count to specify how many of these instances
we want to deploy. We're specifying our AMI for our image
in our instance type, which we're using a T3 micro,
and then we're specifying our tags down here.
First, we're specifying a name,
which we have long with the bat and this expression here,
we're basically gonna designate the number of the instance
and add it onto the end of our name.
And then we're gonna specify it in an environment
that this is gonna be in.
So it's gonna tag it as our dev environment.
So pretty basic and simple configuration.
I'm going to go ahead and clear my screen here.
The first thing we're gonna need to do
before working with our Terraform configuration
is we need to configure our AWS CLI
to use our access key ID and our access key secret
so we communicate with our AWS environment.
So if we do an AWS configure,
it'll first ask us for our access key ID
which I'm gonna go ahead and paste here, enter.
Now it's gonna ask us for our secret access key,
go ahead and paste that here,
and our default region is US East One,
which is the region that I want.
So I'm gonna hit enter,
and then our default output format,
we're just gonna go ahead and accept the defaults
and now our AWS CLI is configured.
So I'll go ahead and clear my screen
and now we can go ahead and I'm gonna actually expand this
so we can see our commands better.
First thing we want to do is to check
to make sure that our Terraform configuration
is formatted correctly.
There is a nice command that'll do this for you.
Just run a Terraform FMT to format.
If this comes up blank,
that means your Terraform configuration
is formatted correctly.
If it lists out a file or multiple files,
that means those files were reformatted correctly.
And after we do our format,
we now want to initialize our Terraform working directory.
We'll do a Terraform init
and if it comes up all green like this,
that means your working directory initialized successfully.
Obviously, if it comes up red,
it'll tell you the error that it ran into, clear my screen.
Now, if you wanna validate your configuration
to make sure that you're using
the correct provider versions, or, you know,
you don't have any resource issues
or general configuration issues,
you can run a Terraform validate,
which if it's successful, you'll get this message.
If it's not, it'll give you an error message
and it'll point you in the direction of what the problem is.
So now that we know our Terraform configuration is correct
and is valid,
we can now do a dry run of the apply
with the Terraform plan command.
This will allow you to see what your configuration will do
without actually making any changes.
So we'll do a Terraform plan, hit enter.
It'll show you everything that it's adding.
Nice thing about Terraform and its use of providers
is if there are any dependencies
or anything that are involved
with what you're trying to create,
it will add everything that is needed.
So all of this will be added
once we apply our configuration to our AWS environment.
So let me go ahead and clear my screen
and we'll now run our Terraform apply command
to apply our configuration, hit enter.
Here, it shows you everything that the plan showed.
Now it's asking if you want to perform these actions
and you type in yes,
hit enter and give it just a minute or two to run,
and once it's complete, you'll get the green apply complete.
It'll let you know how many resources you added,
how many have been changed and how many have been destroyed?
Go ahead and clear my screen again.
So to check to see what was added or changed,
there's a couple of commands you can use.
You can do a Terraform show,
which will show you everything that was deployed,
which is a lot of information here.
See it breaks it down per app server.
Let you know the state that it's in, it's running,
there's our instance size, tags that we added,
so on and so forth.
Lots of detail there.
If you wanna just see a list of resources
that your Terraform configuration is responsible for,
you can do a Terraform state list,
and it shows the three app servers
that we deploy to our AWS environment.
Let me go ahead and move over here to my AWS Console
and we can confirm here as well
that our instances were created.
See our instances running are three.
Here's our three instances.
You can see the names with the number attached to the end.
It's where our count expression came in.
Click on one of these getting more information.
See here, we ever two tags that we set,
our name and our environment.
So we have successfully provisioned three EC2 instances
with just a simple configuration file and a few commands.
As you can see, it's pretty fast and efficient
and also very repeatable.
Yeah, it was pretty simple.
A good example of how Terraform can save you time.
Let's say you were asked to create
a dev staging and production environment in AWS.
How would you do this?
You can do it manually,
but it would take a considerable amount of time.
With Terraform, you can use the same configuration code
and use some variables to swap out the environment names
and deploy all three environments in a matter of minutes.
Now, what about deleting resources?
Manually deleting resources can be a pain,
especially when dealing with dependencies.
Sometimes it can even take a while
finding problem dependencies blocking your deletion,
but with Terraform,
deleting dependencies is a much faster process.
So it was pretty easy to provision
our infrastructure resources.
It's just as easy to delete them.
Now, say, if you just wanted to delete
one of our EC2 instances from our current configuration,
you would do a Terraform destroy and use the target flag
and then specify your resource that you wanted to destroy.
In this case, we want to destroy all three instances.
So we'll just use the Terraform destroy command by itself.
So if I go ahead and hit enter,
I'm gonna go ahead and expand this
so we can see a little bit better.
As you can see it is deleting
all of the infrastructure that we have deployed.
So just like the apply it'll ask you
if you want to destroy your resources,
type in yes, hit enter.
I'll give it just a few seconds,
and just like that,
all of our resources have been destroyed.
Go ahead and clear my screen,
and if I do a Terraform show now it should come up blank.
Also, if I do a Terraform state list,
now shows that there are no resources being managed
by our configuration, and to go a step further to confirm,
if we come back over here to our instances,
and we refresh,
you can see that our instances are in a terminated state.
Wow, that was a breeze.
Just like that, our infrastructure has been destroyed.
So we're able to write our Terraform configuration
to communicate with our AWS environment.
We defined the resources that we wanted to create,
we used Terraform to check the format of our configuration,
initialized our working directory,
validated our configuration,
then planned our deployment
and finally applied our configuration.
Once we confirmed it created our infrastructure,
we demonstrated how easy it is to then delete
our infrastructure using the Terraform destroy command.
As you can see when dealing with provisioning
and deleting resources in AWS,
Terraform is a very useful tool
that can make a cloud admins job much easier and efficient.
Maintaining your cloud infrastructure
is a bit less time consuming.
If you enjoyed this video, give it a thumbs up.
Have a question, let me know in the comments.
Until next time folks, keep being awesome.