-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_lambda.py
More file actions
326 lines (271 loc) · 10.9 KB
/
Copy pathdeploy_lambda.py
File metadata and controls
326 lines (271 loc) · 10.9 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python3
"""
Deployment script for S3 Upload Lambda function
"""
import boto3
import zipfile
import os
import json
from pathlib import Path
import tempfile
import shutil
def create_lambda_package():
"""Create a deployment package for Lambda"""
# Create temporary directory for package
with tempfile.TemporaryDirectory() as temp_dir:
package_dir = Path(temp_dir) / "package"
package_dir.mkdir()
print("Creating Lambda deployment package...")
# Copy source code
src_files = [
"lambda_handler.py"
]
for item in src_files:
src_path = Path(item)
if src_path.is_file():
shutil.copy2(src_path, package_dir / src_path.name)
print(f" Added: {src_path.name}")
# Create zip file
zip_path = Path("lambda_deployment.zip")
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(package_dir):
for file in files:
file_path = Path(root) / file
arcname = file_path.relative_to(package_dir)
zipf.write(file_path, arcname)
print(f"Package created: {zip_path}")
return zip_path
def deploy_lambda_function(function_name="s3-upload-api", region="us-east-1", bucket_name=None):
"""Deploy the Lambda function"""
# Create package
zip_path = create_lambda_package()
# Initialize clients
lambda_client = boto3.client('lambda', region_name=region)
s3_client = boto3.client('s3', region_name=region)
try:
# Create S3 bucket if it doesn't exist
if bucket_name:
try:
s3_client.head_bucket(Bucket=bucket_name)
print(f"Using existing bucket: {bucket_name}")
except:
print(f"Creating bucket: {bucket_name}")
if region == 'us-east-1':
s3_client.create_bucket(Bucket=bucket_name)
else:
s3_client.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={'LocationConstraint': region}
)
# Set CORS configuration
cors_config = {
'CORSRules': [
{
'AllowedHeaders': ['*'],
'AllowedMethods': ['GET', 'PUT', 'POST', 'DELETE', 'HEAD'],
'AllowedOrigins': ['*'],
'MaxAgeSeconds': 3000
}
]
}
s3_client.put_bucket_cors(Bucket=bucket_name, CORSConfiguration=cors_config)
# Read zip file
with open(zip_path, 'rb') as f:
zip_content = f.read()
# Check if function exists
function_exists = False
try:
lambda_client.get_function(FunctionName=function_name)
function_exists = True
except lambda_client.exceptions.ResourceNotFoundException:
pass
# Environment variables
env_vars = {
'LAMBDA_RUNTIME': 'true',
'PYTHONPATH': '/var/task'
}
if bucket_name:
env_vars['S3_BUCKET_NAME'] = bucket_name
if function_exists:
# Update existing function
print(f"Updating existing function: {function_name}")
response = lambda_client.update_function_code(
FunctionName=function_name,
ZipFile=zip_content
)
# Update environment variables
lambda_client.update_function_configuration(
FunctionName=function_name,
Environment={'Variables': env_vars}
)
else:
# Create new function
print(f"Creating new function: {function_name}")
# Get account ID for role ARN
sts = boto3.client('sts')
account_id = sts.get_caller_identity()['Account']
role_arn = f"arn:aws:iam::{account_id}:role/lambda-s3-execution-role"
response = lambda_client.create_function(
FunctionName=function_name,
Runtime='python3.10',
Role=role_arn,
Handler='lambda_handler.lambda_handler',
Code={'ZipFile': zip_content},
Description='S3 File Upload API Lambda Function',
Timeout=300, # 5 minutes
MemorySize=1024,
Environment={
'Variables': env_vars
}
)
print(f"Function ARN: {response['FunctionArn']}")
# Create API Gateway integration
create_api_gateway(function_name, region)
finally:
# Clean up
if zip_path.exists():
zip_path.unlink()
def create_api_gateway(function_name, region):
"""Create API Gateway for the Lambda function"""
apigateway = boto3.client('apigateway', region_name=region)
lambda_client = boto3.client('lambda', region_name=region)
try:
# Create REST API
api = apigateway.create_rest_api(
name=f'{function_name}-api',
description='S3 Upload API Gateway',
binaryMediaTypes=['*/*'] # Support binary uploads
)
api_id = api['id']
# Get root resource
resources = apigateway.get_resources(restApiId=api_id)
root_id = resources['items'][0]['id']
# Get account ID
sts = boto3.client('sts')
account_id = sts.get_caller_identity()['Account']
lambda_arn = f"arn:aws:lambda:{region}:{account_id}:function:{function_name}"
# Create upload resource
upload_resource = apigateway.create_resource(
restApiId=api_id,
parentId=root_id,
pathPart='upload'
)
# Create upload method
apigateway.put_method(
restApiId=api_id,
resourceId=upload_resource['id'],
httpMethod='POST',
authorizationType='NONE'
)
apigateway.put_integration(
restApiId=api_id,
resourceId=upload_resource['id'],
httpMethod='POST',
type='AWS_PROXY',
integrationHttpMethod='POST',
uri=f"arn:aws:apigateway:{region}:lambda:path/2015-03-31/functions/{lambda_arn}/invocations"
)
# Create download resource with proxy
download_resource = apigateway.create_resource(
restApiId=api_id,
parentId=root_id,
pathPart='download'
)
proxy_resource = apigateway.create_resource(
restApiId=api_id,
parentId=download_resource['id'],
pathPart='{file_key+}'
)
# Create download method
apigateway.put_method(
restApiId=api_id,
resourceId=proxy_resource['id'],
httpMethod='GET',
authorizationType='NONE',
requestParameters={
'method.request.path.file_key': True
}
)
apigateway.put_integration(
restApiId=api_id,
resourceId=proxy_resource['id'],
httpMethod='GET',
type='AWS_PROXY',
integrationHttpMethod='POST',
uri=f"arn:aws:apigateway:{region}:lambda:path/2015-03-31/functions/{lambda_arn}/invocations"
)
# Create files list resource
files_resource = apigateway.create_resource(
restApiId=api_id,
parentId=root_id,
pathPart='files'
)
apigateway.put_method(
restApiId=api_id,
resourceId=files_resource['id'],
httpMethod='GET',
authorizationType='NONE'
)
apigateway.put_integration(
restApiId=api_id,
resourceId=files_resource['id'],
httpMethod='GET',
type='AWS_PROXY',
integrationHttpMethod='POST',
uri=f"arn:aws:apigateway:{region}:lambda:path/2015-03-31/functions/{lambda_arn}/invocations"
)
# Create health resource
health_resource = apigateway.create_resource(
restApiId=api_id,
parentId=root_id,
pathPart='health'
)
apigateway.put_method(
restApiId=api_id,
resourceId=health_resource['id'],
httpMethod='GET',
authorizationType='NONE'
)
apigateway.put_integration(
restApiId=api_id,
resourceId=health_resource['id'],
httpMethod='GET',
type='AWS_PROXY',
integrationHttpMethod='POST',
uri=f"arn:aws:apigateway:{region}:lambda:path/2015-03-31/functions/{lambda_arn}/invocations"
)
# Deploy API
deployment = apigateway.create_deployment(
restApiId=api_id,
stageName='dev'
)
api_url = f"https://{api_id}.execute-api.{region}.amazonaws.com/dev"
print(f"API Base URL: {api_url}")
print(f"Upload endpoint: {api_url}/upload")
print(f"Files list endpoint: {api_url}/files")
print(f"Health endpoint: {api_url}/health")
print(f"Download endpoint: {api_url}/download/{{file_key}}")
# Add Lambda permissions for API Gateway
statement_ids = ['api-gateway-upload', 'api-gateway-download', 'api-gateway-files', 'api-gateway-health']
for statement_id in statement_ids:
try:
lambda_client.add_permission(
FunctionName=function_name,
StatementId=statement_id,
Action='lambda:InvokeFunction',
Principal='apigateway.amazonaws.com',
SourceArn=f"arn:aws:execute-api:{region}:{account_id}:{api_id}/*/*"
)
except lambda_client.exceptions.ResourceConflictException:
pass # Permission already exists
except Exception as e:
print(f"Error creating API Gateway: {e}")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Deploy S3 Upload Lambda Function")
parser.add_argument("--function-name", default="s3-upload-api", help="Lambda function name")
parser.add_argument("--region", default="us-east-1", help="AWS region")
parser.add_argument("--bucket-name", help="S3 bucket name (will be created if doesn't exist)")
args = parser.parse_args()
bucket_name = args.bucket_name or f"great-ai-hackathon-uploads-{args.function_name}"
deploy_lambda_function(args.function_name, args.region, bucket_name)