0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS SSM Session Managerでタグ付きEC2インスタンスへの接続制御

Last updated at Posted at 2025-09-01

概要

AWS Systems Manager(SSM)のSession Managerを利用する際、特定のタグを持つEC2インスタンスのみに接続を許可するため、IAMポリシーを付与した所、AccessDeniedExceptionが発生し、接続できないという事象が発生したため、原因と解決策を整理しました。

事象

IAMユーザーの既存のポリシーにインラインで「Condition要素(またはCondition block)」を付与したところ、StartSession 実行時にAccessDeniedException が発生。

エラーが発生したIAMポリシー

※26行目〜31行目に「Condition要素(またはCondition block)」を追加

{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"ssm:StartSession",
				"ssm:DescribeSessions",
				"ssm:TerminateSession"
			],
			"Resource": [
				"arn:aws:ssm:ap-northeast-1:<AWSアカウントID>:document/SSM-SessionManagerRunShell",
				"arn:aws:ec2:ap-northeast-1:<AWSアカウントID>:instance/*"
			],
            "Condition": {
                "StringEquals": {
                    "aws:ResourceTag/TAG1": "KEY1",
                    "aws:ResourceTag/TAG2": "KEY2",
                    "aws:ResourceTag/TAG3": "KEY3"
            }
		},
		{
			"Effect": "Allow",
			"Action": [
				"ec2:DescribeInstances",
				"ssm:DescribeInstanceInformation"
			],
			"Resource": "*"
		}
	]
}

エラー内容

An error occurred (AccessDeniedException) when calling the StartSession operation:
User: arn:aws:iam::<AWSアカウントID>:user/test is not authorized to perform: ssm:StartSession
on resource: arn:aws:ssm:ap-northeast-1:<AWSアカウントID>:document/SSM-SessionManagerRunShell
because no identity-based policy allows the ssm:StartSession action

原因

  • ssm:StartSession2種類のリソースに対して評価される
    • SSMドキュメント(arn:aws:ssm:...:document/SSM-SessionManagerRunShell
    • 接続対象のEC2インスタンス(arn:aws:ec2:...:instance/i-xxxxxxxx
  • 初期ポリシーで ドキュメントとインスタンスを同一Statementに記述し、タグ条件を設定したため、ドキュメントにもタグ条件が適用され、権限不足で拒否された。

解決策

  • ドキュメント用の許可インスタンス用の許可(タグ条件付き)別々の Statement に分けることです。

修正版IAMポリシー

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ssm:StartSession"
            ],
            "Resource": "arn:aws:ssm:ap-northeast-1:<AWSアカウントID>:document/SSM-SessionManagerRunShell"
        },
        {
            "Effect": "Allow",
            "Action": [
                "ssm:StartSession",
                "ssm:DescribeSessions",
                "ssm:TerminateSession"
            ],
            "Resource": "arn:aws:ec2:ap-northeast-1:<AWSアカウントID>:instance/*",
            "Condition": {
                "StringEquals": {
                    "aws:ResourceTag/TAG1": "KEY1",
                    "aws:ResourceTag/TAG2": "KEY2",
                    "aws:ResourceTag/TAG2": "KEY3",
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "ec2:DescribeInstances",
                "ssm:DescribeInstanceInformation"
            ],
            "Resource": "*"
        }
    ]
}

ポイント

  • ドキュメント用とインスタンス用のStatementを分離する
  • インスタンス側にのみタグベース条件を付与する
  • これにより、指定タグ付きEC2のみ接続可能となる

検証結果

  • タグ付き EC2 → 接続成功
  • タグなし EC2 → AccessDenied(接続拒否)

まとめ

SSM Session Managerの制御では、ssm:StartSessionが複数リソース(Document + Instance)にまたがることが重要ポイントでした。

IAMポリシーをリソースごとに分割し、EC2側にのみタグ条件を適用することで要件を満たし、エラーが解消かれます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?