Define the HitCounter API

Create a new file for our hit counter construct #

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

package com.myorg;

import software.amazon.awscdk.services.lambda.IFunction;

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() {
                @Override
                public IFunction getDownstream() {
                    return downstream;
                }
            };
        }
    }
}

AND HitCounter.java with this content:

package com.myorg;

import software.constructs.Construct;

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.

We use analytics to make this content better, but only with your permission.

More information