in subfunctions/ALE_single_account.py [0:0]
def flow_log_activator(region_list, account_number, unique_end):
"""Function that turns on the VPC Flow Logs, for VPCs identifed without them"""
for aws_region in region_list:
ec2 = boto3.client('ec2', region_name=aws_region)
logging.info("Creating a list of VPCs without Flow Logs on in region " + aws_region + ".")
try:
VPCList: list = []
FlowLogList: list = []
logging.info("DescribeVpcs API Call")
vpcs = ec2.describe_vpcs()
for vpc_id in vpcs["Vpcs"]:
VPCList.append(vpc_id["VpcId"])
logging.info("List of VPCs found within account " + account_number + ", region " + aws_region + ":")
print(VPCList)
logging.info("DescribeFlowLogs API Call")
vpcflowloglist = ec2.describe_flow_logs()
for resource_id in vpcflowloglist["FlowLogs"]:
FlowLogList.append(resource_id["ResourceId"])
working_list = (list(set(VPCList) - set(FlowLogList)))
logging.info("List of VPCs found within account " + account_number + ", region " + aws_region + " WITHOUT VPC Flow Logs:")
print(working_list)
for no_logs in working_list:
logging.info(no_logs + " does not have VPC Flow logging on. It will be turned on within this function.")
logging.info("Activating logs for VPCs that do not have them turned on.")
logging.info("If all VPCs have Flow Logs turned on, you will get an MissingParameter error. That is normal.")
logging.info("CreateFlowLogs API Call")
flow_log_on = ec2.create_flow_logs(
ResourceIds=working_list,
ResourceType='VPC',
TrafficType='ALL',
LogDestinationType='s3',
LogDestination='arn:aws:s3:::aws-log-collection-' + account_number + '-' + region + '-' + unique_end + '/vpcflowlogs',
LogFormat='${version} ${account-id} ${interface-id} ${srcaddr} ${dstaddr} ${srcport} ${dstport} ${protocol} ${packets} ${bytes} ${start} ${end} ${action} ${log-status} ${vpc-id} ${type} ${tcp-flags} ${subnet-id} ${sublocation-type} ${sublocation-id} ${region} ${pkt-srcaddr} ${pkt-dstaddr} ${instance-id} ${az-id} ${pkt-src-aws-service} ${pkt-dst-aws-service} ${flow-direction} ${traffic-path}',
TagSpecifications=[
{
'ResourceType': 'vpc-flow-log',
'Tags': [
{
'Key': 'workflow',
'Value': 'assisted-log-enabler'
},
]
}
]
)
logging.info("VPC Flow Logs are turned on.")
except Exception as exception_handle:
logging.error(exception_handle)