serverless-plugin-local-env
This plugin is used to resolve AWS resources in the debugger environment variables when using the serverless framework.
Use case
When debugging with serverless framework, resources created and referenced as part of the framework will often resolve to "[object object]". This makes debugging with serverless framework very difficult as reproducing the lambda environment must then be done with command line attributes.
This plugin will connect with your AWS Stack and its resources in order to resolve "Ref", "Fn::GetAtt" and "Fn::ImportValue" statements when setting up your environment variables for local debugging.
Implementing
Install the plugin into the directory your serverless.yml is located
npm install @moe-tech/serverless-plugin-local-env
Add the plugin to your serverless yaml plugins
plugins:
- "@moe-tech/serverless-plugin-local-env"
Invoke your function
serverless invoke local --function {Function Name}
Cache
In order to improve performance this plugin creates a cache of resources references. This cache is automatically cleared out when a new deployment occurs, but can also be manually cleared by deleting the ".serverless/invoke.cache.json" file.
Example
The example provided is a serverless project which contains a DynamoDB Table, an SNS Topic, an SQS Queue, a Lambda Function and it exports a value.
Steps:
- navigate to the example directory in the console "./example"
- install dependencies
npm ci
- deploy the serverless project
serverless deploy
- Un-comment lines #21 & #22 in the serverless.yml file
- Invoke the function to run locally
or
serverless invoke local --function hello
npm start
- View the console logs which are printed