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

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 = “”
azs = data.aws_availability_zones.available.names
private_subnets = [“”, “”]
public_subnets = [“”, “”]
#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 = [“”]

egress {
from_port = 0
to_port = 0
protocol = “-1”
cidr_blocks = [“”]

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.

Leave A Comment

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