-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_utils.py
190 lines (163 loc) · 4.88 KB
/
main_utils.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import logging
import json
from zipfile import ZipFile
import boto3
from botocore.exceptions import ClientError
AWS_REGION = os.environ.get('AWS_REGION', 'us-east-1')
AWS_PROFILE = os.environ.get('AWS_PROFILE', 'localstack')
ENDPOINT_URL = os.environ.get('ENDPOINT_URL', 'http://localhost:4566')
LAMBDA_ZIP = os.environ.get('LAMBDA_ZIP', './function.zip')
boto3.setup_default_session(profile_name=AWS_PROFILE)
# logger config
logger = logging.getLogger()
logging.basicConfig(level=logging.INFO,
format='%(asctime)s: %(levelname)s: %(message)s')
def get_boto3_client(service):
"""
Initialize Boto3 Lambda client.
"""
try:
lambda_client = boto3.client(
service,
region_name=AWS_REGION,
endpoint_url=ENDPOINT_URL
)
except Exception as e:
logger.exception('Error while connecting to LocalStack.')
raise e
else:
return lambda_client
def get_boto3_resource(service):
"""
Initialize Boto3 Resource.
"""
try:
boto3_resource = boto3.resource(
service,
endpoint_url=ENDPOINT_URL
)
except Exception as e:
logger.exception('Error while connecting to LocalStack.')
raise e
else:
return boto3_resource
def create_lambda_zip(function_name):
"""
Generate ZIP file for lambda function.
"""
try:
with ZipFile(LAMBDA_ZIP, 'w') as zip:
zip.write(function_name + '.py')
except Exception as e:
logger.exception('Error while creating ZIP file.')
raise e
def create_lambda(function_name):
"""
Creates a Lambda function in LocalStack.
"""
try:
lambda_client = get_boto3_client('lambda')
_ = create_lambda_zip(function_name)
# create zip file for lambda function.
with open(LAMBDA_ZIP, 'rb') as f:
zipped_code = f.read()
lambda_client.create_function(
FunctionName=function_name,
Runtime='python3.8',
Role='role',
Handler=function_name + '.handler',
Code=dict(ZipFile=zipped_code)
)
except Exception as e:
logger.exception('Error while creating function.')
raise e
def delete_lambda(function_name):
"""
Deletes the specified lambda function.
"""
try:
lambda_client = get_boto3_client('lambda')
lambda_client.delete_function(
FunctionName=function_name
)
# remove the lambda function zip file
os.remove(LAMBDA_ZIP)
except Exception as e:
logger.exception('Error while deleting lambda function')
raise e
def invoke_function(function_name):
"""
Invokes the specified function and returns the result.
"""
try:
lambda_client = get_boto3_client('lambda')
response = lambda_client.invoke(
FunctionName=function_name)
logger.info('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
return json.loads(
response['Payload']
.read()
.decode('utf-8')
)
except Exception as e:
logger.exception('Error while invoking function')
raise e
def create_secret(name, secret_value):
"""
Creates a secret in LocalStack.
"""
try:
client_secret = get_boto3_client('secretsmanager')
kwargs = {'Name': name}
if isinstance(secret_value, str):
kwargs['SecretString'] = secret_value
elif isinstance(secret_value, bytes):
kwargs['SecretBinary'] = secret_value
response = client_secret.create_secret(**kwargs)
except Exception as e:
logger.exception('Error while creating secret.')
raise e
return response
def get_value(name):
"""
Gets a secret in LocalStack.
"""
try:
client_secret = get_boto3_client('secretsmanager')
kwargs = {'SecretId': name}
response = client_secret.get_secret_value(SecretId=name)
except Exception as e:
logger.exception('Error while get value from a secret.')
raise e
return response
def delete_secret(name):
"""
Gets a secret in LocalStack.
"""
try:
client_secret = get_boto3_client('secretsmanager')
kwargs = {'SecretId': name}
response = client_secret.delete_secret(SecretId=name, ForceDeleteWithoutRecovery=True)
except Exception as e:
logger.exception('Error while delete secret.')
raise e
def create_bucket(bucket_name, region = None):
"""
Create bucket
"""
client_s3 = get_boto3_client('s3')
try:
client_s3.create_bucket(Bucket=bucket_name)
except ClientError as e:
logging.error(e)
return False
return True
def list_buckets():
client_s3 = get_boto3_client('s3')
return client_s3.list_buckets()
def delete_bucket_with_objects(bucket_name):
s3_resource = get_boto3_resource('s3')
bucket = s3_resource.Bucket(bucket_name)
bucket.objects.delete()
bucket.delete()