Terraform Question and Ans Part3

Q1. How do you save any provisioner to fail on terraform apply ?

Answer: You can use on_failure

resource "aws_instance" "web" {

  provisioner "local-exec" {
    command  = "echo The server's IP address is ${self.private_ip}"
    on_failure = "continue" # This will ignore the error and continue with creation or destruction or 
    fail       = It will Raise an Error 
  }
}

Q2. Is it Possible to skip Terraform backend ? If Yes, then how

Answer: Yes we can skip terraform backend by using terraform init -backend=false command

Q3. How can you remove Plugin installation while initializing the terraform?

Answer: terraform init -get-plugins=false

Q4. What is the use of terraform plan command ?

Answer: Terraform plan command helps in creation of execution plan and determines which actions are necessary to achieve the desired state.

Q5. How can you allow terraform to self approve and deploy the infrastructure ?

Answer: Using terraform apply -auto-approve command.

Q6. How can you preview the behavior of terraform destroy command ?

Answer: terraform plan -destroy

Q7. How can you save the execution Plan ?

Answer: We can save the execution Plan by using terraform plan -out=tf-plan

Q8. How can you see single attribute in state file?

Answer: terraform state show ‘resource name’.

Q9. How can you get detailed exit code while running plan in Terraform ?

Answer: terraform plan -detailed-exitcode.

Q10. If You remove EC2 instance manually from AWS console which was created by terraform, When you run apply next time does terraform recreate it ?

Answer: Yes , it does recreate it as this is already defined in state file.

Q11. What are backends?

Answer: Backends determines where state is stored or loaded from. By default we have local backend but we can give remote backed such as S3

Q12. What do you mean by state lock ?

Answer: State lock gets applied as soon as you it could write state i.e update the statefile. It helps in corruption of your state file.

Q13. Can you revert from remote backend to Local backend ? If you what next needs to be done?

Answer: Yes we can revert from remote backend to local backend. We would need to run terraform init.

Q14. What is Command to Sync or reconcile your terraform state file if you modify terraform created resource manually?

Answer: Use Terraform refresh command.

Q15. Can you use output’s generated from one Module to other module ? If yes how?

Answer: Yes we can use output generated from one module to other module . We can call module syntax by entering the source and version and then use it.

Q16. What is correct approach for declaring meta argument ? a = “${a}” or “${}” = a

Answer: a = “${a}” is correct way to use meta arguments. Now interpolation is used very rarely.

Q17. Name some important Data types in Terraform ?

Answer: String , lists , set, map , tuple , bool, number and object

Q18. How do you convert built in function from String to number ?

Answer: parseint(“100″,”10”)

Q19. Which built in function evaluates expression and return Boolean result ?

Answer: can .

Q20. How can you encode built in function to a string using JSON Syntax ?

Answer: jsonencode({“hello”=”America”})