LoginSignup
3
2

More than 3 years have passed since last update.

[AWS] ssm セッションマネージャー タグによるアクセス制限

Posted at

特定のタグを持つEC2にのみセッションマネージャーでログインできるようにするIAMポリシーです。
部署ごとにEC2をタグで分けている場合などに使えると思います。

公式ドキュメント

IAMポリシー

タグ"DIV"に"APP"を含むものにのみアクセスできるポリシー

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ssm:DescribeSessions",
                "ssm:GetConnectionStatus",
                "ssm:DescribeInstanceProperties",
                "ec2:describeInstances"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "ssm:StartSession"
            ],
            "Resource": [
                "arn:aws:ec2:*:*:instance/*"
            ],
            "Condition": {
                "StringLike": {
                    "ssm:resourceTag/DIV": [
                        "APP"
                    ]
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "ssm:TerminateSession"
            ],
            "Resource": [
                "arn:aws:ssm:*:*:session/${aws:username}-*"
            ]
        }
    ]
}

複数タグを指定する場合

Conditionの箇所に下記のように複数記載します。

"Condition":{
            "StringLike":{
               "ssm:resourceTag/tag-key1":[
                  "tag-value1"
               ],
               "ssm:resourceTag/tag-key2":[
                  "tag-value2"
               ]
            }

ポリシーについて

AWSコンソールからアクセスしたい場合は下記の部分が必要でした。CLIからのみアクセスする場合は不要です。

    {  
            "Effect": "Allow",
            "Action": [
                "ssm:DescribeSessions",
                "ssm:GetConnectionStatus",
                "ssm:DescribeInstanceProperties",
                "ec2:describeInstances"
            ],
            "Resource": [
                "*"
            ]
        },

ログイン試行結果

CLI

タグがついているインスタンス

% aws ssm start-session \
--target タグがついているインスタンスIDを指定

Starting session with SessionId: セッションID
sh-4.2$

タグが付いていないインスタンス

% aws ssm start-session \
--target タグがついていないインスタンスIDを指定

An error occurred (AccessDeniedException) when calling the StartSession operation: User: ユーザ is not authorized to perform: ssm:StartSession on resource: インスタンスID

コンソール

タグがついていないインスタンスに接続を試みると同様のエラー

User: ユーザ is not authorized to perform: ssm:StartSession on resource: インスタンスID

その他

Windowsでも同様

3
2
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
3
2