简体   繁体   English

Cloudformation 条件模板URL

[英]Cloudformation conditional templateURL

Is there a way to do conditional template urls in cloudformation?有没有办法在 cloudformation 中执行条件模板 url?

This fails because it is not evaluated until the aws cloudformation deploy step and it errors out saying that the templateURL must be an s3 link.这失败了,因为直到aws cloudformation deploy步骤才对其进行评估,并且错误地指出 templateURL 必须是 s3 链接。 When I hard code in one of the urls it will upload that relative file to s3 and in the packaged final template it will just have the s3 url in place.当我在其中一个 url 中进行硬编码时,它会将该相关文件上传到 s3,并且在打包的最终模板中,它将只有 s3 url 就位。

This doesn't work (pre aws cloudformation package step)这不起作用(预 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']

When a url is hardcoded in it will package into this (post aws cloudformation pacakge step)当 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. 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. As long as your template url paths are distinguished differently.只要您的模板 url 路径区分不同。

To provide a very flexible example, you could use,Sub with an:If replacement statement on the dynamic name component, which will also allow your to use !Ref inside the !If statement:为了提供一个非常灵活的示例,您可以在动态名称组件上使用,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"]}

The above should meet almost any sort of combination of dynamic naming you want to achieve;以上应该满足您想要实现的几乎任何类型的动态命名组合; however, you could also very much simplify the above with a simple.Sub replacing the stage name.但是,您也可以使用 simple.Sub 替换舞台名称来大大简化上述内容。

Here is what I ended up doing, but I don't like it.这是我最终做的,但我不喜欢它。

  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