2016-09-15 19 views
15

Tôi đang cố tạo Ứng dụng cân bằng tải trong CloudFormation, với nhóm mục tiêu chuyển tiếp lưu lượng truy cập đến các phiên bản EC2. Dưới đây là đoạn mã có liên quan, nơi ELBSubnets, ECSCluster, taskdefinition, và VpcId được thông qua tại như các thông số:Tạo Nhóm mục tiêu ALB trong CloudFormation

"EcsElasticLoadBalancer" : { 
    "Type" : "AWS::ElasticLoadBalancingV2::LoadBalancer", 
    "Properties" : { 
    "Subnets" : { "Ref" : "ELBSubnets" }, 
    "SecurityGroups": [ 
     { "Ref": "ELBAccessSecurityGroup" } 
    ] 
    } 
}, 
"LoadBalancerListener": { 
    "Type": "AWS::ElasticLoadBalancingV2::Listener", 
    "Properties": { 
    "DefaultActions": [{ 
     "Type": "forward", 
     "TargetGroupArn": { "Ref": "TargetGroup" } 
    }], 
    "LoadBalancerArn": { "Ref": "EcsElasticLoadBalancer" }, 
    "Port": 80, 
    "Protocol": "HTTP" 
    } 
}, 
"TargetGroup": { 
    "Type": "AWS::ElasticLoadBalancingV2::TargetGroup", 
    "Properties": { 
    "Name": { "Fn::Join": [ "-", [ { "Ref": "AWS::StackName" }, "TargetGroup" ] ] }, 
    "Port": 80, 
    "Protocol": "HTTP", 
    "VpcId": { "Ref": "VpcId" } 
    }, 
    "DependsOn": [ "EcsElasticLoadBalancer" ] 
}, 
"service": { 
    "Type": "AWS::ECS::Service", 
    "Properties" : { 
    "Cluster": { "Ref": "ECSCluster" }, 
    "DesiredCount": "1", 
    "LoadBalancers": [ 
     { 
     "ContainerName": "main-app", 
     "ContainerPort": 3000, 
     "TargetGroupArn": { "Ref": "TargetGroup" } 
     } 
    ], 
    "Role" : {"Ref":"ECSServiceRole"}, 
    "TaskDefinition" : {"Ref":"taskdefinition"} 
    } 
}, 
"ECSServiceRole": { 
    "Type": "AWS::IAM::Role", 
    "Properties": { 
    "AssumeRolePolicyDocument": { 
     "Statement": [ 
     { 
      "Effect": "Allow", 
      "Principal": { 
      "Service": [ 
       "ecs.amazonaws.com" 
      ] 
      }, 
      "Action": [ 
      "sts:AssumeRole" 
      ] 
     } 
     ] 
    }, 
    "Path": "/", 
    "Policies": [ 
     { 
     "PolicyName": "ecs-service", 
     "PolicyDocument": { 
      "Statement": [ 
      { 
       "Effect": "Allow", 
       "Action": [ 
       "elasticloadbalancing:Describe*", 
       "elasticloadbalancing:DeregisterInstancesFromLoadBalancer", 
       "elasticloadbalancing:RegisterInstancesWithLoadBalancer", 
       "ec2:Describe*", 
       "ec2:AuthorizeSecurityGroupIngress" 
       ], 
       "Resource": "*" 
      } 
      ] 
     } 
     } 
    ] 
    } 
} 

tôi nhận được lỗi sau khi tạo dịch vụ:

Các nhóm đối tượng với targetGroupArn ARN : aws: elasticloadbalancing: us-east-1: xxxxxxxx: targetgroup/AlbServiceStack-TargetGroup/6ba9c037c26cdb36 không có bộ cân bằng tải liên quan.

Tôi đang thiếu gì? Trong tài liệu, dường như không có cách nào để chỉ định một cân bằng tải cho nhóm đích.

Trả lời

23

Got nó làm việc - vấn đề là có hai phần:

  1. Những dòng này bị mất tích từ PolicyDocument Vai trò:
    • "elasticloadbalancing:DeregisterTargets"
    • "elasticloadbalancing:RegisterTargets"
  2. dịch vụ cần thiết "DependsOn": [ "LoadBalancerListener" ] như một thuộc tính bổ sung.

mẫu Cập nhật trông như thế này:

"EcsElasticLoadBalancer" : { 
    "Type" : "AWS::ElasticLoadBalancingV2::LoadBalancer", 
    "Properties" : { 
    "Subnets" : { "Ref" : "ELBSubnets" }, 
    "SecurityGroups": [ 
     { "Ref": "ELBAccessSecurityGroup" } 
    ] 
    } 
}, 
"LoadBalancerListener": { 
    "Type": "AWS::ElasticLoadBalancingV2::Listener", 
    "Properties": { 
    "DefaultActions": [{ 
     "Type": "forward", 
     "TargetGroupArn": { "Ref": "TargetGroup" } 
    }], 
    "LoadBalancerArn": { "Ref": "EcsElasticLoadBalancer" }, 
    "Port": 80, 
    "Protocol": "HTTP" 
    } 
}, 
"TargetGroup": { 
    "Type": "AWS::ElasticLoadBalancingV2::TargetGroup", 
    "Properties": { 
    "Name": { "Fn::Join": [ "-", [ { "Ref": "AWS::StackName" }, "TargetGroup" ] ] }, 
    "Port": 80, 
    "Protocol": "HTTP", 
    "VpcId": { "Ref": "VpcId" } 
    }, 
    "DependsOn": [ "EcsElasticLoadBalancer" ] 
}, 
"service": { 
    "Type": "AWS::ECS::Service", 
    "DependsOn": [ "LoadBalancerListener" ], 
    "Properties" : { 
    "Cluster": { "Ref": "ECSCluster" }, 
    "DesiredCount": "1", 
    "LoadBalancers": [ 
     { 
     "ContainerName": "main-app", 
     "ContainerPort": 3000, 
     "TargetGroupArn": { "Ref": "TargetGroup" } 
     } 
    ], 
    "Role" : {"Ref":"ECSServiceRole"}, 
    "TaskDefinition" : {"Ref":"taskdefinition"} 
    } 
}, 
"ECSServiceRole": { 
    "Type": "AWS::IAM::Role", 
    "Properties": { 
    "AssumeRolePolicyDocument": { 
     "Statement": [ 
     { 
      "Effect": "Allow", 
      "Principal": { 
      "Service": [ 
       "ecs.amazonaws.com" 
      ] 
      }, 
      "Action": [ 
      "sts:AssumeRole" 
      ] 
     } 
     ] 
    }, 
    "Path": "/", 
    "Policies": [ 
     { 
     "PolicyName": "ecs-service", 
     "PolicyDocument": { 
      "Statement": [ 
      { 
       "Effect": "Allow", 
       "Action": [ 
       "elasticloadbalancing:Describe*", 
       "elasticloadbalancing:DeregisterInstancesFromLoadBalancer", 
       "elasticloadbalancing:RegisterInstancesWithLoadBalancer", 
       "ec2:Describe*", 
       "ec2:AuthorizeSecurityGroupIngress", 
       "elasticloadbalancing:DeregisterTargets", 
       "elasticloadbalancing:RegisterTargets" 
       ], 
       "Resource": "*" 
      } 
      ] 
     } 
     } 
    ] 
    } 
} 
+0

DependsOn Thuộc tính: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html – lasec0203

+1

Đối với tôi, chìa khóa là «AWS :: ECS :: Service' phải có một' DependsOn' với cả hai 'LoadBalancerListener' * và *' AWS :: ElasticLoadBalancingV2 :: TargetGroup' phải có 'DependsOn' với' EcsElasticLoadBalancer', mà bạn không gọi ra trong câu trả lời gấp đôi của bạn. Ngoài ra, tôi đề nghị bạn sử dụng AWS PolicyDocument chính thức 'arn: aws: iam :: aws: chính sách/vai trò dịch vụ/AmazonEC2ContainerServiceRole' thay vì tạo của riêng bạn. – Pete

Các vấn đề liên quan