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.