Create a new file for our hit counter construct #
Create a new folder lib
mkdir lib
Create a new file under lib called hitcounter.ts with the following content:
import { Construct } from "constructs";
import { IFunction } from "terraconstructs/lib/aws/compute";
export interface HitCounterProps {
/** the function for which we want to count url hits **/
downstream: 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,idandprops. And as usual, we propagatescopeandidto thecdk.Constructbase class. - The
propsargument is of typeHitCounterPropswhich includes a single propertydownstreamof 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.