【问题标题】:AWS ECS: IAM related error when creating ECS service via CloudformationAWS ECS:通过 Cloudformation 创建 ECS 服务时出现 IAM 相关错误
【发布时间】:2021-05-13 22:11:46
【问题描述】:

我正在尝试通过以下清单创建 ECS 服务:

---
AWSTemplateFormatVersion: 2010-09-09
Description: 'CloudFormation template for ui service definition'
Resources:

  UIService:
    Type: AWS::ECS::Service
    Properties:
      Cluster: !ImportValue MyCSClusterName
      DesiredCount: 1
      LaunchType: EC2
      LoadBalancers:
        - ContainerName: !ImportValue UIContainerName
          ContainerPort: '80'
          TargetGroupArn: !ImportValue UITGArn
      Role: !Ref UIServiceRole
      ServiceName: ui-service
      ServiceRegistries:
       - RegistryArn: arn:aws:servicediscovery:eu-west-1:4309430903:service/srv-oh45959hj55yesez7
      TaskDefinition: !ImportValue UITaskArn


  UIServiceRole:
    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:DeregisterInstancesFromLoadBalancer', 'elasticloadbalancing:DeregisterTargets',
              'elasticloadbalancing:Describe*', 'elasticloadbalancing:RegisterInstancesWithLoadBalancer',
              'elasticloadbalancing:RegisterTargets', 'ec2:Describe*', 'ec2:AuthorizeSecurityGroupIngress']
            Resource: '*'

通过亚马逊控制台创建失败并出现以下错误,我不知道这是怎么回事。

您不能为需要服务相关角色的服务指定 IAM 角色。

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation amazon-ecs


    【解决方案1】:

    根据this terraform github issue,有人通过从清单中删除IAM 角色解决了这个错误(Role: !Ref UIServiceRole)。也许值得一试?

    【讨论】:

    【解决方案2】:

    在大多数情况下,当您创建需要执行某些 ECS 活动的集群或角色时,ECS 服务将创建 IAM 角色 AWSServiceRoleForECS。 Cloudformation 有时可能会遇到时间问题;解决这个问题的两种方法

    (1) 在您的 cloudformation 中确保 ECSCluster 具有依赖于可以访问 ecs 服务的 IAM 角色;在你的例子中DependsOn: UIServiceRole

    另一种看起来更“正确”的方法 (2) 在你的cloudformation中添加一个新资源IAM Service Linked Role as

      ECSServiceLinkedRole:
        Type: 'AWS::IAM::ServiceLinkedRole'
        Properties:
          AWSServiceName: ecs.amazonaws.com
          Description: ECS Service Linked Role
    

    注意:服务关联角色是否已经存在于帐户中,并且您定义了必须确保描述匹配的结构(Cloudformation 将不支持更改描述)

    Unable to assume service linked role when using ecs-cli相关

    【讨论】:

      猜你喜欢
      • 2018-05-18
      • 2019-04-19
      • 2017-08-14
      • 2017-03-26
      • 2022-12-21
      • 2019-08-17
      • 2019-11-12
      • 2018-09-17
      • 1970-01-01
      相关资源
      最近更新 更多