【问题标题】:SSM document does not use role to modify volumeSSM 文档不使用角色修改卷
【发布时间】:2020-11-07 03:45:20
【问题描述】:

我正在尝试使用 SSM 修改 EC2 实例上的现有卷。我附加了一个带有允许修改 EC2 实例上的卷的策略的角色。我在策略中选择的权限称为 ModifyVolume。当我尝试运行我的脚本时,它给了我以下消息:

调用 ModifyVolume 操作时发生错误(UnauthorizedOperation):您无权执行此操作。

我确信我使用的策略授予我此权限,所以我想知道为什么它不起作用?

这是我的 SSM 文档:

---
schemaVersion: "2.2"
assumeRole: "{{AutomationAssumeRole}}"
description: "Resizes the specified EBS volume to the target size"
parameters:
  AutomationAssumeRole:
    type: "String"
    description: "The ARN of the role that allows Automation to perform the actions on your behalf."
    default: "arn:aws:iam::accountnumber:role/SSMUpdateVolume"
  VolumeId:
    type: "String"
    description: "(Required) EBS volume ID"
  Size:
    type: "String"
    description: "(Required) Target size for the selected volume in GB"
mainSteps:
- action: "aws:runShellScript"
  name: "ModifyVolumeSize"
  inputs:
    runCommand:
    - "export AWS_DEFAULT_REGION=eu-central-1"
    - "aws ec2 modify-volume --size {{Size}} --volume-id {{VolumeId}}"

这是我下面需要的角色:

我注意到,当我将权限直接分配给分配给实例本身的角色时,它会起作用。但是,我只想在使用 SSM 文档时暂时允许此权限。因此,这意味着 SSM 文档不会应用此权限,而是使用实例本身上缺少此 ModifyVolume 权限的那个权限。我该如何解决这个问题?

我认为这可能是因为我正在使用 aws:runShellScript 命令,所以它根本不应用角色而只是调用实例上的脚本?这可能是原因吗?如果是这种情况,我需要做什么才能完成这项工作?

【问题讨论】:

  • 不确定是否相关,但您的花括号数量不匹配:assumeRole: "{{AutomationAssumeRole}"
  • 是的,这是一个错字,不幸的是它没有改变任何东西
  • 能否将政策添加到您的问题中,以便我们尝试重现?
  • 我添加了政策并添加了一些关于我的案例的其他信息
  • 我正要尝试重现您的情况,但后来我注意到assumeRole 指的是一个参数。这可能不起作用,因为参数是单独定义的。您可以尝试在assumeRole 中对IAM 角色的ARN 进行硬编码(例如assumeRole: arn:aws:iam::accountnumber:role/SSMUpdateVolume,看看是否可行?

标签: amazon-web-services amazon-ec2 amazon-ebs aws-systems-manager


【解决方案1】:

我花了一些时间来解决这个问题,但我想我已经有了答案:

AWS Systems Manager 文档的工作方式如下:

他们有行动(例如aws:runShellScriptaws:createStack等)

这些操作使用默认的SSM 服务角色,或通过assumeRole 注入的角色。

这意味着在您的示例中,action aws:runShellScript 是使用 role SSMUpdateVolume 执行的。

但是,shell 脚本中的各个命令是使用 EC2 实例上的本地权限运行的,即附加到实例配置文件的角色,它没有所需的权限。

所以,这不起作用是有道理的。

要实现您想要的效果,您可以使用 aws:executeAwsApi 操作来修改音量,而不是执行 shell 命令。

请注意:您正在使用命令类型的文档。为此,您需要创建一个Automation 类型的文档。为此,请在 AWS Systems Manager 文档控制台中选择创建自动化而不是创建命令或会话

最终文档可能类似于以下内容:

description: Resizes the specified EBS volume to the target size
schemaVersion: '0.3'
assumeRole: '{{ AutomationAssumeRole }}'
parameters:
  AutomationAssumeRole:
    type: String
    default: 'arn:aws:iam::accountnumber:role/SSMUpdateVolume'
    description: The ARN of the role that allows Automation to perform the actions on your behalf.
  VolumeId:
    type: String
    description: (Required) EBS volume ID
  Size:
    type: String
    description: (Required) Target size for the selected volume in GB
mainSteps:
  - name: ModifyVolumeSize
    action: 'aws:executeAwsApi'
    inputs:
      Service: ec2
      Api: ModifyVolume
      Size: '{{ Size }}'
      VolumeId: '{{ VolumeId }}'

【讨论】:

  • 我在尝试使用此代码时收到此错误:InvalidDocumentContent: Unknown plugin name: aws:executeAwsApi
  • 是的,你是对的。 aws:executeAwsApi 在不同的模式中使用。话虽如此,我最初的答案使它变得比需要的复杂得多。使用 AWS SSSM 自动化文档,您可以直接调用 ec2:ModifyVolume API。您无需在 EC2 实例中运行脚本。我将相应地更新我的答案。
  • 完成。我希望这对现在有所帮助。请注意,您必须创建不同类型的 SSM 文档。在 AWS Systems Manager 文档控制台中时,请选择创建自动化而不是创建命令或会话
  • Mhhh,我更喜欢以前的解决方案以及一个有效的 aws:executeAwsApi 命令....背景是我计划执行一些脚本操作,我认为这些操作不能通过使用 SSM仅文档...我知道我没有在这里提到它,但这是我最初的计划...,我将用我需要的其他内容更新我的答案,所以很清楚为什么这样做对我更有利在此处使用脚本命令
  • 好的,我会将其标记为已接受的答案,因为它正在解决问题,并将尝试使用 aws:executeScript 自己解决其余问题,非常感谢
猜你喜欢
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 2020-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多