Terraform Provisioners Flashcards
What are provisioners?
Commissioners are used to execute scripts on a local or remote machine as part of resource creation or destruction
Whats an example of a provisioner user case?
After VM is launched, install software package required for application.
What are the types of provisioners?
- local-exec provisioner
- remote-exec provisioner
3.file-provisioner
Whats a local-exec provisioners and an example?
The local - exec provisioner invoke a local executive after a resource is created
Ex: After EC2 is launched, fetch the IP and store it in file server_Ip.txt.
Local because its done on the local machine
Whats a remote-exec provisioner
Remote – executor visitor allowed to invoke scripts or run commands directly on the remote server.
Ex: after E2 is launched install Apache software
How to define provisioners?
Local:
provisioner “local-exec” {}
Remote:
provisioner “remote-exec” {}
What do you need to declare for remote-exec provisioners to connect?
You need to add a connection block inside the resource block. It supports both SSH (Linux) and WINRM (Window Server)
Ex:
connection {
type = “ssh”
user = “ec2-user”
private_key = file(“./terraform-key.pem”)
host = self.public_ip
}
Does the provisioner block need to be declared from within the resource block?
Yes
Is it necessary to define a aws_instance resource block for provisioner to run?
No, they can be defined inside other resource types.
Ex:
resource “aws_iam_user”
Can you define multiple providers inside a single resource block?
Yesf
What happens when a destroy provisioner is ran?
Destroy provisioners are run before the resource is detroyed.
What happens if a creation-time provisioner fails?
- The resource is marked as tainted and the resource will be planned for destruction and recreation upon the NEXT terraform apply.
- The terraform apply will also fail.
Ex: reason permission issue
Why does terraform mark a resource for destruction if a creation-time provisioner fails?
This occurs because a failed provisioner can leave a resource in a semi-configured state.
Whats a workaround to the terraform apply from failing if provisioner failes at creation time?
With the use of the ON_FAILURE setting it can be changed to the value of “continue” to ignore the error and continue with creation/destruction.
By default, the value is set to FAIL, raising an error and to stop applying (default) and taint the resource.