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.
DependsOn Thuộc tính: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html – lasec0203
Đố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