logo
Mastering the Creative Alchemy: Generative AI CDK Constructs for Your Apps

Mastering the Creative Alchemy: Generative AI CDK Constructs for Your Apps

Discover how AWS Generative AI CDK Constructs turbocharge app development. These Constructs simplify resource management and empower you to swiftly bring your AI projects to life. Learn to harness their potential in our hands-on guide and join the community shaping the future of AI app development.

Published Dec 21, 2023

As our expertise in building applications with generative AI continues to mature, a pivotal moment in this evolution arrived at re:Invent 2023 with the introduction of the AWS Generative AI Constructs Library. This open-source extension of the AWS Cloud Development Kit (AWS CDK) signifies a significant leap forward in our journey. In this article, we will explore the significance of these CDK constructs, showcasing how they simplify the development of Generative AI-powered applications. Additionally, we will provide a practical example that illustrates how these innovations streamline the process of document summarization.
CDK Constructs are pivotal elements within the AWS Cloud Development Kit (AWS CDK), forming the foundation of AWS CDK applications. These constructs are reusable cloud components that encapsulate the complete set of requirements for creating and configuring AWS resources, either individually or as a collection. They excel at handling the boilerplate logic associated with resource management, allowing developers to concentrate on their core business logic.
Within the domain of CDK Constructs, various categories cater to diverse needs. These categories encompass low-level constructs that are finely tuned for managing individual resources, high-level constructs that furnish extensive functionality with built-in sensibility, and pattern constructs that embody best practices tailored for commonly encountered use cases. Specifically, the newly introduced AWS Generative AI CDK Constructs fall into the category of high-level constructs, tailored to address the unique demands of constructing generative AI applications on AWS.
These specialized Generative AI constructs empower AWS customers with the capability to develop scalable, dependable Generative AI applications. They enable users to articulate intricate infrastructure requirements in a concise and comprehensible manner, thereby accelerating adoption and simplifying the path to production. Furthermore, AWS Generative AI CDK Constructs are built atop the foundation of common AWS CDK constructs and assets, packaging these reusable components into a library that accelerates the development of generative AI use cases. This library is open source on GitHub, offering accessibility in familiar programming languages like JavaScript and Python. Illustrating with a document summarization scenario, we will now delve into the process of incorporating a CDK Construct into your potential application.

To include AWS Generative AI CDK Constructs in your project, you need to follow a few steps:
1) Ensure prerequisites
  • Node >= v20.10, can be downloaded from nodejs.org
  • Typescript >= 5.2.2, to install npm install -g typescript
  • AWS CDK >= 2.111.x: npm install aws-cdk-lib
  • Docker in running mode
  • Amazon Bedrock model access that can be added as instructed in the user guide.
2) Initialize a TypeScript Project: First, open your preferred IDE and create a new folder. Then, create a TypeScript project using the AWS CDK command-line interface (CLI). This will generate the necessary project structure and files.
1
2
mkdir gen-constructs && cd gen-constructs
cdk init app —language typescript
Your project folder structure will look like this:
3) Install Dependencies: Next, you need to install the AWS Generative AI CDK Constructs library as a dependency in your project. When we go to package.json file we can define a new dependency ( "@cdklabs/generative-ai-cdk-constructs": "0.1.11") in the "dependencies". Thus, our package.json will look like as follows:
Now to install this dependency we only need to run npm install in the terminal.
4) Import the Constructs: In our TypeScript file in the lib/ folder, we import the required AWS Generative AI CDK Constructs using import statements: import { SummarizationAppsyncStepfn, SummarizationAppsyncStepfnProps } from '@cdklabs/generative-ai-cdk-constructs';
5) Make sure to import any other necessary dependencies. The construct we’re using required Cognito for authorization, thus, we separately create Cognito User Pool as described in the Developer Guide and add Cognito construct to the file in lib/ folder.
6) Configure the Constructs and initialize them: You can configure the AWS Generative AI CDK Constructs by passing the required properties and options. These configurations will depend on the specific construct you are using, so in the case with the summarization construct, we need to pass the property for Cognito User Pool. We’ll also add the property isFileTransformationRequired to enable the transformation from PDF to text format. The entire file in the lib/ folder then has such code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as cognito from 'aws-cdk-lib/aws-cognito';
import { SummarizationAppsyncStepfn, SummarizationAppsyncStepfnProps } from '@cdklabs/generative-ai-cdk-constructs';

export class GenConstructsStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

// get an existing userpool
const cognitoPoolId = 'us-east-1_1IDj441ey';
const cognitoUserPool = cognito.UserPool.fromUserPoolId(this, 'cognito-pool-123', cognitoPoolId);

const summaryProps : SummarizationAppsyncStepfnProps = {
cognitoUserPool: cognitoUserPool,
isFileTransformationRequired: 'True'
}
const summary = new SummarizationAppsyncStepfn(this, 'summarization', summaryProps);

}
}
7) Deploy Your Project: Finally, we are ready to deploy our project using AWS CDK CLI, which will create the necessary AWS resources based on the code and configurations. Let’s run cdk deploy in the terminal!

To review what we have build in several steps we’ll look into the summary of a concrete document. So firstly, we log into AWS account, go to the S3 service, and upload the document for summarizing to the input asset bucket created by the construct (“input-assets-summary-bucket-dev-...). The screenshots show the usage of AWS Whitepaper on Implementing Microservices on AWS (worth reading cover to cover!).
Then, use the API to trigger the summarization process. You can do this through the AWS AppSync console, providing the document's name and unique job ID.
Follwoing the pub/sub pattern, we start with initiating Subscription on AWS AppSync Console:
Then simultaneously we trigger the summarization in another tab using Mutation with generateSummary():
Now we can observe in the previous tab that there has appeared a subscription and we can look at the status of the summarization job.

Once the summarization process is complete, we have the summary in base64-encoded format!
We can now decode it using any online base64 decoder to view the plain text summary:
Our CDK Construct has successfully completed the summarization job, would you agree?

Have we observed the simplicity and effectiveness of AWS Generative AI CDK Constructs together? By abstracting away the complexity of infrastructure management, these constructs allow developers to focus on creating innovative AI applications. Whether you're working on document summarization, question answering, or other AI tasks, AWS Generative AI CDK Constructs can help you accelerate your projects and bring your ideas to production faster. Give it a try for your next generative AI project, and start building intelligent solutions with ease.
Furthermore, AWS actively encourages community contributions to enrich the catalog of available constructs and expand its capabilities. You can refer to Developer Guide that bootstraps the steps for your contributions. Do you know what Generative AI CDK Construct would you like to build next?