简体   繁体   English

aws cli:ssm启动会话无法将变量用作参数值

[英]aws cli: ssm start-session not working with a variable as a parameter value

I am trying to automate some part of my work by creating a bash function that let's me easily ssm into one of our instances. 我试图通过创建一个bash函数来使我的工作的一部分自动化,让我轻松地将其输入到我们的一个实例中。 To do that, I only need to know the instance id. 为此,我只需要知道实例ID。 Then I run aws ssm start-session with the proper profile. 然后,使用适当的配置文件运行aws ssm start-session Here's the function: 功能如下:

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
}

When I run this function, I always get an error like the following: 当我运行此函数时,总是出现如下错误:

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

However, then I take that instance id and run it from my terminal directly, it works: 但是,然后我获取该实例ID并直接从我的终端运行它,它的工作原理是:

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

Why is this? 为什么是这样?

You're sending instance ID as "i-0599385eb144ff93c" instead of i-0599385eb144ff93c . 您将实例ID发送为"i-0599385eb144ff93c"而不是i-0599385eb144ff93c

Modified function that should work - 修改后的功能应该起作用-

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM