要件
- AWS Systems Manager Session Manager (以降AWS Session Manager)経由でEC2インスタンスにログインする。
- AWS Session Manager経由でログインするとssm-userでログインされてしまうが、IAMごとに別のユーザでログインする。
- rsync over sshできる。
前提となる環境
- AWS Session Managerがセットアップ済み
- 利用するregionにおいて、
Enable Run As support for Linux instancesにチェックが入っている

AWS上でのIAM設定
- policyを作成する。
- policyレベルでユーザを指定したい場合は、TagsにSSMSessionRunAsを追加してValueにユーザ名を指定する。
特定のインスタンスにSession Manager経由でログインできるようにする例
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ssm:StartSession",
"Resource": [
"arn:aws:ssm:*:*:document/AWS-StartSSHSession",
"arn:aws:ec2:ap-northeast-1:*:instance/i-xxxxxxxxxxxxxxxx"
]
},
{
"Effect": "Allow",
"Action": [
"ssm:DescribeSessions",
"ssm:GetConnectionStatus",
"ssm:DescribeInstanceProperties",
"ssm:DescribeInstanceInformation",
"ec2:DescribeInstances"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "ssm:TerminateSession",
"Resource": "arn:aws:ssm:*:*:session/${aws:username}-*"
}
]
}
- 個人のIAMアカウントを作成する
- 上で作ったpolicyをattachする。
- IAMユーザレベルでshellアカウントを指定する場合は
SSMSessionRunAsタグを設定して、値にログインさせたいsshアカウントを指定する
ログイン先端末での設定
- 公開鍵をログインしたいホスト上のログインさせたいユーザのauthorized_keysに登録しておく。
ログイン元端末での設定
- 必須プログラム
- aws-cli
- aws session manager plugin
aws configureなどを使ってcredentialを設定ファイルに記述しておく- AWS Session managerを使ってi-xxxxxxxxxxxxxxxxにssh接続テストしてみる
aws ssm start-session --target i-xxxxxxxxxxxxxxxx
~/.ssh/configへ以下の設定を追加する- もし、複数のprofileを使っている場合は
AWS_PROFILE環境変数に設定してsshコマンド経由でも同じawsのcredentialを使うように注意する必要がある。
- もし、複数のprofileを使っている場合は
host i-* mi-*
ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
- sshコマンドで接続してみる
ssh username@i-xxxxxxxxxxxxxxxx- SSMSessionRunAsで設定したユーザ名を指定する必要がある。
- rsyncコマンド経由でファイルを転送してみる
rsync -aruzv -e ssh <source> <username>@i-xxxxxxxxxxxxxxxxx:<destination>
考察
- rsyncをする場合はAWS Session Managerの設定と、sshの設定の両方が必要になる。


Comments