github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/website/source/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  ```
    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  <a id="meta-parameters"></a>
    45  ### Meta-parameters
    46  
    47  There are **meta-parameters** available to all resources:
    48  
    49    * `count` (int) - The number of identical resources to create.
    50        This doesn't apply to all resources. For details on using variables in
    51        conjunction with count, see [Using Variables with
    52       `count`](#using-variables-with-count) below.
    53  
    54    * `depends_on` (list of strings) - Explicit dependencies that this
    55        resource has. These dependencies will be created before this
    56        resource. The dependencies are in the format of `TYPE.NAME`,
    57        for example `aws_instance.web`.
    58  
    59    * `provider` (string) - The name of a specific provider to use for
    60        this resource. The name is in the format of `TYPE.ALIAS`, for example,
    61        `aws.west`. Where `west` is set using the `alias` attribute in a
    62        provider. See [multiple provider instances](#multi-provider-instances).
    63  
    64    * `lifecycle` (configuration block) - Customizes the lifecycle
    65        behavior of the resource. The specific options are documented
    66        below.
    67  
    68  The `lifecycle` block allows the following keys to be set:
    69  
    70    * `create_before_destroy` (bool) - This flag is used to ensure
    71        the replacement of a resource is created before the original
    72        instance is destroyed. As an example, this can be used to
    73        create an new DNS record before removing an old record.
    74  
    75    * `prevent_destroy` (bool) - This flag provides extra protection against the
    76        destruction of a given resource. When this is set to `true`, any plan
    77        that includes a destroy of this resource will return an error message.
    78  
    79  <a id="ignore-changes"></a>
    80  
    81    * `ignore_changes` (list of strings) - Customizes how diffs are evaluated for
    82        resources, allowing individual attributes to be ignored through changes.
    83        As an example, this can be used to ignore dynamic changes to the
    84        resource from external resources. Other meta-parameters cannot be ignored.
    85  
    86  ~> **NOTE on create\_before\_destroy and dependencies:** Resources that utilize
    87  the `create_before_destroy` key can only depend on other resources that also
    88  include `create_before_destroy`. Referencing a resource that does not include
    89  `create_before_destroy` will result in a dependency graph cycle.
    90  
    91  ~> **NOTE on ignore\_changes:** Ignored attribute names can be matched by their
    92  name, not state ID. For example, if an `aws_route_table` has two routes defined
    93  and the `ignore_changes` list contains "route", both routes will be ignored.
    94  Additionally you can also use a single entry with a wildcard (e.g. `"*"`)
    95  which will match all attribute names. Using a partial string together with a
    96  wildcard (e.g. `"rout*"`) is **not** supported.
    97  
    98  
    99  <a id="connection-block"></a>
   100  
   101  ### Connection block
   102  
   103  Within a resource, you can optionally have a **connection block**.
   104  Connection blocks describe to Terraform how to connect to the
   105  resource for
   106  [provisioning](/docs/provisioners/index.html). This block doesn't
   107  need to be present if you're using only local provisioners, or
   108  if you're not provisioning at all.
   109  
   110  Resources provide some data on their own, such as an IP address,
   111  but other data must be specified by the user.
   112  
   113  The full list of settings that can be specified are listed on
   114  the [provisioner connection page](/docs/provisioners/connection.html).
   115  
   116  <a id="provisioners"></a>
   117  
   118  ### Provisioners
   119  
   120  Within a resource, you can specify zero or more **provisioner
   121  blocks**. Provisioner blocks configure
   122  [provisioners](/docs/provisioners/index.html).
   123  
   124  Within the provisioner block is provisioner-specific configuration,
   125  much like resource-specific configuration.
   126  
   127  Provisioner blocks can also contain a connection block
   128  (documented above). This connection block can be used to
   129  provide more specific connection info for a specific provisioner.
   130  An example use case might be to use a different user to log in
   131  for a single provisioner.
   132  
   133  <a id="using-variables-with-count"></a>
   134  
   135  ## Using Variables With `count`
   136  
   137  When declaring multiple instances of a resource using [`count`](#count), it is
   138  common to want each instance to have a different value for a given attribute.
   139  
   140  You can use the `${count.index}`
   141  [interpolation](/docs/configuration/interpolation.html) along with a map
   142  [variable](/docs/configuration/variables.html) to accomplish this.
   143  
   144  For example, here's how you could create three [AWS
   145  Instances](/docs/providers/aws/r/instance.html) each with their own
   146  static IP address:
   147  
   148  ```
   149  variable "instance_ips" {
   150    default = {
   151      "0" = "10.11.12.100"
   152      "1" = "10.11.12.101"
   153      "2" = "10.11.12.102"
   154    }
   155  }
   156  
   157  resource "aws_instance" "app" {
   158    count = "3"
   159    private_ip = "${lookup(var.instance_ips, count.index)}"
   160    # ...
   161  }
   162  ```
   163  
   164  <a id="multi-provider-instances"></a>
   165  
   166  ## Multiple Provider Instances
   167  
   168  By default, a resource targets the provider based on its type. For example
   169  an `aws_instance` resource will target the "aws" provider. As of Terraform
   170  0.5.0, a resource can target any provider by name.
   171  
   172  The primary use case for this is to target a specific configuration of
   173  a provider that is configured multiple times to support multiple regions, etc.
   174  
   175  To target another provider, set the `provider` field:
   176  
   177  ```
   178  resource "aws_instance" "foo" {
   179  	provider = "aws.west"
   180  
   181  	# ...
   182  }
   183  ```
   184  
   185  The value of the field should be `TYPE` or `TYPE.ALIAS`. The `ALIAS` value
   186  comes from the `alias` field value when configuring the
   187  [provider](/docs/configuration/providers.html).
   188  
   189  ```
   190  provider "aws" {
   191    alias = "west"
   192  
   193    # ...
   194  }
   195  ```
   196  
   197  If no `provider` field is specified, the default provider is used.
   198  
   199  ## Syntax
   200  
   201  The full syntax is:
   202  
   203  ```
   204  resource TYPE NAME {
   205  	CONFIG ...
   206  	[count = COUNT]
   207  	[depends_on = [RESOURCE NAME, ...]]
   208  	[provider = PROVIDER]
   209  
   210      [LIFECYCLE]
   211  
   212  	[CONNECTION]
   213  	[PROVISIONER ...]
   214  }
   215  ```
   216  
   217  where `CONFIG` is:
   218  
   219  ```
   220  KEY = VALUE
   221  
   222  KEY {
   223  	CONFIG
   224  }
   225  ```
   226  
   227  where `LIFECYCLE` is:
   228  
   229  ```
   230  lifecycle {
   231      [create_before_destroy = true|false]
   232      [prevent_destroy = true|false]
   233      [ignore_changes = [ATTRIBUTE NAME, ...]]
   234  }
   235  ```
   236  
   237  where `CONNECTION` is:
   238  
   239  ```
   240  connection {
   241  	KEY = VALUE
   242  	...
   243  }
   244  ```
   245  
   246  where `PROVISIONER` is:
   247  
   248  ```
   249  provisioner NAME {
   250  	CONFIG ...
   251  
   252  	[CONNECTION]
   253  }
   254  ```