IaC stands for Infrastructure-as-Code. 🚀 Firstly let's understand what is Infrastructure in IT. In simple words, it is a set of resources required to run any application. Infrastructure is composed of physical and virtual resources that support the flow, storage, processing and analysis of data. There are essentially 3 pieces to IT infrastructures: infrastructure hardware, software and networking.
What is Infrastructure-as-Code (IaC)?
IaC is a process that describes and provisions all the infrastructure resources in a cloud environment using a simple text file. We can say that it's a procedure to provision and configure the infrastructure using scripts/tools 📝 instead of manual configuration of resources. It is similar to writing a program in a high-level language for automation, testing and deployment.
Need of IaC
Assume that you are creating a Web App 🧑💻 in an organization for which you have to set up 3 Linux Servers, a Load Balancer, a Database & a Router for DNS (example resources). To implement this you can create the required resources on a cloud environment like AWS, Azure or GCP.
Suppose the production server handles a maximum of 1000 requests for the first month and it grows to 5000 requests in the next month as your app makes progress; your setup is not going to handle the load and you will need to update the whole environment. You'll have to manually set up the resources for scaling and load balancing and the same changes will be also applied to the Disaster Recovery environment which will consume more time and effort!! 👀⏳
Let's assume that you're able to achieve this by updating the resources but suppose in the future requests grow from 5k ➡️ 300k and you are also asked to build 5 more similar apps😬, you'll again have to provision and update the environment. It becomes a burden at such times and any mistake in the provisioning of resources can lead to downtime which affects the business value of the organization📉.
Any possible solutions for this? 🤔
What we need is a tool 🔧 to configure the resources quickly and make them reusable any time we need to create a similar infrastructure so that we don't have to set up the whole environment again and again. Any possibility of creating resources with the help of some coding tool will be advantageous for us. 💁🏼 Such a solution is IaC. With IaC, configuration files (code scripts) are created that contain your infrastructure specifications, which makes it easier to edit and distribute configurations.
Now, what will be the advantages of using code for setting up the infrastructure?💻
We can easily modify the resources
Testing the environment
Re-execution and Disaster recovery
Automation becomes easy
Maintaining the version history of the Infrastructure
Scalability, Consistency, high speed and security
IaC also facilitates DevOps integration by offering developers quick access to the IT infrastructure that DevOps requires to perform. Businesses can now adopt DevOps best practices, including continuous integration & continuous delivery (CI/CD) and version control that makes their activities safer, more efficient, and optimal ✅🐱.
Different types of IaC
There are many IaC tools available, some of which are developed by the cloud service providers themselves.
There are a few more you can google it up! 🤗
Out of these AWS CloudFormation is only able to configure the resources in the AWS Cloud environment. Same with Azure and GCP tools which only work for their respective cloud environments. On the other hand, Terraform is widely used and is considered best as it is able to provision resources for every available cloud service (AWS, Azure, Oracle Cloud, Heroku, GCP, etc.).
There are some differences and similarities between Terraform, Ansible and Puppet which we will discuss in a new blog. For now, let's understand Terraform 🤠.
💡Keep reading...
What is Terraform?
Terraform, developed by HashiCorp(HCL) is an IaC tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version reuse, and share.
Features of Terraform:
It helps to build, change, and version cloud and on-prem resources safely and efficiently.
It does not install and manage software on existing devices; instead, it creates, modifies, and destroys servers and various other cloud services.
Manage any infrastructure
Track your infrastructure
Automate changes
Standardize configurations
Open-source
Working of Terraform ⚙️
The core Terraform workflow consists of three stages:
Write: You define resources, which may be across multiple cloud providers and services. For example, you might create a configuration to deploy an application on virtual machines in a Virtual Private Cloud (VPC) network with security groups and a load balancer.
Plan: Terraform creates an execution plan describing the infrastructure it will create, update, or destroy based on the existing infrastructure and your configuration.
Apply: On approval, Terraform performs the proposed operations in the correct order, respecting any resource dependencies.
Terraform is open source. you can check out the HashiCorp GitHub repo for terraform 👇
https://github.com/hashicorp/terraform
To use Terraform you will need to install it. HashiCorp distributes Terraform as a binary package. You can also install Terraform using popular package managers. It consists of files with .tf
extension which has the set of code written in that. Terraform will process any files with a .tf
extension. As the configuration becomes more complex, you may want to split the config into separate files and modules. Have a look at the file below.
For example, a script to create AWS S3 bucket🪣:
resource "aws_s3_bucket" "b" {
bucket = "my-tf-test-bucket"
tags = {
Name = "My bucket"
Environment = "Dev"
}
}
After finishing the code you can run the following commands on cmd at the path where you have saved your main.tf configuration file.
terraform init
- to initiate the configuration and download any plugins required.terraform plan
-The plan command is important because it allows you to preview the changes for accuracy before actually making them.terraform apply
- After you review the execution plan, apply your changes.You can use
--auto-approve
on the command line for a briefer output.terraform destroy
- It destroys all the resources which you have configured in .tf file and applied them.
init
, plan
, and apply
are the top three commands you will use when initializing, creating an execution plan, and executing the desired state of your configuration file. For now, we have understood the basics of Terraform and will continue with the actual implementation in the next blog.
Terraform is widely used in DevOps field. As per stackshare.io/terraform 1862 companies reportedly use Terraform in their tech stacks, including Uber, Udemy, Starbucks, DigitalOcean and Instacart.
Almost done!
🙋♂️Let's quickly summarize what You have read till now:
⭐️Deep understanding of Infrastructure as a Code (IaC)
⭐️Need for IaC and its use cases
⭐️Different types of IaC tools
⭐️Terraform and it's working.
Thank you! for reading till here, if you have any questions or suggestions do comment them down. Hope you learned something new and enjoyed reading this article. In the upcoming blogs, we will understand the actual implementation of terraform 🎯 with a cool project. Till then keep learning and enjoy your coding journey! 🧑💻🐱