To use a provisioner in Terraform, you must include it as part of a resource configuration block. This is because a provisioner operates on a resource that has been created or updated by Terraform.
Answer is A: True
Multiple provisioners can be specified within a resource. Multiple provisioners are executed in the order they're defined in the configuration file.
https://developer.hashicorp.com/terraform/language/resources/provisioners/syntax
resource "aws_instance" "web" {
# ...
provisioner "local-exec" {
command = "echo first"
}
provisioner "local-exec" {
command = "echo second"
}
}
https://developer.hashicorp.com/terraform/language/resources/provisioners/connection
Connection blocks don't take a block label and can be nested within either a resource or a provisioner.
A connection block nested directly within a resource affects all of that resource's provisioners.
A connection block nested in a provisioner block only affects that provisioner and overrides any resource-level connection settings.
in the question they asked 'must' so may be the answer is False : B . I think so please confirm. I am confused now.
No Standalone Provisioners: Terraform does not support defining provisioners as standalone elements outside of resource blocks. All provisioner configurations must be part of a resource definition because their execution context is tightly coupled with resources.
Alternatives to Provisioners: For scenarios where you might think about using provisioners in a more decoupled or independent manner, Terraform suggests other mechanisms. For instance, if the goal is to manage configuration or orchestration that seems beyond the scope of what should be tightly coupled to a single resource's lifecycle, tools like Ansible, Chef, Puppet, or Terraform itself (through resource dependencies and proper module design) are recommended to handle such configurations. These tools can be used in conjunction with Terraform but managed through their mechanisms for orchestration or configuration management, rather than through Terraform provisioners.
True provisioner should only placed under the resource block
PS E:\Terraform> terraform.exe validate
╷
│ Error: Unsupported block type
│
│ on main.tf line 34:
│ 34: provisioner "local-exec" {
│
│ Blocks of type "provisioner" are not expected here.
╵
While it's common to place provisioners inside the resource block for clarity and organization, they can also be defined outside of the block at the same level. Both approaches are valid, and the choice depends on your preference and the organization of your Terraform code. The main point is that provisioners are associated with a specific resource and are configured within the resource block or at the same level in the code.
https://developer.hashicorp.com/terraform/language/resources/provisioners/syntax
Provisioners are used to execute scripts on a local or remote machine as part of resource creation or destruction. Provisioners can be used to bootstrap a resource, cleanup before destroy, run configuration management, etc.
How to use Provisioners
Note: Provisioners should only be used as a last resort. For most common situations there are better alternatives. For more information, see the sections above.
If you are certain that provisioners are the best way to solve your problem after considering the advice in the sections above, you can add a provisioner block inside the resource block of a compute instance.
resource "aws_instance" "web" {
# ...
provisioner "local-exec" {
command = "echo The server's IP address is ${self.private_ip}"
}
}
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
quixo
Highly Voted 2 years, 4 months agokiran15789
Highly Voted 1 year, 6 months ago090200f
Most Recent 4 months, 2 weeks ago090200f
4 months, 2 weeks agoravk2321
8 months, 2 weeks agosamimshaikh
10 months, 4 weeks agogold4otas
11 months agoRamdi1
12 months agoNi33
1 year, 6 months agoBluemoon22
1 year, 7 months agoSaadKhamis
1 year, 7 months agohalfway
1 year, 6 months agoPower123
1 year, 7 months agovyhak
1 year, 9 months agokartikjena31
1 year, 10 months agoBere
1 year, 11 months agochimons
1 year, 11 months agodv00thay
1 year, 11 months agoVincentMenzel
1 year, 11 months agoziancom
1 year, 9 months agofaltu1985
2 years, 1 month ago