Terraform

Terraform

by Yogesh V. Lale

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

COPY

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

COPY

variable "ami" { default = "ami-0557a15b87f6559cf"}

variable "instance_type" { default ="t2.micro"}

ec2_module/output.tf

COPY

output "ec2_ip_address" {
  value = aws_instance.web_server.public_ip
}

main.tf

COPY

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.

***Common use cases for Terraform***

Infrastructure as Code:

Infrastructure as code to enable and accelerate cloud adoption

Multi-Cloud Deployment:

Deploy serverless functions with AWS Lambda, manage Microsoft Azure Active Directory resources, provision a load balancer in Google Cloud, and more.

Manage Kubernetes :

Provision and manage Kubernetes clusters on AWS, Microsoft Azure, or Google Cloud, and interact with your cluster using the Kubernetes Terraform provider.

Infrastructure as code to enable and accelerate cloud adoption

Network Infrastructure Automation

Automate key networking tasks such as updating load-balancer member pools or applying firewall policies.

Manage Virtual Machine Images

Create multi-cloud golden image pipelines with HCP Packer and Terraform Cloud

Manage Virtual Machine Images

Create multi-cloud golden image pipelines with HCP Packer and Terraform Cloud

Enforce Policy as Code

Enforce policies before your users create infrastructure using Sentinel policy as code.

Inject Secrets into Terraform

Automate the usage of dynamically generated secrets and credentials.

___________________________________________________________________________________

For more visit to :- Documentation | Terraform | HashiCorp Developer

#terraform #DevOps