+++ title = "HitCounter API の定義" weight = 100 +++ ## HitCounter コンストラクトのためのファイルを作成 `cdk_workshop` の配下に `hitcounter.py` というファイルを以下の内容で作成します。 ```python from constructs import Construct from aws_cdk import ( aws_lambda as _lambda, ) class HitCounter(Construct): def __init__(self, scope: Construct, id: str, downstream: _lambda.IFunction, **kwargs): super().__init__(scope, id, **kwargs) # TODO ``` ファイルを保存します。 ## コードの解説 * `HitCounter` という新しいコンストラクトを作成しました。 * 今までどおり、コンストラクターの引数は `scope`、`id`、`kwargs` であり、そのまま `cdk.Construct` ベースクラスに伝搬させます。 * また、`HitCounter` クラスは `lambda.IFunction` 型の `downstream` という明示的な引数を持ちます。前の章で作成した Lambda 関数をこの引数に接続することで、アクセス数を数えることができます。 ---- 次に、HitCounter ハンドラーのコードを記述します。