Terraform

What is Infrastructure as Code?
In today's agile-driven industry time and accuracy is the key. It's really hard for a DevOps engineer to keep up pace with the 2 weeks sprint duration. This is where Infrastructure as Code turns out to be a magic wand. There are a lot of tools in today's market but Terraform is the most popular one.
Why Terraform?
✔ Module support to work with the Cloud providers.
✔ Easy to learn and understand.
✔ Better code maintenance.
Let's look at an example where we can create some AWS resources using Terraform.
ec2_module/main.tf
terraform{
required_version=">0.12"
}
resource "aws_instance" "web_server"{
ami=var.ami
instance_type=var.instance_type
vpc_security_group_ids = [aws_security_group.SG1.id]
tags={
Name="Server-1"
}
}
resource "aws_security_group" "SG1" {
name = "WebServer-Security-Group"
ingress {
cidr_blocks = [ "0.0.0.0/0" ]
description = "Port for http"
from_port = 80
protocol = "TCP"
to_port = 80
}
}
ec2_module/var.tf
variable "ami" { default = "ami-0557a15b87f6559cf"}
variable "instance_type" { default ="t2.micro"}
ec2_module/output.tf
output "ec2_ip_address" {
value = aws_instance.web_server.public_ip
}
main.tf
rovider "aws"{
region="us-east-1"
access_key=var.access_key
secret_key=var.secret_key
}
module "ec2_modules"{
source=".//ec2_modules"
}
In the above code, I have used modules for the reuse of code. With the modular approach, each module contains main.tf, variables.tf and outputs.tf . Every time an EC2 instance is required a call to the module is just referenced from the main.tf which is existing in the root. With this, code redundancy can be kept at bay.
Comments
Join the discussionNo comments yet. Be the first to comment.
