【问题标题】:ssm automation document input in AWS-RunShellScript not substituting variableAWS-RunShellScript 中的 ssm 自动化文档输入不替换变量
【发布时间】:2020-09-17 18:38:34
【问题描述】:

我正在尝试在 bash 中运行命令,其中部分命令被我在上一步中创建的变量替换,但是字符串替换不起作用。我已经用单引号、双引号等尝试了很多变体,但不能让它工作。

mainSteps:
  - name: getIps
    action: 'aws:invokeLambdaFunction'
    timeoutSeconds: 1200
    maxAttempts: 1
    onFailure: Abort
    inputs:
      FunctionName: Automation-GetIPs
      Payload: '{"asg": "Staging_web_ASG"}'
    outputs:
      - Name: asg_ips
        Selector: $.Payload.IPs
        Type: StringList
  - name: updatelsync
    action: 'aws:runCommand'
    timeoutSeconds: 1200
    inputs:
      DocumentName: AWS-RunShellScript
      InstanceIds:
        - '{{ InstanceID }}'
      Parameters:
        commands:
          - 'echo {{getIps.asg_ips}} > /root/asg_ips.test'

在上面的代码中。我在step1中设置了asg_ips,其OutputPayload如下:

{"Payload":{"IPs": ["172.xx.x.xxx", "172.xx.x.xxx"]},"StatusCode":200}

但是对于第二步的输入,显示如下...

{"commands":["echo {{getIps.asg_ips}} > /root/asg_ips.test"]}

我需要让它显示这样的东西......

{"commands":["echo ["172.xx.x.xxx", "172.xx.x.xxx"] > /root/asg_ips.test"]}

【问题讨论】:

  • 您对aws:invokeLambdaFunction 中的outputs 的使用有什么参考吗? aws docs 中没有这样的字段。因此,如果您执行'echo {{getIps.Payload}} > /root/asg_ips.test',它将起作用,但显然需要处理 asg_ips.test 文件以从完整有效负载中获取 ip 值。
  • 这看起来是我的问题。谢谢!
  • 没问题。如果您不介意,我会提供答案以供将来参考。
  • 嗨。只是想知道进展如何?

标签: amazon-web-services aws-ssm


【解决方案1】:

基于 cmets。

问题是由于在aws:invokeLambdaFunction SSM 操作中错误使用了outputs。具体来说,lambda 操作没有outputs 属性,如链接文档中所示:

name: invokeMyLambdaFunction
action: aws:invokeLambdaFunction
maxAttempts: 3
timeoutSeconds: 120
onFailure: Abort
inputs:
  FunctionName: MyLambdaFunction

相比之下,作为旁注,outputs 属性在aws:executeAwsApi 中有效。

因此,解决方法是直接引用 lambda动作返回的Payload

mainSteps:
  - name: getIps
    action: 'aws:invokeLambdaFunction'
    timeoutSeconds: 1200
    maxAttempts: 1
    onFailure: Abort
    inputs:
      FunctionName: Automation-GetIPs
      Payload: '{"asg": "Staging_web_ASG"}'
  - name: updatelsync
    action: 'aws:runCommand'
    timeoutSeconds: 1200
    inputs:
      DocumentName: AWS-RunShellScript
      InstanceIds:
        - '{{ InstanceID }}'
      Parameters:
        commands:
          - 'echo {{getIps.Payload}} > /root/asg_ips.test'

副作用是现在asg_ips.test 需要后处理以获取 IP 范围值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2021-04-11
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    相关资源
    最近更新 更多