【问题标题】:aws cli: ssm start-session not working with a variable as a parameter valueaws cli:ssm start-session 不使用变量作为参数值
【发布时间】:2023-04-09 17:00:05
【问题描述】:

我正在尝试通过创建一个 bash 函数来自动化我的某些工作,该函数让我可以轻松地 ssm 进入我们的一个实例。为此,我只需要知道实例 ID。然后我使用正确的配置文件运行aws ssm start-session。函数如下:

function ssm_to_cluster() {
  local instance_id=$(aws ec2 describe-instances --filters \
    "Name=tag:Environment,Values=staging" \
    "Name=tag:Name,Values=my-cluster-name" \
    --query 'Reservations[*].Instances[*].[InstanceId]' \
    | grep i- | awk '{print $1}' | tail -1)
  aws ssm start-session --profile AccountProfile --target $instance_id
}

当我运行这个函数时,我总是收到如下错误:

An error occurred (TargetNotConnected) when calling the StartSession operation: "i-0599385eb144ff93c" is not connected.

但是,然后我使用该实例 ID 并直接从我的终端运行它,它可以工作:

aws ssm start-session --profile MyProfile --target i-0599385eb144ff93c

这是为什么?

【问题讨论】:

    标签: bash amazon-web-services aws-cli ssm


    【解决方案1】:

    您将实例 ID 发送为 "i-0599385eb144ff93c" 而不是 i-0599385eb144ff93c

    修改后的功能应该可以工作 -

    function ssm_to_cluster() {
      local instance_id=$(aws ec2 describe-instances --filters \
        "Name=tag:Environment,Values=staging" \
        "Name=tag:Name,Values=my-cluster-name" \
        --query 'Reservations[*].Instances[*].[InstanceId]' \
        | grep i- | awk '{print $1}' | tail -1 | tr -d '"')
      aws ssm start-session --profile AccountProfile --target $instance_id
    }
    

    【讨论】:

    • 这行得通。然而,我注意到的一些有趣的事情是,当我直接运行aws ssm start-session 命令时,我可以在实例 id 字符串周围加上双引号(甚至是 2 个双引号)并且它可以工作。我想这可能与 bash 如何处理变量中的双引号而不是直接在命令行输入中处理有关。
    • 我也遇到了同样的问题,但不太了解解决方案。我的错误是:调用 StartSession 操作时发生错误(InternalServerError)(达到最大重试次数:4):java.io.IOException:com.ctc.wstx.exc.WstxUnexpectedCharException:非法字符((CTRL-CHAR,代码 27) ) 在 [row,col {unknown-source}]: [2,42]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 2019-09-02
    • 2020-01-21
    • 2016-12-18
    相关资源
    最近更新 更多