github.com/hugorut/terraform@v1.1.3/website/docs/language/configuration-0-11/resources.mdx (about)

     1  ---
     2  page_title: Resources - 0.11 Configuration Language
     3  description: >-
     4    The most important thing you'll configure with Terraform are resources.
     5    Resources are a component of your infrastructure. It might be some low level
     6    component such as a physical server, virtual machine, or container. Or it can
     7    be a higher level component such as an email provider, DNS record, or database
     8    provider.
     9  ---
    10  
    11  # Resources
    12  
    13  -> **Note:** This page is about Terraform 0.11 and earlier. For Terraform 0.12
    14  and later, see
    15  [Configuration Language: Resources](/language/resources).
    16  
    17  The most important thing you'll configure with Terraform are
    18  resources. Resources are a component of your infrastructure.
    19  It might be some low level component such as a physical server,
    20  virtual machine, or container. Or it can be a higher level
    21  component such as an email provider, DNS record, or database
    22  provider.
    23  
    24  This page assumes you're familiar with the
    25  [configuration syntax](/language/configuration-0-11/syntax)
    26  already.
    27  
    28  ## Example
    29  
    30  A resource configuration looks like the following:
    31  
    32  ```hcl
    33  resource "aws_instance" "web" {
    34    ami           = "ami-408c7f28"
    35    instance_type = "t1.micro"
    36  }
    37  ```
    38  
    39  ## Description
    40  
    41  The `resource` block creates a resource of the given `TYPE` (first
    42  parameter) and `NAME` (second parameter). The combination of the type
    43  and name must be unique.
    44  
    45  Within the block (the `{ }`) is configuration for the resource. The
    46  configuration is dependent on the type. Consult the [provider's documentation](https://registry.terraform.io/browse/providers) for details.
    47  
    48  details.
    49  
    50  ### Meta-parameters
    51  
    52  There are **meta-parameters** available to all resources:
    53  
    54  - `count` (int) - The number of identical resources to create. This doesn't
    55    apply to all resources. For details on using variables in conjunction with
    56    count, see [Using Variables with `count`](#using-variables-with-count) below.
    57  
    58    -> Modules don't currently support the `count` parameter.
    59  
    60  - `depends_on` (list of strings) - Explicit dependencies that this resource has.
    61    These dependencies will be created before this resource. For syntax and other
    62    details, see the section below on [explicit
    63    dependencies](#explicit-dependencies).
    64  
    65  - `provider` (string) - The name of a specific provider to use for this
    66    resource. The name is in the format of `TYPE.ALIAS`, for example, `aws.west`.
    67    Where `west` is set using the `alias` attribute in a provider. See [multiple
    68    provider instances](#multiple-provider-instances).
    69  
    70  - `lifecycle` (configuration block) - Customizes the lifecycle behavior of the
    71    resource. The specific options are documented below.
    72  
    73    The `lifecycle` block allows the following keys to be set:
    74  
    75    - `create_before_destroy` (bool) - This flag is used to ensure the replacement
    76      of a resource is created before the original instance is destroyed. As an
    77      example, this can be used to create an new DNS record before removing an old
    78      record.
    79  
    80    - `prevent_destroy` (bool) - This flag provides extra protection against the
    81      destruction of a given resource. When this is set to `true`, any plan that
    82      includes a destroy of this resource will return an error message.
    83  
    84    - `ignore_changes` (list of strings) - Customizes how diffs are evaluated for
    85      resources, allowing individual attributes to be ignored through changes. As
    86      an example, this can be used to ignore dynamic changes to the resource from
    87      external resources. Other meta-parameters cannot be ignored.
    88  
    89      ```
    90      ~> Ignored attribute names can be matched by their name, not state ID.
    91      For example, if an `aws_route_table` has two routes defined and the
    92      `ignore_changes` list contains "route", both routes will be ignored.
    93      Additionally you can also use a single entry with a wildcard (e.g. `"*"`)
    94      which will match all attribute names. Using a partial string together
    95      with a wildcard (e.g. `"rout*"`) is **not** supported.
    96      ```
    97  
    98    -> Interpolations are not currently supported in the `lifecycle` configuration block (see [issue #3116](https://github.com/hugorut/terraform/issues/3116))
    99  
   100  ### Timeouts
   101  
   102  Individual Resources may provide a `timeouts` block to enable users to configure the
   103  amount of time a specific operation is allowed to take before being considered
   104  an error. For example, the
   105  [aws_db_instance](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_instance#timeouts)
   106  resource provides configurable timeouts for the
   107  `create`, `update`, and `delete` operations. Any Resource that provides Timeouts
   108  will document the default values for that operation, and users can overwrite
   109  them in their configuration.
   110  
   111  Example overwriting the `create` and `delete` timeouts:
   112  
   113  ```hcl
   114  resource "aws_db_instance" "timeout_example" {
   115    allocated_storage = 10
   116    engine            = "mysql"
   117    engine_version    = "5.6.17"
   118    instance_class    = "db.t1.micro"
   119    name              = "mydb"
   120  
   121    # ...
   122  
   123    timeouts {
   124      create = "60m"
   125      delete = "2h"
   126    }
   127  }
   128  ```
   129  
   130  Individual Resources must opt-in to providing configurable Timeouts, and
   131  attempting to configure the timeout for a Resource that does not support
   132  Timeouts, or overwriting a specific action that the Resource does not specify as
   133  an option, will result in an error. Valid units of time are  `s`, `m`, `h`.
   134  
   135  ### Explicit Dependencies
   136  
   137  Terraform ensures that dependencies are successfully created before a
   138  resource is created. During a destroy operation, Terraform ensures that
   139  this resource is destroyed before its dependencies.
   140  
   141  A resource automatically depends on anything it references via
   142  [interpolations](/language/configuration-0-11/interpolation). The automatically
   143  determined dependencies are all that is needed most of the time. You can also
   144  use the `depends_on` parameter to explicitly define a list of additional
   145  dependencies.
   146  
   147  The primary use case of explicit `depends_on` is to depend on a _side effect_
   148  of another operation. For example: if a provisioner creates a file, and your
   149  resource reads that file, then there is no interpolation reference for Terraform
   150  to automatically connect the two resources. However, there is a causal
   151  ordering that needs to be represented. This is an ideal case for `depends_on`.
   152  In most cases, however, `depends_on` should be avoided and Terraform should
   153  be allowed to determine dependencies automatically.
   154  
   155  The syntax of `depends_on` is a list of resources and modules:
   156  
   157  - Resources are `TYPE.NAME`, such as `aws_instance.web`.
   158  - Modules are `module.NAME`, such as `module.foo`.
   159  
   160  When a resource depends on a module, _everything_ in that module must be
   161  created before the resource is created.
   162  
   163  An example of a resource depending on both a module and resource is shown
   164  below. Note that `depends_on` can contain any number of dependencies:
   165  
   166  ```hcl
   167  resource "aws_instance" "web" {
   168    depends_on = ["aws_instance.leader", "module.vpc"]
   169  }
   170  ```
   171  
   172  -> **Use sparingly!** `depends_on` is rarely necessary.
   173  In almost every case, Terraform's automatic dependency system is the best-case
   174  scenario by having your resources depend only on what they explicitly use.
   175  Please think carefully before you use `depends_on` to determine if Terraform
   176  could automatically do this a better way.
   177  
   178  ### Connection block
   179  
   180  Within a resource, you can optionally have a **connection block**.
   181  Connection blocks describe to Terraform how to connect to the
   182  resource for
   183  [provisioning](/language/resources/provisioners/syntax). This block doesn't
   184  need to be present if you're using only local provisioners, or
   185  if you're not provisioning at all.
   186  
   187  Resources provide some data on their own, such as an IP address,
   188  but other data must be specified by the user.
   189  
   190  The full list of settings that can be specified are listed on
   191  the [provisioner connection page](/language/resources/provisioners/connection).
   192  
   193  ### Provisioners
   194  
   195  Within a resource, you can specify zero or more **provisioner
   196  blocks**. Provisioner blocks configure
   197  [provisioners](/language/resources/provisioners/syntax).
   198  
   199  Within the provisioner block is provisioner-specific configuration,
   200  much like resource-specific configuration.
   201  
   202  Provisioner blocks can also contain a connection block
   203  (documented above). This connection block can be used to
   204  provide more specific connection info for a specific provisioner.
   205  An example use case might be to use a different user to log in
   206  for a single provisioner.
   207  
   208  ## Using Variables With `count`
   209  
   210  When declaring multiple instances of a resource using [`count`](#count), it is
   211  common to want each instance to have a different value for a given attribute.
   212  
   213  You can use the `${count.index}`
   214  [interpolation](/language/configuration-0-11/interpolation) along with a map
   215  [variable](/language/configuration-0-11/variables) to accomplish this.
   216  
   217  For example, here's how you could create three [AWS
   218  Instances](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) each with their own
   219  static IP address:
   220  
   221  ```hcl
   222  variable "instance_ips" {
   223    default = {
   224      "0" = "10.11.12.100"
   225      "1" = "10.11.12.101"
   226      "2" = "10.11.12.102"
   227    }
   228  }
   229  
   230  resource "aws_instance" "app" {
   231    count = "3"
   232    private_ip = "${lookup(var.instance_ips, count.index)}"
   233    # ...
   234  }
   235  ```
   236  
   237  To reference a particular instance of a resource you can use `resource.foo.*.id[#]` where `#` is the index number of the instance.
   238  
   239  For example, to create a list of all [AWS subnet](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) ids vs referencing a specific subnet in the list you can use this syntax:
   240  
   241  ```hcl
   242  resource "aws_vpc" "foo" {
   243    cidr_block = "198.18.0.0/16"
   244  }
   245  
   246  resource "aws_subnet" "bar" {
   247    count      = 2
   248    vpc_id     = "${aws_vpc.foo.id}"
   249    cidr_block = "${cidrsubnet(aws_vpc.foo.cidr_block, 8, count.index)}"
   250  }
   251  
   252  output "vpc_id" {
   253    value = "${aws_vpc.foo.id}"
   254  }
   255  
   256  output "all_subnet_ids" {
   257    value = "${aws_subnet.bar.*.id}"
   258  }
   259  
   260  output "subnet_id_0" {
   261    value = "${aws_subnet.bar.*.id[0]}"
   262  }
   263  
   264  output "subnet_id_1" {
   265    value = "${aws_subnet.bar.*.id[1]}"
   266  }
   267  ```
   268  
   269  ## Multiple Provider Instances
   270  
   271  By default, a resource targets the provider based on its type. For example
   272  an `aws_instance` resource will target the "aws" provider. As of Terraform
   273  0.5.0, a resource can target any provider by name.
   274  
   275  The primary use case for this is to target a specific configuration of
   276  a provider that is configured multiple times to support multiple regions, etc.
   277  
   278  To target another provider, set the `provider` field:
   279  
   280  ```hcl
   281  resource "aws_instance" "foo" {
   282  	provider = "aws.west"
   283  
   284  	# ...
   285  }
   286  ```
   287  
   288  The value of the field should be `TYPE` or `TYPE.ALIAS`. The `ALIAS` value
   289  comes from the `alias` field value when configuring the
   290  [provider](/language/configuration-0-11/providers).
   291  
   292  ```hcl
   293  provider "aws" {
   294    alias = "west"
   295  
   296    # ...
   297  }
   298  ```
   299  
   300  If no `provider` field is specified, the default provider is used.
   301  
   302  ## Syntax
   303  
   304  The full syntax is:
   305  
   306  ```text
   307  resource TYPE NAME {
   308  	CONFIG ...
   309  	[count = COUNT]
   310  	[depends_on = [NAME, ...]]
   311  	[provider = PROVIDER]
   312  
   313      [LIFECYCLE]
   314  
   315  	[CONNECTION]
   316  	[PROVISIONER ...]
   317  }
   318  ```
   319  
   320  where `CONFIG` is:
   321  
   322  ```text
   323  KEY = VALUE
   324  
   325  KEY {
   326  	CONFIG
   327  }
   328  ```
   329  
   330  where `LIFECYCLE` is:
   331  
   332  ```text
   333  lifecycle {
   334      [create_before_destroy = true|false]
   335      [prevent_destroy = true|false]
   336      [ignore_changes = [ATTRIBUTE NAME, ...]]
   337  }
   338  ```
   339  
   340  where `CONNECTION` is:
   341  
   342  ```text
   343  connection {
   344  	KEY = VALUE
   345  	...
   346  }
   347  ```
   348  
   349  where `PROVISIONER` is:
   350  
   351  ```text
   352  provisioner NAME {
   353  	CONFIG ...
   354  
   355  	[when = "create"|"destroy"]
   356  	[on_failure = "continue"|"fail"]
   357  
   358  	[CONNECTION]
   359  }
   360  ```