9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AWS SessionManagerによる接続とエラーについて

Last updated at Posted at 2021-09-08

概要

  • SessionManager(以下SSM)で接続する
  • 既にシステム側にはSSMは設定されている状態
  • なのでシステム側設定は他の記事へどうぞ
  • 主にローカルPC側の設定をメモ
  • 接続うまく行かなかったエラーなどもメモ
  • ローカルPCはMacの場合です

詳細

ローカル設定

  • ~/.ssh/configの設定
ローカルPC(Macの設定)

% cat ~/.ssh/config
host web
    HostName i-02b08efa398999XXX
    Port 22
    User ubuntu
    IdentityFile ~/.ssh/id_rsa
    ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

ローカルから接続確認

  • AWSコマンドがない場合のエラー
ローカルPC(Macの設定)
% ssh web

sh: aws: command not found

% curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
:
installer: The install was successful.
  • aws configureしてない場合のエラー
ローカルPC(Macの設定)
% ssh web
You must specify a region. You can also configure your region by running "aws configure".
kex_exchange_identification: Connection closed by remote host

% aws configure
AWS Access Key ID [None]: AKIXXXXXXXXXXXXXXXXX
AWS Secret Access Key [None]: BEfXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Default region name [None]: ap-northeast-1
Default output format [None]: json

  • セッションマネージャプラグインがローカルに入ってない場合のエラー
ローカルPC(Macの設定)
% ssh web

An error occurred (TargetNotConnected) when calling the StartSession operation: i-06d97b1XXXXXXX is not connected.
kex_exchange_identification: Connection closed by remote host

% aws --version
aws-cli/2.2.36 Python/3.8.8 Darwin/20.6.0 exe/x86_64 prompt/off

% session-manager-plugin --version
zsh: command not found: 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
Creating install directories: /usr/local/sessionmanagerplugin/bin
Creating Symlink from /usr/local/sessionmanagerplugin/bin/session-manager-plugin to /usr/local/bin/session-manager-plugin
Installation successful!

$ % session-manager-plugin --version
1.2.245.0

参考:公式

  • 変わらず接続できない
ローカルPC(Macの設定)
% ssh web

An error occurred (TargetNotConnected) when calling the StartSession operation: i-06d97b1e8ac1xxxxx is not connected.
kex_exchange_identification: Connection closed by remote host
% ssh -vvv web
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/user/.ssh/config
debug1: /Users/user/.ssh/config line 1: Applying options for iryoo-biz-mng-1a
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Executing proxy command: exec sh -c "aws ssm start-session --target i-06d97b1e8ac1xxxxx --document-name AWS-StartSSHSession --parameters 'portNumber=22'"
debug1: identity file /Users/user/.ssh/id_rsa type -1
debug1: identity file /Users/user/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1

An error occurred (TargetNotConnected) when calling the StartSession operation: i-06d97b1e8ac1xxxxx is not connected.
kex_exchange_identification: Connection closed by remote host

  • AWS側権限が不足している場合のエラー
$ ssh web

An error occurred (403) when calling the StartSession operation: Server authentication failed: <UnauthorizedRequest xmlns=""><message>Forbidden.</message></UnauthorizedRequest>

//AWS権限を確認してみる
$ aws sts get-caller-identity

An error occurred (SignatureDoesNotMatch) when calling the GetCallerIdentity operation: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

//AWSSecretAccessKeyがおかしいとのこと、確認してみる
$ printenv | grep AWS
AWS_ACCESS_KEY_ID=*********************
AWS_SECRET_ACCESS_KEY=*********************

//確認した所間違っていた
正しい値を入れ直す

AWS_SECRET_ACCESS_KEY=*********************

//再度確認。権限の確認ができた。
$ aws sts get-caller-identity                                    
{
    "UserId": "*******************",
    "Account": "***************",
    "Arn": "arn:aws:iam::*************:user/********username"
}

//再度ログイン。成功。
$ ssh web

参考:公式トラブルシューティング

コンソールから接続確認

  • 上記対応でも改善しないためコンソールから確認

image.png

  • 以下の画面からエージェントが導入されているか確認

image.png

image.png

  • SSMエージェントが導入されていないまたは有効でない場合はエラーとなる
  • はじめにコンソールでSSMエージェントの状態を確認したほうが良い
  • 自分でSSM環境を一から作った場合はこういったトラブルはあまりなさそう
9
11
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
9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?