LoginSignup
3
1

More than 3 years have passed since last update.

SSMって便利。でも管理が大変。なのでToolを作った話

Posted at

はじめに

各EC2のユーザー管理がしんどいしんどい。

だってインスタンスが20~30種類あって、インスタンス毎にユーザーが独立されて管理されているんですぜ。

そもそも、EC2にユーザーいる?sshする必要ってある?しなくても済むように設計するべきだよね。
接続する時はSSMでいいよね。

と言う訳でそんな構成に。
したはよいが、今度はSSMする時に

「え〜っと。どうやるんだっけ?」

と言う経年劣化による記憶容量がScaleIn、コマンドが思い出せない事案が発生。

ならToolを作りましょう。

構築

コマンドを実装

$ vim ssm.sh
ssm.sh
#!/bin/sh
AWS_CONFIG='~/.aws/config'
profile=''
function showProfile() {
  echo '# Input profile.'
  echo '## profile list.'
  cat ~/.aws/config | grep profile
  echo '##'
  read input
  if [ -z $input ] ; then
    echo 'Done.'
    exit 1
  else
    profile=$input
  fi
}
function ssmInstance() {
  echo '# Input ResourceId.'
  echo '## ResourceId list.'
  aws ec2 describe-tags --profile $profile  --output table --filters "Name=resource-type,Values=instance"
  echo '## ResourceId list.'
  read input
  if [ -z $input ] ; then
    echo 'Done.'
    exit 1
  else
    aws ssm start-session --target $input --profile $profile
  fi
}
showProfile
ssmInstance

コマンドを移動

$ chmod 744 ssm.sh
$ mv ssm.sh /usr/local/bin/ssm

以上。

実際に使ってみる。

$ ssm
# Input profile.
## profile list.
[profile profile-dev]
[profile profile-prod]
[profile teck]
[profile pricate-dev]
##
-- 接続対象のprofileを入力
# Input ResourceId.
## ResourceId list.
------------------------------------------------------------------------------------------
|                                      DescribeTags                                      |
+----------------------------------------------------------------------------------------+
||                                         Tags                                         ||
|+----------------------------+-----------------------+---------------+-----------------+|
||             Key            |      ResourceId       | ResourceType  |      Value      ||
|+----------------------------+-----------------------+---------------+-----------------+|
||  Name                      |  i-xxx                |  instance     |  webapp_1       ||
||  Name                      |  i-xxx                |  instance     |  webapp_2       ||
||  Name                      |  i-xxx                |  instance     |  batch          ||
||  Name                      |  i-xxx                |  instance     |  nat            ||
|+----------------------------+-----------------------+---------------+-----------------+|
## ResourceId list.
-- 接続対象のResourceIdを入力
Starting session with SessionId: xxxxxxxx
sh-4.2$
-- 接続!!!

終わりに

EC2のユーザーを脳死で追加してしまうと、いざと言うときに管理や棚卸しがとても大変。
なのでそもそも「EC2に接続しないように・させないように」する設計が大事。

※ ECS / Fargateを使えば万事解決

3
1
0

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
  3. You can use dark theme
What you can do with signing up
3
1