Define the HitCounter API

Create a new file for our hit counter construct

Create a new file under lib called hitcounter.ts with the following content:

import cdk = require('@aws-cdk/core');
import lambda = require('@aws-cdk/aws-lambda');

export interface HitCounterProps {
  /** the function for which we want to count url hits **/
  downstream: lambda.IFunction;

export class HitCounter extends cdk.Construct {
  constructor(scope: cdk.Construct, id: string, props: HitCounterProps) {
    super(scope, id);

    // TODO

Save the file. Oops, an error! No worries, we’ll be using props shortly.

What’s going on here?

  • We declared a new construct class called HitCounter.
  • As usual, constructor arguments are scope, id and props, and we propagate them to the cdk.Construct base class.
  • The props argument is of type HitCounterProps which includes a single property downstream of type lambda.Function. 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.