Use the hit counter
- 1. TerraConstructs - AWS Intro Workshop: not reached
- 2. Prerequisites: not reached
- 3. AWS CLI: not reached
- 4. AWS Account and User: not reached
- 5. Node.js: not reached
- 6. IDE for your programming language: not reached
- 7. CDKTN CLI Toolkit: not reached
- 8. Terraform CLI: not reached
- 9. TypeScript Workshop: not reached
- 10. New Project: not reached
- 11. CDKTN init: not reached
- 12. Project structure: not reached
- 13. TerraConstructs: not reached
- 14. CDKTN synth: not reached
- 15. CDKTN deploy: not reached
- 16. Hello, CDKTN!: not reached
- 17. Cleanup sample: not reached
- 18. Hello Lambda: not reached
- 19. Optional - Watch: not reached
- 20. API Gateway: not reached
- 21. Writing constructs: not reached
- 22. Define the HitCounter API: not reached
- 23. Hit counter handler: not reached
- 24. Define resources: not reached
- 25. Use the hit counter: not reached (this page)
- 26. CloudWatch Logs: not reached
- 27. Granting permissions: not reached
- 28. Test the hit counter: not reached
- 29. Using construct libraries: not reached
- 30. Learning about the Table Viewer construct: not reached
- 31. Installing the library: not reached
- 32. Add the table viewer to your app: not reached
- 33. Exposing our hit counter table: not reached
- 34. Deploying our app: not reached
- 35. Extra credit: not reached
- 36. Clean up: not reached
- 37. Advanced Topics: not reached
- 38. Testing Constructs: not reached
- 39. Assertion Tests: not reached
- 40. Congrats!: not reached
Add a hit counter to our stack
Okay, our hit counter is ready. Let's use it in our app. Open main.ts and add
the following highlighted code:
import { App } from "cdktn";
import { Construct } from "constructs";
import { AwsStack, AwsStackProps } from "terraconstructs/lib/aws";
import {
Code,
LambdaFunction,
Runtime,
LambdaRestApi,
} from "terraconstructs/lib/aws/compute";
import { HitCounter } from "./lib/hitcounter";
class MyStack extends AwsStack {
constructor(scope: Construct, id: string, props: AwsStackProps) {
super(scope, id, props);
// defines an AWS Lambda resource
const hello = new LambdaFunction(this, "HelloHandler", {
runtime: Runtime.NODEJS_24_X,
code: Code.fromAsset("lambda"),
handler: "hello.handler",
});
const helloWithCounter = new HitCounter(this, "HelloHitCounter", {
downstream: hello,
});
// defines an API Gateway REST API resource backed by our "hello" function.
new LambdaRestApi(this, "Endpoint", {
cloudWatchRole: false,
handler: helloWithCounter.handler,
registerOutputs: true,
});
}
}
const app = new App();
new MyStack(app, "cdk-workshop", {
environmentName: "dev",
gridUUID: "cdk-workshop-dev",
providerConfig: {
region: "us-east-1",
},
});
app.synth();Notice that we changed our API Gateway handler to helloWithCounter.handler
instead of hello. This basically means that whenever our endpoint is hit, API
Gateway will route the request to our hit counter handler, which will log the
hit and relay it over to the hello function. Then, the responses will be
relayed back in the reverse order all the way to the user.
Deploy
cdktn deployIt might take a little while.
And the output:
cdk-workshop
EndpointOutputs = {
"restApiId": "xxxxxxxxxx",
"restApiName": "cdkworkshopEndpoint424A4D39",
"restApiRootResourceId": "yyyyyyyy",
"url": "https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/prod/"
}Test
Okay, ready to give this a go? (you should, again, see the URL of your API in the output of the "deploy" command).
Use curl or your web browser to hit your endpoint (we use -i to show HTTP
response fields and status code):
curl -i https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/prod/Oh no... seems like something went wrong:
HTTP/2 502 Bad Gateway
...
{"message": "Internal server error"}Let's see how to find out what happened and fix it.