Search code examples
pythonamazon-web-servicesbotords

Access RDS security Group using Boto


I am trying to fetch the security group attached to an RDS in AWS. It is not returning anything other than RDS name. Need a way to get the security group attached to an RDS.

My current code->

rds_sg = conn2.get_all_dbinstances()

for r in rds_sg:
    sec = []
    sec = r.security_groups
    for s in sec:
        print s

This is not printing anything.


Solution

  • The following works for me (assumes a single RDS Instance but you can change that):

    def get_db_info():
    
        rds_conn = boto.connect_rds2(profile_name=AWS_PROFILE)
        if not rds_conn:
            print(_red('Cannot connect to AWS.RDS'))
            return
    
        instances = rds_conn.describe_db_instances()
        if not instances:
            print(_red('No instances found'))
            return
    
        inst = instances['DescribeDBInstancesResponse']['DescribeDBInstancesResult']['DBInstances'][0]
    
        dbinfo = {}
        endpoint = inst['Endpoint']
        dbinfo['VPCSecurityGroupId'] = inst['VpcSecurityGroups'][0]['VpcSecurityGroupId']
        dbinfo['dbSecurityGroupName'] = inst['DBSecurityGroups'][0]['DBSecurityGroupName']
    
        print('')
        print(_blue('db Info ===========>\n'))
        for item in dbinfo:
            print(_green('%20s : %s' % (item, dbinfo[item])))
    

    Instead of a profile_name, you can use the AWS Key/Secret arguments