lambda_function.py 文件源码

python
阅读 39 收藏 0 点赞 0 评论 0

项目:Cloud-Custodian 作者: jtroberts83 项目源码 文件源码
def lambda_handler(event, context):
    s3resource = boto3.resource('s3', config=Config(signature_version='s3v4'), region_name=bucketregion)
    s3resource.meta.client.download_file(bucketname, key, '/tmp/OpenSGs.txt')
    AllOpenSGs=[]
    f = open('/tmp/OpenSGs.txt', 'r')
    AllOpenSGs = f.read()
    AllOpenSGs = AllOpenSGs.split('\n')

    #print("Existing SGS are: %s" % AllOpenSGs)
    response = ec2.describe_security_groups()
    for sg in response['SecurityGroups']:
        IPPermissions=sg['IpPermissions']
        for ingress in IPPermissions:
            IpRanges=ingress['IpRanges']
            for range in IpRanges:
                cidr=range['CidrIp']
                if '0.0.0.0/0' in cidr:
                    print(cidr)
                    sgname=sg['GroupId']
                    AllOpenSGs.append(sgname)

    # Creates array of unique values to remove duplicate SGs                
    AllUniqueSGs = list(set(AllOpenSGs))

    # Convert the List to a String to avoid S3 errors
    StringOfSGs = '\n'.join(AllUniqueSGs)

    # Upload the txt file to S3
    response = s3.put_object(
        Body=StringOfSGs,
        Bucket=bucketname,
        Key=key
    )
    return 'File Has Been Uploaded To S3'
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号