Terraform_01 - Create AWS EC2 Instance

Create a folder for storing the main.tf file and add the below code in HCL format

mkdir terraform_folder

cd terraform_folder

vim main.tf

provider "aws" {
         region = "us-east-1"
}

resource "aws_instance" "vm" {
        ami = "ami-0bef12ee7bc073414"
        subnet_id = "subnet-031e9d8f92273792e"
        instance_type = "t3.micro"
        tags = {
                Name = "my-first-tf-node"
        }
}

save the file and execute the below command from the same folder where the main.tf file is existing (terraform_folder)

1) terraform init

The terraform init command initializes a Terraform working directory by downloading modules, providers, and setting up the backend for storing state files. It is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control.

 

2) terraform plan

The plan command alone does not actually carry out the proposed changes You can use this command to check whether the proposed changes match what you expected before you apply the changes or share your changes with your team for broader review.

The terraform plan command creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure. By default, when Terraform creates a plan it:

  • Reads the current state of any already existing remote objects to make sure that the Terraform state is up-to-date.
  • Compares the current configuration to the prior state and note any differences.
  • Proposes a set of change actions that should, if applied, make the remote objects match the configuration.

3) terraform apply

When you run terraform apply without passing a saved plan file, Terraform automatically creates a new execution plan as if you had run terraform plan, prompts you to approve that plan, and takes the indicated actions. You can use all of the planning modes and planning options to customize how Terraform will create the plan.

4) terraform destroy

The terraform destroy command is a convenient way to destroy all remote objects managed by a particular Terraform configuration.

While you will typically not want to destroy long-lived objects in a production environment, Terraform is sometimes used to manage ephemeral infrastructure for development purposes, in which case you can use terraform destroy to conveniently clean up all of those temporary objects once you are finished with your work.

.

terraform validate

The terraform validate command validates the configuration files in a directory, referring only to the configuration and not accessing any remote services such as remote state, provider APIs, etc.

Validate runs checks that verify whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state. It is thus primarily useful for general verification of reusable modules, including the correctness of attribute names and value types.

It is safe to run this command automatically, for example as a post-save check in a text editor or as a test step for a re-usable module in a CI system.