What is it?
Terraform uses configuration files to describe the components needed to run a single application or your entire datacenter. Terraform creates a plan describing what it will do to reach the desired state, then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied!
Here is a summary of how the command line version works.
The first thing we must do is follow the instructions that can be found here to install Terraform and setup the other prerequisites.
The below guide focuses on creating a EC2 instance with a Security Group allowing for HTTP access from anywhere in the world. There is some user data that will be executed when the instance starts up, this will configure the website for us! This code is not production ready code but just something to help you understand what you can do with a few lines of code.
Now we can create the two files needed before we try to plan and apply the code to build our server.
The first file is going to be a main.tf this file is used store the core configuration for managing resources that you will create. The second file will be a outputs.tf, this file is used to store what we want the console to display once Terraform has executed.
Naming of these files does not really matter, and you can have all your configuration in one file, Terraform will read all the files with certain extensions in the working directory and and figure out what order to execute actions. The naming of files follows best practices for management of the code.
Below are examples that you can copy from to speed up file creation. The first file is main.tf and the second outputs.tf
From this directory you can now run the Terraform initialisation and plan command, this will download any required plugins and validate your code then it will display the plan to what it will execute.
The output at the end should look like this and show you how many resources it will create if you execute the apply on this plan.
Now you have reviewed the output it’s time to create your very own instance and a security group with rules that will allow you to browse to it.
This will run the plan again but this time ask you if you want to proceed with the creation of resources and to enter yes, you can simply add the -auto-approve switch if you do not wish you be prompted each time it runs.
This time once it has completed you will get the output from the execution. It was able to build my instance and attach the security group and finally give me a URL for my application http://22.214.171.124
If we browse to this web address you will now find the Hello World page and message.
This is a simple example to help you understand and start to think about how you could potentially use Terraform in your business. Now we have some code to build the infrastructure this can be version controlled and duplicated easily.
The final task to do is to remove all the infrastructure resources we have created. To do this the task is similar to that of apply, you run a terraform destroy. I will add the switch I previous mentioned
terraform destroy -auto-approve
All the code used is referenced in our GitHub repository https://github.com/MagnetarIT/Terraform-HelloWorld
source references: https://www.terraform.io/
Drop us a message or give us a call and we will be in touch to help you gain a better understanding
Date: March 16, 2020