Skip to content

Instantly share code, notes, and snippets.

@reecestart
Last active April 30, 2024 17:40
Show Gist options
  • Save reecestart/5d11d609fa4af1fd8b5cd0e4e05e25d7 to your computer and use it in GitHub Desktop.
Save reecestart/5d11d609fa4af1fd8b5cd0e4e05e25d7 to your computer and use it in GitHub Desktop.
Will print out RDS DB Instance DB Identifiers with their ENI and Private IP. Will only work if one SG is used per RDS DB (EDIT: Updated to work for DB Clusters e.g. Aurora Serverless)
import boto3
ec2_client = boto3.client('ec2')
rds_client = boto3.client('rds')
rdsNetworkInterfaces = ec2_client.describe_network_interfaces(
Filters=[
{
'Name': 'attachment.instance-owner-id',
'Values': [
'amazon-rds',
]
},
]
)
rdsDBInstances = rds_client.describe_db_instances()
for rds in rdsDBInstances['DBInstances']:
for eni in rdsNetworkInterfaces['NetworkInterfaces']:
vpcSecurityGroups = rds['VpcSecurityGroups']
groups = eni['Groups']
for vpcSecurityGroup in vpcSecurityGroups:
for group in groups:
if group['GroupId'] == vpcSecurityGroup['VpcSecurityGroupId']:
print(rds['DBInstanceIdentifier'] + ' ' + eni['NetworkInterfaceId'] + ' ' + eni['PrivateIpAddress'])
rdsNetworkInterfaces = ec2_client.describe_network_interfaces(
Filters=[
{
'Name': 'attachment.instance-owner-id',
'Values': [
'amazon-aws'
]
},
]
)
rdsDBClusters = rds_client.describe_db_clusters()
for rds in rdsDBClusters['DBClusters']:
for eni in rdsNetworkInterfaces['NetworkInterfaces']:
vpcSecurityGroups = rds['VpcSecurityGroups']
groups = eni['Groups']
for vpcSecurityGroup in vpcSecurityGroups:
for group in groups:
if group['GroupId'] == vpcSecurityGroup['VpcSecurityGroupId']:
print(rds['DBClusterIdentifier'] + ' ' + eni['NetworkInterfaceId'] + ' ' + eni['PrivateIpAddress'])
@starcry
Copy link

starcry commented Sep 7, 2022

oh this is just all kinds of amazing, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment