我想做的-仅在Beanstalk环境名称为“ prod”或“ prod-2”时才添加ALB ListenerRule。如果Beanstalk环境名称不同,我将跳过该资源并继续而不创建它。
这是我的.ebextensions / 00-testing-condition.config:
"Conditions" : {
"createRedirect" : {"Fn::Or" : [ {"Fn::Equals" : [{"Ref" : "AWSEBEnvironmentName"}, "prod"]}, {"Fn::Equals" : [{"Ref" : "AWSEBEnvironmentName"}, "prod-2"]} ]}
}
Resources:
ALBListenerSSLRule:
Condition: createRedirect
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
Actions:
- RedirectConfig:
Host: "#{host}"
Path: "/#{path}"
Port: 443
Protocol: "HTTPS"
Query: "#{query}"
StatusCode: HTTP_301
Type: "redirect"
Conditions:
- Field: host-header
Values:
- "MYDOMAINNAME.com"
ListenerArn:
Ref: AWSEBV2LoadBalancerListener
Priority: 1
结果:如果env名称为prod或prod-2,则没有任何问题,就会创建ListenerRule。但是,如果名称不同(因此该语句为假),则在Beanstalk中收到以下错误:
Service:AmazonCloudFormation, Message:Template format error: Unresolved resource dependencies [ALBListenerSSLRule] in the Resources block of the template
关于任何错误的任何想法?
似乎条件无法在Beanstalk中正常工作。因此,我最终为每个env设置了域名并重定向,并使用Beanstalk中的Environment Property(变量)添加了此资源:
Resources:
ALBListenerSSLRule:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
Actions:
- RedirectConfig:
Host: "#{host}"
Path: "/#{path}"
Port: 443
Protocol: "HTTPS"
Query: "#{query}"
StatusCode: HTTP_301
Type: "redirect"
Conditions:
- Field: host-header
Values:
- "Fn::GetOptionSetting":
Namespace: "aws:elasticbeanstalk:application:environment"
OptionName: "ENV_DOMAIN_NAME"
DefaultValue: {"Ref":"AWS::NoValue"}
ListenerArn:
Ref: AWSEBV2LoadBalancerListener
Priority: 1