0
1

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 3 years have passed since last update.

AWS Session Managerを使って踏み台にする

Last updated at Posted at 2021-04-01

AWS SystemManagerには、踏み台として使えるSession Managerがあります。
こちらを利用する方法をまとめます。

1.EC2にIAMロールを追加する。

まずは、EC2にSession Managerが使えるようにIAMポリシーを設定したIAMロールの割当が必要です。
EC2に割り当てたIAMロールに対して、下記のIAMポリシーを割り当てます。
・AmazonSSMManagedInstanceCore
・CloudWatchAgentServerPolicy
・コマンド結果を S3 へ出力するためのカスタムポリシー
image.png
(引用: Applying managed instance policy best practices)
https://aws.amazon.com/jp/blogs/mt/applying-managed-instance-policy-best-practices/

コマンド結果を S3 へ出力するためのカスタムポリシーの例

先に、SSMのログを保存するためのS3バケットを作っておいてください。
バケットのバージョニングはONにしておくと、削除された際に復元できるようです。
暗号化はONにしておいてください。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:PutObjectAcl", 
                "s3:GetEncryptionConfiguration" 
            ],
            "Resource": [
                "arn:aws:s3:::(バケット名)/*",
                "arn:aws:s3:::(バケット名)" 
            ]
        }
    ]
}

(引用: Task 1: (Optional) Create a custom policy for S3 bucket access)
https://docs.aws.amazon.com/systems-manager/latest/userguide/setup-instance-profile.html#instance-profile-custom-s3-policy

2.EC2にSessionManagerAgentをインストールする

以下のIAMから作成されたEC2には既にSessionManagerAgentがインストールされています。
・Amazon Linux
・Amazon Linux 2
・Amazon Linux 2 ECS-Optimized AMIs
・Ubuntu Server 16.04, 18.04, and 20.04
その他のIAMから作成されたEC2には、自分でインストールする必要があります。
下記を参照してインストールしてください。

Linux 用の EC2 インスタンスに手動で SSM エージェント をインストールする
https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/sysman-manual-agent-install.html

3.SSMのログのS3への保存設定を行う

「AWSコンソールTOP」→「SystemManager」→「SessionManager」→「設定」で
S3 loggingの設定を行います。
image.png

4.接続ユーザーのIAMにSessionManagerに接続できるようにIAMポリシーを割り当てる

接続はIAM単位でコントロールできます。IAMユーザーに下記のIAMポリシーを割り当てて、指定したEC2インスタンスのみ接続できるようにします。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ssm:StartSession"
            ],
            "Resource": [
                "arn:aws:ec2:ap-northeast-1:(AWSアカウントID):instance/(インスタンスID)",
                "arn:aws:ssm:*:*:document/SSM-SessionManagerRunShell",
                "arn:aws:ssm:*:*:document/AWS-StartSSHSession" //SSH接続したいときのみ指定する。
            ],
            "Condition": {
                "BoolIfExists": {
                    "ssm:SessionDocumentAccessCheck": "true"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "ssm:DescribeSessions",
                "ssm:GetConnectionStatus",
                "ssm:DescribeInstanceProperties",
                "ec2:DescribeInstances"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "ssm:TerminateSession"
            ],
            "Resource": [
                "arn:aws:ssm:*:*:session/${aws:username}-*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "kms:GenerateDataKey"
            ],
            "Resource": "*"
        }
    ]
}

クイックスタート Session Manager のデフォルト IAM ポリシー
https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/getting-started-restrict-access-quickstart.html

5.SessionManagerPluginをPCにインストールする

PCから接続するためには、AWS-CLIとSessionManagerPluginのインストールが必要です

AWS-CLIをインストールして、AWS-CLIの設定ファイルと認証情報ファイルの設定を先に作成してください。

AWS CLI のインストール、更新、アンインストール
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-chap-install.html

設定ファイルと認証情報ファイルの設定
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-files.html

Ubuntuの場合

$ curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb"
$ sudo dpkg -i session-manager-plugin.deb

Macの場合

$ 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

その他詳しくは下記をご覧ください。
https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html

6.SessionManagerで接続する

PCから下記コマンドで接続ができます。

$ aws ssm start-session --target (インスタンスID) --profile hogehoge --region ap-northeast-1 --document-name SSM-SessionManagerRunShell

こちらは、S3などにログが出力されます。
接続終わったら、exitで抜けてください。

7.SessionManagerでSSHで接続する。

PC→A→BというふうにSSHポートフォワーディングなどを使いたい場合などは、SSH接続を利用します。
こちらは、S3などにログが出力されません。

SSH接続設定

$ vi ~/.ssh/config

# SSH over Session Manager
host (AのインスタンスID)
  IdentityFile ~/.ssh/Aの鍵.pem
  User ec2-user
  ProxyCommand sh -c ""aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p' --region ap-northeast-1 --profile hogehoge""
Host            B
 HostName       (BのIP)
 User           ec2-user
 IdentityFile   ~/.ssh/Bの鍵.pem
 ProxyCommand   ssh -W %h:%p (AのインスタンスID)

※profileのhogehogeはご自身のawsプロファイル名に変更してください。
※sshの鍵はパーミッションを600にしてください。

Aに接続するとき

$ ssh (AのインスタンスID)

接続終わったら、exitで抜けてください。

Bに接続するとき

$ ssh B

接続終わったら、exitで抜けてください。

PC→A→RDSに接続するとき

$ ssh (AのインスタンスID) -L 9090:DBインスタンス名.ap-northeast-1.rds.amazonaws.com:3306

→これでトンネリング接続している際に、MySQL WorkbenchなどのGUIで接続できます。
image.png

→もしくは別のターミナルからmysqlコマンドでも接続できます。

mysql -u (DBユーザー名)  h 127.0.0.1 -P 9090 -p(DBパスワード)

接続終わったら、ctrl+cで抜けてください。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?