Something that has been consistently annoying while developing Alexa skills is the testing process. Whenever I find myself working on a skill I have to have my IDE, AWS Lambda, Cloud Watch and the Developer Portal all open simultaneously. This becomes cumbersome and inefficient very quickly, and adds time to the testing process. It is especially time consuming when working with files to large to edit in the Lambda console as it requires a re-upload every time you make changes to re-test. My previous solution was to add a quick line to the bottom of the file to invoke the function with the desired event. This works, but you have to add in values for environment variables and remember to revert the changes later.
There is a simple and quick solution to this problem, that allows you to edit the file in your IDE, setup and log responses, and you can directly upload the deployment package without making any changes to you files.
UsageAll you have to do is download the files and add them to the folder where you currently keep your code. If you do this don't download the lambda_function.py
file, if you haven't started yet you can download the lambda_function.py
and start working inside the function.
When using this method paste the event JSON into input.json
. The response JSON will be found in output.json
after the script is run with these commands.
>> cd /path-to-directory
>> python test.py
This can be done in your command line on Windows or Linux and will yield the same results of executing it in Lambda. It should work on OSX, but Python doesn't always work the same.
Using Environment VariablesTo use environment variables you have to add the following code to your file at the top and add the vars.json
file to your folder. The code format for the file is included as well.
import json
try:
with open('vars.json') as data_file:
process = json.load(data_file)
except:
pass
This will allow you to use environment variables while testing. You might want to remove it for final deployment, as it will add a small amount to runtime.
Format variables as such:
{"env": {"var-name": "var-value"}}
ExplanationAll the test.py script does is import your lambda function, pass it the contents of input.json
and write the results to output.json
. This is fairly simple and should be easily replicable in any language of your choice. It also imports a JSON file as process to replace what Lambda normally provides.
The most likely cause of a problem is that you're using a module (maybe an AWS one) in your code that Lambda automatically includes but you haven't included. You should install it locally, the redundancy won't be harmful and you always can choose to not zip the module into your deployment package.
Another possible cause is that you used environment variables and updated them in the console since creating the package. The solution to this is to not zip vars.json
up in your package.
Lastly, check that input.json
is valid JSON, otherwise it won't be loaded successfully.
Comments