nanda
nanda

Reputation: 121

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

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?

Upvotes: 7

Views: 5947

Answers (1)

dmulter
dmulter

Reputation: 2758

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']

Upvotes: 18

Related Questions