-
Notifications
You must be signed in to change notification settings - Fork 19
/
deploy.py
executable file
·214 lines (184 loc) · 5.75 KB
/
deploy.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
import json
import os
import random
import subprocess
import sys
import tempfile
import zipfile
AWS_ACCOUNT_ID = "671946291905"
AWS_PROFILE = "scalableinternetservices"
AWS_REGION = "us-west-2"
aws_username = None
def aws_command(command_args, **check_output_args):
args = ["aws"] + command_args + ["--profile", AWS_PROFILE, "--region", AWS_REGION]
print(f"Executing: {' '.join(args)}")
output = subprocess.check_output(args, text=True, **check_output_args)
result = json.loads(output)
return result
def configure_api_gateway(function_arn):
api_id = find_or_create_rest_api()
arn = f"arn:aws:apigateway:{AWS_REGION}:lambda:path/2015-03-31/functions/{function_arn}/invocations"
result = aws_command(["apigateway", "get-resources", "--rest-api-id", api_id])
root_item = None
proxy_item = None
for item in result["items"]:
if item["path"] == "/":
root_item = item
elif item["path"] == "/{proxy+}":
proxy_item = item
else:
print("Need to delete path")
if "resourceMethods" not in root_item:
aws_command(
[
"apigateway",
"put-method",
"--authorization-type=NONE",
"--http-method=ANY",
"--resource-id",
root_item["id"],
"--rest-api-id",
api_id,
]
)
configure_api_method(
api_id=api_id, arn=arn, method_id=root_item["id"], method="ANY", path="/"
)
if not proxy_item:
result = aws_command(
[
"apigateway",
"create-resource",
"--parent-id",
root_item["id"],
"--path-part={proxy+}",
"--rest-api-id",
api_id,
]
)
proxy_item = result
if "resourceMethods" not in proxy_item:
aws_command(
[
"apigateway",
"put-method",
"--authorization-type=NONE",
"--http-method=ANY",
"--resource-id",
proxy_item["id"],
"--rest-api-id",
api_id,
]
)
configure_api_method(
api_id=api_id, arn=arn, method_id=proxy_item["id"], method="ANY", path="/*"
)
aws_command(
[
"apigateway",
"create-deployment",
"--rest-api-id",
api_id,
"--stage-name=prod",
]
)
return f"https://{api_id}.execute-api.{AWS_REGION}.amazonaws.com/prod/"
def configure_api_method(api_id, arn, method_id, method, path):
aws_command(
[
"apigateway",
"put-integration",
"--http-method",
method,
"--integration-http-method=POST",
"--resource-id",
method_id,
"--rest-api-id",
api_id,
"--type=AWS_PROXY",
"--uri",
arn,
]
)
aws_command(
[
"lambda",
"add-permission",
"--action=lambda:InvokeFunction",
"--function-name",
aws_username,
"--principal=apigateway.amazonaws.com",
"--source-arn",
f"arn:aws:execute-api:{AWS_REGION}:{AWS_ACCOUNT_ID}:{api_id}/*/*{path}",
f"--statement-id=apigateway-test-{method_id}",
]
)
def zip_add_directory(zip_fp, path):
for item in sorted(os.listdir(path)):
current_path = os.path.join(path, item)
if os.path.isfile(current_path):
zip_fp.write(current_path)
else:
zip_add_directory(zip_fp, current_path)
def create_or_update_lambda_function():
with tempfile.NamedTemporaryFile(suffix=".zip") as fp:
with zipfile.ZipFile(fp.name, "w") as zip_fp:
zip_fp.write("function.rb")
zip_add_directory(zip_fp, "vendor")
try:
result = aws_command(
[
"lambda",
"create-function",
"--environment",
f"Variables={{JWT_SECRET={random.randint(0, 0xFFFFFF)}}}",
"--function-name",
aws_username,
"--handler=function.main",
"--role",
f"arn:aws:iam::{AWS_ACCOUNT_ID}:role/ScalableInternetServicesLambda",
"--runtime=ruby3.2",
"--zip",
f"fileb://{fp.name}",
],
stderr=subprocess.DEVNULL,
)
except subprocess.CalledProcessError:
result = aws_command(
[
"lambda",
"update-function-code",
"--function-name",
aws_username,
"--zip",
f"fileb://{fp.name}",
]
)
return result["FunctionArn"]
def find_or_create_rest_api():
result = aws_command(["apigateway", "get-rest-apis"])
for item in result["items"]:
if item["name"] == aws_username:
return item["id"]
result = aws_command(
[
"apigateway",
"create-rest-api",
"--endpoint-configuration=types=REGIONAL",
"--name",
aws_username,
]
)
return result["id"]
def main():
global aws_username
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} AWS_USERNAME")
return 1
aws_username = sys.argv[1]
function_arn = create_or_update_lambda_function()
print(configure_api_gateway(function_arn))
return 0
if __name__ == "__main__":
sys.exit(main())