SSM Session Managerのポートフォワード先のIAMでの制限方法を確認してみた

SSM Session Managerのポートフォワード先のIAMでの制限方法を確認してみた

AWS SSM Session Managerでユーザごとにポートフォワーディングの接続先を制限する方法を検証しました。カスタムドキュメントとIAMポリシーの組み合わせで、どのように制御が機能するのかを確認しています。
2026.12.04

こんにちは、連山です。
突然ですが、AWS Systems Manager Session Manager は、SSM agentがインストールされたインスタンスに対してセッションを開始できる、AWS Systems Managerのツールの一つです。

https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/session-manager.html

Session Manager を使用すると、セキュリティ面では以下のメリットがあります。

  • インスタンス側のSecurity Groupでインバウンドポートを開く必要がない
  • SSH鍵・証明書・踏み台サーバの管理が必要ない
  • 接続権限をIAMで管理できる
  • 接続記録がcloudtrailに残る

この記事では、このSSM Session Managerでポートフォワーディングを行う際に、ユーザごとに接続を許可するポートを分ける方法を確かめます。

結論

AWSデフォルトのAWS-StartPortForwardingSessionは、ポートによる制限がなく、任意のポートへの接続が可能です。

以下の対応により、ユーザごとに接続可能ポートを分けることができました。

  • 許可ポートごとにカスタムのSSM ドキュメントを作成する
  • カスタムSSMドキュメントごとにIAMポリシーを分ける

例として、3389番ポート(RDP)だけを許可するドキュメントはこちらです。

{
  "schemaVersion": "1.0",
  "description": "test document for 3389",
  "sessionType": "Port",
  "parameters": {
    "localPortNumber": {
      "type": "String",
      "description": "local port 0-65535. 0 = random.",
      "allowedPattern": "^([0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$",
      "default": "0"
    }
  },
  "properties": {
    "portNumber": "3389",
    "type": "LocalPortForwarding",
    "localPortNumber": "{{ localPortNumber }}"
  }
}

このドキュメントだけに ssm:StartSession を許可するIAMポリシーを割り当てれば、利用者は3389番ポートにしか転送できなくなります。


AWS SSM Session Document とは?

SSM Documentは、Manager がマネージドインスタンスで実行するアクションを定義したものです。
Automation ランブック、コマンドドキュメント等いくつもの種類がありますが、今回使用するSession Documentは、Session Managerが開始するセッションのタイプを決定します。

https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/documents.html

この記事で確認すること

AWS管理のSession Document AWS-StartPortForwardingSession をベースに、ポートを3389番に固定したカスタムドキュメントを作成し、「どのドキュメントを許可したポリシーで」「どのドキュメントを指定して」セッションを開始するとどうなるかを確かめます。

パターン IAMポリシーで許可するドキュメント セッション開始時に指定するドキュメント 確認すること
① AWS-StartPortForwardingSession AWS-StartPortForwardingSession 任意のポートに接続できること
②-A カスタムドキュメント(3389番に固定) カスタムドキュメント(3389番に固定) 3389番で接続でき、他のポートでは接続できないこと
②-B カスタムドキュメント(3389番に固定) AWS-StartPortForwardingSession 権限エラーになること
②-C カスタムドキュメント(3389番に固定) 指定なし 接続できないこと

①でデフォルトドキュメントの挙動を確認します。
②-A〜Cで「カスタムドキュメントだけを許可すれば、正規の使い方(A)以外の抜け道(B・C)は塞がるか」を確認します。


検証環境の構成

  • リージョン: 東京(ap-northeast-1)

EC2

  • Amazon Linux 2023 / t3.micro
  • プライベートサブネットに配置し、パブリックIPなし・セキュリティグループのインバウンドルールなし
  • VPCエンドポイント(ssm / ssmmessages)経由で Session Manager に接続
  • 3389番と8080番で python3 -m http.server を起動。
    • curl すると port 3389 / port 8080 と返すようにしています
    • 3389番を「許可したいポート」、8080番を「許可したくないポート」として扱います

IAM

権限を持たないIAMユーザ(aws login でログイン)から、パターン毎にスイッチロールする。

  • パターン①用ロール: ssm-port-control-allports(AWS-StartPortForwardingSession を許可)
    以下のポリシーを許可
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowStartSession",
      "Effect": "Allow",
      "Action": "ssm:StartSession",
      "Resource": [
        "arn:aws:ec2:ap-northeast-1:123456789012:instance/i-xxxxxxxxxxxxxxxxx", # 接続先のインスタンス
        "arn:aws:ssm:ap-northeast-1::document/AWS-StartPortForwardingSession" # 許可するドキュメント(AWS管理)
      ]
    },
    {
      "Sid": "AllowManageOwnSessions",
      "Effect": "Allow",
      "Action": ["ssm:TerminateSession", "ssm:ResumeSession"],
      "Resource": "arn:aws:ssm:ap-northeast-1:123456789012:session/*"
    }
  ]
}
  • パターン②用ロール: ssm-port-control-portlimited(カスタムドキュメントのみ許可)
    以下のポリシーを許可
{
"Version": "2012-10-17",
"Statement": [
  {
    "Sid": "AllowStartSession",
    "Effect": "Allow",
    "Action": "ssm:StartSession",
    "Resource": [
      "arn:aws:ec2:ap-northeast-1:123456789012:instance/i-xxxxxxxxxxxxxxxxx", # 接続先のインスタンス
      "arn:aws:ssm:ap-northeast-1:123456789012:document/ssm-port-control-PortForward-RDP-3389" # 許可するドキュメント(カスタムドキュメント)
    ]
  },
  {
    "Sid": "AllowManageOwnSessions",
    "Effect": "Allow",
    "Action": ["ssm:TerminateSession", "ssm:ResumeSession"],
    "Resource": "arn:aws:ssm:ap-northeast-1:123456789012:session/*"
  }
]
}

ローカル環境

$ aws --version
aws-cli/2.38.1 Python/3.14.6 Darwin/27.2.0 script-exe/arm64
$ session-manager-plugin --version
1.2.896.0

スイッチロール用のプロファイル

~/.aws/config に、各ロールへスイッチロールするプロファイルを用意しました。
ssm-test は aws login で作成した、検証用IAMユーザのプロファイルです。

[profile ssm-test]
region = ap-northeast-1
login_session = arn:aws:iam::123456789012:user/aws_login_test

[profile ssm-allports]
role_arn = arn:aws:iam::123456789012:role/ssm-port-control-allports
source_profile = ssm-test
role_session_name = ssm-allports

[profile ssm-portlimited]
role_arn = arn:aws:iam::123456789012:role/ssm-port-control-portlimited
source_profile = ssm-test
role_session_name = ssm-portlimited

パターン①: AWS-StartPortForwardingSession を許可している場合

AWS管理のセッションドキュメント AWS-StartPortForwardingSession の中身は以下の通りです。

{
  "schemaVersion": "1.0",
  "description": "Document to start port forwarding session over Session Manager",
  "sessionType": "Port",
  "parameters": {
    "portNumber": {
      "type": "String",
      "description": "(Optional) Port number of the server on the instance",
      "allowedPattern": "^([1-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$",
      "default": "80"
    },
    "localPortNumber": {
      "type": "String",
      "description": "(Optional) Port number on local machine to forward traffic to. An open port is chosen at run-time if not provided",
      "allowedPattern": "^([0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$",
      "default": "0"
    }
  },
  "properties": {
    "portNumber": "{{ portNumber }}",
    "type": "LocalPortForwarding",
    "localPortNumber": "{{ localPortNumber }}"
  }
}

ポートフォワード先、ローカルポートが共に変数化されており、セッション開始時に--parameters で渡す仕組みになっています。

※ 何も指定しなかった場合はデフォルトで、ポートフォワード先は80番、ローカルポートは1~65535からランダムに選ばれます。

任意のポートに接続できることを確認

3389番 ポート

$ aws ssm start-session --profile ssm-allports \
    --target i-xxxxxxxxxxxxxxxxx \
    --document-name AWS-StartPortForwardingSession \
    --parameters "portNumber=3389,localPortNumber=13389"

Starting session with SessionId: ssm-allports-xxxxxxxxxxxxxxxxxxxxxxxxxx
Port 13389 opened for sessionId ssm-allports-xxxxxxxxxxxxxxxxxxxxxxxxxx.
Waiting for connections...

別のターミナルから、ローカルの13389番に curl します。

$ curl -s localhost:13389
port 3389

3389番ポートに接続できました。

8080番ポート

同じロールのまま、portNumber を8080に変えてみます。

$ aws ssm start-session --profile ssm-allports \
    --target i-xxxxxxxxxxxxxxxxx \
    --document-name AWS-StartPortForwardingSession \
    --parameters "portNumber=8080,localPortNumber=18080"

Starting session with SessionId: ssm-allports-xxxxxxxxxxxxxxxxxxxxxxxxxx
Port 18080 opened for sessionId ssm-allports-xxxxxxxxxxxxxxxxxxxxxxxxxx.
Waiting for connections...
$ curl -s localhost:18080
port 8080

8080番ポートにも接続できました。

IAMポリシーではドキュメントに渡すパラメータの値を条件にできないので、AWS-StartPortForwardingSession を許可している限り、ポート単位の制御はできません。

パターン② カスタムドキュメントを許可している場合

パターン②-A: カスタムドキュメントを指定

AWS-StartPortForwardingSession をベースに以下のカスタムドキュメントを作成しました。

{
  "schemaVersion": "1.0",
  "description": "test document for 3389",
  "sessionType": "Port",
  "parameters": {
    "localPortNumber": {
      "type": "String",
      "description": "local port 0-65535. 0 = random.",
      "allowedPattern": "^([0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$",
      "default": "0"
    }
  },
  "properties": {
    "portNumber": "3389",
    "type": "LocalPortForwarding",
    "localPortNumber": "{{ localPortNumber }}"
  }
}

`AWS-StartPortForwardingSession` との違いは以下の2点です。

- `parameters` から `portNumber` を削除
- `properties` の `portNumber` を `"3389"` で固定

※ ローカルポートを指定しなかった場合は、`AWS-StartPortForwardingSession` と同様にランダムに選ばれます。

IAMポリシーでは、このカスタムドキュメントのみを許可しています([IAM](#iam) のパターン②用ロール)。  
このロールで、指定するドキュメントを変えて3通り試します。

### ②-A. カスタムドキュメントを指定した場合

```shell
$ aws ssm start-session --profile ssm-portlimited \
    --target i-xxxxxxxxxxxxxxxxx \
    --document-name ssm-port-control-PortForward-RDP-3389 \
    --parameters "localPortNumber=13389"

Starting session with SessionId: ssm-portlimited-xxxxxxxxxxxxxxxxxxxxxxxxxx
Port 13389 opened for sessionId ssm-portlimited-xxxxxxxxxxxxxxxxxxxxxxxxxx.
Waiting for connections...

別のターミナルから、ローカルの13389番に curl します。

$ curl -s localhost:13389
port 3389

3389番ポートに接続できました。

試しに、portNumber を渡して8080番に上書きしようとしてみます。

$ aws ssm start-session --profile ssm-portlimited \
    --target i-xxxxxxxxxxxxxxxxx \
    --document-name ssm-port-control-PortForward-RDP-3389 \
    --parameters "portNumber=8080,localPortNumber=18080"

aws: [ERROR]: An error occurred (InvalidParameters) when calling the StartSession operation: Parameter "portNumber" is not defined in the document.

ドキュメントに portNumber パラメータが定義されていないため、InvalidParameters で拒否されました。

②-B. AWS-StartPortForwardingSession を指定した場合

$ aws ssm start-session --profile ssm-portlimited \
    --target i-xxxxxxxxxxxxxxxxx \
    --document-name AWS-StartPortForwardingSession \
    --parameters "portNumber=8080,localPortNumber=18080"

aws: [ERROR]: An error occurred (AccessDeniedException) when calling the StartSession operation: User: arn:aws:sts::123456789012:assumed-role/ssm-port-control-portlimited/ssm-portlimited is not authorized to perform: ssm:StartSession on resource: arn:aws:ssm:ap-northeast-1::document/AWS-StartPortForwardingSession because no identity-based policy allows the ssm:StartSession action

AWS-StartPortForwardingSession は許可していないため、AccessDeniedException になりました。

②-C. ドキュメントを指定しない

--document-name を省略した場合の挙動も確認しました。

$ aws ssm start-session --profile ssm-portlimited \
    --target i-xxxxxxxxxxxxxxxxx

aws: [ERROR]: An error occurred (AccessDeniedException) when calling the StartSession operation: User: arn:aws:sts::123456789012:assumed-role/ssm-port-control-portlimited/ssm-portlimited is not authorized to perform: ssm:StartSession on resource: arn:aws:ssm:ap-northeast-1:123456789012:document/SSM-SessionManagerRunShell because no identity-based policy allows the ssm:StartSession action

エラー文を見たところ、SSM-SessionManagerRunShell に対するssm:StartSession の権限が無いために失敗しているとのことでした。

公式ドキュメントによると、Session ManagerのデフォルトのSession DocumentとしてSSM-SessionManagerRunShell が使用されるとのことです。

https://docs.aws.amazon.com/systems-manager/latest/userguide/getting-started-default-session-document.html

今回のポリシーではSSM-SessionManagerRunShellを許可ドキュメントに含めていないため、ドキュメント未指定時にエラーとなったと考えられます。
|


まとめ

SSM Session Managerのポートフォワード先は、カスタムドキュメントを利用することでIAMで制御できることがわかりました。

同一サーバーに対して、運用担当はRDP(3389)、アプリ開発者はアプリの管理画面(8080など)だけで繋がせたい、のような場面で活かせそうです。

この記事をシェアする

AWSのお困り事はクラスメソッドへ

関連記事