Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
deploy-to-lambda.py
# coding: utf-8
import editor
import os.path
import zipfile
import StringIO
import boto3
id='your id'
secret='your secret'
region='your region, e.g. eu-west-1'
# Lambda function name is assumed to be the directory your script is in
current_dir = os.path.dirname(editor.get_path())
lambda_function_name = os.path.basename(current_dir)
def zipdir(path, ziph=None):
count = 0
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
archive_name = os.path.join(root[len(path):], file)
if archive_name.startswith('/'):
archive_name = archive_name[1:]
#print 'Adding: ' + archive_name
ziph.write(os.path.join(root, file), archive_name)
count += 1
#print os.path.join(root, file)
return count
print 'Zipping: ' + current_dir[len(os.path.expanduser('~')):]
in_mem = StringIO.StringIO()
count = 0
with zipfile.ZipFile(in_mem, 'w') as zip_file:
count = zipdir(current_dir, zip_file)
print 'Zipped files: ' + str(count) + ' - Total size: ' + str(in_mem.len)
print 'Deploying: ' + lambda_function_name
b = boto3.client('lambda', aws_access_key_id=id, aws_secret_access_key=secret, region_name=region)
result = b.update_function_code(FunctionName=lambda_function_name, ZipFile=in_mem.getvalue())
print 'Deployed: ' + result['Handler'] + ' at ' + result['FunctionArn']
print ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.