Share
That's why infrastructure as code (represented by Terraform, for example) has emerged. While deploying new systems, we can start by configuring them through the user interface (console) offered by cloud service providers. However, as the system grows in complexity, this configuration process can become cumbersome.
Infrastructure as Code is the practice of using files to define stacks related to the systems. This allows automating the process of configuring the infrastructure rather than having to set it up manually.
Nowadays, there are numerous infrastructures as Code support tools such as Terraform, Chef, Ansible, Puppet, SaltStack, CloudFormation...
You have deployed a system consisting of VPC, EC2, RDS, and Redis on AWS, and since these resources are interconnected, the deployment process can be quite time-consuming. One day, if you need to build a similar system, using Infrastructure as Code, you only need to copy the old configuration file (adjusting a few parameters if necessary) and run it again. It's effortless, and you'll have a similar system right away.
Infrastructure as Code Model
Terraform is an open-source tool that helps with the implementation of Infrastructure as Code more conveniently and efficiently, using the Go language established by Hashicorp. You might learn more here: Terraform GitHub Repository
By creating and executing .tf files your descriptions in the Terraform file will be validated, and then Terraform will proceed to create the corresponding resources as described in the file.
How Terraform works
Many tools support Infrastructure as Code, but Terraform is renowned as the most preferred tool. A few reasons why you might choose Terraform include:
> Terraform provides comprehensive and easy-to-read documentation.
> The Terraform user community is quite extensive. As evidence, according to statistics from IAC (a third-party platform for statistics and comparisons between brands), there are a considerable number of job opportunities for developers who know Terraform, and there is also a substantial library of resources.
> Terraform supports multiple cloud platforms (AWS, Azure, Google Cloud, etc.).
> It allows you to manage your system versions very clearly, similar to Git, through its state. Before making changes, you can review which components are modified to avoid confusion.
IAC comparison table between Terraform and other tools
You might be interested in: Managing Infrastructure with Terraform - The Benefits that Make It a Must-Choose.
To complete this example, you first need to do the following:
> Install Terraform here
> Install AWS CLI here
> WS account and AWS credentials, which you can create here
> Configure your AWS key on your machine using the "aws configure" command. When you configure it this way, the settings will be saved in the ~/.aws/credentials file on macOS and Linux, or the %UserProfile%.aws\credentials file on Windows.
Let's start building the first Terraform file:
> Create a directory named "make_ec2" with the command "mkdir make_ec2".
> Navigate to that directory with "cd make_ec2".
> Create the "main.tf" file with "touch main.tf".
> Using a text editor, edit the main.tf configuration file as follows.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.14.9"
}
provider "aws" {
profile = "default"
region = "us-west-2"
}
resource "aws_instance" "app_server" {
ami = "ami-830c94e3"
instance_type = "t2.micro"
tags = {
Name = "ExampleAppServerInstance"
}
}
> Next, run the following command to initialize the Terraform directory with "Terraform
init." This command will download the plugins defined in the file beforehand.
> Check if the file is formatted correctly with “terraform validate”
> After successful validation, run “terraform apply" to create the EC2 instance. After running this command, Terraform will show you what has changed, and what has been added, or removed.
The changes will be displayed.
> Type "yes" to complete the process.
At this point, the process of creating an EC2 instance using Terraform is complete.
Through this article, we've understood the significance of Terraform and Infrastructure as Code. Hopefully, you can apply the knowledge gained above to your software projects. Thank you for reading.
If you need advice on anything related to technology, no matter how small, don't hesitate to contact Rabiloo's professional and understanding consulting team. We are here to listen and share with you.
Share