LoginSignup
0
0

ssm-agentを利用したportforwardingの設定

Posted at

ssm-agentを利用したportfowerdingの利用方法は以下の通りです

IAMの権限に関して

IAMのSSM-userには下記ロールを割り当てておくこと

・AmazonEC2ReadOnlyAccess
・AmazonSSMFullAccess

設定手順

①Session Manager plugin のインストール
 ※下記はローカルで実行します。

curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/mac/sessionmanager-bundle.zip" -o "sessionmanager-bundle.zip"
unzip sessionmanager-bundle.zip
sudo ./sessionmanager-bundle/install -i /usr/local/sessionmanagerplugin -b /usr/local/bin/session-manager-plugin

②aws accountのcredentialsをローカルに設定します
設定方法:

aws configure --profile hoge
AWS Access Key ID [None]: ****************
AWS Secret Access Key [None]: ****************
Default region name [None]: ap-northeast-1
Default output format [None]:

③portfowerdingの実施

aws ssm start-session --profile hoge \
    --target i-123456789 \
    --document-name AWS-StartPortForwardingSessionToRemoteHost \
    --parameters '{"host":["aaa.bbb.ap-northeast-1.rds.amazonaws.com"],"portNumber":["3306"], "localPortNumber":["3306"]}'

上記はprofile hogeを利用して、インスタンス( i-123456789)を踏み台にして、RDS(aaa.bbb.ap-northeast-1.rds.amazonaws.com)にportfowerdingするコマンドになります

④portfowerding後、RDSへ接続を行う
上記、コマンドを実施しているターミナルとは別に新規のターミナルを開いて、以下のコマンドでRDSに接続出来ます

ローカルにて別のコマンドプロンプト

mysql -u admin -p -h 127.0.0.1 -P 3306

上記のコマンドで注意する点は、hostの127.0.0.1として指定しましょう。

備忘

注:EC2のSSMエージェントが古い場合、下記エラーが出力されます。

An error occurred (BadRequest) when calling the StartSession operation: The version of the SSM agent installed on this instance doesn't support port forwarding to remote hosts. Install the latest version of the SSM agent to use port forwarding sessions to remote hosts.

参考資料:
https://dev.classmethod.jp/articles/aws-ssm-remote-host-port-forward-ssmversion/

対象のEC2でSSMエージェントのバージョンを確認
対象のEC2にログインしSSMエージェントのバージョンを確認してみます。
SSMエージェントのバージョンが3.1.1374.0 以降を満たしていません。

Systems Manager Run CommandでSSMエージェントをアップデート
Systems Managerのダッシュボードから「Run Command」を選択し「AWS-UpdateSSMAgent」を
検索・選択します。
※「Run Command」を使うので、System ManagerでEC2インスタンスを管理できている前提です。
※オプションでSSMエージェントのバージョンの指定をしていないので最新バージョンのSSMエージェントにアップデートされます。

対応前:

[root@ip-10-xx-x-xx bin]# yum info amazon-ssm-agent
Available Packages
Name        : amazon-ssm-agent
Arch        : x86_64
Version     : 3.1.1732.0
Release     : 1.amzn2
Size        : 24 M
Repo        : amzn2-core/2/x86_64
Summary     : Manage EC2 Instances using SSM APIs
URL         : http://docs.aws.amazon.com/ssm/latest/APIReference/Welcome.html
License     : ASL 2.0
Description : This package provides Amazon SSM Agent for managing EC2 Instances using SSM APIs

対応後:

[root@ip-10-xx-x-xx bin]# yum info amazon-ssm-agent
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
247 packages excluded due to repository priority protections
Installed Packages
Name        : amazon-ssm-agent
Arch        : x86_64
Version     : 3.2.1041.0
Release     : 1
Size        : 104 M
Repo        : installed
Summary     : Manage EC2 Instances using SSM APIs
URL         : http://docs.aws.amazon.com/ssm/latest/APIReference/Welcome.html
License     : Apache License, Version 2.0
Description : This package provides Amazon SSM Agent for managing EC2 Instances using SSM APIs

自動でportfowerdingしたい場合は下記をご活用ください。

0
0
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
0
0