Define the HitCounter API

Create a new file for our hit counter construct

Create two new files under src/main/java/com/myorg called with the following content:

package com.myorg;


public interface HitCounterProps {
    // Public constructor for the props builder
    public static Builder builder() {
        return new Builder();

    // The function for which we want to count url hits
    IFunction getDownstream();

    // The builder for the props interface
    public static class Builder {
        private IFunction downstream;

        public Builder downstream(final IFunction function) {
            this.downstream = function;
            return this;

        public HitCounterProps build() {
            if(this.downstream == null) {
                throw new NullPointerException("The downstream property is required!");

            return new HitCounterProps() {
                public IFunction getDownstream() {
                    return downstream;

AND with this content:

package com.myorg;


public class HitCounter extends Construct {
    public HitCounter(final Construct scope, final String id, final HitCounterProps props) {
        super(scope, id);
        // TODO

Save the files.

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 getDownStream of type IFunction and a Builder to create the props. 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.