AWS System Manager能否只针对正在运行的实例进行目标设置?

0

【以下的问题经过翻译处理】 我正在使用自动化 Runbook 在托管的实例上安装和配置 CloudWatch Agent。当我在基于标签的资源组上执行我的 Runbook 时,它尝试在没有“运行”状态的实例上运行。当它在停止的实例上运行时,它最终会超时。如何只针对运行中的实例执行我的资源组?我尝试或查看的事情:

  • 在我的 Runbook 开头添加一个步骤来断言 AwsResourceProperty、DescribeInstanceStatus 和“运行”。这可以防止超时,但运行在停止的实例上时返回空值,因此中止并标记为失败。这对我来说是不可取的,因为我不认为 Runbook 失败,而是因为有一个合法的原因跳过了执行。此外,如果在大批机器上运行,只要一个停止的实例结束,整个父执行就会被标记为失败。
  • 将我的资源组过滤为仅包含正在运行的实例。我看不出有什么方法可以做到这一点。
  • 向我的 Runbook 添加属性,使其仅能在运行的实例上运行。类似于我如何使用 TargetType 属性将其限制为 /AWS::EC2::Instance。这对我来说最有意义,因为作为开发人员,我知道我的 Runbook 只有在实例运行时才能成功。我想要类似于设置 TargetType 为 service-provider::service-name::data-type-name::status 的东西。我没有找到任何方法。
  • 在AWS Systems Manager控制台选择执行目标时应用筛选器。这是另一个可能找到它的地方,但我没有找到。如果当我选择资源组时,交互式实例选择器将更新为仅显示该组中的实例,这将很麻烦,但至少允许我手动取消选择未运行的实例。

有没有一种我忽略的简单方法可以实现这一点?

profile picture
EXPERTE
gefragt vor 5 Monaten54 Aufrufe
1 Antwort
0

【以下的回答经过翻译处理】 这是我使用自动化文档 ( automation document) 完成的,受到 AWS-User-7841548 的回复的启示。它将针对正在运行 SSM 代理的实例。过滤器相当于 aws ssm describe-instance-information --filter "Key=InstanceIds,Values=xxx"

description: "changeme"
schemaVersion: '0.3'
assumeRole: '{{AutomationAssumeRole}}'
outputs:
  - runShellCommandLinux.Output
  - runPowerShellCommand.Output
  - runShellCommandMac.Output
parameters:
  AutomationAssumeRole:
    default: ''
    type: String
  InstanceId:
    description: (Required) EC2 InstanceId to run command
    type: String
mainSteps:

  - name: GetInstance
    action: 'aws:executeAwsApi'
    inputs:
      Service: ssm
      Api: DescribeInstanceInformation
      Filters:
        - Key: InstanceIds
          Values:
            - '{{ InstanceId }}'
    outputs:
      - Name: myInstance
        Selector: '$.InstanceInformationList[0].InstanceId'
        Type: String
      - Name: platform
        Selector: '$.InstanceInformationList[0].PlatformType'
        Type: String
      - Name: PingStatus
        Selector: '$.InstanceInformationList[0].PingStatus'
        Type: String
      - Name: ResourceType
        Selector: '$.InstanceInformationList[0].ResourceType'
        Type: String
  - name: SelectOnlineInstances
    action: 'aws:branch'
    isEnd: true
    inputs:
      Choices:
        - And:
            - Variable: '{{GetInstance.PingStatus}}'
              StringEquals: Online
            - Variable: '{{GetInstance.ResourceType}}'
              StringEquals: EC2Instance
          NextStep: ChoosePlatform
  - name: ChoosePlatform
    action: 'aws:branch'
    isEnd: true
    inputs:
      Choices:
        - NextStep: runPowerShellCommand
          Variable: '{{GetInstance.platform}}'
          StringEquals: Windows
        - NextStep: runShellCommandLinux
          Variable: '{{GetInstance.platform}}'
          StringEquals: Linux
        - NextStep: runShellCommandMac
          Variable: '{{GetInstance.platform}}'
          StringEquals: MacOS
  - name: runShellCommandLinux
    action: 'aws:runCommand'
    isEnd: true
    inputs:
      DocumentName: AWS-RunShellScript
      InstanceIds:
        - '{{GetInstance.myInstance}}'
      Parameters:
        commands:
          - echo "hello from linux"
  - name: runPowerShellCommand
    action: 'aws:runCommand'
    isEnd: true
    inputs:
      DocumentName: AWS-RunPowerShellScript
      InstanceIds:
        - '{{GetInstance.myInstance}}'
      Parameters:
        commands:
          - Write-Output "hello from windows"
  - name: runShellCommandMac
    action: 'aws:runCommand'
    isEnd: true
    inputs:
      DocumentName: AWS-RunShellScript
      InstanceIds:
        - '{{GetInstance.myInstance}}'
      Parameters:
        commands:
          - echo "hello from mac"
profile picture
EXPERTE
beantwortet vor 5 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen