Terraform for beginners.

Terraform is an opensource IoC service to develop, manage and support complex infrastructure or cloud in the organizations.

It uses the provider specific modules to implement it ex for Azure it will be azurerm .

You can download terraform. it is just an exe file. 1.4.4. is the latest version.

you can find the version using terraform –version

There are 3 phases in the terraform implementation. actually, it will be 4. can refer below diagram for this.

Terraform files have .tf extension.

For every terraform flow we must first initialize so that it will download all the necessary provider specific jars through API from the global repository which will be store in. terraform directory inside the present working directory.

For every run of a terraform scrip the state file will be maintained, and this file should be kept securely. If we need to add or delete any resource from the environment, we should update the terraform script then your run plan that will show what will do and finally apply the script.

it will destroy the entire resource that we deployed using terraform file with terraform destroy.

Following are the editions of Terraform:

  • Terraform Open Source or Terraform CLI
  • Terraform Cloud
  • Terraform Enterprise

Terraform Open Source: It is a free and open-source tool of Terraform, which makes Command-Line Interaction (CLI). With Terraform CLI, you can manage thousands of resources and services. Terraform CLI supports provisioning infrastructure with any Cloud Provider. Not only this, but it also manages configuration, infrastructure, plugins, and state.

Terraform Cloud: It is a SaaS application and supports Terraform to run in a stable and secured environment. Terraform Cloud comes with a rich user interface and RBAC, in addition to providing a private registry for sharing modules and Terraform providers. Considering integration, it integrates with Terraform CLI, GitHub, BitBucket, and GitLab. Moreover, it publishes configuration modules in the Terraform Cloud Private Registry that has approved infrastructure patterns.

Terraform Enterprises: This edition allows self-hosted distribution of Terraform Cloud. In other words, it provides a private instance of Terraform Cloud. It offers customizable resource limits, and strict security and compliance requirements. Apart from all these, Terraform Enterprises allow installations, private networking, sophisticated architecture, and job scaling for performance.

Terraform Open Source: It is a free and open-source tool of Terraform, which makes Command-Line Interaction (CLI). With Terraform CLI, you can manage thousands of resources and services. Terraform CLI supports provisioning infrastructure with any Cloud Provider. Not only this, but it also manages configuration, infrastructure, plugins, and state.

Terraform Cloud: It is a SaaS application and supports Terraform to run in a stable and secured environment. Terraform Cloud comes with a rich user interface and RBAC, in addition to providing a private registry for sharing modules and Terraform providers. Considering integration, it integrates with Terraform CLI, GitHub, BitBucket, and GitLab. Moreover, it publishes configuration modules in the Terraform Cloud Private Registry that has approved infrastructure patterns.

Terraform Enterprises: This edition allows self-hosted distribution of Terraform Cloud. In other words, it provides a private instance of Terraform Cloud. It offers customizable resource limits, and strict security and compliance requirements. Apart from all these, Terraform Enterprises allow installations, private networking, sophisticated architecture, and job scaling for performance.

Below are some of the features of Terraform?

  • Initialising Working Directories
  • Provisioning Infrastructure
  • Authenticating
  • Automating Terraform
  • Writing and Modifying Codes
  • Managing Plugins

Terrafrom Example for Nginx with AWS

#Declare AWS
provider "aws" {
  profile = "default"
  region  = "us-east-2"
}
#ADJUST

#Provision VPC
resource "aws_vpc" "alexlop_vpc" {
  cidr_block 	= "192.168.4.0/24"
  enable_dns_support = true
  enable_dns_hostnames = true
  tags = {
    Name = "alexlop_vpc"
  }
}

#Provision Subnet
resource "aws_subnet" "alexlop_subnet" {
  availability_zone = "us-east-2a"
  vpc_id	= aws_vpc.alexlop_vpc.id
  cidr_block	= "192.168.4.0/26"
  tags = {
    Name = "alexlop_subnet"
  }
}
#ADJUST

#Provision Internet Gateway
resource "aws_internet_gateway" "alexlop_ig" {
  vpc_id	= aws_vpc.alexlop_vpc.id
  tags = {
    Name = "alexlop_ig"
  }
}

#Provision Route Table and Routes
#WARNING: Allows OPEN access to VPC
resource "aws_route_table" "alexlop_routetable" {
  vpc_id	= aws_vpc.alexlop_vpc.id
  route {
    cidr_block	= "0.0.0.0/0"
    gateway_id	= aws_internet_gateway.alexlop_ig.id
  }
  tags = {
    Name = "alexlop_routetable"
  }
}

#Associate Route Table to Subnet
resource "aws_route_table_association" "alexlop_associate" {
  subnet_id			= aws_subnet.alexlop_subnet.id
  route_table_id		= aws_route_table.alexlop_routetable.id
}

#Provision Security Group
#Allows HTTP access to EC2 Instance
#WARNING: Allows all outbound traffic
resource "aws_security_group" "alexlop_secgroup" {
  description 	= "alexlop example: provision vpc & ec2 using terraform"
  vpc_id	= aws_vpc.alexlop_vpc.id
  ingress {
    description = "HTTP into VPC"
    from_port	= 80
    to_port	= 80
    protocol 	= "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
  tags = {
    Name = "alexlop_secgroup"
  }
}

#Provision EC2 Instance
#Image:AmazonLinux2AMI
resource "aws_instance" "alexlop_ec2" {
  ami				= "ami-08962a4068733a2b6"
  associate_public_ip_address 	= true
  instance_type			= "t2.micro"
  security_groups		= [aws_security_group.alexlop_secgroup.id]
  subnet_id			= aws_subnet.alexlop_subnet.id
  user_data			= <<-EOF
					#!/bin/bash
					sudo apt install nginx -y
					sudo systemctl start nginx
  				EOF
  tags				= {
    Name			= "alexlop_ec2"
  }
}

initialize the project with the following command.

terraform init

Then, you can provision the NGINX server container with the Terraform apply command. And then, press ‘enter’ to say ‘yes’ to the prompt raised by Terraform.

terraform apply

If you want to stop the container, you can run Terraform destroy for the same.

terraform destory 

Refer below links for the terraform more details.

https://github.com/Azure
https://developer.hashicorp.com/terraform/language/modules/develop#creating-modules
https://registry.terraform.io/search/modules?namespace=Azure
https://prudentialservices.udemy.com/course/terraform-for-the-absolute-beginners/learn/lecture/27884550#overview

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *