AWS CLI Get Security Group ID with Name.[ wild card support] Bash Script
Hi guys,
If you are wondering How can I get the ID of an AWS security group if I know the name?
Here is the solution.
Get the Security ID with the wild card Name.
#!/bin/bash #Author Praveen SearchString=<*YourSearchString*> # Note : wild cards supported for VPCS in `aws ec2 --output text --query 'Vpcs[*].{VpcId:VpcId}' describe-vpcs` ; do aws ec2 describe-security-groups --filter Name=vpc-id,Values=$VPCS Name=group-name,Values=$searchstring --query 'SecurityGroups[*].[GroupId]' --output text done 2. With the above solution we can automate the other tasks. eg: add tags to security groups. for VPCS in `aws ec2 --output text --query 'Vpcs[*].{VpcId:VpcId}' describe-vpcs` ; do echo " Tagging Search String Created Security Groups" aws ec2 describe-security-groups --filter Name=vpc-id,Values=$VPCS Name=group- name,Values=$searchstring --query 'SecurityGroups[*].[GroupId]' --output text | xargs - I {} aws ec2 create-tags --resources {} --tags --tags '[{"Key":"tag1","Value":"tag1"}, {"Key":"tag2", "Value":"tag2"},{"Key":"tag3", "Value":"tag3"},{"Key":"tag4", "Value":"tag4"},{"Key":"tag5", "Value":"tag5"}]' echo "Successfully Tagged " done
Comments
Post a Comment