Wednesday, May 4, 2022

Bash Script to attach AWS Security Group in all EC2 Available in Account



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.txt

for EC2ID in `cat EC2ID.txt`

do
OLDGRP=`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 $OLDGRP
echo -e "Security Group changed for EC2 Instance ID $EC2ID "

done


Mastering Cron Jobs in Linux: A Comprehensive Guide with Examples

Introduction: Cron jobs are an indispensable tool in the world of system administration and automation. They allow users to schedule tasks t...