Lambda functions often get triggered by several AWS events (e.g an EventBridge event).
I am developing a python lambda that once triggered by an EventBridge event (that matches a pattern for Security Hub), creates tickets on Jira.
My question is: How can one develop a such function, locally, without having the actual triggers? Is there a way to mock the EventBridge Events?
I am aware of the existence of moto (https://github.com/spulec/moto) but I have a hard time understanding how it works
CodePudding user response:
Lambda-functions can be invoked locally like any other Python function, as long as you know the input-parameters. Some example events that are send by SecurityHub can be found here: https://docs.aws.amazon.com/securityhub/latest/userguide/securityhub-cwe-event-formats.html
Mocking EventBridge could be simply done by manually curating a set of representative events, based on that format, and verifying that the Lambda-function behaves as it should when invoked with one of those events.
Moto mocks all boto3-calls for you, and mimicks the AWS behaviour as closely as possible. That means you can execute any of your business logic as part of your tests, without having to worry about creating resources in AWS, and without having to mock things manually.
Some simple examples can be found here: http://docs.getmoto.org/en/latest/docs/getting_started.html
However:
Moto does not yet implement the EventBridge->Lambda implementation, so that will not help you in this situation. (Only CloudWatch/SQS integration is supported as of version 2.3.0).
CodePudding user response:
Instead of using lambda you can run your python code locally. To do that you need to follow these requirements:
- download python
- download boto3 using this command:
pip3 install boto3
orpip install boto3
- download awscli and configure credentials
- run this command:
python filename.py
orpython3 filename.py
In lambda there was a requirement to write the code in "def lamdba_handler()". But in this case you can write usual python code without this function