-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
113 lines (100 loc) · 3.49 KB
/
lambda_function.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
# -- coding: utf-8 --
"""
File: lambda_function.py
Author: Adeel Ahmad
Description: AWS Lambda function to create PrivateLink.
"""
from __future__ import absolute_import, division, print_function, unicode_literals
__version__ = "0.1"
import unittest
from botocore.exceptions import ClientError
import boto3
try:
import json
except ImportError:
import simplejson as json
try:
from urllib2 import HTTPError, build_opener, HTTPHandler, Request
except ImportError:
from urllib.error import HTTPError
from urllib.request import build_opener, HTTPHandler, Request
SUCCESS = "SUCCESS"
FAILED = "FAILED"
EC2 = boto3.client('ec2')
def send(event, context, response_status, reason= \
None, response_data=None, physical_resource_id=None):
"""
building own response function
"""
response_data = response_data or {}
response_body = json.dumps(
{
'Status': response_status,
'Reason': reason or "See the details in \
CloudWatch Log Stream: " + context.log_stream_name,
'PhysicalResourceId': physical_resource_id or context.log_stream_name,
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
'Data': response_data
}
)
opener = build_opener(HTTPHandler)
request = Request(event['ResponseURL'], data=response_body)
request.add_header('Content-Type', '')
request.add_header('Content-Length', len(response_body))
request.get_method = lambda: 'PUT'
try:
response = opener.open(request)
print("Status code: {}".format(response.getcode()))
print("Status message: {}".format(response.msg))
return True
except HTTPError as exc:
print("Failed executing HTTP request: {}".format(exc.code))
return False
def get_my_log_stream(context):
"""
Logging function for the lambda handler to call.
"""
print("Log stream name:", context.log_stream_name + '\n' + "Log group name:", \
context.log_group_name + '\n' + "Request ID:", context.aws_request_id \
+ '\n' + "Mem. limits(MB):", context.memory_limit_in_mb + '\n' + \
"Time remaining (MS):", context.get_remaining_time_in_millis())
def handler(event, context):
"""
Handler to build Private-Link
"""
response = {}
# There is nothing to do for a delete request
if event['RequestType'] == 'Delete':
send(event, context, SUCCESS)
return
try:
response = EC2.create_vpc_endpoint(
DryRun=False,
VpcEndpointType=event['ResourceProperties']['VpcEndpointType'],
VpcId=event['ResourceProperties']['VpcId'],
ServiceName=event['ResourceProperties']['ServiceName'],
SubnetIds=event['ResourceProperties']['SubnetIds'],
PrivateDnsEnabled=False
)
response['VpcEndpoint'].pop('CreationTimestamp')
send(event, context, SUCCESS, response)
except ClientError as error:
print(error)
print("Error: {0}".format(error))
send(event, context, FAILED, response)
if __name__ == "__main__":
handler(event, context)
import doctest
doctest.testmod()
class MyTest(unittest.TestCase):
"""
Class to initiate to test function
"""
def test(self):
"""
Test Function
"""
self.assertEqual(handler(event, context),)