簡體   English   中英

Cloudformation 條件模板URL

[英]Cloudformation conditional templateURL

有沒有辦法在 cloudformation 中執行條件模板 url?

這失敗了,因為直到aws cloudformation deploy步驟才對其進行評估,並且錯誤地指出 templateURL 必須是 s3 鏈接。 當我在其中一個 url 中進行硬編碼時,它會將該相關文件上傳到 s3,並且在打包的最終模板中,它將只有 s3 url 就位。

這不起作用(預 aws cloudformation package 步驟)

   Vpc:
    Type: 'AWS::CloudFormation::Stack'
    Properties:
      Parameters:
        AlertingModule: !GetAtt 'Alerting.Outputs.StackName'
        NatGateways: 'true'
      TemplateURL: !If [IsProduction, './default-vpc-module.yml', './production-vpc-module.yml']

當 url 被硬編碼時,它將 package 進入此(發布 aws cloudformation pacakge 步驟)

    VpcModule:
          Fn::GetAtt:
          - Vpc
          - Outputs.StackName
        AlertingModule:
          Fn::GetAtt:
          - Alerting
          - Outputs.StackName
        Priority: '2'
        HealthCheckPath: /health-check.php
      TemplateURL: https://s3.amazonaws.com/my-cicd-bucket-/fe556ff9386a28c063c4a110b31b.template

Yes you absolutely can achieve what your after, the way i did it was reference the s3 url, by using an aws cloudformaiton package but also a s3 cp in the codebuild stage to enforce naming conventions on the url. 只要您的模板 url 路徑區分不同。

為了提供一個非常靈活的示例,您可以在動態名稱組件上使用,Sub 和:If 替換語句,這也將允許您在 !If 語句中使用 !Ref :

Parameters:
  StageProd:
    Description: Environment
    Default: "production"
    Type: String
.......

......
    TemplateURL: !Sub 
        - https://${CfnBucketName}.s3-ap-southeast-2.amazonaws.com/${CfnKeyPrefix}/SomeFileName-${Stage}.yaml
        - { Stage: !If [ IsProduction, !Ref StageProd, "default"]}

以上應該滿足您想要實現的幾乎任何類型的動態命名組合; 但是,您也可以使用 simple.Sub 替換舞台名稱來大大簡化上述內容。

這是我最終做的,但我不喜歡它。

  Vpc:
    Condition: IsProduction
    Type: 'AWS::CloudFormation::Stack'
    Properties:
      Parameters:
        AlertingModule: !GetAtt 'Alerting.Outputs.StackName'
        NatGateways: 'true' # reduce costs
      TemplateURL: ./production-vpc-module.yml
  Vpc:
    Condition: IsNotProduction
    Type: 'AWS::CloudFormation::Stack'
    Properties:
      Parameters:
        AlertingModule: !GetAtt 'Alerting.Outputs.StackName'
        NatGateways: 'true' # reduce costs
      TemplateURL: ./default-vpc-module.yml

暫無
暫無

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

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