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;
using Constructs;
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
andprops
, and we propagate them to theConstruct
base class. - The
props
argument is of typeHitCounterProps
which includes a single propertyDownstream
of typeIFunction
. 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.