特定の VPC に属するすべてのリソースを一覧表示するにはどうすればいいですか? 質問する

特定の VPC に属するすべてのリソースを一覧表示するにはどうすればいいですか? 質問する

私の AWS アカウントには、いくつかの VPC があります。特定の VPC の下にあるすべてのリソースを一覧表示する方法を見つけようとしています。

ありがとう!!

ベストアンサー1

AWS CLI、AWS コンソール、コードの 3 つの方法で実行できます。

AWS CLI


AWS CLI を使用して、VPC に関連付けられているすべての ENI を一覧表示し、--queryパラメータを使用して出力を整えて、必要なフィールド (AZ、インスタンス ID など) を含むリソース リストを取得できます。

  1. aws ec2 describe-network-interfaces --filters Name=vpc-id,Values=<vpc-id> --query  'NetworkInterfaces[*].[AvailabilityZone, OwnerId, Attachment.InstanceId, PrivateIpAddresses[*].Association.PublicIp]'
    
  2. aws ec2 describe-network-interfaces --filters Name=vpc-id,Values=<vpc-id> --query  'NetworkInterfaces[*].[RequesterId,Description]'
    

    生の出力のサンプル (VPC 上のインスタンスは 1 つのみ):

     "NetworkInterfaces": [
         {
             "Association": {
                 "IpOwnerId": "amazon",
                 "PublicDnsName": "ec2-54-196-57-169.compute-1.amazonaws.com",
                 "PublicIp": "54.196.57.169"
             },
             "Attachment": {
                 "AttachTime": "2020-08-24T10:59:16+00:00",
                 "AttachmentId": "eni-attach-047e562690aabbffd",
                 "DeleteOnTermination": true,
                 "DeviceIndex": 0,
                 "InstanceId": "i-0fe495a6c17bd0f82",
                 "InstanceOwnerId": "570398916848",
                 "Status": "attached"
             },
             "AvailabilityZone": "us-east-1d",
             "Description": "",
             "Groups": [
                 {
                     "GroupName": "launch-wizard-1",
                     "GroupId": "sg-0aa7d8257bb487e1b"
                 }
             ],
             "InterfaceType": "interface",
             "Ipv6Addresses": [],
             "MacAddress": "0e:58:38:33:9a:31",
             "NetworkInterfaceId": "eni-0b20855178d276783",
             "OwnerId": "570398916848",
             "PrivateDnsName": "ip-172-31-34-30.ec2.internal",
             "PrivateIpAddress": "172.31.34.30",
             "PrivateIpAddresses": [
                 {
                     "Association": {
                         "IpOwnerId": "amazon",
                         "PublicDnsName": "ec2-54-196-57-169.compute-1.amazonaws.com",
                         "PublicIp": "54.196.57.169"
                     },
                     "Primary": true,
                     "PrivateDnsName": "ip-172-31-34-30.ec2.internal",
                     "PrivateIpAddress": "172.31.34.30"
                 }
             ],
             "RequesterManaged": false,
             "SourceDestCheck": true,
             "Status": "in-use",
             "SubnetId": "subnet-e2bc5fbd",
             "TagSet": [],
             "VpcId": "vpc-6ad2e110"
         }
     ]
    

そしてフィルタリングされました:

  1. 初めて--query

     [
         "us-east-1d",
         "57039816848",
         "i-0fe495a6c17bd0f82",
         [
             "44.196.57.169"
         ]
     ]
    
  2. 2 番目--query(別の VPC) の場合:

     [
         "amazon-elasticache",
         "ElastiCache alon-001"
     ],
     [
         "amazon-elasticache",
         "ElastiCache alon-002"
     ],
     [
         "975289786086",
         "arn:aws:ecs:us-east-2:57039916848:attachment/22a90802-fae7-4afb-9a7e-43e6f4be8ca4"
     ],
     [
         "074689309192",
         "Interface for NAT Gateway nat-069344579d8bda20"
     ],
     [
         "amazon-elb",
         "ELB app/EC2Co-EcsEl-YX74WCWEGOK/0b6d7bc60b540b1"
     ],
     [
         "amazon-elb",
         "ELB app/EC2Co-EcsEl-YX74WCWGGOK/0b6bd7c60b540b1"
     ],
     [
         "amazon-elasticache",
         "ElastiCache alon-003"
     ]
    

AWS コンソール


AWS コンソールを使用して同じ操作を実行できます。EC2 -> ネットワーク インターフェイスで、vpc-id検索バーに目的のものを検索します。ここに画像の説明を入力してください

コード


Pythonスクリプトを使用してvpc-inside.pyすべての VPC リソースを記述できます。

usage: vpc-inside.py [-h] -v VPC [-r REGION] [-p PROFILE]

optional arguments:
  -h, --help                     show this help message and exit
  -v VPC, --vpc VPC              The VPC to annihilate
  -r REGION, --region REGION     AWS region that the VPC resides in
  -p PROFILE, --profile PROFILE  AWS profile

出力は次のようになります。


EKSs in VPC vpc-07ef7f777429cfd82:
Omikron
--------------------------------------------
ASGs in VPC vpc-07ef7f777429cfd82:
eks-pooks-9ebf225b-70a9-a026-034f-c7431df9b7ba resides in vpc-07ef7f777429cfd82
eks-pooks-9ebf225b-70a9-a026-034f-c7431df9b7ba
--------------------------------------------
RDSs in VPC vpc-07ef7f777429cfd82:
--------------------------------------------
EC2s in VPC vpc-07ef7f777429cfd82:
i-0c63874d77ea2ba78
i-043740f224015e69e
--------------------------------------------
Lambdas in VPC vpc-07ef7f777429cfd82:
--------------------------------------------
Classic ELBs in VPC vpc-07ef7f777429cfd82:
--------------------------------------------
ELBs V2 in VPC vpc-07ef7f777429cfd82:
--------------------------------------------
NAT GWs in VPC vpc-07ef7f777429cfd82:
--------------------------------------------
VPC EndPoints in VPC vpc-07ef7f777429cfd82:
--------------------------------------------
IGWs in VPC vpc-07ef7f777429cfd82:
--------------------------------------------
ENIs in VPC vpc-07ef7f777429cfd82:
eni-079231232dc136305
eni-05ff227eca8341a08
eni-0c01b2871887ac3f7
eni-00e11d4f9590161b4
--------------------------------------------
Security Groups in VPC vpc-07ef7f777429cfd82:
sg-0b4554a65e1560745
sg-0f93574d6b180b263
--------------------------------------------
Routing tables in VPC vpc-07ef7f777429cfd82:
rtb-0694bdbdd696b2bed
rtb-072ec82a18d8a04ba
--------------------------------------------
ACLs in VPC vpc-07ef7f777429cfd82:
acl-0c0087eabf9335940
--------------------------------------------
Subnets in VPC vpc-07ef7f777429cfd82:
subnet-0b8cc1132727e5b5d
subnet-0e47ee92a9ca80280
subnet-0c25990d9a138616b
--------------------------------------------

おすすめ記事