Define resources
Page 24 of 40Verified:0Not verified:0Done:0
- 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 (this page)
- 25. Use the hit counter: not reached
- 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 resources to the hit counter construct
Now, let's define the AWS Lambda function and the DynamoDB table in our
HitCounter construct. Go back to lib/hitcounter.ts and add the following highlighted code:
import { Construct } from "constructs";
import {
Code,
LambdaFunction,
IFunction,
Runtime,
} from "terraconstructs/lib/aws/compute";
import { AttributeType, Table } from "terraconstructs/lib/aws/storage";
export interface HitCounterProps {
/** the function for which we want to count url hits **/
downstream: IFunction;
}
export class HitCounter extends Construct {
/** allows accessing the counter function */
public readonly handler: LambdaFunction;
constructor(scope: Construct, id: string, props: HitCounterProps) {
super(scope, id);
const table = new Table(this, "Hits", {
partitionKey: { name: "path", type: AttributeType.STRING },
});
this.handler = new LambdaFunction(this, "HitCounterHandler", {
runtime: Runtime.NODEJS_24_X,
handler: "hitcounter.handler",
code: Code.fromAsset("lambda"),
environment: {
DOWNSTREAM_FUNCTION_NAME: props.downstream.functionName,
HITS_TABLE_NAME: table.tableName,
},
});
}
}What did we do here?
This code is hopefully quite easy to understand:
- We defined a DynamoDB table with
pathas the partition key. - We defined a Lambda function which is bound to the
lambda/hitcounter.handlercode. - We wired the Lambda's environment variables to the
functionNameandtableNameof our resources.
Late-bound values
The functionName and tableName properties are values that only resolve when
we deploy our stack (notice that we haven't configured these physical names when
we defined the table/function, only logical IDs). This means that if you print
their values during synthesis, you will get a "TOKEN", which is how the CDK
represents these late-bound values. You should treat tokens as opaque strings.
This means you can concatenate them together for example, but don't be tempted
to parse them in your code.