Create a new file for our hit counter construct #
Create a new file under lib
called hitcounter.ts
with the following content:
import * as cdk from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from "constructs";
export interface HitCounterProps {
/** the function for which we want to count url hits **/
downstream: lambda.IFunction;
}
export class HitCounter extends Construct {
constructor(scope: 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
andprops
. And as usual, we propagatescope
andid
to thecdk.Construct
base class. - The
props
argument is of typeHitCounterProps
which includes a single propertydownstream
of 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.