Added code to wait for ec2 instances to terminate before deleting the security group