###### tags: `mis-notas` `terraform` # Terraform Is really great for moduling your cloud infrastructure through code version: terraform 1.0.x 1. terraform config _/ 2. providers _/ 3. variables _/ ? 4. local variales _/ 5. modules 6. outputs ## How does Terraform work How does Terraform connect to the infra platform provider? How does terraform connect to AWS, to create virtual space, start EC@ instances, configure networking, etc. ![](https://i.imgur.com/iE4582K.png) In order to do the job, Terraform has 2 main components that make up its architecture. ## Terraform Architecture ### 2 main components ## FIRST ONE IS: CORE The core uses 2 input sources in order to do its job. So it takes: - TF-Config: you as a user write and where you define what needs to be created or provisioned. ## SECOND ONE IS: STATE State where Terraform keeps up-to-date state of how the current set up of the infra looks like. So what CORE then does is -> it takes this input and it ifgures out the plan of what needs to be done so it compares the state - what is the current states - what is the configuration that you desire - the end result and compares that and then it sees there is a difference so yu want sth else than what the current state is it figures out what needs to be done to get that desired state in the cnfiguracion file. So - what needs to be created - what needs to be updated/deleted - In which order on that infrastructure setup -> SECOND COMPONENT or SECOND PART or the architecture are provider for specific technologies. - Cloud providers like AWS or other infrastructure as a SErvice Platforms so for the infrastructure level tasks but terraform as I mentioned is also providers for more HIGH LEVEL COMPONENTS like: - Kubernetes - other platform as-a-service tools - even software-as-a-services tools Gives you possibility to create stuff on different levels like: - create AWS infra - Deploy or create Kubernetes - create services inside that kubernetes - create components inside that kubernetes cluster ![](https://i.imgur.com/ESEbYWw.png) ## Terraform has over 100 providers ![](https://i.imgur.com/TqsSK9u.png) - Through providers you get access to resources ### AWS So through AWs providers you have access to hundreds of AWS resources like: - EC2 instances - AWS users - etc.. ![](https://i.imgur.com/J50LkSV.png) ### Kubernetes with K8s provider you access to commodities resources like: - services - deployments - namespaces - etc. ![](https://i.imgur.com/lgLIMNh.png) - Terraform tries to help you provision and cover the complete application setup. From infrastructure and all the way to the applications... ## Terraforms strength is in the infrastructure provisioning, and for the other stuff Ansible. ![](https://i.imgur.com/1WI1Bqg.png) # TERRAFORM FLOW 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 carry out those execution steps... ![](https://i.imgur.com/xRm0fpx.png) ## Example Configuration Files ### AWS ![](https://i.imgur.com/46zm07H.png) ### K8s ![](https://i.imgur.com/dVsk2yo.png) - `DEFINE RESOURCE AND ITS ATTRIBUTE -> that's what Terraform will create or do for you` ![](https://i.imgur.com/XozIYHk.png) ## DECLARATIVE ![](https://i.imgur.com/XEtkoZo.png) ![](https://i.imgur.com/G5xVUjB.png) # Terraform commands for different stages ## STAGES ## refresh ```shell= $ terraform refresh # Terraform will query the infrastructure provider (AWS) to get current (up-to-date) state # terraform willnow know what is the current state of the infrastructure. ``` - plan > The CORE is responsible for taking current state in your configuration file is input and decide based on the difference what needs to be done <- that is the (PLAN) So what Terraform needs to do in order to achieve that desired state that you defined in a terraform configuration file. * If it is an initial setup it figures out all the steps to create the setup * If it is an update, it compares the existing setup with a new desired state and figures aout what changes and adjustments need to made in which order to create a new desired STATE. Example - Add a new server - Add a new permission - etc. - ->THIS IS JUST A PLAN - This is where the core kind of construct the plan logically or what needs to be done ## PLAN Is the command where the actual execution happens ```shell= $ terraform apply # with apply we execute the plan ``` Plan command is like a preview of what is gonna happen if you execute `terraform apply` Terraform in the background will do the refresh at the up-to-date STATE > then create the plan > and then apply it. MEANS if you want to execute a configuration file you can just execute the `apply` command. ## DESTROY Destroy the whole setup removing elements one by one in the right order and cleaning up all the resources that were created. Basically reverting everthing that has been created and this could be used if let's say... ...you create an environment for an important demo day and you didn't want to interfere with the existing environments once the demo is over you can destroy the whole setup. - DESTROY like APPLY will also check what's currently running and then create a plan of what needs to be removed in which order. ![](https://i.imgur.com/inpf4mg.png)