-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlambda_vpc_flow_log.py
More file actions
47 lines (36 loc) · 1.27 KB
/
lambda_vpc_flow_log.py
File metadata and controls
47 lines (36 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import boto3
import os
def lambda_handler(event, context):
'''
Extract the VPC ID from the event and enable VPC Flow Logs.
'''
try:
vpc_id = event['detail']['responseElements']['vpc']['vpcId']
print('VPC: ' + vpc_id)
ec2_client = boto3.client('ec2')
response = ec2_client.describe_flow_logs(
Filter=[
{
'Name': 'resource-id',
'Values': [
vpc_id,
]
},
],
)
if len(response[u'FlowLogs']) != 0:
print('VPC Flow Logs are ENABLED')
else:
print('VPC Flow Logs are DISABLED')
print('FLOWLOGS_GROUP_NAME: ' + os.environ['FLOWLOGS_GROUP_NAME'])
print('ROLE_ARN: ' + os.environ['ROLE_ARN'])
response = ec2_client.create_flow_logs(
ResourceIds=[vpc_id],
ResourceType='VPC',
TrafficType='ALL',
LogGroupName=os.environ['FLOWLOGS_GROUP_NAME'],
DeliverLogsPermissionArn=os.environ['ROLE_ARN'],
)
print('Created Flow Logs: ' + response['FlowLogIds'][0])
except Exception as e:
print('Error - reason "%s"' % str(e))