DEV Community

Cover image for Terraform Basic Commands
Srinivasulu Paranduru for AWS Community Builders

Posted on • Updated on

Terraform Basic Commands

1.Terraform Initialization : It will initialize the modules based on provider AWS / Azure / GCP / Oracle

terraform init
Enter fullscreen mode Exit fullscreen mode

2.Terraform Plan : : It will show the plan, what is going to happen

terraform plan
Enter fullscreen mode Exit fullscreen mode

3.Terraform apply : It applies the code to provision

 terraform apply
Enter fullscreen mode Exit fullscreen mode

It will ask for the confirmation and need to enter 'yes' for applying the changes

3a. If you don't wanted to provide the confirmation and wants to apply the changes automatically then use the below command

terraform apply --auto-approve
Enter fullscreen mode Exit fullscreen mode

4. Terraform destroy : It allows us to destroy all the resources that are created in a folder

terraform destroy
Enter fullscreen mode Exit fullscreen mode

It will ask for the confirmation and need to enter 'yes' for applying the changes

4a. If you wanted to destroy a specific resource (consider we have created an aws_instance with the name myfirstServer )

terraform destroy --target aws_instance.myfirstServer
Enter fullscreen mode Exit fullscreen mode

Note :
aws_instance = type of the resource
myfirstServer = name of the resource we have used

5.Terraform state file : Terraform stores the state of the infrastructure that is being created from the tf files.
This state allows terraform to map real world resources to your existing configuration

terraform.tfstate 
Enter fullscreen mode Exit fullscreen mode

Note: terraform.tfstate file will be created at the first time when you run terraform apply

6. Terraform refresh:
Consider a scenario where we have created an AWS EC2 Instance / Azure VM through terraform configuration then infrastructure related information will be stored in the terraform.tfstate file.

If we stopped /terminate the server the changes wont be reflected in terraform.tfstate file and to reflect to the changes we need to run the below command

terraform refresh
Enter fullscreen mode Exit fullscreen mode

7. Desired State: Terraform's primary function is to create, modify and destroy infrastructure resources to match the desired state described in your configuration files

8. Current State : Its the actual state of the resource currently been deployed

9.Depedency Lock File :

  • Terraform dependency lock file allows us to lock to a specific version of the provider.
  • If a particular version provider already has a selection recorded in the lock file, terraform will always re-select that version for installation, even if a newer version has become available.
  • You can override that behaviour by adding the -upgrade option to terraform init.
terraform init -upgrade
Enter fullscreen mode Exit fullscreen mode

10. Output attributes : An outputted variables can not only used for user reference , it can also act as input to other resource being created via terraform

#Example : Creating Elastic IP and attaching to EC2 Instance 

provider "aws" {
  region     = "eu-west-1"
  access_key = "**********"
  secret_key = "*************"
}


resource "aws_instance" "myfirstec2" {
    ami = "ami-0721c9af7b9b75114"  # Change the AMI Id
    instance_type = "t2.micro"
}

resource  "aws_eip"  "my_ip" {
  vpc = true
}

# Elastic IP Association: Passing the output of ec2 instance id (aws_instance.myfirstec2.id) and elastic ip address (aws_eip.my_ip.id) 
resource "aws_eip_association" "eip_assoc" {
  instance_id   = aws_instance.myfirstec2.id
  allocation_id = aws_eip.my_ip.id
}

Enter fullscreen mode Exit fullscreen mode

Note : In the above example, we have taken output of aws_instance.myfirstec2.id and aws_eip.my_ip.id , passed as input to aws_eip_association

Conclusion : Discussed about terraform basic commands and
If you like my blogs, please like,share,comment and follow me in linked in https://www.linkedin.com/in/srinivasuluparanduru/

Top comments (0)