참조 사용

구성 또는 템플릿의 속성을 정의할 때 직접 값을 제공하는 대신 다른 리소스의 속성에 대한 참조를 사용할 수 있습니다. 예를 들어 동일한 배포의 인스턴스 템플릿을 사용하는 인스턴스 그룹 관리자를 만들려는 경우 인스턴스 템플릿의 전체 링크를 명시적으로 입력하는 대신 $(ref.instance-template.selfLink) 구문을 통해 참조를 사용할 수 있습니다.

참조로 다음을 수행할 수 있습니다.

  • 리소스가 생성될 때까지 정의되지 않은 속성에 액세스합니다. 예를 들어 구성에 가상 머신을 정의할 때는 가상 머신의 IP 주소를 모릅니다. 그러나 IP 주소에 대한 참조는 계속 사용할 수 있습니다. 구성을 배포할 때 VM이 먼저 생성되고 Deployment Manager가 외부 IP 주소(있는 경우)를 가져옵니다.

  • 가독성이 뛰어나고 문제 해결도 용이한 구성 또는 템플릿을 만듭니다. 예를 들어 전달 규칙을 여러 개 구성해야 하는 경우 사용할 네트워크도 지정해야 합니다. 각 전달 규칙마다 네트워크 링크를 제공하는 대신 다음 구문을 사용하여 네트워크의 selfLink 속성에 대한 참조를 만들 수 있습니다.

    $(ref.network-name.selfLink) 

    구성 문제를 해결해야 하는 경우 참조를 통해 전달 규칙에서 어떤 네트워크가 사용되는지 쉽게 알 수 있습니다.

리소스에 대한 참조를 만들 때 리소스 간에 종속 항목도 만듭니다. 다음과 같이 sandbox-vmnetwork-a에 대한 참조를 사용하는 예시를 들어 보겠습니다.

resources: - name: sandbox-vm   type: compute.v1.instance   properties:     network: $(ref.network-a.selfLink)     ... ... - name: network-a   type: compute.v1.network   properties:     ... 

이 구성을 배포하면 참조가 확인될 수 있도록 Deployment Manager가 network-a를 만든 후 sandbox-vm을 만듭니다. 참조가 성공적으로 확인되지 않으면 배포가 실패합니다.

참조는 구성 및 템플릿 모두에서 사용할 수 있습니다.

시작하기 전에

구성 파일에서 참조 만들기

다음 형식을 사용하여 구성에서 참조를 선언합니다.

$(ref.RESOURCE_NAME.PROPERTY) 

다음 예제에서는 네트워크를 만든 다음 새로 만든 네트워크에 대한 참조를 사용하는 두 개의 인스턴스를 만듭니다. 이 예시에서 참조는 다음과 같습니다.

$(ref.a-new-network.selfLink) 
# 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-11     networkInterfaces:     # The resource's "network value" has been replaced with a     # reference to the new network's "selfLink" property. The network      # resource has been added to the end of this file.     - 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-11     networkInterfaces:     # As in the resource above, the "network" value has been replaced with      # the new network's "selfLink" property.      - network: $(ref.a-new-network.selfLink)       accessConfigs:       - name: External NAT         type: ONE_TO_ONE_NAT # The following network is a new resource added to the "two-vms.yaml" file. - name: a-new-network   type: compute.v1.network   properties:     routingConfig:       routingMode: REGIONAL     autoCreateSubnetworks: true 

이 구성을 배포하면 네트워크가 두 인스턴스보다 먼저 생성되며 참조는 네트워크 리소스의 selfLink로 확인됩니다.

템플릿에서 참조 만들기

템플릿 파일에서 전체 참조는 앞에 $를 표시하고 괄호로 묶어야 합니다.

$(ref.RESOURCE_NAME.PROPERTY) 

참조에는 템플릿 속성 및 환경 변수와 같은 다른 기능을 조합할 수 있습니다. Deployment Manager가 참조를 올바르게 파싱하도록 하려면 전체 참조 문자열을 괄호로 묶어야 합니다.

다음은 템플릿에 참조를 선언하는 방법에 대한 몇 가지 예입니다.

Jinja

  • 환경 변수를 포함하는 참조

    network: $(ref.{{ env["deployment"] }}-network.selfLink) 
  • 배열의 값에 대한 참조

    subnetwork: $(ref.{{ env["deployment"] }}-vm.networkInterfaces[2].subnetwork) 
  • 템플릿 속성을 포함하는 참조

    network: $(ref.{{ properties["network"] }}.selfLink) 
  • Jinja 매개변수를 사용한 참조

    network: $(ref.{{ NETWORK_NAME }}.selfLink) 
  • 출력의 참조

      outputs:   - name: UrlToService     value: http://$(ref.{{ env["deployment"] }}-network.networkInterfaces[0].accessConfigs[0].natIp):8080/ 

Python

  • 환경 변수를 포함하는 참조

    'network': '$(ref.' + context.env['deployment'] + '-network.selfLink)' 
  • 배열의 값에 대한 참조

    'subnetwork': '$(ref.' + context.env['deployment'] + '-vm.networkInterfaces[2].subnetwork)' 
  • 템플릿 속성을 포함하는 참조

    'network': '$(ref.' + context.properties['network'] + '.selfLink)' 
  • Python 매개변수를 사용한 참조

    'value': '$(ref.' + base_name + '.networkInterfaces[0].networkIP)' 
  • 출력의 참조

    outputs = [{'name': 'UrlToService',             'value': '$(ref.' + context.env['deployment'] + '-network.networkInterfaces[0].accessConfigs[0].natIP):8080'}] 

다음 단계