-
Notifications
You must be signed in to change notification settings - Fork 6
/
import_nat_gateways.py
55 lines (47 loc) · 1.74 KB
/
import_nat_gateways.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
import os
import boto3
import logging
import time
from sts import establish_role
from botocore.exceptions import ClientError
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def import_nat_gateways(event, context):
dynamodb = boto3.resource('dynamodb')
client = boto3.client('ec2')
nat_table = dynamodb.Table(os.environ['DYNAMODB_TABLE_NAT_GATEWAYS'])
account = event['account']
region = event['region']
# ttl time to expire items in DynamoDB table, default 48 hours
# ttl provided in seconds
ttl_expire_time = (
int(time.time()) + os.environ.get('TTL_EXPIRE_TIME', 172800))
ACCESS_KEY, SECRET_KEY, SESSION_TOKEN = establish_role(account)
client = boto3.client('ec2',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN,
region_name=region)
try:
nats = client.describe_nat_gateways()
except ClientError as e:
if e.response['Error']['Code'] == "UnauthorizedOperation":
return logger.warning(
f"Unable to access resources in {account}:{region}")
for nat in nats['NatGateways']:
public_ip = nat['NatGatewayAddresses'][0]['PublicIp']
nat_id = nat['NatGatewayId']
nat_vpc_id = nat['VpcId']
logger.info('Logging NAT Gateway: {0} for account {1}'.format(
public_ip, account)
)
nat_table.put_item(
Item={
'id': nat_id,
'PublicIp': public_ip,
'AccountID': account,
'VpcId': nat_vpc_id,
'Region': region,
'ttl': ttl_expire_time
}
)