AWS CLIでSSOを使用しつつ、SSM Session ManagerでSSHポートフォワードしてみる
こんにちは、まるとです。
セキュリティの観点からAWS CLI用にアクセスキーを発行せず、AWS IAM Indentity CenterでSSOをしつつ、EC2インスタンスにSSM経由でアクセスする場合、どういった形になるのかやってみました。
今回はscpなどを使用したいので、SSHポートフォワードでやっていきたいと思います。
試す構成図
プライベートサブネットにEC2を配置し、エンドポイント経由でSystems Manager側と通信できるようにします。
その上で、IAM Indentity CenterでSSOをしつつ、EC2にSSM経由で接続します。
やってみる
本記事の前提条件
- IAM Identity CenterでSSOの設定を行い、AWSアクセスポータル経由でAWSアカウントにアクセスできること
- AWS CLIがインストールされていること
- AWS CLI用のSession Managerがインストールされていること
- AWS CLIでSession Managerのセッションを開始するのに必要です
- https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html
1. AWS CLIでSSOの設定をする
AWS CLIでaws configure SSO
を実行し、SSOの設定を行います。
maruto-demo@DEMO-PC ~ % aws configure sso
SSO session name (Recommended): maruto-demo # SSOセッション名(任意の文字列)
SSO start URL [None]: https://<文字列>.awsapps.com/start/# # SSO開始URL(AWSアクセスポータルから取得)
SSO region [None]: ap-northeast-1 # SSOのリージョン (IAM Identity Centerのリージョン)
SSO registration scopes [sso:account:access]: # 空でOK
Attempting to automatically open the SSO authorization page in your default browser.
If the browser does not open, open the following URL:
<SSOアクセス確認URL>
SSO start URL
、SSO region
はAWSアクセスポータルにアクセスし、アカウントのロール名横にある「アクセスキー」を選択すると取得できます。
入力していくと、途中、ブラウザでアクセスを承認するか聞かれますので、「アクセスを許可」を選択します。
アクセスを許可後、権限があるAWSアカウントが複数存在する場合、操作対象を聞かれます。
矢印キーで選択します。
選択後はaws configure
と同じようにデフォルトのリージョン、出力フォーマット、プロファイル名を聞かれるので入力していきます。
※プロファイル名は区別がつきやすい命名にすることを推奨します。(デフォルトは<ロール名>-<AWSアカウントID>
)
There are 2 AWS accounts available to you.
Using the account ID XXXXXXXXXXXX # 複数AWSアカウントがある場合は選択
The only role available to you is: AdministratorAccess
Using the role name "AdministratorAccess"
Default client Region [ap-northeast-1]: # デフォルトのリージョン
CLI default output format (json if not specified) [json]: # 出力フォーマット
Profile name [AdministratorAccess-XXXXXXXXXXXX]: # プロファイル名(分かりやすい命名)
To use this profile, specify the profile name using --profile, as shown:
aws sts get-caller-identity --profile kapuverse-demo-member
これでSSOの設定は完了です。
2. 作成されたプロファイルで通常通り使用する
あとは通常通り、AWS CLIを実行するだけです。
今回はタイトルにある通り、EC2にSSM Session ManagerでSSHポートフォワードしてみます。
aws ssm start-session --profile <作成されたプロファイル名> \
--target <EC2インスタンスID> \
--document-name AWS-StartPortForwardingSession \
--parameters '{"portNumber":["<EC2側ポート>"],"localPortNumber":["<ローカルマシンで受け付ける任意のポート番号>"]}'
実際に実行すると以下の表示が得られます。
maruto-demo@DEMO-PC ~ % aws ssm start-session --profile kapuverse-demo-member \
--target i-0897e4365dc69c1fd \
--document-name AWS-StartPortForwardingSession \
--parameters '{"portNumber":["22"],"localPortNumber":["50022"]}'
Starting session with SessionId: account-1@example.com-xxxxxxxxxxxxxxxxxxxxxxxx.
Port 50022 opened for sessionId account-1@example.com-xxxxxxxxxxxxxxxxxxxxxxxx.
Waiting for connections...
この状態で、EC2にSSHをしてみます。SSH接続をする際には下記コマンドで実行します。
ssh -i <鍵ファイル> ユーザ名@localhost -p <ローカルマシン側受付ポート>
やってみます。
maruto-demo@DEMO-PC ~ % ssh -i demo-key.pem ec2-user@localhost -p 50022
, #_
~\_ ####_ Amazon Linux 2023
~~ \_#####\
~~ \###|
~~ \#/ ___ https://aws.amazon.com/linux/amazon-linux-2023
~~ V~' '->
~~~ /
~~._. _/
_/ _/
_/m/'
[ec2-user@ip-10-0-130-105 ~]$
正常に接続できました。試しにscpを利用してファイルを転送してみます。
maruto-demo@DEMO-PC ~ % scp -i demo-key.pem -P 50022 10mfile.bin ec2-user@localhost:~/.
maruto-demo@DEMO-PC ~ % ssh -i demo-key.pem ec2-user@localhost -p 50022
, #_
~\_ ####_ Amazon Linux 2023
~~ \_#####\
~~ \###|
~~ \#/ ___ https://aws.amazon.com/linux/amazon-linux-2023
~~ V~' '->
~~~ /
~~._. _/
_/ _/
_/m/'
[ec2-user@ip-10-0-130-105 ~]$ ls -l
total 10240
-rw-------. 1 ec2-user ec2-user 10485760 Sep 25 06:46 10mfile.bin
正常に転送できました。
SSHで接続する際に都度aws ssm start-sessionコマンドを実行したくない場合
現状ではSSHで接続する際に、都度aws ssm start-session
を実行してからSSHで接続する必要があります。
それだと手間なので、.ssh/config
に以下の設定を投入します。(Linux/macOS、Windowsは参考元をご確認ください。)
Host i-* mi-*
ProxyCommand sh -c "aws ssm start-session --profile <プロファイル名> --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
User <ユーザ名>
これでホスト名にインスタンスIDを指定することで接続できます。
maruto-demo@DEMO-PC ~ % ssh -i demo-key.pem ec2-user@i-0897e4365dc69c1fd
, #_
~\_ ####_ Amazon Linux 2023
~~ \_#####\
~~ \###|
~~ \#/ ___ https://aws.amazon.com/linux/amazon-linux-2023
~~ V~' '->
~~~ /
~~._. _/
_/ _/
_/m/'
Last login: Thu Sep 25 06:47:06 2025 from 127.0.0.1
終わりに
SSOが必要な環境でも特に難しい設定をせずに、AWS CLIを利用することができることがわかりました。
特に永続的なアクセスキーをセキュリティの観点から発行したくない場合でもSSOにより、一時的な認証情報を利用できるため嬉しい方も多いのではないでしょうか。
参考文献