■ はじめに
最近、ちょくちょく使う aws ssm xxx について、 徐々にだが、メモっていく
目次
【1】API仕様 - aws ssm 1)aws ssm start-session 2)aws ssm describe-session 3)aws ssm terminate-session 【2】Session Manager プラグイン 1)インストール 【3】サンプル 1)Systems Manager でEC2に繋ぐ 2)ポートフォワードで繋ぐ 3)ポートフォワードで繋いでAirflow UIを表示する(Windows版)
【1】API仕様 - aws ssm
* 詳細は、以下の公式ドキュメントを参照
https://docs.aws.amazon.com/cli/latest/reference/ssm/?highlight=ssm
1)aws ssm start-session
* セッション開始 => ポートフォワーディングとかもできる
https://docs.aws.amazon.com/cli/latest/reference/ssm/start-session.html
2)aws ssm describe-sessions
* Session ID を取得できる
https://docs.aws.amazon.com/cli/latest/reference/ssm/describe-sessions.html
3)aws ssm terminate-session
* セッション終了
【2】Session Manager プラグイン
aws ssm start-session をするために、 Session Manager プラグインのインストールする必要がある => インストールしていないと、以下のエラーメッセージが表示される
エラーメッセージ例
SessionManagerPlugin is not found. Please refer to SessionManager Documentation here: http://docs.aws.amazon.com/console/systems-manager/session-manager-plugin-not-found
1)インストール
http://docs.aws.amazon.com/console/systems-manager/session-manager-plugin-not-found
Ubuntuの場合
https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/install-plugin-debian-and-ubuntu.html
# [1] Session Manager プラグイン deb パッケージをダウンロード curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb" # [2] インストールコマンドを実行 sudo dpkg -i session-manager-plugin.deb
https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/install-plugin-verify.html
# [3] 確認 session-manager-plugin # The Session Manager plugin is installed successfully. Use the AWS CLI to start a session.
【3】サンプル
1)Systems Manager でEC2に繋ぐ
#! /bin/bash # Set bation EC2 instance ID EC2_TARGET=i-0123456789ABCDEFG AWS_REGION=us-west-2 aws ssm start-session --region $AWS_REGION --target $EC2_TARGET
2)ポートフォワードで繋ぐ
#! /bin/bash # Set bation EC2 instance ID EC2_TARGET=i-0123456789ABCDEFG AWS_REGION=us-west-2 aws ssm start-session --region $AWS_REGION --target $EC2_TARGET ¥ --document-name AWS-StartPortForwardingSessionToRemoteHost ¥ --parameters '{"portNumber":["3306"],"localPortNumber":["13306"],"host":["remote-database-host-name"]}' ¥ > /dev/null 2>&1 & sleep 10 # Connect Web UI with port forward xdd open "http://localhost: 13306/" > /dev/null 2>&1
3)ポートフォワードで繋いでAirflow UIを表示する(Windows版)
@echo off setlocal REM Set bastion EC2 instance ID and region set EC2_TARGET=i-0123456789ABCDEFG set AWS_REGION=us-west-2 set MWAA_HOST=xxxxx set MWAA_NAME=xxxxxx REM Start port forwarding session start "" cmd /c ^ aws ssm start-session --region %AWS_REGION% --target %EC2_TARGET% ^ --document-name AWS-StartPortForwardingSessionToRemoteHost ^ --parameters "{\"portNumber\":[\"443\"],\"localPortNumber\":[\"18081\"],\"host\":[\"% MWAA_HOST% > NUL 2>&1 for /f "delims=" %%A in ('aws mwaa create-web-login-token --name %MWAA_NAME% --query WebToken --output text') do set WEB_TOKEN=%%A set MWAA_UI_URL="https://%MWAA_HOST%:18081/aws_mwaa/aws-console-sso?login=true#%WEB_TOKEN%" REM Wait for the port forwarding to be ready timeout /t 10 > NUL REM Open the web UI start "" "%MWAA_UI_URL%" endlocal
解説
* start "" を使ってバックグラウンド実行(非同期)を模倣 => start文は、Windows環境でプログラムやファイルを簡単に起動する * Bash の sleep 10 に相当するのは timeout /t 10。 * 出力の抑制(> /dev/null 2>&1)は > NUL 2>&1 に相当。 * xdd open のようなコマンドは Windows にはないため、 start "" を使って既定のブラウザで開く
使用上の注意
* AWS CLI v2 だけでなく SSM プラグイン を インストールしておく必要がある
https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/install-plugin-windows.html
関連記事
AWS Systems Manager ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/10/05/121119
AWS Systems Manager ~ Session Manager ~
https://dk521123.hatenablog.com/entry/2020/04/09/215235
バッチ ~ コマンド結果を変数に格納する ~
https://dk521123.hatenablog.com/entry/2023/02/24/000000