Tech Blog

Cloud Computing, Programming, Technology

Terraform As a DevOps Tool (IaC) And Benefits (Part 2)

By Utsav Solanki2 min read
← Back to blog

In this article, we will explore terminologies and basic commands that can be used to interact with Terraform.

Using Terraform init, an existing or new Terraform configuration can be initialized. As a beginner, you may get syntax errors or indentation errors. To validate theYAML data, use the Terraform “validate” command. The “Terraform apply“ command changes the existing resources or creates new resources. A Terraform plan previews all resource changes and generates and shows an execution plan. There are many other interactive commands such as Terraform graph, Terraform import, Terraform version, Terraform output, Terraform destroy, etc.

Below is the practice code. It will create a Virtual Private Cloud (VPC) in Terraform. First create a file main.tf and add the below-mentioned code.

module “vpc” {
source = “terraform-aws-modules/vpc/aws”
name = “${var.namespace}-vpc”
cidr = “10.0.0.0/16”
azs = data.aws_availability_zones.available.names
private_subnets = [“10.0.1.0/24”, “10.0.2.0/24”]
public_subnets = [“10.0.101.0/24”, “10.0.102.0/24”]
#assign_generated_ipv6_cidr_block = true
create_database_subnet_group = true
enable_nat_gateway = true
single_nat_gateway = true
}

resource “aws_security_group” “allow_ssh_pub” {
name = “${var.namespace}-allow_ssh”
description = “Allow SSH inbound traffic”
vpc_id = module.vpc.vpc_id

ingress {
description = “SSH from the internet”
from_port = 22
to_port = 22
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 = “${var.namespace}-allow_ssh_pub”
}
}

Now, create another file named variabe.tf and copy-paste the below-mentioned code in it:

variable “namespace” { 
type = string
}

After saving both the files, apply the command “Terraform init” and then perform the “Terraform apply” command. It will ask for a name as input, and you should confirm by typing ‘yes’. It will list and create all the mentioned resources. Hurray, you have just completed the basic Terraform tutorial.

Share this post if you liked it.

Keep reading

You may be interested in

Browse all articles →
Terraform as a DevOps tool (IaC) and benefits (part 1)
September 21, 20222 min read

Terraform as a DevOps tool (IaC) and benefits (part 1)

Nowadays, the term “Infrastructure as Code (IaC) is very popular. This code is written in the YAML or JSON format and directly deployed in cloud services. One of the IaC tools is Terraform. It is used for creating and managing existing infrastructure, including versions of stats. Terraform supports many cloud providers, such as Amazon Web […]

By Utsav SolankiRead More →
Zip files from Amazon S3
March 11, 20222 min read

Zip files from Amazon S3

There is no method that retrieves multiple files as a single zip file from Amazon S3. But, with AWS Lambda we can achieve our goal

By Yaswanth BellamkondaRead More →
How Angular is Different from AngularJS
September 27, 20227 min read

How Angular is Different from AngularJS

What is Angular? Angular is a popular open-source Typescript framework and free to use. Considered to be a JavaScript-based framework,TypeScript.is its primary programming language. TypeScript is Developed by Microsoft and its superset of JavaScript, which simply means that TypeScript has all the capabilities that JS has and more additional features. Angular is majorly used for […]

By Sachin DevadigaRead More →