Infrastructure as Code (IaC) allows for the building and managing of infrastructure through the use of a file or files rather than manually configuring resources in a user interface. This enables the keeping of application code and the infrastructure needed to run the code in a central code repository. IaC facilitates building, testing and deploying as a unit as well as version control for the infrastructure.
One of the tools that can be used for Infrastructure as Code is Terraform which provides a safe and efficient way in doing the above building, changing and versioning of Infrastructure.
Configuration
The files that are used to build the infrastructure are simply known as the Terraform configuration. A configuration is made up of one or more in a directory and uses a declarative model when specifying code. The configuration declares the desired state and it is up to Terraform and the provider (AWS, Azure, Google…) to determine how to create and configure the infrastructure to match the desired state. When working in Terraform the commonly used files are:
main.tf
The .tf files contain the configuration code and specifies the infrastructure resources that are needed. This file can be named anything but it is mostly main.tf
Terraform.tfvars
A terraform configuration can include the use of variables and can be included in the .tf files or in .tfvars. Variable allows for easier reuse of Terraform code without the need to modify the source code. An example of this is specifying the region or location as a variable
Terraform.tfstate
terraform.tfstate is called the state file which is used by Terraform to map the resources defined in the plan to what is already build. The first time we run Terraform apply, it builds a state file and then references it each time a build is re-run. The state file can be stored locally or in a shared central location, Azure blob storage can be used as the shared central location. Using a shared central location is required when working as a team on Terraform so that all users can have access to the state file.