From 269e192135d043485de1e4696c37767e4849d78c Mon Sep 17 00:00:00 2001 From: Alan Deutscher Date: Wed, 12 Aug 2020 15:08:41 -0700 Subject: [PATCH 1/2] Fix KeyError if LAMBDASH_FUNCTION is not set, print a prompt telling the user how to fix things if the target function is not found. --- lambdash | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lambdash b/lambdash index b60eb01..1923872 100755 --- a/lambdash +++ b/lambdash @@ -10,11 +10,17 @@ from __future__ import print_function import boto3, json, sys, os, base64 client = boto3.client('lambda', region_name='us-east-1') -response = client.invoke( - InvocationType='RequestResponse', - FunctionName=os.environ['LAMBDASH_FUNCTION'] or 'lambdash', - Payload='{"command":'+json.dumps(" ".join(sys.argv[1:]))+'}') -result = json.load(response['Payload']) +function_name = os.environ.get('LAMBDASH_FUNCTION') or 'lambdash' +try: + response = client.invoke( + InvocationType='RequestResponse', + FunctionName=function_name, + Payload='{"command":'+json.dumps(" ".join(sys.argv[1:]))+'}') + result = json.load(response['Payload']) +except client.exceptions.ResourceNotFoundException: + print('Lambda function not found (Set in LAMBDASH_FUNCTION environment variable).') + print('Attempted function:', function_name) + exit(1) os.write(sys.stdout.fileno(), base64.b64decode(result['stdout'])) os.write(sys.stderr.fileno(), base64.b64decode(result['stderr'])) From fbe93c7a7118495256efd33169439aed03904264 Mon Sep 17 00:00:00 2001 From: Alan Deutscher Date: Wed, 12 Aug 2020 15:19:09 -0700 Subject: [PATCH 2/2] Walk back my try/except idea. The existing exception message is already explicit enough, and the environment variable is covered in documentation. --- lambdash | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lambdash b/lambdash index 1923872..8446826 100755 --- a/lambdash +++ b/lambdash @@ -10,17 +10,11 @@ from __future__ import print_function import boto3, json, sys, os, base64 client = boto3.client('lambda', region_name='us-east-1') -function_name = os.environ.get('LAMBDASH_FUNCTION') or 'lambdash' -try: - response = client.invoke( - InvocationType='RequestResponse', - FunctionName=function_name, - Payload='{"command":'+json.dumps(" ".join(sys.argv[1:]))+'}') - result = json.load(response['Payload']) -except client.exceptions.ResourceNotFoundException: - print('Lambda function not found (Set in LAMBDASH_FUNCTION environment variable).') - print('Attempted function:', function_name) - exit(1) +response = client.invoke( + InvocationType='RequestResponse', + FunctionName=os.environ.get('LAMBDASH_FUNCTION') or 'lambdash', + Payload='{"command":'+json.dumps(" ".join(sys.argv[1:]))+'}') +result = json.load(response['Payload']) os.write(sys.stdout.fileno(), base64.b64decode(result['stdout'])) os.write(sys.stderr.fileno(), base64.b64decode(result['stderr']))