【发布时间】:2021-04-28 19:29:39
【问题描述】:
尝试使用 cloudformation 创建 ECS 服务(在 Fargate 上)但出现错误:
ARN 中的服务无效(服务:AmazonECS;状态代码:400;错误 代码:无效参数异常;请求 ID:xxx)。
根据错误消息,似乎有些 ARN 是错误的,但我没有找到原因,我检查了 IAM 角色的 ARN 并且没问题。另一个 ARN 通过 !Ref 函数传递(所以不是拼写错误)
创建所有资源(包括来自所有其他嵌套模板、vpc、集群、alb 等),“服务”资源(ECS 服务)除外。
下面是使用的模板(嵌套模板)。所有参数都可以(从根模板传递)。参数 TaskExecutionRole 和 ServiceRole 是 ECS 向导创建的 IAM 角色的 ARN:
Description: >
Deploys xxx ECS service, with load balancer listener rule,
target group, task definition, service definition and auto scaling
Parameters:
EnvironmentName:
Description: An environment name that will be prefixed to resource names
Type: String
EnvironmentType:
Description: See master template
Type: String
VpcId:
Type: String
PublicSubnet1:
Type: String
PublicSubnet2:
Type: String
ALBListener:
Description: ALB listener
Type: String
Cluster:
Description: ECS Cluster
Type: String
TaskExecutionRole:
Description: See master template
Type: String
ServiceRole:
Description: See master template
Type: String
ServiceName:
Description: Service name (used as a variable)
Type: String
Default: xxx
Cpu:
Description: Task size (CPU)
Type: String
Memory:
Description: Task size (memory)
Type: String
Conditions:
HasHttps: !Equals [!Ref EnvironmentType, production]
HasNotHttps: !Not [!Equals [!Ref EnvironmentType, production]]
Resources:
ServiceTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Name: !Sub '${EnvironmentName}-${ServiceName}'
VpcId: !Ref VpcId
TargetType: ip
Port: 80
Protocol: HTTP
AlbListenerRule:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref ServiceTargetGroup
Conditions:
- Field: host-header
Values: [www.mydomain.com] # test
ListenerArn: !Ref ALBListener
Priority: 1
TaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: !Sub '${EnvironmentName}-${ServiceName}-Task'
ContainerDefinitions:
- Name: !Ref ServiceName
Image: nginx
PortMappings:
- ContainerPort: 80
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref EnvironmentName
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: !Ref ServiceName
NetworkMode: awsvpc
RequiresCompatibilities: [FARGATE]
Cpu: !Ref Cpu
Memory: !Ref Memory
ExecutionRoleArn: !Ref TaskExecutionRole
Service:
Type: AWS::ECS::Service
DependsOn: TaskDefinition
Properties:
Cluster: !Ref Cluster
ServiceName: !Ref ServiceName
TaskDefinition: !Ref TaskDefinition
LaunchType: FARGATE
DesiredCount: 1
LoadBalancers:
- ContainerName: !Ref ServiceName
ContainerPort: 80
TargetGroupArn: !Ref ServiceTargetGroup
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: ENABLED
Subnets:
- !Ref PublicSubnet1
- !Ref PublicSubnet2
Role: !Ref ServiceRole
我在这方面浪费了几个小时并且无法解决它,我在文档中查看了很多但没有任何内容,如果有人知道如何提供帮助。
谢谢!
【问题讨论】:
-
输入参数很多,有没有可能是漏掉了一个,或者是其中一个输入了错误的值?
-
是的,但是在 Service 资源上使用的那些正在工作:集群和子网在其他嵌套模板上工作(在主模板上仔细检查),角色是手动输入的,但我复制了从 IAM 控制台(选中)。谢谢@juanreyesv
-
你是对的@juanreyesv,Cluster 参数错误(!Ref on master),更改为从另一个嵌套模板(!GetAtt)获取输出并且可以工作。现在又报错了(您不能为需要服务关联角色的服务指定 IAM 角色),我会研究一下!非常感谢!!!
-
在哪里可以找到错误“您无法为需要服务链接角色的服务指定 IAM 角色”的原因,我们也面临同样的问题。 @RodrigoPires
标签: amazon-web-services amazon-cloudformation amazon-ecs aws-fargate