- Welcome my DevOps blog./
- 🔰Posts/
- 🗂️My Trainings/
- Terraform Trainings and Certifications/
- Terraform on AWS with SRE & IaC DevOps/
- Terraform resource block reference 🔥/
Terraform resource block reference 🔥
Table of Contents
The resource block defines a piece of infrastructure and specifies the settings for Terraform to create it with.
The arguments that an individual resource supports are determined by the provider. Refer to the provider documentation for more information about specific resource configuration.
More info: https://developer.hashicorp.com/terraform/language/block/resource
Configuration model #
The resource block supports the following arguments:
resource "<TYPE>" "<LABEL>"blockPROVIDER ARGUMENTSblock | refer to your provider documentationcountnumber | mutually exclusive withfor_eachdepends_onlist of referencesfor_eachmap or set of strings | mutually exclusive withcountproviderreferencelifecycleblockaction_triggerblockcreate_before_destroybooleanprevent_destroybooleanignore_changeslist of attributesreplace_triggered_bylist of referencespreconditionblockconditionstringerror_messagestring
postconditionblockconditionstringerror_messagestring
connectionblocktypestringuserstringpasswordstringhoststringportstringtimeoutstringscript_pathstringprivate_keystringcertificatestringagentstringagent_identitystringhost_keystringtarget_platformstringscript_pathstringhttpsstringinsecurestringuse_ntlmstringcacertstringbastion_hoststringbastion_host_keystringbastion_portstringbastion_userstringbastion_passwordstringbastion_private_keystringbastion_certificatestringproxy_schemestringproxy_portstringproxy_user_namestringproxy_user_passwordstring
provisioner "<TYPE>"blocksourcestringdestinationstringcontentstringcommandstringworking_dirstringinterpreterstringenvironmentstringwhenkeywordquietbooleaninlinelist of stringsscriptstringscriptsstringon_failurekeywordconnectionblock
Complete configuration #
The following resource block defines all of the supported built-in arguments you can set on a resource:
resource "<TYPE>" "<LABEL>" {
<PROVIDER_ARGUMENTS>
count = <NUMBER> # `for_each` and `count` are mutually exclusive
depends_on = [ <RESOURCE.ADDRESS.EXPRESSION> ]
for_each = { # `for_each` and `count` are mutually exclusive
<KEY> = <VALUE>
}
for_each = [ # `for_each` accepts a map or a set of strings
"<VALUE>",
"<VALUE>"
]
provider = <REFERENCE.TO.ALIAS>
lifecycle {
action_trigger {
events = [ # specify one or more lifecycle events as a list
before_create,
after_create,
before_update,
after_update
]
condition = <EXPRESSSION>
actions = [ action.<TYPE>.<LABEL> ]
}
create_before_destroy = <true || false>
prevent_destroy = <true || false>
ignore_changes = [ <ATTRIBUTE> ]
replace_triggered_by = [ <RESOURCE.ADDRESS.EXPRESSION> ]
precondition {
condition = <EXPRESSION>
error_message = "<STRING>"
}
postcondition {
condition = <EXPRESSION>
error_message = "<STRING>"
}
}
connection {
type = <"ssh" or "winrm">
host = <EXPRESSION>
<DEFAULT_CONNECTION_SETTINGS>
}
provisioner "<TYPE>" {
source = "<PATH>"
destination = "<PATH>"
content = "<CONTENT TO COPY TO `destination`>"
command = <COMMAND>
working_dir = "<PATH TO DIR WHERE TERRAFORM EXECUTES `command`>"
interpreter = [
"<PATH TO INTERPRETER EXECUTABLE>",
"<COMMAND> <ARGUMENTS>"
]
environment {
"<KEY>" = "<VALUE>"
}
when = <TERRAFORM COMMAND>
quiet = <true || false>
inline = [ "<COMMAND>" ]
script = "<PATH>"
scripts = [
"<PATH>"
]
on_failure = <continue || fail>
connection {
type = <"ssh" or "winrm">
host = <EXPRESSION>
<SPECIFIC_CONNECTION_SETTINGS>
}
}
}