Skip to content

Commit c0bb594

Browse files
committed
use s3 to upload lambdas
1 parent bd3f9ce commit c0bb594

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

blambda/deploy.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ def publish(name, role, zipfile, options, dryrun):
267267
Returns:
268268
str: the arn of the new or updated function
269269
"""
270-
client = clients.lambda_client
270+
AWS_REGION = "us-east-1"
271+
client = boto3.client('lambda', region_name=AWS_REGION)
272+
s3_client = boto3.client('s3', region_name=AWS_REGION)
273+
271274
options.pop('name', None)
272275
sha = git_sha()
273276
mods = "!" * git_local_mods()
@@ -278,41 +281,36 @@ def publish(name, role, zipfile, options, dryrun):
278281

279282
with open(zipfile, 'rb') as f:
280283
file_bytes = f.read()
281-
print("Function Package: {} bytes".format(len(file_bytes)))
282-
if not dryrun:
283-
try:
284-
# TODO: Remove this once all py 3.8 lambdas are deployed on prod
285-
# Added below code to update lambda runtime first and then deploy it
286-
# cprint("Updating lambda function configuration - todo: remove", 'yellow')
287-
# response = client.update_function_configuration(
288-
# FunctionName=name,
289-
# **options
290-
# )
284+
print("Function Package: {} bytes".format(len(file_bytes)))
291285

292-
# time.sleep(15)
286+
bucket_name = "balihoo-lambda-deployments-prod"
287+
s3_key = f"{name}-{sha}.zip"
293288

289+
if not dryrun:
290+
# Upload to S3
291+
cprint(f"Uploading {zipfile} to s3://{bucket_name}/{s3_key}", 'yellow')
292+
s3_client.upload_file(zipfile, bucket_name, s3_key)
293+
294+
try:
295+
# Update Lambda using S3
294296
cprint("Updating lambda function code", 'yellow')
295297
response = client.update_function_code(
296298
FunctionName=name,
297-
ZipFile=file_bytes
299+
S3Bucket=bucket_name,
300+
S3Key=s3_key
298301
)
299-
300-
time.sleep(15)
301-
302+
time.sleep(5)
302303
cprint("Updating lambda function configuration", 'yellow')
303304
response = client.update_function_configuration(
304305
FunctionName=name,
305306
**options
306307
)
307-
except ClientError as e:
308-
if e.response['Error']['Code'] == 'ResourceNotFoundException':
309-
response = client.create_function(
310-
FunctionName=name,
311-
Code={'ZipFile': file_bytes},
312-
**options
313-
)
314-
else:
315-
raise e
308+
except client.exceptions.ResourceNotFoundException:
309+
response = client.create_function(
310+
FunctionName=name,
311+
Code={'S3Bucket': bucket_name, 'S3Key': s3_key},
312+
**options
313+
)
316314
return response['FunctionName'], response['FunctionArn']
317315
return name, "DRYRUN"
318316

0 commit comments

Comments
 (0)