Exploring AWS Serverless Deployments with CDK v2: From RSS to X Posts - Part 3 of the Odyssey

Exploring AWS Serverless Deployments with CDK v2: From RSS to X Posts - Part 3 of the Odyssey

Welcome to part 3 of “Exploring AWS Serverless Deployments with CDK v2”. Firstly I’d like to thank you for your patience as there’s been a bit of a gap since part 2. I was deep into studying and working on serverless projects at work which kept me away, but i’m excited to get back on track and continue our exploration.

In previous posts, we’ve defined our constructs and deployed them to AWS. Today, we’ll focus on an essential practice: testing. Proper testing ensures that our deployments work as expected and can save us from potential issues.

Getting Started With Testing

To get started, you’ll need to add pytest to your project’s dependencies (the main requirements.txt file for our stack).

1
pip install -r requirements.txt

Within our project, navigate towards the test directory, then unit and open the test_rss_lambda_ddb_socialshare_stack.py file. This auto generated test file includes an example test.

We don’t have an SQS construct in our stack but reviewing the example test provides some level of insight on how to test a construct. Let’s delete the auto generated example test and create our own test.

Setting Up The Testing Function

First, let’s create a reusable function to get the CloudFormation template from the stack:

Testing DynamoDB Table Properties

We’ll start by adding a test to check that the DynamoDB table in our stack has the correct properties. Here’s how to do it:

Testing Lambda Functions

Next, let’s ensure that our stack creates the correct number of Lambda functions and verifies their runtime version:

Running Tests

To run tests you can execute pytest in the terminal:

1
pytest

Below is the output you should receive:

1
2
3
4
5
6
7
8
9
================================================================================= test session starts =================================================================================
platform darwin -- Python 3.12.4, pytest-8.1.1, pluggy-1.4.0
rootdir: /Users/adrian/Developer/Projects/rss-lambda-ddb-socialshare
plugins: typeguard-2.13.3
collected 2 items

tests/unit/test_rss_lambda_ddb_socialshare_stack.py .. [100%]

================================================================================= 2 passed in 21.86s ==================================================================================

Conclusion

In Part 3 of our series, we’ve learned how to test our CDK constructs. In the final installment, we will explore how to test Lambda functions locally.

Resources

Exploring AWS Serverless Deployments with CDK v2: From RSS to X Posts - Part 3 of the Odyssey

https://adrianthegreat.com/2024/07/27/Exploring-AWS-Serverless-Deployments-with-CDK-v2-From-RSS-to-X-Posts-Part-3-of-the-Odyssey/

Author

Adrian Mudzwiti

Posted on

2024-07-27

Updated on

2024-07-28

Licensed under

Comments