Lab3: Terraform Commands - OT-TRAINING/TerraformAWS GitHub Wiki
Terraform Commands
- First we need to create one file where we will keep all code provided by team.
- Now we need to initialize the terraform code and see what all things have been changed.
- Make sure the indentation of HCL Language is fine.
- Make sure that we are not having syntax error in tf code.
- Plan the infrastructure by using Terraform.
- Create the infrastructure using Terraform.
- Check what all resources have created in human readble format.
- Also see all the values assigned to these resources.
- Add another block of code for SG.
- Plan the new changes and observe what exactly is getting changed.
- Update the new changes by using Terraform.
- Now try to remove ec2 from the console and notice what will happen in terraform.
- Update the tfstate file without running apply command of terraform.
- Destroy the complete infrastructure.
Let's Start
Step 1: First we need to create one file where we will keep all code provided by team.
provider "aws" {
  region = "us-east-2"
}
variable "resource_name" {
  default = "test"
}
#  create VPC
resource "aws_vpc" "main" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"
  tags = {
    Name = var.resource_name
  }
}
# create public subnet
resource "aws_subnet" "main" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.1.0/24"
  map_public_ip_on_launch = true
  tags = {
    Name = "${var.resource_name}-subnet"
  }
}
# for ami
data "aws_ami" "ubuntu" {
  most_recent = true
  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
  }
  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
  owners = ["099720109477"] # Canonical
}
# create ec-2
resource "aws_instance" "main" {
  ami           = data.aws_ami.ubuntu.id
  count         = 2
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.main.id
  #  vpc_security_group_ids = [aws_security_group.main.id]
  tags = {
    Name = "${var.resource_name}-instance"
  }
}
Now we need to initialise the terraform code and see what all things have been changed.
terraform init

Make sure the indentation of HCL Language is fine.
terraform fmt

Make sure that we are not having syntax error in tf code.
terraform validate

Plan the infrastructure by using Terraform.
terraform plan
 

Create the infrastructure using Terraform.
 terraform apply

Check what all resources have created in human readable format.
terraform show

Also see all the values assigned to these resources.
terraform state list
Note:- Explore other option as well
Add another block of code for SG.
provider "aws" {
  region = "us-east-2"
}
variable "resource_name" {
  default = "test"
}
#  create VPC
resource "aws_vpc" "main" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"
  tags = {
    Name = var.resource_name
  }
}
# create public subnet
resource "aws_subnet" "main" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.1.0/24"
  map_public_ip_on_launch = true
  tags = {
    Name = "${var.resource_name}-subnet"
  }
}
# for ami
data "aws_ami" "ubuntu" {
  most_recent = true
  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
  }
  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
  owners = ["099720109477"] # Canonical
}
# create ec-2
resource "aws_instance" "main" {
  ami           = data.aws_ami.ubuntu.id
  count         = 2
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.main.id
  vpc_security_group_ids = [aws_security_group.main.id]
  tags = {
    Name = "${var.resource_name}-instance"
  }
}
    # Create the Security Group
resource "aws_security_group" "main" {
  vpc_id = aws_vpc.main.id
  name   = "main Security Group"
  # allow ingress of port 22
  ingress {
    cidr_blocks = ["0.0.0.0/0"]
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
  }
  # allow egress of all ports
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
  tags = {
    Name = "${var.resource_name}-SG"
  }
}
Plan the new changes and observe what exactly is getting changed.
terraform plan
Update the new changes by using Terraform.
terraform apply
Now try to remove ec2 from the console and notice what will happen in terraform.

Update the tfstate file without running apply command of terraform.
terraform refresh
Destroy the complete infrastructure.
terraform destroy