Add a table viewer to your stack #
Add the following hightlighted lines to
lib/cdk-workshop-stack.ts
to add a TableViewer
construct to your stack:
import * as cdk from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as apigw from "aws-cdk-lib/aws-apigateway";
import { HitCounter } from "./hitcounter";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import * as path from "path";
import { TableViewer } from 'cdk-dynamo-table-viewer';
export class CdkWorkshopStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const hello = new NodejsFunction(this, "HelloHandler", {
runtime: lambda.Runtime.NODEJS_20_X,
entry: path.join(__dirname, "../lambda/hello.ts"),
handler: "handler",
});
const helloWithCounter = new HitCounter(this, "HelloHitCounter", {
downstream: hello,
});
// defines an API Gateway REST API resource backed by our "hello" function.
new apigw.LambdaRestApi(this, "Endpoint", {
handler: helloWithCounter.handler,
});
new TableViewer(this, 'ViewHitCounter', {
title: 'Hello Hits',
table: //??????
});
}
}
What about the table? #
As you’ll notice, TableViewer
requires that you specify a table
property.
What we want is to somehow access the DynamoDB table behind our hit counter. However, the current API of our hit counter doesn’t expose the table as a public member.
In the next section, we’ll expose our table as a property of HitCounter
so we
can access it from our stack.