Smi
Smi

Reputation: 183

Error while creating Application Load Balancer in Cloudformation... XXXXX must be in ARN format

Using the AWS CloudFormation service, I am trying to create an Application Elastic Load Balancer on 2 EC2 instances, but I am getting an error while creating Listener [AWS::ElasticLoadBalancingV2::Listener] as below:

"AELB-ElasticLoadBa-XDTNTTXRZMC8' must be in ARN format (Service: AmazonElasticLoadBalancingV2; Status Code: 400; Error Code: ValidationError; Request ID: 9b18bb79-9e58-11e8-9b70-c9b2be714e80)"

I have referred aws code template and added below code, am I missing anything?

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

Upvotes: 3

Views: 10411

Answers (1)

user818510
user818510

Reputation: 3652

You are using the wrong cloudformation resource. The Type of an application load balancer is AWS::ElasticLoadBalancingV2::LoadBalancer. Note the V2. The one you are using creates a classic load balancer.

The error you are getting is due to the difference in the return values for Ref function between classic LB and application LB.

When you specify:

LoadBalancerArn: !Ref ElasticLoadBalancer

RefClassic LB returns the resource name(AELB-ElasticLoadBa-XDTNTTXRZMC8) while the Ref ALB returns the resource Arn which is what the V2 listener expects for LoadBalancerArn attribute.

Replacing the resource with logical name ElasticLoadBalancer with the V2 Load Balancer with appropriate attributes described here should fix your issue.

Upvotes: 3

Related Questions