Define the HitCounter API

Create a new file for our hit counter construct

Create a new file under src/CdkWorkshop called HitCounter.cs with the following content:

using Amazon.CDK;
using Amazon.CDK.AWS.Lambda;

namespace CdkWorkshop
{
    public class HitCounterProps
    {
        // The function for which we want to count url hits
        public IFunction Downstream { get; set; }
    }

    public class HitCounter : Construct
    {
        public HitCounter(Construct scope, string id, HitCounterProps props) : base(scope, id)
        {
            // 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 props, and we propagate them to the Construct base class.
  • The props argument is of type HitCounterProps which includes a single property Downstream of type 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, we are going to write the handler code of our hit counter.