Define the HitCounter API

Create a new file for our hit counter construct

Create a new file under cdkworkshop called with the following content:

from aws_cdk import (
    aws_lambda as _lambda,

class HitCounter(core.Construct):

    def __init__(self, scope: core.Construct, id: str, downstream: _lambda.IFunction, **kwargs):
        super().__init__(scope, id, **kwargs)

        # TODO

Save the file.

What's going on here?

  • We declared a new construct class called HitCounter.
  • As usual, constructor arguments are scope, id and kwargs, and we propagate them to the cdk.Construct base class.
  • The HitCounter class also takes one explicit keyword parameter downstream of type lambda.IFunction. This is where we are going to “plug in” the Lambda function we created in the previous chapter so it can be hit-counted.

Next, are are going to write the handler code of our hit counter.