简体   繁体   English

aws cloudformation 如何在映射块中使用条件?

[英]aws cloudformation how can I use conditions in the mapping block?

Hi I have the following mapping that I wanted to use condition statements in:嗨,我有以下映射,我想在其中使用条件语句:

Conditions:
  IsChinaSite:
    !Equals [!Ref SiteType, 'ChinaSite']


Mappings:
  Environment2Env:
    Develop:
      ENV: Dev
      ApiRegion: us-east-1  # There is no test CN data so we are going to keep using test US data in test envs
    Test:
      ENV: Qual
      ApiRegion: us-east-1
    Staging:
      ENV: Test
      ApiRegion: us-east-1
    Production:
      ENV: Prod
      FleetApiRegion: !If[IsChinaSite, ap-northeast-1, us-east-1]

As you can see I am trying to sigv4 sign to a different region depending on the value of an input param in the prod version of my site.正如您所看到的,我正在尝试根据我网站 prod 版本中输入参数的值将 sigv4 签名到不同的区域。 However after reading the docs it seems you cant use condition statements in the Mappings block of your template.但是, 在阅读文档后,您似乎无法在模板的 Mappings 块中使用条件语句。 How/where should I go about checking the IsChinaSite condition?我应该如何/去哪里检查IsChinaSite条件?

I image you can mix an If and FindInMap to get the result you want我想你可以混合一个 If 和 FindInMap 来得到你想要的结果

Conditions:
  IsChinaSite:
    !Equals [!Ref SiteType, 'ChinaSite']


Mappings:
  Environment2Env:
    Develop:
      ENV: Dev
      ApiRegion: us-east-1
    Test:
      ENV: Qual
      ApiRegion: us-east-1
    Staging:
      ENV: Test
      ApiRegion: us-east-1
    USProduction:
      Env: Prod
      ApiRegion: us-east-1
    ChinaProduction:
      ENV: Prod
      ApiRegion: ap-northeast-1

Resources:
  Ec2Instance:
    Type: AWS::EC2::Instance
    Properties:
      AvailabilityZone:
        !If
          - IsChainSite
          - !FindInMap [Environment2Env, ChinaProduction, ApiRegion]
          - !FindInMap [Environment2Env, USProduction, ApiRegion]

Ended up having to just split out the regions into their own mapping under each env.最终不得不在每个环境下将区域拆分为自己的映射。 Wound up with this:结束了这个:

Mappings:
  Environment2Env:
    Develop:
      ENV: Dev
    Test:
      ENV: Qual
    Staging:
      ENV: Test
    USProduction:
      Env: Prod
    ChinaProduction:
      ENV: Prod

  Develop:
    ROW: us-east-1
    ChinaSite: us-east-1
  Test:
    ROW: us-east-1
    ChinaSite: us-east-1
  Staging:
    ROW: us-east-1
    ChinaSite: us-east-1
  Production:
    ROW: us-east-1
    ChinaSite: ap-northeast-1

This solution worked for me because thankfully I was only parameterizing with 2 dimensions (the max/min dimensions required to make a cloudformation mapping) but that was kind of dumb luck.这个解决方案对我有用,因为幸运的是我只使用 2 个维度(制作云形成映射所需的最大/最小维度)进行参数化,但那是一种愚蠢的运气。 Had I been in a situation where I needed to deploy in multiple envs, with multiple types of sites, AND each type of site had some sort of sub-type, I would have been scr---d.如果我处于需要在多个环境中部署的情况,具有多种类型的站点,并且每种类型的站点都有某种子类型,我会被 scr --- d。 I still think the right answer here is something that can support x^n number of dimensions so I don't think I am going to accept my own answer but I wanted to share my solution to my problem.我仍然认为这里的正确答案是可以支持 x^n 个维度的东西,所以我认为我不会接受我自己的答案,但我想分享我对问题的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Cloudformation - 如何使用 If Else 条件 - AWS Cloudformation - How to use If Else conditions AWS CloudFormation:如何使用参数来形成字符串? - AWS CloudFormation: how can I use parameters to form strings? 我可以将AWS CloudFormation与自定义AMI一起使用吗? - Can I use AWS CloudFormation with a custom AMI? 如何在 Cloudformation 中为属性使用条件 - How to use conditions for properties in Cloudformation 如何在 AWS CloudFormation 的参数部分添加条件? - How to add conditions in Parameters section in AWS CloudFormation? 如何在 AWS Lambda 中加载 AWS Cloudformation 的 Output? - How Can I Load The Output of AWS Cloudformation In AWS Lambda? AWS CloudFormation CodeBuild:我可以使用 ECR 图像作为环境图像吗 - AWS CloudFormation CodeBuild: Can i use ECR image as Environment image 我可以在 AWS Cloudformation json 模板的“参数”中使用“Fn::Join”吗 - Can I use "Fn::Join" in "Parameters" of AWS Cloudformation json template 如何在CloudFormation模板中使用来自其他AWS账户的AssumeRole? - How can I use AssumeRole from another AWS account in a CloudFormation template? 如何在CloudFormation中获取用户选择的VPC的CIDR块? - How can I get the CIDR block of the VPC selected by the user in CloudFormation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM