簡體   English   中英

AWS Cloud Formation 中的 DependsOn 最佳實踐(包括示例)

[英]DependsOn best practices in AWS Cloud Formation (example included)

在 CloudFormation 中使用 Depends On 的最佳實踐是什么? 我相信從我讀到的內容來看,不建議在 Azure 中這樣做並盡量減少它的使用。

例如,我想在 ASG 策略和 ASG 組之間建立 DependsOn 關系。

在此處輸入圖像描述

在上圖中,您可以看到 ASG Policy 有一個字段AutoScalingGroupName

因此,ASG 策略取決於 AutoScaling 組的創建。

這兩者之間是否存在依賴關系?

通常,CloudFormation 模板中引用另一個資源的任何資源都將自動具有隱含DependsOn

例如:

  PrivateRouteTable1:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Private Routes (AZ1)

  DefaultPrivateRoute1:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable1
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NatGateway1

DefaultPrivateRoute1將具有隱含的DependsOnPrivateRouteTable1NatGateway1

因此,唯一需要添加DependsOn的情況是沒有直接關系,但需要創建順序。 這是一個例子:

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  NatGateway1EIP:
    Type: AWS::EC2::EIP
    DependsOn: InternetGatewayAttachment
    Properties:
      Domain: vpc

在這種情況下,在彈性 IP 地址和 InternetGateway 之間定義了一個DependsOn 這很有幫助,因為彈性 IP 地址和 Internet 網關(鏈接到 VPC)之間沒有直接關系。

我見過 Amazon EC2 實例在其用戶數據腳本中出現故障的情況,因為其他資源尚未“准備好”,因此該腳本無法訪問 Internet。 診斷此類情況可能很困難,因為它們可能是暫時的。 因此,您可能希望在所需資源之間沒有直接引用的情況下專門添加一些DependsOn引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM