What Is Infrastructure as Code (IaC)?
Infrastructure-as-Code (IaC) tools?automate the management of IT infrastructure using programming languages and automation tools. These tools help businesses manage computer data centres located in the cloud or on-premises through code.
Popular IaC Tools:
1. Terraform?An open-source declarative tool that offers pre-written modules to build and manage an infrastructure.
2. Chef:?A configuration management tool that uses cookbooks and recipes to deploy the desired environment. Best used for Deploying and configuring applications using a pull-based approach.
3. Puppet:?Popular tool for configuration management that follows a Client-Server Model. The puppet needs agents to be deployed on the target machines before the puppet can start managing them.
4. Ansible:?Ansible is used for building infrastructure as well as deploying and configuring applications on top of them. Best used for Ad hoc analysis.
5. Packer:?Unique tool that generates VM images (not running VMs) based on the steps you provide. Best used for Baking compute images.
6. Vagrant:?Builds VMs using a workflow. Best used for Creating pre-configured developer VMs within VirtualBox.
How Does IaC Work?
When building a project infrastructure, software developers realize that there is no one-size-fits-all. Therefore, they have to establish a new infrastructure with its own working specifics and a set of tools each time they start a new project. Nevertheless, there are some key features that can be traced in different infrastructures. For example, all infrastructures can be characterized by:
Let’s have a closer look at each category.
Imperative approach. With an Imperative approach, IT specialists define the steps/commands they should take/use to implement any changes to the infrastructure. This approach mostly relies on object-oriented languages, e.g.?Java, C++, and others used for imperative programming.
Declarative approach. In the Declarative approach, DevOps professionals outline the desired result and the tools and platforms that take action to reach it. SQL and AWS CouldFormation templates are among the examples of declarative programming.
What Is Terraform?
Terraform?is one of the most popular?Infrastructure-as-code (IaC) tool, used by DevOps teams to automate infrastructure tasks. It is used to automate the provisioning of your cloud resources. Terraform is an open-source, cloud-agnostic provisioning tool developed by HashiCorp and written in GO language.
Benefits of using Terraform:
Terraform Lifecycle
The provisioning workflow (Terraform’s life cycle) in Terraform is based on the following commands:
1.?Terraform init?initializes the (local) Terraform environment. Usually executed only once per session.
2.?Terraform plan?compares the Terraform state with the as-is state in the cloud, build and display an
execution plan. This does not change the deployment (read-only).
3.?Terraform apply?executes the plan. This potentially changes the deployment.
4.?Terraform destroy?deletes all resources that are governed by this specific terraform environment.
Terraform Core Concepts
1. Variables: Terraform has input and output variables, it is a key-value pair. Input variables are used as parameters to input values at run time to customize our deployments. Output variables are return values of a terraform module that can be used by other configurations.
2. Provider: Terraform users provision their infrastructure on the major cloud providers such as AWS, Azure, OCI, and others. A?provider?is a plugin that interacts with the various APIs required to create, update, and delete various resources.
领英推荐
3. Module: Any set of Terraform configuration files in a folder is a?module. Every Terraform configuration has at least one module, known as its?root module.
4. State: Terraform records information about what infrastructure is created in a Terraform?state?file. With the state file, Terraform is able to find the resources it created previously, supposed to manage and update them accordingly.
5. Resources: Cloud Providers provides various services in their offerings, they are referenced as Resources in Terraform. Terraform resources can be anything from compute instances, virtual networks to higher-level components such as DNS records. Each resource has its own attributes to define that resource.
6. Data Source:?Data source performs a read-only operation. It allows data to be fetched or computed from resources/entities that are not defined or managed by Terraform or the current Terraform configuration.
7. Plan: It is one of the stages in the Terraform lifecycle where it determines what needs to be created, updated, or destroyed to move from the real/current state of the infrastructure to the desired state.
8. Apply: It is one of the stages in the Terraform lifecycle where it applies the changes real/current state of the infrastructure in order to achieve the desired state.
Terraform Installation
Before you start working, make sure you have Terraform installed on your machine, it can be installed on any OS, say Windows, macOS, Linux, or others. Terraform installation is an easy process and can be done in a few minutes.
Terraform Providers
A provider is responsible for understanding API interactions and exposing resources. It is an executable plug-in that contains code necessary to interact with the API of the service.
Terraform configurations must declare which providers they require so that Terraform can install and use them.
Terraform has over a hundred providers for 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 EC2 instances, the AWS users, etc.
Terraform Configuration Files
Configuration files are a set of files used to describe infrastructure in Terraform and have the file extensions?.tf?and?.tf.json. Terraform uses a declarative model for defining infrastructure. Configuration files let you write a configuration that declares your desired state. Configuration files are made up of resources with settings and values representing the desired state of your infrastructure.
A Terraform configuration is made up of one or more files in a directory, provider binaries, plan files, and state files once Terraform has run the configuration.
1. Configuration file (*.tf files):?Here we declare the provider and resources to be deployed along with the type of resource and all resources specific settings
2. Variable declaration file (variables.tf or variables.tf.json):?Here we declare the input variables required to provision resources
3. Variable definition files (terraform.tfvars):?Here we assign values to the input variables
4. State file (terraform.tfstate):?a state file is created once after Terraform is run. It stores state about our managed infrastructure.
How to Install Terraform
Homebrew?is a free and open-source package management system for Mac OS X. Install the official?Terraform formula?from the terminal.
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
brew update
brew upgrade hashicorp/tap/terraform