Bash Script to attach AWS Security Group in EC2 Available in Account.
Note: Make sure you change NEWSGNAME="new group ip as required"
#!/bin/bash#Change below security group name according to your case, also if you have more then one security group please# change below variable like SGNAME="sg-0ab87fjhg3y3e53 sg-0ahsiek27sh5debcb"NEWSGNAME="sg-0ab263804b7d9476r"aws ec2 describe-instances --instance-ids --output table | grep InstanceId | cut -d" " -f17 > EC2ID.txtfor EC2ID in `cat EC2ID.txt`doOLDGRP=`aws ec2 describe-instances --instance-ids $EC2ID --output text | grep SECURITYGROUPS | awk '{print $2}' | tr '\n' ' ' | grep -v "%"`aws ec2 modify-instance-attribute --instance-id $EC2ID --groups $NEWSGNAME $OLDGRPecho -e "Security Group changed for EC2 Instance ID $EC2ID "done
No comments:
Post a Comment