LoginSignup
3
2

More than 1 year has passed since last update.

AWS Session Managerを使用したSSHログイン

Last updated at Posted at 2022-07-27

AWS System Managerとは

EC2インスタンスやオンプレミスサーバー、仮想マシン (VM) などを管理できます。
Session Managerを使用すると安全にノードの管理が実現でき、インバウンドポートを開いたり、踏み台ホストを維持したり、SSHキーの管理したりする必要がなくなります。

Session Managerのメリット

  • IAM ポリシーを使用してマネージドノードの一元化アクセス制御
  • インバウンドポートを開いたり、踏み台ホストやSSH キーを管理したりする必要がなくなる
  • コンソールと CLI からワンクリックでマネージドノードへアクセス
  • ログ記録と監査のセッションアクティビティ

Session Managerの導入

  1. SSM Agentのインストール
    EC2インスタンスにssm-agentがインストールされている必要があります。Amazon Linuxを使用している場合は最初から入っていますが、入っていない場合は以下を参照してインストールを行ってください。
    https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/ssm-agent.html
  2. ロールの作成しインスタンスにアタッチ
    AmazonEC2RoleforSSMというデフォルトポリシーを付与したIAMロールを作成し、対象のEC2インスタンスにアタッチをして再起動を行ってください。
  3. EC2のインスタンスIDを確認
aws ec2 describe-instances | jq '.Reservations[].Instances[] | {InstanceId, PrivateIpAddress, InstanceName: (.Tags[] | select(.Key==\"Name\").Value)}'

{
  "InstanceId": "i-xxxxxxxxxxxxxxxxx",
  "PrivateIpAddress": "10.x.xx.xxx",
  "InstanceName": "production"
}
{
  "InstanceId": "i-xxxxxxxxxxxxxxxxx",
  "PrivateIpAddress": "10.x.xx.xxx",
  "InstanceName": "staging"
}
{
  "InstanceId": "i-xxxxxxxxxxxxxxxxx",
  "PrivateIpAddress": "10.x.xx.xxx",
  "InstanceName": "develop"
}

※1. AWSコマンドが使用できること
※2. jqコマンドが使用できること
4. SSHログイン
~/.ssh/configに以下を記載

# SSMでのSSH
Host i-* mi-*
  ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

sshを行う(3で取得したログインしたいインスタンスIDを指定する)

ssh i-xxxxxxxxxxxxxxxxx
3
2
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
2