Testing that a Lambda has been deployed correctly

To test a deployed Lambda, you can run the following command:

$ aws lambda invoke --invocation-type Event 
--function-name lambda-dynamo-data-api-sam --region eu-west-1
--payload file://../../sample_data/request-api-gateway-get-valid.json outputfile.tmp

To automate it, we can put the following code into a shell script, serverless-microservice-data-api/bash/apigateway-lambda-dynamodb/invoke-lambda.sh:

#!/bin/sh
. ./common-variables.sh
rm outputfile.tmp
status_code=$(aws lambda invoke --invocation-type RequestResponse
--function-name ${template}-sam --region ${region}
--payload file://../../sample_data/request-api-gateway-get-valid.json
outputfile.tmp --profile ${profile})
echo "$status_code"
if echo "$status_code" | grep -q "200";
then
cat outputfile.tmp
if grep -q error outputfile.tmp;
then
echo " error in response"
exit 1
else
echo " pass"
exit 0
fi
else
echo " error status not 200"
exit 1
fi

We invoke the Lambda, but also check the response given in the outputfile.tmp file using the grep command. We return an exit code of 1 if an error is detected, and 0 otherwise. This allows you to chain logic when involved by other tools or by CI/CD steps.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset