Unleashing the Power of Generative AI with AWS Bedrock, AWS CDK, TypeScript

Rushi Patel
AWS Tip
Published in
6 min readOct 27, 2023

--

In the ever-evolving landscape of cloud computing, AWS continues to push the boundaries of what’s possible. With the introduction of AWS Bedrock, a groundbreaking Generative AI service, developers now have the ability to create intelligent applications that can generate rich content, opening doors to a new realm of possibilities. In this blog post, we will explore the synergy between AWS Bedrock, AWS CDK, TypeScript, and the AWS SDK. We’ll dive deep into writing Lambda using Bedrock and AWS SDK, all orchestrated and managed seamlessly through AWS CDK, bringing you the power of Generative AI at your fingertips.

Understanding AWS Bedrock: A Revolution in Generative AI

Before we embark on our journey, let’s grasp the essence of AWS Bedrock. Bedrock is Amazon Web Services’ innovative Generative AI service, designed to generate creative content such as images and text. Its deep learning algorithms empower developers to create applications that can compose music, generate artwork, or even write creatively. Leveraging the generative capabilities of Bedrock, developers can create unique and personalized user experiences that were previously unimaginable.

Explore AWS Bedrock

Accessing AWS Bedrock: Your Gateway to Creative Innovation

Before you can embark on your journey to harness the power of AWS Bedrock’s generative models, you’ll need to gain access to this cutting-edge service. Here’s a step-by-step guide to help you get started:

  1. Login to your AWS Console: Begin by logging into your AWS Management Console using your credentials. If you don’t have an AWS account, you’ll need to create one to proceed.
  2. Navigate to Amazon Bedrock: Once you’re logged in, navigate to the Amazon Bedrock service. You can find it in the AWS Console’s services menu. Click on “Amazon Bedrock” to enter the Bedrock dashboard.
  3. Access Model Permissions: In the Bedrock dashboard, look for the left navigation menu. Among the options available, find and click on “Model Access.” This section is your gateway to requesting permissions for specific Bedrock models.
  4. Request Access: Within the “Model Access” section, you’ll find details about the available generative models, including Jurassic-2, Claude, Command, Stable Diffusion, Amazon Titan. To gain access to a particular model, click on the edit button and then select model name and after selecting the model, click on the “Save Changes” button to confirm your selection and submit your access request.

Note: Access requests might require additional information about your intended use case and the scope of your project. Be prepared to provide relevant details to expedite the approval process.

By following these steps, you initiate the process of accessing AWS Bedrock’s powerful generative models. Once your access request is approved, you’ll be equipped to integrate Bedrock’s capabilities into your applications, unleashing a world of creative innovation and redefining user experiences.

Setting up AWS CDK for Infrastructure as Code

To ensure a repeatable and scalable deployment process, we’ll use AWS CDK as Infrastructure as Code. CDK allows you to define your cloud infrastructure using familiar programming languages like TypeScript, Python, or Java. Let’s quickly go through the setup process:

  1. Install AWS CDK CLI: Follow the official documentation to install the AWS CDK CLI on your local machine.
  2. Initialize a New CDK Project: Create a new directory for your project and run below command to initialize a new CDK project. Here we’re using typescript as a programming language.

cdk init app — language=[csharp|fsharp|go|java|javascript|python|typescript]

3. Install Necessary Dependencies: Install the required AWS SDK and any other dependencies using npm or yarn.

Writing Lambda using Bedrock Runtime Client

To harness the capabilities of Bedrock, we’ll delve into writing Lambda that leverage its generative prowess. Using the @aws-sdk/client-bedrock-runtime library, we can seamlessly integrate Bedrock into our serverless applications. You can write something like below which uses AI21 Labs Jurassic-2 Mid model.

AWS CDK simplifies the process of defining cloud infrastructure, allowing developers to express infrastructure components using familiar programming languages. Here’s an example of how you can define a Bedrock-powered Lambda function using AWS CDK in TypeScript.

Deploying Your AWS CDK Application

Once you have crafted your AWS CDK application with Bedrock-powered Lambdas, it’s time to deploy your infrastructure to the AWS cloud. AWS CDK simplifies this process significantly. After writing your infrastructure as code using TypeScript and the CDK library, you can deploy your application by running the following command in your terminal or command prompt:

cdk deploy

This command initiates the deployment process, creating the specified AWS resources in your AWS account. Ensure you have the necessary AWS credentials configured on your system to perform the deployment. AWS CDK will guide you through the deployment steps and provide details about the resources being created.

Testing Your Lambda Function from AWS Console

Once your AWS CDK application is successfully deployed, you can test your Bedrock-powered Lambda function directly from the AWS Management Console. Here’s how you can do it:

  1. Navigate to AWS Lambda: Go to the AWS Management Console, locate the Lambda service, and click on it to enter the Lambda dashboard.
  2. Locate Your Lambda Function: In the Lambda dashboard, you will find a list of your deployed Lambda functions. Click on the name of the Lambda function that you created using AWS CDK.
  3. Test Your Lambda Function: Inside your Lambda function’s details page, you will find a “Test” button. Click on it to configure and execute a test for your function.
  4. Configure Test Event: Create a new test event by providing a name for the event and configuring the input data that your Lambda function expects. Click “Create” to save the test event.
  5. Run the Test: After creating the test event, click the “Test” button again to execute the test using the configured event data.

Once the test is executed, you should see a response from your Lambda function, indicating the output generated by your Bedrock-powered function. This output will demonstrate the creative content generated by your Lambda, showcasing the capabilities of Bedrock in action.

In addition to testing your Bedrock-powered Lambda function from the AWS Management Console, you can make your Lambda accessible to the outside world using Amazon API Gateway. API Gateway allows you to create REST APIs that act as a bridge between your Lambda functions and external clients, such as web or mobile applications.

You can check my Github repository for more information.

Conclusion

AWS Bedrock’s diverse generative models, coupled with seamless accessibility, open a world of creative possibilities. From interactive gaming experiences and e-commerce product visualisation, content generation for marketing to virtual interior design. Bedrock empowers developers to revolutionise various industries. By harnessing the creative potential of Bedrock, developers can craft innovative applications that resonate with users, offering them unparalleled and personalised content experiences. Start exploring Bedrock’s models today and redefine the way the world experiences creativity.

Cheers :)

--

--

Senior Software Engineer at Cancer Research UK. Combining a passion for coding with a love for cricket on and off the field