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 aHitCounter
. - As usual, arguments for constructs are
scope
,id
andprops
. - The
props
argument is of typeHitCounterProps
which includes a single propertydownstream
of typeawslambda.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.