Terraform : Chapter 2
Now we decide that we want to add five more servers to the existing infrastructure and also we want to add some security configuration or maybe remove some stuff . So now we are in the phase of managing the existing infrastructure . The good thing is we have Terraform, using terraform we can make such adjustments to infrastructure pretty easily. So once you are set up with terraform to create and change or maintain your infrastructure, a common use case could be Replicating that infrastructure.
Let’s say, After you have tested this setup and everything works fine you decide now you want to release your application in production environments, so you want to create a production environment that replicates this exact setup. We can easily spin up an identical infrastructure by using terraform .
So how does terraform do all this?
How does Terraform actually connect to this infrastructure provider platforms, how does terraform connect to AWS to create virtual space, start EC2 instances, configure networking etc.
In order to do the job terraform has two main components that make up its architecture:
1. Terraform core
The core uses 2 input sources in order to do its job.
- Terraform configuration (TF-config) , where we define what needs to be created or provisioned,
- State, where Terraform keeps the up-to-date state of how the current set up of the infrastructure looks like.
So Core basically takes the input and figures out what needs to be done to get to that desired state in the configuration file. so what needs to be created, updated, deleted.
2. Providers
Terraform is also providers for more high-level components like kubernetes or other platform as-a-service tools. It gives you possibility to create stuff on different levels like create a AWS infrastructure, then deploy or create Kubernetes on top of it and then create services inside that or components inside that Kubernetes cluster. It gives you all these possibilities, and it does that through those providers.
Terraform has over hundred providers for these different technologies and each provider then gives terraform user access to its resources. So through AWS provider for example you have access to hundreds of AWS resources
like it’s instances, the AWS users etc and with kubernetes provider you access to commodities resources like services and deployments and namespaces etc, so this is how this works and this way terraform tries to help you provision and cover the complete application setup from infrastructure.
Once the core creates an execution plan based on the input from config file and state, it then uses providers for specific technologies to execute the plan to connect to those platforms and to actually carry out those execution steps.
Terraform Configuration file Example :
Now we have an idea of how terraform configuration file looks like. This is an example where you see AWS provider and Kubernetes provider are configured
Terraform Commands
refresh with this command terraform will query the infrastructure provider in get the up-to-date state so terraform will now know what is the current state of the infrastructure.
plan with this command terraform creates an execution plan based on current state and desired state.
apply with this command terraform executes the plan
destroy which obviously destroys the whole setup removing elements one by one in the right order and cleaning up all the resources that were created.
I hope we have learnt some basics about terraform. See you in my next blog.
Happy Learning !!