如何在Cloudformation模板中指定多个资源条件?

时间:2019-04-02 21:40:36

标签: amazon-cloudformation aws-cli

我有一个包含2个条件的Cloudformation模板

Conditions:
  ProdEnvironment:
    !Equals [ !Ref VPCStackNameParameter, 'ProductionVPC' ]
  CertExists:
    !Not [!Equals [!Ref SslCertificateArn, '']]

如何在要创建的资源中同时指定两个条件?像

Resources:
  Alb:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Condition: ProdEnvironment !And CertExists <--- this gives error!

1 个答案:

答案 0 :(得分:0)

您可以创建一个同时满足这两个条件的条件。

Conditions:
  ProdEnvironment:
    !Equals [ !Ref VPCStackNameParameter, 'ProductionVPC' ]
  CertExists:
    !Not [!Equals [!Ref SslCertificateArn, '']]
  CertExistsAndProd: !And
    - !Equals [ !Ref VPCStackNameParameter, 'ProductionVPC' ]
    - !Not [!Equals [!Ref SslCertificateArn, '']]
Resources:
  Alb:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Condition: CertExistsAndProd

documentation似乎建议您也可以在条件中引用条件。因此,您也可以尝试:

Conditions:
  ProdEnvironment:
    !Equals [ !Ref VPCStackNameParameter, 'ProductionVPC' ]
  CertExists:
    !Not [!Equals [!Ref SslCertificateArn, '']]
  CertExistsAndProd: !And
    - !Condition ProdEnvironment
    - !Condition CertExists
Resources:
  Alb:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Condition: CertExistsAndProd