特定VPCに特定セキュリティグループのEC2インスタンスのみ起動するように制限してみました。


この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので十分ご注意ください。

背景

以前、特定VPCに特定セキュリティグループのEC2インスタンスのみ起動するように制限してみましたので、備忘録として残していきたいと思います。

構成図

file

特定VPCにEC2インスタンスへのセキュリティグループ関連付けを制限したいです。 具体的には特定セキュリティグループのみ付与が可能、 ほかセキュリティグループはインスタンスへ関連付けできないようにします。

その他VPCに制限しない

ポリシー

ポリシー1(cpi-kobayashi-tag-limit-test1)

特定VPCの条件を下記のように記載します。

      "Condition": {
        "ArnEquals": {
          "ec2:Vpc": "arn:aws:ec2:region:account-id:vpc/vpc-id"
        }
      }
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DenyNotAllowedSGAttach",
            "Effect": "Deny",
            "Action": [
                "ec2:ModifyNetworkInterfaceAttribute",
                "ec2:RunInstances"
            ],
            "Resource": [
                "arn:aws:ec2:*:*:security-group/*"
            ],
            "Condition": {
                "ArnEquals": {
                    "ec2:Vpc": "arn:aws:ec2:ap-northeast-1:717076937412:vpc/vpc-0133514a2d1f31b3c"
                },
                "StringNotEqualsIfExists": {
                    "aws:ResourceTag/AdminManaged": "yes"
                }
            }
        }
    ]
}

ポリシー2(cpi-kobayashi-tag-limit-test2)

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DenyTagOperationsForManagedSG",
            "Effect": "Deny",
            "Action": [
                "ec2:CreateTags",
                "ec2:DeleteTags"
            ],
            "Resource": [
                "arn:aws:ec2:*:*:security-group/*"
            ],
            "Condition": {
                "Null": {
                    "aws:ResourceTag/AdminManaged": false
                }
            }
        },
        {
            "Sid": "DenyTagCreationForNonManagedSG",
            "Effect": "Deny",
            "Action": "ec2:CreateTags",
            "Resource": [
                "arn:aws:ec2:*:*:security-group/*"
            ],
            "Condition": {
                "Null": {
                    "aws:ResourceTag/AdminManaged": true,
                    "aws:RequestTag/AdminManaged": false
                }
            }
        }
    ]
}

ポリシー3(cpi-kobayashi-tag-limit-test3)

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ec2:AuthorizeSecurityGroupEgress",
                "ec2:AuthorizeSecurityGroupIngress",
                "ec2:RevokeSecurityGroupEgress",
                "ec2:RevokeSecurityGroupIngress"
            ],
            "Resource": "arn:aws:ec2:*:*:security-group/*",
            "Condition": {
                "StringLike": {
                    "aws:ResourceTag/AdminManaged": "yes"
                }
            }
        }
    ]
}

IAMユーザの権限

IAMユーザ「CPI-KOBAYASHI-TEST」に下記の権限を付与します。

AmazonEC2FullAccess
cpi-kobayashi-tag-limit-test1
cpi-kobayashi-tag-limit-test2
cpi-kobayashi-tag-limit-test3

file

動作確認(特定VPC)

SGに"AdminManaged": "yes"が付いていない場合

(EC2の起動が制限される


file

file


file

SGに"AdminManaged": "yes"が付いている場合

(EC2の起動が制限されない)


file


file


file

動作確認(その他VPC)

SGに"AdminManaged": "yes"が付いていない場合

(EC2の起動が制限されない)

file


file


file

SGに"AdminManaged": "yes"が付いている場合

(EC2の起動が制限されない)

file


file

file

参考

https://cloud5.jp/tag-limit-for-sg/

https://docs.aws.amazon.com/ja_jp/vpc/latest/userguide/vpc-policy-examples.html

Last modified: 2023-02-26

Author