Automation testing for aws lambda functions in python
I have an aws lambda function that will write s3 file metadata information to dynamodb for every object created into s3 bucket, for that I have an event trigger on s3 bucket. So I am planning to automate testing with python. Can anyone help how I can automate this lambda function to test the following using the unittest package.
- Check existence of dynamodb table
- Check if bucket exists or not in s3 to fire event.
- Check the number of files in the s3 bucket and the number of records in the Dynamodb table.
source to share
This can be done using moto
and unittest
. What moto
will do is add to the stateful mock for AWS - your code can continue the call boto
as usual, but the calls will not actually be made to AWS. Instead, it moto
will create a state in memory.
For example, you could
- Activate layout for DynamoDB
- create DynamoDB table
- Add items to table
- Extract the items from the table and see that they exist.
If you are creating functionality for DynamoDB and S3, you would use methods from mock_s3
and .mock_dynamodb2
moto
I wrote a tutorial on how to do this (it uses pytest
instead unittest
, but that should be a minor difference). Check it out: joshuaballoch.github.io/testing-lambda-functions/
source to share