Create a new file for our hit counter construct #
Create a new file under cdk_workshop called hitcounter.py with the following content:
from constructs import Construct
from aws_cdk import (
aws_lambda as _lambda,
)
class HitCounter(Construct):
def __init__(self, scope: 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,idandkwargs, and we propagate them to thecdk.Constructbase class. - The
HitCounterclass also takes one explicit keyword parameterdownstreamof typelambda.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, we are going to write the handler code of our hit counter.