github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/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-123456"
    29      instance_type = "m1.small"
    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  There are **meta-parameters** available to all resources:
    45  
    46    * `count` (int) - The number of identical resources to create.
    47        This doesn't apply to all resources. For details on using variables in
    48        conjunction with count, see [Using Variables with
    49       `count`](#using-variables-with-count) below.
    50  
    51    * `depends_on` (list of strings) - Explicit dependencies that this
    52        resource has. These dependencies will be created before this
    53        resource. The dependencies are in the format of `TYPE.NAME`,
    54        for example `aws_instance.web`.
    55  
    56    * `lifecycle` (configuration block) - Customizes the lifecycle
    57        behavior of the resource. The specific options are documented
    58        below.
    59  
    60  The `lifecycle` block allows the following keys to be set:
    61  
    62    * `create_before_destroy` (bool) - This flag is used to ensure
    63        the replacement of a resource is created before the original
    64        instance is destroyed. As an example, this can be used to
    65        create an new DNS record before removing an old record.
    66  
    67    * `prevent_destroy` (bool) - This flag provides extra protection against the
    68        destruction of a given resource. When this is set to `true`, any plan
    69        that includes a destroy of this resource will return an error message.
    70  
    71  -------------
    72  
    73  Within a resource, you can optionally have a **connection block**.
    74  Connection blocks describe to Terraform how to connect to the
    75  resource for
    76  [provisioning](/docs/provisioners/index.html). This block doesn't
    77  need to be present if you're using only local provisioners, or
    78  if you're not provisioning at all.
    79  
    80  Resources provide some data on their own, such as an IP address,
    81  but other data must be specified by the user.
    82  
    83  The full list of settings that can be specified are listed on
    84  the [provisioner connection page](/docs/provisioners/connection.html).
    85  
    86  -------------
    87  
    88  Within a resource, you can specify zero or more **provisioner
    89  blocks**. Provisioner blocks configure
    90  [provisioners](/docs/provisioners/index.html).
    91  
    92  Within the provisioner block is provisioner-specific configuration,
    93  much like resource-specific configuration.
    94  
    95  Provisioner blocks can also contain a connection block
    96  (documented above). This connection block can be used to
    97  provide more specific connection info for a specific provisioner.
    98  An example use case might be to use a different user to log in
    99  for a single provisioner.
   100  
   101  <a id="using-variables-with-count"></a>
   102  
   103  ## Using Variables With `count`
   104  
   105  When declaring multiple instances of a resource using [`count`](#count), it is
   106  common to want each instance to have a different value for a given attribute.
   107  
   108  You can use the `${count.index}`
   109  [interpolation](/docs/configuration/interpolation.html) along with a mapping [variable](/docs/configuration/variables.html) to accomplish this.
   110  
   111  For example, here's how you could create three [AWS Instances](/docs/providers/aws/r/instance.html) each with their own static IP
   112  address:
   113  
   114  ```
   115  variable "instance_ips" {
   116    default = {
   117      "0" = "10.11.12.100"
   118      "1" = "10.11.12.101"
   119      "2" = "10.11.12.102"
   120    }
   121  }
   122  
   123  resource "aws_instance" "app" {
   124    count = "3"
   125    private_ip = "${lookup(var.instance_ips, count.index)}"
   126    # ...
   127  }
   128  ```
   129  
   130  ## Multiple Provider Instances
   131  
   132  By default, a resource targets the resource based on its type. For example
   133  an `aws_instance` resource will target the "aws" provider. As of Terraform
   134  0.5.0, a resource can target any provider by name.
   135  
   136  The primary use case for this is to target a specific configuration of
   137  a provider that is configured multiple times to support multiple regions, etc.
   138  
   139  To target another provider, set the `provider` field:
   140  
   141  ```
   142  resource "aws_instance" "foo" {
   143  	provider = "aws.west"
   144  
   145  	# ...
   146  }
   147  ```
   148  
   149  The value of the field should be `TYPE` or `TYPE.ALIAS`. The `ALIAS` value
   150  comes from the `alias` field value when configuring the
   151  [provider](/docs/configuration/providers.html).
   152  
   153  If no `provider` field is specified, the default (provider with no alias)
   154  provider is used.
   155  
   156  ## Syntax
   157  
   158  The full syntax is:
   159  
   160  ```
   161  resource TYPE NAME {
   162  	CONFIG ...
   163  	[count = COUNT]
   164  	[depends_on = [RESOURCE NAME, ...]]
   165  	[provider = PROVIDER]
   166  
   167      [LIFECYCLE]
   168  
   169  	[CONNECTION]
   170  	[PROVISIONER ...]
   171  }
   172  ```
   173  
   174  where `CONFIG` is:
   175  
   176  ```
   177  KEY = VALUE
   178  
   179  KEY {
   180  	CONFIG
   181  }
   182  ```
   183  
   184  where `LIFECYCLE` is:
   185  
   186  ```
   187  lifecycle {
   188      [create_before_destroy = true|false]
   189  }
   190  ```
   191  
   192  where `CONNECTION` is:
   193  
   194  ```
   195  connection {
   196  	KEY = VALUE
   197  	...
   198  }
   199  ```
   200  
   201  where `PROVISIONER` is:
   202  
   203  ```
   204  provisioner NAME {
   205  	CONFIG ...
   206  
   207  	[CONNECTION]
   208  }
   209  ```