github.com/jzbruno/terraform@v0.10.3-0.20180104230435-18975d727047/website/docs/configuration/resources.html.md (about)

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