github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/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  ```
    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](http://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  
    64  + aws_eip.ip
    65      instance:   "" => "${aws_instance.example.id}"
    66      private_ip: "" => "<computed>"
    67      public_ip:  "" => "<computed>"
    68  
    69  + aws_instance.example
    70      ami:               "" => "ami-aa7ab6c2"
    71      availability_zone: "" => "<computed>"
    72      instance_type:     "" => "t1.micro"
    73      key_name:          "" => "<computed>"
    74      private_dns:       "" => "<computed>"
    75      private_ip:        "" => "<computed>"
    76      public_dns:        "" => "<computed>"
    77      public_ip:         "" => "<computed>"
    78      security_groups:   "" => "<computed>"
    79      subnet_id:         "" => "<computed>"
    80  ```
    81  
    82  Terraform will create two resources: the instance and the elastic
    83  IP. In the "instance" value for the "aws\_eip", you can see the
    84  raw interpolation is still present. This is because this variable
    85  won't be known until the "aws\_instance" is created. It will be
    86  replaced at apply-time.
    87  
    88  Next, run `terraform apply`. The output will look similar to the
    89  following:
    90  
    91  ```
    92  aws_instance.example: Creating...
    93    ami:           "" => "ami-aa7ab6c2"
    94    instance_type: "" => "t1.micro"
    95  aws_eip.ip: Creating...
    96    instance: "" => "i-0e737b25"
    97  
    98  Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    99  ```
   100  
   101  It is clearer to see from actually running Terraform, but
   102  Terraform creates the EC2 instance before the elastic IP
   103  address. Due to the interpolation earlier where the elastic
   104  IP requires the ID of the EC2 instance, Terraform is able
   105  to infer a dependency, and knows to create the instance
   106  first.
   107  
   108  ## Implicit and Explicit Dependencies
   109  
   110  Most dependencies in Terraform are implicit: Terraform is able
   111  to infer dependencies based on usage of attributes of other
   112  resources.
   113  
   114  Using this information, Terraform builds a graph of resources.
   115  This tells Terraform not only in what order to create resources,
   116  but also what resources can be created in parallel. In our example,
   117  since the IP address depended on the EC2 instance, they could
   118  not be created in parallel.
   119  
   120  Implicit dependencies work well and are usually all you ever need.
   121  However, you can also specify explicit dependencies with the
   122  `depends_on` parameter which is available on any resource. For example,
   123  we could modify the "aws\_eip" resource to the following, which
   124  effectively does the same thing and is redundant:
   125  
   126  ```
   127  resource "aws_eip" "ip" {
   128  	instance = "${aws_instance.example.id}"
   129  	depends_on = ["aws_instance.example"]
   130  }
   131  ```
   132  
   133  If you're ever unsure about the dependency chain that Terraform
   134  is creating, you can use the [`terraform graph` command](/docs/commands/graph.html) to view
   135  the graph. This command outputs a dot-formatted graph which can be
   136  viewed with
   137  [Graphviz](http://www.graphviz.org/).
   138  
   139  ## Non-Dependent Resources
   140  
   141  We can now augment the configuration with another EC2 instance.
   142  Because this doesn't rely on any other resource, it can be
   143  created in parallel to everything else.
   144  
   145  ```
   146  resource "aws_instance" "another" {
   147  	ami = "ami-aa7ab6c2"
   148  	instance_type = "t1.micro"
   149  }
   150  ```
   151  
   152  You can view the graph with `terraform graph` to see that
   153  nothing depends on this and that it will likely be created
   154  in parallel.
   155  
   156  Before moving on, remove this resource from your configuration
   157  and `terraform apply` again to destroy it. We won't use the
   158  second instance anymore in the getting started guide.
   159  
   160  ## Next
   161  
   162  In this page you were introduced to both multiple resources
   163  as well as basic resource dependencies and resource attribute
   164  interpolation.
   165  
   166  Moving on, [we'll use provisioners](/intro/getting-started/provision.html)
   167  to do some basic bootstrapping of our launched instance.