Overview¶
Setting up Serverless:¶
Within the PyDPS project is the "serverless" application.
You can spin this up by running vagrant up serverless (see the Vagrantfile for specific details on this process).
This vagrant box will run the local playbook vagrant_serverles.yml. This will provision the box and run the mysql role which will install mysql and bring a copy of the attrib and attrib_client db down. It will install and configure awscli, and finally it will run the ansible role serverless and prepare the local directories for development.
Though installing the mysql is a little bit of overkill, it does provide the API development environment with an up to date copy of the tables etc..
Once this provision has finished, you will be presented with an output of text that will give you commands to run.
Note
When you try to run sls from command line initially you may hit one of these issues:
- When you run
slsfrom the command line, you might be asked to log into AWS dont do this.Check you've got the correct~/.aws/credentialsfile. - Run
serverless plugin install -n <plugin-name>for each plug in that has been defined in thepluginsection ofserverless/api/serverless.yml - The ansibles will set up the
~/.aws/config | ~/.aws/credentialsfiles but if serverless has an issue with reading the profile run this
Local Development¶
To develop locally you will need to activate the pyenv and start adding tests. Its not impossible to create a python file and run that, but the lambdas are just python functions, so they should be testable during development using pytest.
Start the venv: source /srv/pydps/serverless/api/.venv/bin/activate
End the venv: deactivate
Run tests with pytest:
cd /srv/pydps/serverless/
pytest tests/file_name.py
auth: /srv/pydps/serverless/api/auth
segments: /srv/pydps/serverless/api/segments_get
visscores: /srv/pydps/serverless/api/visscore_get
General Lambda advice¶
The entry point for each lambda is defined in serverless.yml in each functions handler property. This function much accept an event object, and a context object.
Click here to see the Cubed lambda functions on AWS.
Legacy
If the serverless-plugin-datadog npm package is installed however, the entry point will be changed to datadog_lambda.handler.handler this function will then act as a decorator for your handler that is now defined in the lambda environment vars under DD_LAMBDA_HANDLER