Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the the ID of an AWS security group if I know the name?

I'm using the AWS CLI and I want to get the ID of security group whose name I know (kingkajou_sg). How can I do it?

When I ask it to list all the security groups, it does so happily:

$ aws ec2 describe-security-groups | wc -l
     430

When I grep through this information, I see that the SG in question is listed:

$ aws ec2 describe-security-groups | grep -i kingkajou_sg
            "GroupName": "kingkajou_sg",

However, when I try to get the information about only that security group, it won't let me. Why?

$ aws ec2 describe-security-groups --group-names kingkajou_sg

An error occurred (InvalidGroup.NotFound) when calling the 
DescribeSecurityGroups operation: The security group 'kingkajou_sg' does not exist in default VPC 'vpc-XXXXXXXX'

Can someone please provide me the one line command that I can use to extract the Security group's ID given its name? You can assume that the command will be run from within an EC2 which is in the same VPC as the Security group.

like image 613
Saqib Ali Avatar asked Aug 12 '18 23:08

Saqib Ali


People also ask

How do I find my AWS security Group ID?

To view your security groups using the consoleOpen the Amazon VPC console at https://console.aws.amazon.com/vpc/ . In the navigation pane, choose Security Groups. Your security groups are listed. To view the details for a specific security group, including its inbound and outbound rules, select the security group.

What is security group ID in AWS?

A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Inbound rules control the incoming traffic to your instance, and outbound rules control the outgoing traffic from your instance. When you launch an instance, you can specify one or more security groups.

What is my AWS security Group IP?

The security group editor in the Amazon EC2 console can automatically detect the public IPv4 address of your local computer for you. Alternatively, you can use the search phrase "what is my IP address" in an internet browser, or use the following service: Check IP .


1 Answers

From the API Documentation:

--group-names (list)

[EC2-Classic and default VPC only] One or more security group names. You can specify either the security group name or the security group ID. For security groups in a nondefault VPC, use the group-name filter to describe security groups by name.

If you are using a non-default VPC, use the Filter

aws ec2 describe-security-groups --filter Name=vpc-id,Values=<my-vpc-id> Name=group-name,Values=kingkajou_sg --query 'SecurityGroups[*].[GroupId]' --output text
like image 82
victor m Avatar answered Sep 29 '22 06:09

victor m