forked from PaloAltoNetworks/Prisma-Enhanced-Remediation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWS-VPC-013.py
69 lines (51 loc) · 1.3 KB
/
AWS-VPC-013.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Remediate Prisma Policy:
AWS:VPC-013 VPC Elastic IP Limit
Description:
If your account is approaching the Elastic IP limitation per region, this remediation script will attempt
to release any/all unassociated EIPs.
Required Permissions:
- ec2:DescribeAddresses
- ec2:ReleaseAddress
Sample IAM Policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EC2Permissions",
"Action": [
"ec2:DescribeAddresses",
"ec2:ReleaseAddress"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
"""
import boto3
from botocore.exceptions import ClientError
def remediate(session, alert, lambda_context):
"""
Main Function invoked by index_prisma.py
"""
resource = None
region = alert['region']
ec2 = session.client('ec2', region_name=region)
try:
eips = ec2.describe_addresses()['Addresses']
except ClientError as e:
print(e.response['Error']['Message'])
return
unassociated = []
for eip in eips:
if 'AssociationId' not in eip:
unassociated.append(eip['AllocationId'])
for id in unassociated:
try:
result = ec2.release_address(AllocationId=id)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print('Released unassociated EIP {} in the {} region.'.format(id, region))
return