github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/website/source/intro/getting-started/dependencies.html.md (about)

     1  ---
     2  layout: "intro"
     3  page_title: "Resource Dependencies"
     4  sidebar_current: "gettingstarted-deps"
     5  description: |-
     6    In this page, we're going to introduce resource dependencies, where we'll not only see a configuration with multiple resources for the first time, but also scenarios where resource parameters use information from other resources.
     7  ---
     8  
     9  # Resource Dependencies
    10  
    11  In this page, we're going to introduce resource dependencies,
    12  where we'll not only see a configuration with multiple resources
    13  for the first time, but also scenarios where resource parameters
    14  use information from other resources.
    15  
    16  Up to this point, our example has only contained a single resource.
    17  Real infrastructure has a diverse set of resources and resource
    18  types. Terraform configurations can contain multiple resources,
    19  multiple resource types, and these types can even span multiple
    20  providers.
    21  
    22  On this page, we'll show a basic example of multiple resources
    23  and how to reference the attributes of other resources to configure
    24  subsequent resources.
    25  
    26  ## Assigning an Elastic IP
    27  
    28  We'll improve our configuration by assigning an elastic IP to
    29  the EC2 instance we're managing. Modify your `example.tf` and
    30  add the following:
    31  
    32  ```hcl
    33  resource "aws_eip" "ip" {
    34    instance = "${aws_instance.example.id}"
    35  }
    36  ```
    37  
    38  This should look familiar from the earlier example of adding
    39  an EC2 instance resource, except this time we're building
    40  an "aws\_eip" resource type. This resource type allocates
    41  and associates an
    42  [elastic IP](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html)
    43  to an EC2 instance.
    44  
    45  The only parameter for
    46  [aws\_eip](/docs/providers/aws/r/eip.html) is "instance" which
    47  is the EC2 instance to assign the IP to. For this value, we
    48  use an interpolation to use an attribute from the EC2 instance
    49  we managed earlier.
    50  
    51  The syntax for this interpolation should be straightforward:
    52  it requests the "id" attribute from the "aws\_instance.example"
    53  resource.
    54  
    55  ## Plan and Execute
    56  
    57  Run `terraform plan` to view the execution plan. The output
    58  will look something like the following:
    59  
    60  ```
    61  $ terraform plan
    62  
    63  + aws_eip.ip
    64      allocation_id:     "<computed>"
    65      association_id:    "<computed>"
    66      domain:            "<computed>"
    67      instance:          "${aws_instance.example.id}"
    68      network_interface: "<computed>"
    69      private_ip:        "<computed>"
    70      public_ip:         "<computed>"
    71  
    72  + aws_instance.example
    73      ami:                      "ami-b374d5a5"
    74      availability_zone:        "<computed>"
    75      ebs_block_device.#:       "<computed>"
    76      ephemeral_block_device.#: "<computed>"
    77      instance_state:           "<computed>"
    78      instance_type:            "t2.micro"
    79      key_name:                 "<computed>"
    80      placement_group:          "<computed>"
    81      private_dns:              "<computed>"
    82      private_ip:               "<computed>"
    83      public_dns:               "<computed>"
    84      public_ip:                "<computed>"
    85      root_block_device.#:      "<computed>"
    86      security_groups.#:        "<computed>"
    87      source_dest_check:        "true"
    88      subnet_id:                "<computed>"
    89      tenancy:                  "<computed>"
    90      vpc_security_group_ids.#: "<computed>"
    91  ```
    92  
    93  Terraform will create two resources: the instance and the elastic
    94  IP. In the "instance" value for the "aws\_eip", you can see the
    95  raw interpolation is still present. This is because this variable
    96  won't be known until the "aws\_instance" is created. It will be
    97  replaced at apply-time.
    98  
    99  Next, run `terraform apply`. The output will look similar to the
   100  following:
   101  
   102  ```
   103  $ terraform apply
   104  aws_instance.example: Creating...
   105    ami:                      "" => "ami-b374d5a5"
   106    instance_type:            "" => "t2.micro"
   107    [..]
   108  aws_instance.example: Still creating... (10s elapsed)
   109  aws_instance.example: Creation complete
   110  aws_eip.ip: Creating...
   111    allocation_id:     "" => "<computed>"
   112    association_id:    "" => "<computed>"
   113    domain:            "" => "<computed>"
   114    instance:          "" => "i-f3d77d69"
   115    network_interface: "" => "<computed>"
   116    private_ip:        "" => "<computed>"
   117    public_ip:         "" => "<computed>"
   118  aws_eip.ip: Creation complete
   119  
   120  Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
   121  ```
   122  
   123  It is clearer to see from actually running Terraform, but
   124  Terraform creates the EC2 instance before the elastic IP
   125  address. Due to the interpolation earlier where the elastic
   126  IP requires the ID of the EC2 instance, Terraform is able
   127  to infer a dependency, and knows to create the instance
   128  first.
   129  
   130  ## Implicit and Explicit Dependencies
   131  
   132  Most dependencies in Terraform are implicit: Terraform is able
   133  to infer dependencies based on usage of attributes of other
   134  resources.
   135  
   136  Using this information, Terraform builds a graph of resources.
   137  This tells Terraform not only in what order to create resources,
   138  but also what resources can be created in parallel. In our example,
   139  since the IP address depended on the EC2 instance, they could
   140  not be created in parallel.
   141  
   142  Implicit dependencies work well and are usually all you ever need.
   143  However, you can also specify explicit dependencies with the
   144  `depends_on` parameter which is available on any resource. For example,
   145  we could modify the "aws\_eip" resource to the following, which
   146  effectively does the same thing and is redundant:
   147  
   148  ```hcl
   149  resource "aws_eip" "ip" {
   150    instance   = "${aws_instance.example.id}"
   151    depends_on = ["aws_instance.example"]
   152  }
   153  ```
   154  
   155  If you're ever unsure about the dependency chain that Terraform
   156  is creating, you can use the [`terraform graph` command](/docs/commands/graph.html) to view
   157  the graph. This command outputs a dot-formatted graph which can be
   158  viewed with
   159  [Graphviz](http://www.graphviz.org/).
   160  
   161  ## Non-Dependent Resources
   162  
   163  We can now augment the configuration with another EC2 instance.
   164  Because this doesn't rely on any other resource, it can be
   165  created in parallel to everything else.
   166  
   167  ```hcl
   168  resource "aws_instance" "another" {
   169    ami           = "ami-b374d5a5"
   170    instance_type = "t2.micro"
   171  }
   172  ```
   173  
   174  You can view the graph with `terraform graph` to see that
   175  nothing depends on this and that it will likely be created
   176  in parallel.
   177  
   178  Before moving on, remove this resource from your configuration
   179  and `terraform apply` again to destroy it. We won't use the
   180  second instance anymore in the getting started guide.
   181  
   182  ## Next
   183  
   184  In this page you were introduced to both multiple resources
   185  as well as basic resource dependencies and resource attribute
   186  interpolation.
   187  
   188  Moving on, [we'll use provisioners](/intro/getting-started/provision.html)
   189  to do some basic bootstrapping of our launched instance.