Amazon Elastic Container Service 的基于身份的策略示例 - Amazon Elastic Container Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

Amazon Elastic Container Service 的基于身份的策略示例

默认情况下,用户和角色没有创建或修改 Amazon ECS 资源的权限。他们也无法使用 Amazon Web Services Management Console、Amazon Command Line Interface(Amazon CLI)或 Amazon API 执行任务。IAM 管理员必须创建 IAM policy,以便为用户和角色授予权限,以对所需资源执行操作。然后,管理员必须为需要这些策略的用户附加这些策略。

要了解如何使用这些示例 JSON 策略文档创建基于 IAM 身份的策略,请参阅《IAM 用户指南》中的创建 IAM policy

有关 Amazon ECS 定义的操作和资源类型的详细信息,包括每种资源类型的 ARN 格式,请参阅服务授权参考中的 Amazon Elastic Container Service 的操作、资源和条件键

策略最佳实践

基于身份的策略确定某个人是否可以创建、访问或删除您账户中的 Amazon ECS 资源。这些操作可能会使 Amazon Web Services 账户 产生成本。创建或编辑基于身份的策略时,请遵循以下准则和建议:

  • Amazon 托管策略及转向最低权限许可入门 - 要开始向用户和工作负载授予权限,请使用 Amazon 托管策略来为许多常见使用场景授予权限。您可以在 Amazon Web Services 账户 中找到这些策略。我们建议通过定义特定于您的使用场景的 Amazon 客户管理型策略来进一步减少权限。有关更多信息,请参阅《IAM 用户指南》中的 Amazon 托管策略工作职能的 Amazon 托管策略

  • 应用最低权限 – 在使用 IAM policy 设置权限时,请仅授予执行任务所需的权限。为此,您可以定义在特定条件下可以对特定资源执行的操作,也称为最低权限许可。有关使用 IAM 应用权限的更多信息,请参阅《IAM 用户指南》中的 IAM 中的策略和权限

  • 使用 IAM policy 中的条件进一步限制访问权限 – 您可以向策略添加条件来限制对操作和资源的访问。例如,您可以编写策略条件来指定必须使用 SSL 发送所有请求。如果通过特定 Amazon Web Service(例如 Amazon CloudFormation)使用服务操作,您还可以使用条件来授予对服务操作的访问权限。有关更多信息,请参阅《IAM 用户指南》中的 IAM JSON 策略元素:条件

  • 使用 IAM Access Analyzer 验证您的 IAM policy,以确保权限的安全性和功能性 – IAM Access Analyzer 会验证新策略和现有策略,以确保策略符合 IAM policy语言 (JSON) 和 IAM 最佳实践。IAM Access Analyzer 提供 100 多项策略检查和可操作的建议,以帮助您制定安全且功能性强的策略。有关更多信息,请参阅《IAM 用户指南》中的 IAM Acess Analyzer 策略验证

  • 需要多重身份验证 (MFA) – 如果您的账户需要 IAM 用户或根用户,请启用 MFA 来提高安全性。若要在调用 API 操作时需要 MFA,请将 MFA 条件添加到您的策略中。有关更多信息,请参阅《IAM 用户指南》中的配置受 MFA 保护的 API 访问

有关 IAM 中的最佳实践的更多信息,请参阅《IAM 用户指南》中的 IAM 中的安全最佳实践

允许用户查看他们自己的权限

该示例说明了您如何创建策略,以允许 IAM 用户查看附加到其用户身份的内联和托管式策略。此策略包括在控制台上完成此操作或者以编程方式使用 Amazon CLI 或 Amazon API 所需的权限。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "ViewOwnUserInfo", "Effect": "Allow", "Action": [ "iam:GetUserPolicy", "iam:ListGroupsForUser", "iam:ListAttachedUserPolicies", "iam:ListUserPolicies", "iam:GetUser" ], "Resource": ["arn:aws:iam::*:user/${aws:username}"] }, { "Sid": "NavigateInConsole", "Effect": "Allow", "Action": [ "iam:GetGroupPolicy", "iam:GetPolicyVersion", "iam:GetPolicy", "iam:ListAttachedGroupPolicies", "iam:ListGroupPolicies", "iam:ListPolicyVersions", "iam:ListPolicies", "iam:ListUsers" ], "Resource": "*" } ] }

Amazon ECS 首次运行向导权限

Amazon ECS 在经典控制台中首次运行向导简化了创建集群并运行您的任务和服务的过程。不过,用户需要通过多种 Amazon 服务来获得执行许多 API 操作的权限才能完成此向导。以下 AmazonECS_FullAccess 托管策略说明了完成 Amazon ECS 首次运行向导所需的权限。

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "application-autoscaling:DeleteScalingPolicy", "application-autoscaling:DeregisterScalableTarget", "application-autoscaling:DescribeScalableTargets", "application-autoscaling:DescribeScalingActivities", "application-autoscaling:DescribeScalingPolicies", "application-autoscaling:PutScalingPolicy", "application-autoscaling:RegisterScalableTarget", "appmesh:ListMeshes", "appmesh:ListVirtualNodes", "appmesh:DescribeVirtualNode", "autoscaling:UpdateAutoScalingGroup", "autoscaling:CreateAutoScalingGroup", "autoscaling:CreateLaunchConfiguration", "autoscaling:DeleteAutoScalingGroup", "autoscaling:DeleteLaunchConfiguration", "autoscaling:Describe*", "cloudformation:CreateStack", "cloudformation:DeleteStack", "cloudformation:DescribeStack*", "cloudformation:UpdateStack", "cloudwatch:DescribeAlarms", "cloudwatch:DeleteAlarms", "cloudwatch:GetMetricStatistics", "cloudwatch:PutMetricAlarm", "codedeploy:CreateApplication", "codedeploy:CreateDeployment", "codedeploy:CreateDeploymentGroup", "codedeploy:GetApplication", "codedeploy:GetDeployment", "codedeploy:GetDeploymentGroup", "codedeploy:ListApplications", "codedeploy:ListDeploymentGroups", "codedeploy:ListDeployments", "codedeploy:StopDeployment", "codedeploy:GetDeploymentTarget", "codedeploy:ListDeploymentTargets", "codedeploy:GetDeploymentConfig", "codedeploy:GetApplicationRevision", "codedeploy:RegisterApplicationRevision", "codedeploy:BatchGetApplicationRevisions", "codedeploy:BatchGetDeploymentGroups", "codedeploy:BatchGetDeployments", "codedeploy:BatchGetApplications", "codedeploy:ListApplicationRevisions", "codedeploy:ListDeploymentConfigs", "codedeploy:ContinueDeployment", "sns:ListTopics", "lambda:ListFunctions", "ec2:AssociateRouteTable", "ec2:AttachInternetGateway", "ec2:AuthorizeSecurityGroupIngress", "ec2:CancelSpotFleetRequests", "ec2:CreateInternetGateway", "ec2:CreateLaunchTemplate", "ec2:CreateRoute", "ec2:CreateRouteTable", "ec2:CreateSecurityGroup", "ec2:CreateSubnet", "ec2:CreateVpc", "ec2:DeleteLaunchTemplate", "ec2:DeleteSubnet", "ec2:DeleteVpc", "ec2:Describe*", "ec2:DetachInternetGateway", "ec2:DisassociateRouteTable", "ec2:ModifySubnetAttribute", "ec2:ModifyVpcAttribute", "ec2:RunInstances", "ec2:RequestSpotFleet", "elasticloadbalancing:CreateListener", "elasticloadbalancing:CreateLoadBalancer", "elasticloadbalancing:CreateRule", "elasticloadbalancing:CreateTargetGroup", "elasticloadbalancing:DeleteListener", "elasticloadbalancing:DeleteLoadBalancer", "elasticloadbalancing:DeleteRule", "elasticloadbalancing:DeleteTargetGroup", "elasticloadbalancing:DescribeListeners", "elasticloadbalancing:DescribeLoadBalancers", "elasticloadbalancing:DescribeRules", "elasticloadbalancing:DescribeTargetGroups", "ecs:*", "events:DescribeRule", "events:DeleteRule", "events:ListRuleNamesByTarget", "events:ListTargetsByRule", "events:PutRule", "events:PutTargets", "events:RemoveTargets", "iam:ListAttachedRolePolicies", "iam:ListInstanceProfiles", "iam:ListRoles", "logs:CreateLogGroup", "logs:DescribeLogGroups", "logs:FilterLogEvents", "route53:GetHostedZone", "route53:ListHostedZonesByName", "route53:CreateHostedZone", "route53:DeleteHostedZone", "route53:GetHealthCheck", "servicediscovery:CreatePrivateDnsNamespace", "servicediscovery:CreateService", "servicediscovery:GetNamespace", "servicediscovery:GetOperation", "servicediscovery:GetService", "servicediscovery:ListNamespaces", "servicediscovery:ListServices", "servicediscovery:UpdateService", "servicediscovery:DeleteService" ], "Resource": [ "*" ] }, { "Effect": "Allow", "Action": [ "ssm:GetParametersByPath", "ssm:GetParameters", "ssm:GetParameter" ], "Resource": "arn:aws:ssm:*:*:parameter/aws/service/ecs*" }, { "Effect": "Allow", "Action": [ "ec2:DeleteInternetGateway", "ec2:DeleteRoute", "ec2:DeleteRouteTable", "ec2:DeleteSecurityGroup" ], "Resource": [ "*" ], "Condition": { "StringLike": { "ec2:ResourceTag/aws:cloudformation:stack-name": "EC2ContainerService-*" } } }, { "Action": "iam:PassRole", "Effect": "Allow", "Resource": [ "*" ], "Condition": { "StringLike": { "iam:PassedToService": "ecs-tasks.amazonaws.com" } } }, { "Action": "iam:PassRole", "Effect": "Allow", "Resource": [ "arn:aws:iam::*:role/ecsInstanceRole*" ], "Condition": { "StringLike": { "iam:PassedToService": [ "ec2.amazonaws.com", "ec2.amazonaws.com.cn" ] } } }, { "Action": "iam:PassRole", "Effect": "Allow", "Resource": [ "arn:aws:iam::*:role/ecsAutoscaleRole*" ], "Condition": { "StringLike": { "iam:PassedToService": [ "application-autoscaling.amazonaws.com", "application-autoscaling.amazonaws.com.cn" ] } } }, { "Effect": "Allow", "Action": "iam:CreateServiceLinkedRole", "Resource": "*", "Condition": { "StringLike": { "iam:AWSServiceName": [ "ecs.amazonaws.com", "spot.amazonaws.com", "spotfleet.amazonaws.com", "ecs.application-autoscaling.amazonaws.com", "autoscaling.amazonaws.com" ] } } } ] }

首次运行向导还会尝试自动创建不同的 IAM 角色,具体取决于所用的任务启动类型。例如,Amazon ECS 服务角色、容器实例 IAM 角色和任务执行 IAM 角色。要确保首次运行时能够创建这些 IAM 角色,必须满足以下条件之一:

集群示例

以下 IAM policy 授予创建和列出群集所需的权限。CreateClusterListClusters 操作不接受任何资源,因此,所有资源的资源定义将设置为 *

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:CreateCluster", "ecs:ListClusters" ], "Resource": [ "*" ] } ] }

以下 IAM policy 授予描述和删除特定集群所需的权限。DescribeClustersDeleteCluster 操作将集群 ARN 接受为资源。

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:DescribeClusters", "ecs:DeleteCluster" ], "Resource": [ "arn:aws:ecs:us-east-1:<aws_account_id>:cluster/<cluster_name>" ] } ] }

以下 IAM policy 可附加到用户或组,该策略仅允许用户或组对特定集群执行操作。

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "ecs:Describe*", "ecs:List*" ], "Effect": "Allow", "Resource": "*" }, { "Action": [ "ecs:DeleteCluster", "ecs:DeregisterContainerInstance", "ecs:ListContainerInstances", "ecs:RegisterContainerInstance", "ecs:SubmitContainerStateChange", "ecs:SubmitTaskStateChange" ], "Effect": "Allow", "Resource": "arn:aws:ecs:us-east-1:<aws_account_id>:cluster/default" }, { "Action": [ "ecs:DescribeContainerInstances", "ecs:DescribeTasks", "ecs:ListTasks", "ecs:UpdateContainerAgent", "ecs:StartTask", "ecs:StopTask", "ecs:RunTask" ], "Effect": "Allow", "Resource": "*", "Condition": { "ArnEquals": { "ecs:cluster": "arn:aws:ecs:us-east-1:<aws_account_id>:cluster/default" } } } ] }

容器实例示例

虽然容器实例注册由 Amazon ECS 代理处理,但有时您可能需要允许用户从集群中手动注销实例。容器实例可能已意外注册到错误的集群,或者实例已终止,但仍有任务在其上运行。

以下 IAM policy 可让用户列出并注销指定群集中的容器类型:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:DeregisterContainerInstance", "ecs:ListContainerInstances" ], "Resource": [ "arn:aws:ecs:<region>:<aws_account_id>:cluster/<cluster_name>" ] } ] }

以下 IAM policy 可让用户描述指定群集中的指定容器实例:要对集群中的所有容器实例开放此权限,您可以将容器实例 UUID 替换为 *

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:DescribeContainerInstances" ], "Condition": { "ArnEquals": { "ecs:cluster": "arn:aws:ecs:<region>:<aws_account_id>:cluster/<cluster_name>" } }, "Resource": [ "arn:aws:ecs:<region>:<aws_account_id>:container-instance/<cluster_name>/<container_instance_UUID>" ] } ] }

任务定义示例

任务定义 IAM policy 不支持资源级权限,但以下 IAM policy 可让用户注册、列出和描述任务定义:

如果您使用新控制台,则必须添加 CloudFormation: CreateStack 作为 Action

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:RegisterTaskDefinition", "ecs:ListTaskDefinitions", "ecs:DescribeTaskDefinition" ], "Resource": [ "*" ] } ] }

运行任务示例

任务定义是 RunTask 的资源。要限制用户可在其上运行任务定义的集群,您可以在 Condition 数据块中指定这些集群。这样做的好处是,您不必在资源中列出任务定义和集群以允许适当的访问。您可以应用任务定义和/或集群。

以下 IAM policy 授予在特定集群上运行特定任务定义的任意修订的权限:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:RunTask" ], "Condition": { "ArnEquals": { "ecs:cluster": "arn:aws:ecs:<region>:<aws_account_id>:cluster/<cluster_name>" } }, "Resource": [ "arn:aws:ecs:<region>:<aws_account_id>:task-definition/<task_family>:*" ] } ] }

启动任务示例

任务定义是 StartTask 的资源。要限制用户可在其中启动任务定义的集群和容器实例,您可以在 Condition 数据块中指定它们。这样做的好处是,您不必在资源中列出任务定义和集群以允许适当的访问。您可以应用任务定义和/或集群。

以下 IAM policy 授予在特定集群和特定容器实例上开始对特定任务定义进行任意修订的权限。

注意

在本示例中,在通过 Amazon CLI 或其他 Amazon 开发工具包调用 StartTask API 时,必须指定任务定义修订,以便 Resource 映射匹配。

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:StartTask" ], "Condition": { "ArnEquals": { "ecs:cluster": "arn:aws:ecs:<region>:<aws_account_id>:cluster/<cluster_name>", "ecs:container-instances" : [ "arn:aws:ecs:<region>:<aws_account_id>:container-instance/<cluster_name>/<container_instance_UUID>" ] } }, "Resource": [ "arn:aws:ecs:<region>:<aws_account_id>:task-definition/<task_family>:*" ] } ] }

列出并描述任务示例

以下 IAM policy 可让用户列出指定集群的任务:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:ListTasks" ], "Condition": { "ArnEquals": { "ecs:cluster": "arn:aws:ecs:<region>:<aws_account_id>:cluster/<cluster_name>" } }, "Resource": [ "*" ] } ] }

以下 IAM policy 可让用户描述指定集群中的指定任务:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:DescribeTasks" ], "Condition": { "ArnEquals": { "ecs:cluster": "arn:aws:ecs:<region>:<aws_account_id>:cluster/<cluster_name>" } }, "Resource": [ "arn:aws:ecs:<region>:<aws_account_id>:task/<cluster_name>/<task_UUID>" ] } ] }

创建服务示例

以下 IAM policy 可让用户在 Amazon Web Services Management Console 中创建 Amazon ECS 服务:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "application-autoscaling:Describe*", "application-autoscaling:PutScalingPolicy", "application-autoscaling:RegisterScalableTarget", "cloudwatch:DescribeAlarms", "cloudwatch:PutMetricAlarm", "ecs:List*", "ecs:Describe*", "ecs:CreateService", "elasticloadbalancing:Describe*", "iam:AttachRolePolicy", "iam:CreateRole", "iam:GetPolicy", "iam:GetPolicyVersion", "iam:GetRole", "iam:ListAttachedRolePolicies", "iam:ListRoles", "iam:ListGroups", "iam:ListUsers" ], "Resource": [ "*" ] } ] }

更新服务示例

以下 IAM policy 可让用户在 Amazon Web Services Management Console 中更新 Amazon ECS 服务:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "application-autoscaling:Describe*", "application-autoscaling:PutScalingPolicy", "application-autoscaling:DeleteScalingPolicy", "application-autoscaling:RegisterScalableTarget", "cloudwatch:DescribeAlarms", "cloudwatch:PutMetricAlarm", "ecs:List*", "ecs:Describe*", "ecs:UpdateService", "iam:AttachRolePolicy", "iam:CreateRole", "iam:GetPolicy", "iam:GetPolicyVersion", "iam:GetRole", "iam:ListAttachedRolePolicies", "iam:ListRoles", "iam:ListGroups", "iam:ListUsers" ], "Resource": [ "*" ] } ] }

基于标签描述 Amazon ECS 服务

您可以在基于身份的策略中使用条件,以便基于标签控制对 Amazon ECS 资源的访问。此示例显示如何创建允许描述服务的策略。但是,仅当服务标签 Owner 具有该用户的用户名的值时,才授予此权限。此策略还授予在控制台上完成此操作的必要权限。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "DescribeServices", "Effect": "Allow", "Action": "ecs:DescribeServices", "Resource": "*" }, { "Sid": "ViewServiceIfOwner", "Effect": "Allow", "Action": "ecs:DescribeServices", "Resource": "arn:aws:ecs:*:*:service/*", "Condition": { "StringEquals": {"ecs:ResourceTag/Owner": "${aws:username}"} } } ] }

您可以将该策略附加到您账户中的 IAM 用户。如果名为 richard-roe 的用户尝试描述 Amazon ECS 服务,则服务必须标记为 Owner=richard-roeowner=richard-roe。否则,他会被拒绝访问。条件标签键 Owner 匹配 Ownerowner,因为条件键名称不区分大小写。有关更多信息,请参阅 IAM 用户指南 中的 IAM JSON 策略元素:条件