Created
October 22, 2017 17:33
-
-
Save kielni/ba90cb0cfbfaa07563f21c5fd632f39c to your computer and use it in GitHub Desktop.
command line deploy for python AWS lambda functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import os | |
import zipfile | |
import boto3 | |
''' | |
create zip, update function code, invoke function, and print result | |
set in environment: | |
FUNCTION_NAME - required | |
AWS_DEFAULT_REGION ie us-west-2 | |
AWS_PROFILE profile name | |
''' | |
zipf = zipfile.ZipFile('../lambda.zip', 'w', zipfile.ZIP_DEFLATED) | |
path = '.' | |
for root, dirs, files in os.walk(path): | |
for file in files: | |
fn = os.path.join(root, file) | |
zipf.write(fn, fn[2:]) | |
zipf.close() | |
print('created zip') | |
client = boto3.client('lambda') | |
function_name = os.environ['FUNCTION_NAME'] | |
print('updating function code') | |
print(client.update_function_code( | |
FunctionName=function_name, | |
ZipFile=open('../lambda.zip', 'rb').read(), | |
)) | |
print('invoking') | |
resp = client.invoke( | |
FunctionName=function_name, | |
InvocationType='RequestResponse', | |
LogType='Tail', | |
) | |
if 'LogResult' in resp: | |
print(base64.b64decode(resp['LogResult'])) | |
else: | |
print(resp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment