AWS Logo
Menu
Generate CDK Construct Methods Using Amazon Q Customizations

Generate CDK Construct Methods Using Amazon Q Customizations

Using the Amazon Q Developer Customization we built last week to generate CDK L2 construct methods.

Pahud Hsieh
Amazon Employee
Published Aug 8, 2024
Last week, we talked about building your own Amazon Q customizations to generate AWS CDK L2 constructs for Redshift Serverless. It was a great experience using Q customizations to generate CDK L2 skeletons but it's still not perfect. In AWS CDK, L2 constructs have the from* methods such as fromResourceName(), fromResourceAttributes() or fromResourceArn() which are essential when you reference a resource which is not created by current CDK stack. Can Amazon Q help us generate those construct methods?


Why fromResource*() construct methods are important

The from* methods allow you to create a reference of a resource that:
  1. was not created by AWS CDK, for example, by console or CLI.
  2. was not created or defined in current CDK stack/account/region
With the from* methods, you get the resource interface instead of the concrete instance. For example:
It is very useful because as it returns the interface of the resource, we could reference the attributes of the resource from it. I would say this is one of the most important design of CDK L2 constructs.
What's in the Demo
In this video, I am having a demo to show you:
  1. How to create a linked repository from Amazon CodeCatalyst to a GitHub private repository.
  2. How to launch an Amazon CodeCatalyst DevEnv for VSCode using the linked repo to iterate my development.
  3. How to enable Amazon Q Developer(PRO) in the CodeCatalyst DevEnv.
  4. My best practice using the chat prompt with Q customizations to describe my intention and generate the desired code with good quality.
  5. How to generate similar code using existing code snippets as a pattern.
  6. How to generate in-line documents from the code it generates.
Check out this video. Make sure to watch in 4K 2160p video quality in your big screen.
Lesson Learned
  1. Make your prompt as descriptive as possible. Try to describe a pattern you'd like Q to implement for you instead of a general intention.
  2. After it successfully generates a desired result, ask Q to replicate this to other constructs so you won't need to repeat your pattern description.
  3. As Q optimizations might have learned some patterns from your code base, you still need to validate its output in your IDE and correct all the mistakes when bad output is returned.
  4. By repeating this to generate more L2 constructs and methods, over time it could be a good source to generate yet another customization and it could be like a flywheel to boost your productivity.
  5. Let Q generate the documents for you. Don't write it yourself.
  6. Always use Amazon CodeCatalyst DevEnv with Amazon Q extension for a linked repo so you have an isolated DevEnv and you won't screw up your desktop or laptop machine.
Hope you enjoy it!
Cheers!

Reference

Feedback on Twitter

 

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

Comments