Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto3: how to find security group id if I know group name

Tags:

python

boto3

I am trying to find security group id by name. response = ec2.describe_security_groups() returns a data structure which includes all groups, ids and everything else. What is the way to filter group id for specific group if group name is provided?

like image 460
nanda Avatar asked Dec 22 '22 23:12

nanda


1 Answers

Replace GROUP_NAME with the security group name you are looking for:

import boto3


ec2 = boto3.client('ec2')
group_name = 'GROUP_NAME'
response = ec2.describe_security_groups(
    Filters=[
        dict(Name='group-name', Values=[group_name])
    ]
)
group_id = response['SecurityGroups'][0]['GroupId']

like image 139
dmulter Avatar answered Jan 13 '23 11:01

dmulter