search
LoginSignup
2
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

AWS Session Managerでinstance idをいちいち調べるのがだるい

aws ssm start-session は大変に便利なんだけど instance idをいちいち調べるのがだるい
サーバの名前からpecoで選んで繋げるようにする

#!/bin/bash

# 必要なら aws ec2 commandでリストアップする 基本重いからベタ書き
target_server_name=$(peco --prompt "Select server to start-session >" << EOS
server-a
server-b
server-c
EOS
)

if [ -z "${target_server_name}" ]; then
  exit
fi

instance_id=$(aws ec2 describe-instances --filter "Name=tag:Name,Values=${target_server_name}" --query "Reservations[].Instances[?State.Name == 'running'].InstanceId[]" --output text)

aws ssm start-session --target "$instance_id"

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
2
Help us understand the problem. What are the problem?