Introduction to Infrastructure as Code with Terraform

Posted on Feb. 15, 2020
terraform
aws
961

infrastructure as code (IaC) is the managing and provisioning of infrastructure through code instead of using a manual process to configure devices or systems. Over the years, IT infrastructure provisioning has been done manually. The people responsible for the process have to rack and stack the servers physically. Also, they have to manually configure the hardware to the desired settings and requirements of the operating system and the hosted application. 

IaC helps you automate the infrastructure deployment process in a repeatable, consistent manner. Infrastructure as Code can simplify and accelerate your infrastructure provisioning process, help you avoid mistakes and comply with policies, keep your environments consistent, and save your company a lot of time and money.

There are two parts in IaC: 

1. Infra provisioning: Provisioning infrastructure manually is a time-consuming process and it is impossible to guarantee zero errors. Automating the provisioning of infrastructure eliminates configuration drift, ensures predictability in what is provisioned, and makes your software delivery process much more reliable.

eg: Terraform, Cloud Formation, ARM templates

2. Configuration: Configuration Management (CM) ensures that the current design and build a state of the system is known, good & trusted.

eg: Ansible, Puppet, Chef.

Here are the few concepts of Terraform.

Provider in Terraform is responsible for the lifecycle of a resource: create, read, update, delete. An example of a provider is AWS, which can manage resources of type aws_instanceaws_eipaws_elb, etc.

Variables serve as parameters for a Terraform module. Variables can be set from CLI arguments and environment variables.

Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records.

In the below example of the Terraform module ( which creates RDS resources on VPC Peering in AWS) we have defined three individual templates for providers, variables and resources.

provider.tf:

provider "aws" {
  region     = "eu-west-1"
  access_key = "${var.access_key}"
  secret_key = "${var.secret_key}"
}

 

variable.tf

variable "access_key" {}
variable "secret_key" {}
variable "peer_owner_id"{}
variable "peer_vpc_id"{}
variable "vpc_id" {}
variable "peer_region"{}
variable "tag_name"{}

main.tf

resource "aws_vpc_peering_connection" "VPC_peering" {
  peer_owner_id = "${var.peer_owner_id}"
  peer_vpc_id   = "${var.peer_vpc_id}"
  peer_region   = "${var.peer_region}"
  vpc_id        = "${var.vpc_id}"
  tags = {
    Name = "${var.tag_name}"
  }
}

FYI: Make sure that the above three files should be in the same folder.

To execute the template run the following commands from within the respective directory.

$ terraform init
$ terraform apply

 




0 comments

Please log in to leave a comment.