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