Лямбда-функция AWS EC2_SSH_Security_Check
не дает правильного значения.
import sys
import boto3
from boto3 import ec2
connection=boto3.client('ec2', region_name='sa-east-1')
sg=connection.describe_security_groups()
#listOfInstances=""
#messages="Following Instances have port 22 open"
def getTag(instanceId):
reservations=connection.get_all_instances(filters={'instance_id':instanceId})
for r in reservations:
for i in r.instances:
return i.tags['Name']
def lambda_handler(event, context):
try:
print(sg)
for securityGroup in sg:
for rule in securityGroup.rules:
global instanceId;
if (rule.from_port=='22' and rule.to_port == '22') and '0.0.0.0/0' in str(rule.grants):
for instanceid in securityGroup.instances():
instanceId=str(instanceid)
listOfInstances += "Instance Name : " + getTag(instanceId.split(':')[1]) + "\t State:" + instanceid.state + "\t SecurityGroup:" +securityGroup.name + "\n"
print(listOfInstances)
except :
print ("Some Error occurred")
Ожидание - показать порт 22, который открыт для всех, но не показывает правильное значение