Define the HitCounter API

Create a new file for our hit counter construct #

Create a new folder called hitcounter and name a new file hitcounter.go with the following content:

package hitcounter

import (
	"github.com/aws/aws-cdk-go/awscdk/v2/awslambda"
	"github.com/aws/constructs-go/constructs/v10"
)

type HitCounterProps struct {
  // Downstream is the function for which we want to count hits
	Downstream awslambda.IFunction
}

type hitCounter struct {
	constructs.Construct
}

type HitCounter interface {
	constructs.Construct
}

func NewHitCounter(scope constructs.Construct, id string, props *HitCounterProps) HitCounter {
	this := constructs.NewConstruct(scope, &id)

	return &hitCounter{this}
}

Save the file. This doesn’t do anything for now, but we’ll be completing this file shortly.

What’s going on here? #

  • We declared a new construct function called NewHitCounter which will return a HitCounter.
  • As usual, arguments for constructs are scope, id and props.
  • The props argument is of type HitCounterProps which includes a single property downstream of type awslambda.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.

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

More information