As you continue development, the resources in your configuration might start to depend on other resources in the same configuration. For example, you might want to add a virtual machine to a new network, and create both these resources in the same deployment. However, Deployment Manager creates resources in parallel, so there is no guarantee that the network is created before the virtual machine.
To help you define these dependencies correctly in your configuration file, you use references to refer to other resources.
References are useful because:
Deployment Manager resolves resources in dependent order, which means that if resource A references resource B, resource B is created first. For example, if a VM instance references a new network in the configuration, Deployment Manager always creates the network before creating the instance.
You can use references to access properties of resources. For example, when you define a virtual machine in your configuration, you do not yet know its IP address. However, you can still use a reference to the IP address. When you deploy your configuration, the VM is created first, and Deployment Manager gets the external IP address when it is available.
To use references, use this syntax in your configuration:
Now, let's update the configuration that you created in Create a Configuration, so that it creates a network, and update the two virtual machine instances to reference the new network.
Add the network definition
Open the configuration from step two,
two-vms.yaml, and add the following
network resource definition to the end of the file:
- name: a-new-network type: compute.v1.network properties: IPv4Range: 10.0.0.0/24
Replace the networks in the virtual machine properties with references
In the properties section of both your virtual machine instances, replace the value of
network with a reference to the new network’s
selfLink property, so that
the network interface property looks like this:
networkInterfaces: - network: $(ref.a-new-network.selfLink)
Save your changes. Your configuration should look like this:
# Copyright 2016 Google Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. resources: - name: the-first-vm type: compute.v1.instance properties: zone: us-central1-f machineType: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/zones/us-central1-f/machineTypes/f1-micro disks: - deviceName: boot type: PERSISTENT boot: true autoDelete: true initializeParams: sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-9 networkInterfaces: - network: $(ref.a-new-network.selfLink) accessConfigs: - name: External NAT type: ONE_TO_ONE_NAT - name: the-second-vm type: compute.v1.instance properties: zone: us-central1-f machineType: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/zones/us-central1-f/machineTypes/g1-small disks: - deviceName: boot type: PERSISTENT boot: true autoDelete: true initializeParams: sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-9 networkInterfaces: - network: $(ref.a-new-network.selfLink) accessConfigs: - name: External NAT type: ONE_TO_ONE_NAT - name: a-new-network type: compute.v1.network properties: IPv4Range: 10.0.0.0/16
Redeploy your configuration
gcloud deployment-manager deployments create deployment-with-references --config two-vms.yaml
See your deployment
gcloud deployment-manager deployments describe deployment-with-references
That’s it! You just created your first reference. Keep references in mind when you begin developing your own deployments.
Next, you will reorganize the configuration into flexible, reusable templates.
Delete Your Deployment
Compute Engine resources incur charges, so you should delete this deployment, as it is no longer necessary for future steps. Deleting this deployment deletes all the resources in this deployment as well. If you don't delete the deployment, you will run into conflicts with future examples.
gcloud deployment-manager deployments delete deployment-with-references
Next: Create a Template