在Cloudformation中创建应用程序负载平衡器时出错... XXXXX必须为ARN格式

时间:2018-08-12 18:22:01

标签: amazon-cloudformation aws-elb

我使用AWS CloudFormation服务尝试在2个EC2实例上创建Application Elastic Load Balancer,但是在创建监听器[AWS :: ElasticLoadBalancingV2 :: Listener]时遇到错误:

“ AELB-ElasticLoadBa-XDTNTTXRZMC8'必须为ARN格式(服务:AmazonElasticLoadBalancingV2;状态代码:400;错误代码:ValidationError;请求ID:9b18bb79-9e58-11e8-9b70-c9b2be714e80)”

我已经参考了aws代码模板并在代码下方添加了代码,我有什么遗漏吗?

ElasticLoadBalancer:
Type: 'AWS::ElasticLoadBalancing::LoadBalancer'
Properties:
  Instances: [!Ref 'webServer1', !Ref 'webServer2']  
  CrossZone: 'true'
  Listeners:
  - LoadBalancerPort: '80'
    InstancePort: '80'
    Protocol: HTTP
  Subnets:
    - !Ref pubSubnet
  SecurityGroups: 
    - !Ref LoadBalancerSecurityGroup
  HealthCheck:
    Target: HTTP:80/
    HealthyThreshold: '3'
    UnhealthyThreshold: '5'
    Interval: '30'
    Timeout: '5'

TargetGroupService1: 
Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
Properties: 
  Name: 
    'Fn::Join': 
      - '-'
      - - Ref: 'AWS::StackName'
        - 'TargetGroupService1'

  Port: 10
  Protocol: HTTP
  #HealthCheckPath: /service1
  Targets:
  - Id:
      Ref: webServer1
    Port: 80
  VpcId: !Ref myDemoVPC

TargetGroupService2: 
Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
Properties: 
  Name: 
    'Fn::Join': 
      - '-'
      - - Ref: 'AWS::StackName'
        - 'TargetGroupService2'

  Port: 10
  Protocol: HTTP
  #HealthCheckPath: /service2
  Targets:
  - Id:
      Ref: webServer2
    Port: 80
  VpcId: !Ref myDemoVPC

Listener:
Type: 'AWS::ElasticLoadBalancingV2::Listener'
Properties:
  DefaultActions:
  - Type: forward
    TargetGroupArn: !Ref TargetGroupService1
  LoadBalancerArn: !Ref ElasticLoadBalancer
  Port: '80'
  Protocol: HTTP

ListenerRuleService1:
Type: 'AWS::ElasticLoadBalancingV2::ListenerRule'
Properties:
  Actions:
    - Type: forward
      TargetGroupArn: !Ref TargetGroupService1
  Conditions:
  - Field: path-pattern
    Values:
    - "/service1"
  ListenerArn: !Ref Listener
  Priority: 1

ListenerRuleService2:
Type: 'AWS::ElasticLoadBalancingV2::ListenerRule'
Properties:
  Actions:
    - Type: forward
      TargetGroupArn: !Ref TargetGroupService2
  Conditions:
  - Field: path-pattern
    Values:
    - "/service2"
  ListenerArn: !Ref Listener
  Priority: 2

1 个答案:

答案 0 :(得分:2)

您使用了错误的cloudformation资源。应用程序负载平衡器的TypeAWS::ElasticLoadBalancingV2::LoadBalancer。请注意V2。您使用的是一个经典的负载均衡器。

您得到的错误是由于经典LB和应用程序LB之间Ref函数的返回值不同而引起的。

当您指定时:

  

LoadBalancerArn:!Ref ElasticLoadBalancer

Ref经典LB返回资源名称(AELB-ElasticLoadBa-XDTNTTXRZMC8),而Ref ALB返回资源Arn,这是V2侦听器期望的LoadBalancerArn属性。

使用具有here描述的适当属性的V2负载平衡器替换逻辑名称为ElasticLoadBalancer的资源应该可以解决您的问题。