用亚马逊自研AI代码助手一键生成云基础设施+应用代码+画架构图

本文将带大家探索利用Amazon Q Developer CLI AI智能代理如何彻底变革云开发传统模式,把大规模生产级无服务器GenAI应用的构建时间从数天缩短到几分钟。只需一个简单的提示词,Amazon Q Developer CLI智能代理就能自动生成所有必要的基础设施(使用TypeScript),创建Python Lambda函数中的应用代码,并与Amazon Bedrock上的Claude 3 Haiku大模型无缝集成。本教程将演示从概念到部署的完整流程,展示开发者如何借助这一工具专注于AI应用开发。

在本篇文章中,我将演示如何用Amazon Q Developer CLI智能代理如何帮助各位开发者加速应用开发,快速生成完整、可用的无服务器云原生应用代码。虽然AI无法也不应取代人类开发者,但它可以显著加快项目初始阶段的开发环节,使大家的开发速度更快、更高效。Amazon Q生成的每一行代码都可以成功编译,并精准部署到云端,让开发者可以专注于自定义的业务逻辑上,而不是繁重的基础设施搭建和云环境配置。

借助Amazon Q Developer CLI智能代理,大家进需要再不到10分钟的时间内,使用几个简单的提示次完成本应需要数天时间的开发工作。通常这些任务包括阅读开发文档、编写代码、应用测试、排查代码问题和各项手动配置。Amazon Q Developer CLI智能代理可以极大地加速从业务概念到应用部署的过程,彻底改变了开发者开发云应用的模式。

我们仅需要从一个简单的全局描述开始,Amazon Q Developer CLI智能代理就能引导我完成整个开发生命周期——包括生成必要的代码、部署基础设施、排查问题,并创建全面的文档。

最让我印象最深的是Amazon Q Developer CLI智能代理能够在需要我极少的输入下完成整个开发生命周期。这种高智能层级的辅助让复杂的亚马逊云科技架构对各种云技能水平的开发者都可以快速实现,使大家能够专注于创新,而不是基础设施的实现细节。

给Amazon Q Developer智能代理的初始提示词

我输入了如下的一个简单提示词开始了整个应用开发流程:

I want to build a serverless application
with an API Gateway on the frontend receiving user inputs,
Lambda function to process the input and
passing it to Claude 3 Haiku model on Amazon Bedrock.
I want to build the AWS infrastructure using CDK in TypeScript
and Lambda Function in Python.

从这一提示词描述出发,Amazon Q CLI只能代理帮助我完成了以下任务:

  • 理解了我要创建的架构
  • 生成了所有必要的代码文件
  • 正确地构建了项目结构
  • 实现了与亚马逊云科技Bedrock的原生集成
  • 用TypeScript创建了CDK基础设施代码
  • 用Python编写了Lambda函数代码应用逻辑
  • 引导我完成了部署
  • 生成了完整的文档

与Amazon Q Developer CLI智能代理的交互过程

以下是我与Amazon Q Developer CLI代理的在命令行交互对话流程:

$ q chat

Hi, I'm Amazon Q. Ask me anything.

Things to try
• Fix the build failures in this project.
• List my s3 buckets in us-west-2.
• Write unit tests for my application.
• Help me understand my git status

/acceptall    Toggles acceptance prompting for the session.
/help         Show the help dialogue
/quit         Quit the application

> pwd

The current working directory is:

Execute shell command
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I will run the following shell command: pwd

Executing `pwd`...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
/private/tmp/project/QDeveloperBuildOnAWS

🟢 Completed in 0.11s

/private/tmp/project/QDeveloperBuildOnAWS

> I want to build a serverless application with with an API Gateway on the frontend receiving user inputs, Lambda function to process the input and passing it to Claude 3 Haiku model on Bedrock. I want to build the AWS infrastructure using CDK in Typescript and Lambda Function in Python.

I'll help you build a serverless application with API Gateway, Lambda, and Amazon Bedrock's Claude 3 Haiku model using CDK in TypeScript and Lambda in Python. Let's break this down into steps:

## Step 1: Set up the project structure

Let's first check if CDK is installed and create our project structure:

Execute shell command
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I will run the following shell command: 
npm list -g aws-cdk || echo "CDK not found globally"

Enter y to run this tool, otherwise continue chatting.

> y

Executing `npm list -g aws-cdk || echo "CDK not found globally"`...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
/Users/test/.local/share/mise/installs/node/20.12.1/lib
└── aws-cdk@2.1003.0

🟢 Completed in 1.746s

Great! CDK is already installed. Let's create our project structure:

Execute shell command
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I will run the following shell command: 
mkdir -p bedrock-serverless-app/lambda

Enter y to run this tool, otherwise continue chatting.

> y

Executing `mkdir -p bedrock-serverless-app/lambda`...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

🟢 Completed in 0.23s

Execute shell command
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I will run the following shell command: 
cd bedrock-serverless-app && cdk init app --language typescript

Enter y to run this tool, otherwise continue chatting.

> y

Executing `cd bedrock-serverless-app && cdk init app --language typescript`...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

NOTICES         (What's this? https://github.com/aws/aws-cdk/wiki/CLI-Notices)

32775   (cli): CLI versions and CDK library versions have diverged

        Overview: Starting in CDK 2.179.0, CLI versions will no longer be in
                  lockstep with CDK library versions. CLI versions will now be
                  released as 2.1000.0 and continue with 2.1001.0, etc.

        Affected versions: cli: >=2.0.0 <=2.1005.0

        More information at: https://github.com/aws/aws-cdk/issues/32775

If you don’t want to see a notice anymore, use "cdk acknowledge <id>". For example, "cdk acknowledge 32775".
`cdk init` cannot be run in a non-empty directory!

🟢 Completed in 1.178s

Let's create a new directory and initialize the CDK project:

Execute shell command
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I will run the following shell command: 
mkdir -p bedrock-api && cd bedrock-api && cdk init app --language typescript

Enter y to run this tool, otherwise continue chatting.

> y

Executing `mkdir -p bedrock-api && cd bedrock-api && cdk init app --language typescript`...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Applying project template app for typescript
# Welcome to your CDK TypeScript project

This is a blank project for CDK development with TypeScript.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

## Useful commands

* `npm run build`   compile typescript to js
* `npm run watch`   watch for changes and compile
* `npm run test`    perform the jest unit tests
* `npx cdk deploy`  deploy this stack to your default AWS account/region
* `npx cdk diff`    compare deployed stack with current state
* `npx cdk synth`   emits the synthesized CloudFormation template

Initializing a new git repository...
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
The following files have been added, modified, moved, or removed:
********************************************************************

A       .gitignore
A       .npmignore
A       README.md
A       bin/bedrock-api.ts
A       cdk.json
A       jest.config.js
A       lib/bedrock-api-stack.ts
A       package.json
A       test/bedrock-api.test.ts
A       tsconfig.json

********************************************************************
Executing npm install...
npm WARN deprecated inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm WARN deprecated glob@7.2.3: Glob versions prior to v9 are no longer supported
✅ All done!

NOTICES         (What's this? https://github.com/aws/aws-cdk/wiki/CLI-Notices)

32775   (cli): CLI versions and CDK library versions have diverged

        Overview: Starting in CDK 2.179.0, CLI versions will no longer be in
                  lockstep with CDK library versions. CLI versions will now be
                  released as 2.1000.0 and continue with 2.1001.0, etc.

        Affected versions: cli: >=2.0.0 <=2.1005.0

        More information at: https://github.com/aws/aws-cdk/issues/32775

If you don’t want to see a notice anymore, use "cdk acknowledge <id>". For example, "cdk acknowledge 32775".

🟢 Completed in 25.185s

## Step 2: Install required dependencies

Execute shell command
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I will run the following shell command: 
cd bedrock-api && npm install aws-cdk-lib constructs @aws-cdk/aws-apigatewayv2-alpha @aws-cdk/aws-apigatewayv2-integrations-alpha

Enter y to run this tool, otherwise continue chatting.

> y

Executing `cd bedrock-api && npm install aws-cdk-lib constructs @aws-cdk/aws-apigatewayv2-alpha @aws-cdk/aws-apigatewayv2-integrations-alpha`...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

added 3 packages, and audited 339 packages in 2s

35 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

🟢 Completed in 2.864s

Execute shell command
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I will run the following shell command: 
cd bedrock-api && mkdir -p lambda

Enter y to run this tool, otherwise continue chatting.

> y

Executing `cd bedrock-api && mkdir -p lambda`...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

🟢 Completed in 0.23s

## Step 3: Create the Lambda function

Let's create a Python Lambda function that will process user input and call the Bedrock model:

Write to filesystem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Path: /private/tmp/project/QDeveloperBuildOnAWS/bedrock-api/lambda/bedrock_handler.py

+     1: import json
+     2: import boto3
+     3: import os
+     4: 
+     5: # Initialize Bedrock client
+     6: bedrock_runtime = boto3.client('bedrock-runtime')
+     7: 
+     8: def handler(event, context):
+     9:     try:
+    10:         # Parse the request body from API Gateway
+    11:         body = json.loads(event.get('body', '{}'))
+    12:         user_input = body.get('prompt', '')
+    13:         
+    14:         if not user_input:
+    15:             return {
+    16:                 'statusCode': 400,
+    17:                 'headers': {
+    18:                     'Content-Type': 'application/json',
+    19:                     'Access-Control-Allow-Origin': '*'
+    20:                 },
+    21:                 'body': json.dumps({'error': 'No prompt provided'})
+    22:             }
+    23:         
+    24:         # Prepare the request for Claude 3 Haiku
+    25:         request_body = {
+    26:             "anthropic_version": "bedrock-2023-05-31",
+    27:             "max_tokens": 1000,
+    28:             "messages": [
+    29:                 {
+    30:                     "role": "user",
+    31:                     "content": user_input
+    32:                 }
+    33:             ]
+    34:         }
+    35:         
+    36:         # Call the Bedrock model (Claude 3 Haiku)
+    37:         response = bedrock_runtime.invoke_model(
+    38:             modelId='anthropic.claude-3-haiku-20240307-v1:0',  # Claude 3 Haiku model ID
+    39:             body=json.dumps(request_body)
+    40:         )
+    41:         
+    42:         # Parse the response
+    43:         response_body = json.loads(response.get('body').read())
+    44:         model_response = response_body.get('content', [{}])[0].get('text', '')
+    45:         
+    46:         # Return the response
+    47:         return {
+    48:             'statusCode': 200,
+    49:             'headers': {
+    50:                 'Content-Type': 'application/json',
+    51:                 'Access-Control-Allow-Origin': '*'
+    52:             },
+    53:             'body': json.dumps({
+    54:                 'response': model_response
+    55:             })
+    56:         }
+    57:     
+    58:     except Exception as e:
+    59:         print(f"Error: {str(e)}")
+    60:         return {
+    61:             'statusCode': 500,
+    62:             'headers': {
+    63:                 'Content-Type': 'application/json',
+    64:                 'Access-Control-Allow-Origin': '*'
+    65:             },
+    66:             'body': json.dumps({
+    67:                 'error': str(e)
+    68:             })
+    69:         }

Enter y to run this tool, otherwise continue chatting.

> y

Writing to filesystem...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Creating: /private/tmp/project/QDeveloperBuildOnAWS/bedrock-api/lambda/bedrock_handler.py

🟢 Completed in 0.0s

Write to filesystem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Path: /private/tmp/project/QDeveloperBuildOnAWS/bedrock-api/lambda/requirements.txt

+    1: boto3>=1.28.0

Enter y to run this tool, otherwise continue chatting.

> y

Writing to filesystem...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Creating: /private/tmp/project/QDeveloperBuildOnAWS/bedrock-api/lambda/requirements.txt

🟢 Completed in 0.6s

Write to filesystem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Path: bedrock-api/lib/bedrock-api-stack.ts

   1,  1: import * as cdk from 'aws-cdk-lib';
   2,  2: import { Construct } from 'constructs';
-  3    : // import * as sqs from 'aws-cdk-lib/aws-sqs';
+      3: import * as lambda from 'aws-cdk-lib/aws-lambda';
+      4: import * as apigw from 'aws-cdk-lib/aws-apigateway';
+      5: import * as iam from 'aws-cdk-lib/aws-iam';
+      6: import * as path from 'path';
   4,  7: 
   5,  8: export class BedrockApiStack extends cdk.Stack {
   6,  9:   constructor(scope: Construct, id: string, props?: cdk.StackProps) {
   7, 10:     super(scope, id, props);
   8, 11: 
-  9    :     // The code that defines your stack goes here
+     12:     // Create IAM role for Lambda with Bedrock permissions
+     13:     const lambdaRole = new iam.Role(this, 'BedrockLambdaRole', {
+     14:       assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
+     15:       managedPolicies: [
+     16:         iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole'),
+     17:       ],
+     18:     });
+     19: 
+     20:     // Add Bedrock permissions
+     21:     lambdaRole.addToPolicy(new iam.PolicyStatement({
+     22:       actions: [
+     23:         'bedrock:InvokeModel',
+     24:       ],
+     25:       resources: ['*'], // In production, scope this down to specific model ARNs
+     26:     }));
+     27: 
+     28:     // Create Lambda function
+     29:     const bedrockFunction = new lambda.Function(this, 'BedrockFunction', {
+     30:       runtime: lambda.Runtime.PYTHON_3_9,
+     31:       handler: 'bedrock_handler.handler',
+     32:       code: lambda.Code.fromAsset(path.join(__dirname, '../lambda'), {
+     33:         bundling: {
+     34:           image: lambda.Runtime.PYTHON_3_9.bundlingImage,
+     35:           command: [
+     36:             'bash', '-c', [
+     37:               'pip install -r requirements.txt -t /asset-output',
+     38:               'cp bedrock_handler.py /asset-output',
+     39:             ].join(' && '),
+     40:           ],
+     41:         },
+     42:       }),
+     43:       timeout: cdk.Duration.seconds(30),
+     44:       memorySize: 256,
+     45:       role: lambdaRole,
+     46:     });
+     47: 
+     48:     // Create API Gateway
+     49:     const api = new apigw.RestApi(this, 'BedrockApi', {
+     50:       restApiName: 'Bedrock Claude 3 Haiku API',
+     51:       description: 'API Gateway for Bedrock Claude 3 Haiku model',
+     52:       defaultCorsPreflightOptions: {
+     53:         allowOrigins: apigw.Cors.ALL_ORIGINS,
+     54:         allowMethods: apigw.Cors.ALL_METHODS,
+     55:       },
+     56:     });
+     57: 
+     58:     // Create API Gateway resource and method
+     59:     const bedrockResource = api.root.addResource('generate');
+     60:     bedrockResource.addMethod('POST', new apigw.LambdaIntegration(bedrockFunction));
  10, 61: 
- 11    :     // example resource
- 12    :     // const queue = new sqs.Queue(this, 'BedrockApiQueue', {
- 13    :     //   visibilityTimeout: cdk.Duration.seconds(300)
- 14    :     // });
+     62:     // Output the API Gateway URL
+     63:     new cdk.CfnOutput(this, 'ApiUrl', {
+     64:       value: api.url,
+     65:       description: 'URL of the API Gateway',
+     66:     });
  15, 67:   }
- 16    : }
+     68: }

Enter y to run this tool, otherwise continue chatting.

> y

Writing to filesystem...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Replacing: bedrock-api/lib/bedrock-api-stack.ts

🟢 Completed in 0.1s

Write to filesystem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Path: bedrock-api/bin/bedrock-api.ts

   1,  1: #!/usr/bin/env node
+      2: import 'source-map-support/register';
   2,  3: import * as cdk from 'aws-cdk-lib';
   3,  4: import { BedrockApiStack } from '../lib/bedrock-api-stack';
   4,  5: 
   5,  6: const app = new cdk.App();
   6,  7: new BedrockApiStack(app, 'BedrockApiStack', {
   7,  8:   /* If you don't specify 'env', this stack will be environment-agnostic.
   8,  9:    * Account/Region-dependent features and context lookups will not work,
   9, 10:    * but a single synthesized template can be deployed anywhere. */
  10, 11: 
  11, 12:   /* Uncomment the next line to specialize this stack for the AWS Account
  12, 13:    * and Region that are implied by the current CLI configuration. */
  13, 14:   // env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
  14, 15: 
  15, 16:   /* Uncomment the next line if you know exactly what Account and Region you
  16, 17:    * want to deploy the stack to. */
  17, 18:   // env: { account: '123456789012', region: 'us-east-1' },
  18, 19: 
  19, 20:   /* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
  20, 21: });

Enter y to run this tool, otherwise continue chatting.

> y

Writing to filesystem...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Replacing: bedrock-api/bin/bedrock-api.ts

🟢 Completed in 0.0s

Write to filesystem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Path: bedrock-api/README.md

-  1    : # Welcome to your CDK TypeScript project
+      1: # Serverless Bedrock API Application
+      2: 
+      3: This project creates a serverless application with:
+      4: - API Gateway to receive user inputs
+      5: - Lambda function to process the input
+      6: - Integration with Amazon Bedrock's Claude 3 Haiku model
+      7: 
+      8: ## Architecture
+      9: 
+     10: ```
+     11: ┌─────────────┐     ┌─────────────┐     ┌─────────────┐
+     12: │             │     │             │     │             │
+     13: │ API Gateway │────▶│   Lambda    │────▶│   Bedrock   │
+     14: │             │     │             │     │             │
+     15: └─────────────┘     └─────────────┘     └─────────────┘
+     16: ```
+     18: ## Prerequisites
+     19: 
+     20: - AWS CLI configured with appropriate permissions
+     21: - Node.js and npm installed
+     22: - AWS CDK installed (`npm install -g aws-cdk`)
+     23: 
+     24: ## Deployment Instructions
+     25: 
+     26: 1. Install dependencies:
+     27:    ```
+     28:    npm install
+     29:    ```
+     30: 
+     31: 2. Build the TypeScript code:
+     32:    ```
+     33:    npm run build
+     34:    ```
+     35: 
+     36: 3. Deploy the stack:
+     37:    ```
+     38:    cdk deploy
+     39:    ```
+     40: 
+     41: 4. After deployment, the API Gateway URL will be displayed in the outputs.
+     42: 
+     43: ## API Usage
+     44: 
+     45: Send a POST request to the `/generate` endpoint with a JSON body:
   2, 46: 
-  3    : This is a blank project for CDK development with TypeScript.
+     47: ```json
+     48: {
+     49:   "prompt": "Your prompt for the model here"
+     50: }
+     51: ```
   4, 52: 
-  5    : The `cdk.json` file tells the CDK Toolkit how to execute your app.
+     53: Example using curl:
+     54: ```bash
+     55: curl -X POST \
+     56:   https://your-api-id.execute-api.region.amazonaws.com/prod/generate \
+     57:   -H 'Content-Type: application/json' \
+     58:   -d '{"prompt": "Tell me about AWS Bedrock"}'
+     59: ```
   6, 60: 
-  7    : ## Useful commands
+     61: ## Clean Up
   8, 62: 
-  9    : * `npm run build`   compile typescript to js
- 10    : * `npm run watch`   watch for changes and compile
- 11    : * `npm run test`    perform the jest unit tests
- 12    : * `npx cdk deploy`  deploy this stack to your default AWS account/region
- 13    : * `npx cdk diff`    compare deployed stack with current state
- 14    : * `npx cdk synth`   emits the synthesized CloudFormation template
+     63: To remove all resources created by this stack:
+     64: ```
+     65: cdk destroy
+     66: ```

Enter y to run this tool, otherwise continue chatting.

> y

Writing to filesystem...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Replacing: bedrock-api/README.md

🟢 Completed in 0.0s

Write to filesystem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Path: bedrock-api/test/bedrock-api.test.ts

-  1    : // import * as cdk from 'aws-cdk-lib';
-  2    : // import { Template } from 'aws-cdk-lib/assertions';
-  3    : // import * as BedrockApi from '../lib/bedrock-api-stack';
+      1: import * as cdk from 'aws-cdk-lib';
+      2: import { Template } from 'aws-cdk-lib/assertions';
+      3: import * as BedrockApi from '../lib/bedrock-api-stack';
+      4: 
+      5: test('API Gateway and Lambda Created', () => {
+      6:   const app = new cdk.App();
+      7:   // WHEN
+      8:   const stack = new BedrockApi.BedrockApiStack(app, 'MyTestStack');
+      9:   // THEN
+     10:   const template = Template.fromStack(stack);
+     11: 
+     12:   // Verify Lambda function is created
+     13:   template.hasResourceProperties('AWS::Lambda::Function', {
+     14:     Runtime: 'python3.9',
+     15:     Handler: 'bedrock_handler.handler',
+     16:   });
   4, 17: 
-  5    : // example test. To run these tests, uncomment this file along with the
-  6    : // example resource in lib/bedrock-api-stack.ts
-  7    : test('SQS Queue Created', () => {
-  8    : //   const app = new cdk.App();
-  9    : //     // WHEN
- 10    : //   const stack = new BedrockApi.BedrockApiStack(app, 'MyTestStack');
- 11    : //     // THEN
- 12    : //   const template = Template.fromStack(stack);
+     18:   // Verify API Gateway is created
+     19:   template.hasResourceProperties('AWS::ApiGateway::RestApi', {
+     20:     Name: 'Bedrock Claude 3 Haiku API',
+     21:   });
  13, 22: 
- 14    : //   template.hasResourceProperties('AWS::SQS::Queue', {
- 15    : //     VisibilityTimeout: 300
- 16    : //   });
- 17    : });
+     23:   // Verify IAM role with Bedrock permissions
+     24:   template.hasResourceProperties('AWS::IAM::Policy', {
+     25:     PolicyDocument: {
+     26:       Statement: [
+     27:         {
+     28:           Action: 'bedrock:InvokeModel',
+     29:           Effect: 'Allow',
+     30:           Resource: '*',
+     31:         },
+     32:       ],
+     33:     },
+     34:   });
+     35: });

Enter y to run this tool, otherwise continue chatting.

> y

Writing to filesystem...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Replacing: bedrock-api/test/bedrock-api.test.ts

🟢 Completed in 0.1s

## Step 4: Create a simple HTML client (optional)

Write to filesystem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Path: /private/tmp/project/QDeveloperBuildOnAWS/bedrock-api/client/index.html

+      1: <!DOCTYPE html>
+      2: <html lang="en">
+      3: <head>
+      4:     <meta charset="UTF-8">
+      5:     <meta name="viewport" content="width=device-width, initial-scale=1.0">
+      6:     <title>Bedrock Claude 3 Haiku Demo</title>
+      7:     <style>
+      8:         body {
+      9:             font-family: Arial, sans-serif;
+     10:             max-width: 800px;
+     11:             margin: 0 auto;
+     12:             padding: 20px;
+     13:         }
+     14:         .container {
+     15:             display: flex;
+     16:             flex-direction: column;
+     17:             gap: 20px;
+     18:         }
+     19:         textarea {
+     20:             width: 100%;
+     21:             height: 150px;
+     22:             padding: 10px;
+     23:             border-radius: 5px;
+     24:             border: 1px solid #ccc;
+     25:         }
+     26:         button {
+     27:             padding: 10px 20px;
+     28:             background-color: #ff9900;
+     29:             color: white;
+     30:             border: none;
+     31:             border-radius: 5px;
+     32:             cursor: pointer;
+     33:         }
+     34:         button:hover {
+     35:             background-color: #e68a00;
+     36:         }
+     37:         .response {
+     38:             white-space: pre-wrap;
+     39:             padding: 15px;
+     40:             background-color: #f5f5f5;
+     41:             border-radius: 5px;
+     42:             min-height: 100px;
+     43:         }
+     44:         .loading {
+     45:             display: none;
+     46:         }
+     47:     </style>
+     48: </head>
+     49: <body>
+     50:     <div class="container">
+     51:         <h1>Bedrock Claude 3 Haiku 3 Demo</h1>
+     52:         <p>Enter your prompt below to interact with Claude 3 Haiku model:</p>
+     53:         
+     54:         <textarea id="prompt" placeholder="Enter your prompt here..."></textarea>
+     55:         
+     56:         <button id="submit">Generate Response</button>
+     57:         
+     58:         <div class="loading" id="loading">Processing...</div>
+     59:         
+     60:         <div>
+     61:             <h3>Response:</h3>
+     62:             <div class="response" id="response"></div>
+     63:         </div>
+     64:     </div>
+     65: 
+     66:     <script>
+     67:         document.getElementById('submit').addEventListener('click', async () => {
+     68:             const promptText = document.getElementById('prompt').value;
+     69:             const responseElement = document.getElementById('response');
+     70:             const loadingElement = document.getElementById('loading');
+     71:             
+     72:             if (!promptText) {
+     73:                 responseElement.textContent = "Please enter a prompt.";
+     74:                 return;
+     75:             }
+     76:             
+     77:             // Replace with your actual API Gateway URL after deployment
+     78:             const apiUrl = 'YOUR_API_GATEWAY_URL/generate';
+     79:             
+     80:             responseElement.textContent = '';
+     81:             loadingElement.style.display = 'block';
+     82:             
+     83:             try {
+     84:                 const response = await fetch(apiUrl, {
+     85:                     method: 'POST',
+     86:                     headers: {
+     87:                         'Content-Type': 'application/json'
+     88:                     },
+     89:                     body: JSON.stringify({ prompt: promptText })
+     90:                 });
+     91:                 
+     92:                 const data = await response.json();
+     93:                 
+     94:                 if (response.ok) {
+     95:                     responseElement.textContent = data.response;
+     96:                 } else {
+     97:                     responseElement.textContent = `Error: ${data.error || 'Unknown error occurred'}`;
+     98:                 }
+     99:             } catch (error) {
+    100:                 responseElement.textContent = `Error: ${error.message}`;
+    101:             } finally {
+    102:                 loadingElement.style.display = 'none';
+    103:             }
+    104:         });
+    105:     </script>
+    106: </body>
+    107: </html>

Enter y to run this tool, otherwise continue chatting.

> y

Writing to filesystem...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Creating: /private/tmp/project/QDeveloperBuildOnAWS/bedrock-api/client/index.html

🟢 Completed in 0.1s

Now that we've created all the necessary files, let's go through what we've built and how to deploy it:

## Project Overview

I've created a complete serverless application with the following components:

1. CDK Infrastructure (TypeScript):
   • API Gateway to receive user requests
   • Lambda function with appropriate IAM permissions
   • Integration with Amazon Bedrock's Claude 3 Haiku model

2. Lambda Function (Python):
   • Processes incoming requests from API Gateway
   • Calls the Bedrock model with the user's prompt
   • Returns the model's response

3. Simple HTML Client:
   • Basic web interface to interact with your API
   • You'll need to update the API URL after deployment

## Deployment Instructions

1. Navigate to the project directory:
  bash
   cd bedrock-api
   

2. Install dependencies:
  bash
   npm install
   

3. Build the TypeScript code:
  bash
   npm run build
   

4. Deploy the stack:
  bash
   cdk deploy
   

5. After deployment, note the API Gateway URL from the outputs and update it in the client/index.html file.

## How It Works

1. The user sends a request to the API Gateway endpoint with their prompt.
2. API Gateway forwards the request to the Lambda function.
3. The Lambda function:
   • Extracts the prompt from the request
   • Formats it for the Bedrock model
   • Calls the Claude 3 Haiku model via the Bedrock API
   • Returns the model's response
4. API Gateway returns the response to the client

## Important Notes

1. Bedrock Access: Ensure your AWS account has access to the Claude 3 Haiku model in Bedrock.

2. IAM Permissions: The stack creates the necessary IAM role with permissions to invoke Bedrock models.

3. Region Consideration: Bedrock is not available in all AWS regions. Make sure to deploy to a region where Bedrock is available (e.g., us-east-1, us-west-2).

4. Cost Management: Be aware that using Bedrock models incurs costs based on the number of input and output tokens.

5. Production Readiness: For production use, consider adding:
   • Authentication and authorization
   • Request validation
   • Rate limiting
   • More specific IAM permissions
   • Error handling and monitoring

Would you like me to explain any specific part of the implementation in more detail?

项目结构

Amazon Q Developer CLI智能代理自动构建了项目,并创建了所有必要的文件和下级目录:

bedrock-api/
├── bin/
│   └── bedrock-api.ts              # Entry point for CDK app
├── lib/
│   └── bedrock-api-stack.ts        # Main CDK stack definition
├── lambda/
│   ├── bedrock_handler.py          # Python Lambda function
│   └── requirements.txt            # Python dependencies
├── client/
│   └── index.html                  # Simple web client for testing
├── architecture-diagram.drawio     # Architecture diagram (draw.io format)
├── architecture-diagram.jpg        # Architecture diagram (JPEG image)
├── architecture-diagram-ascii.txt  # ASCII art version of diagram
├── README.md                       # Project documentation
├── cdk.json                        # CDK configuration
├── package.json                    # Node.js dependencies
└── tsconfig.json                   # TypeScript configuration

这个项目结构遵循了亚马逊云科技的最佳实践,清晰地分离了:

  • 基础设施代码(CDK TypeScript,存放在 /lib 目录)
  • Lambda函数代码(Python,存放在 /lambda 目录)
  • 客户端应用(HTML/JS,存放在 /client 目录)
  • 文档(包含架构图和README)

使用Amazon Q Developer CLI智能代理进行部署和测试

在完成基础设施代码和Lambda函数的自动生成后,就可以开始部署应用了。以下是部署和测试这个无服务器GenAI应用的完整流程。

当准备部署时,我只需告诉Amazon Q:

let us deploy

Amazon Q立刻可以理解并提示出下一步,并引导我完成部署流程:

构建应用

Amazon Q首先理解出要先构建TypeScript基础设施代码:

cd bedrock-api && npm run build

亚马逊云科技权限配置

当它检测到亚马逊云科技访问权限配置有问题时,Amazon Q会自动提示我提供正确的亚马逊云科技访问权限配置文件,我只需要输入:

use profile aws-demo-profile

成功完成部署

在构建完成并配置了亚马逊云科技权限凭证后,Amazon Q执行了部署操作:

cd bedrock-api && AWS_PROFILE=aws-demo-profile cdk deploy

这个部署过程创建了所有必要的亚马逊云科技云资源,并输出了重要的部署结果信息:

Outputs:
BedrockApiStack.ApiUrl = https://<unique-string>.execute-api.us-east-1.amazonaws.com/prod/

更新前端代码

Amazon Q自动更新了前端端HTML文件,填入了实际的API Gateway URL:

const apiUrl = 'https://<unique-string>.execute-api.us-east-1.amazonaws.com/prod/generate';

测试应用部署

Amazon Q提供了两种方式来测试应用部署是否成功:

直接调用Lambda函数:

Execute shell command
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I will run the following shell command: 
cd bedrock-api && AWS_PROFILE=aws-demo-profile aws lambda invoke --function-name BedrockApiStack-BedrockFunctionADEDD58A-g7Hd22vs8fS7 --payload '{"body": "{\"prompt\": \"Hello, how are you?\"}"}' --region us-east-1 --cli-binary-format raw-in-base64-out response.json

Enter y to run this tool, otherwise continue chatting.

> y

Executing `cd bedrock-api && AWS_PROFILE=aws-demo-profile aws lambda invoke --function-name BedrockApiStack-BedrockFunctionADEDD58A-g7Hd22vs8fS7 --payload '{"body": "{\"prompt\": \"Hello, how are you?\"}"}' --region us-east-1 --cli-binary-format raw-in-base64-out response.json`...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
{
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}

🟢 Completed in 2.919s
Execute shell command
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I will run the following shell command: 
cd bedrock-api && cat response.json

Enter y to run this tool, otherwise continue chatting.

> y

Executing `cd bedrock-api && cat response.json`...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
{"statusCode": 200, "headers": {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}, "body": "{\"response\": \"Hello! As an AI language model, I don't have feelings or a physical body, but I'm functioning well and ready to assist you with any questions or tasks you might have. How can I help you today?\"}"}

🟢 Completed in 0.21s

使用curl访问API端点:

如果两种测试均成功,确认应用已经正确部署并正常运行。

API endpoint testing with curl:
Execute shell command
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I will run the following shell command: 
cd bedrock-api && AWS_PROFILE=aws-demo-profile curl -X POST https://<unique-string>.execute-api.us-east-1.amazonaws.com/prod/generate -H "Content-Type: application/json" -d '{"prompt": "What is AWS Bedrock?"}'

Enter y to run this tool, otherwise continue chatting.

> y

Executing `cd bedrock-api && AWS_PROFILE=aws-demo-profile curl -X POST https://<unique-string>.execute-api.us-east-1.amazonaws.com/prod/generate -H "Content-Type: application/json" -d '{"prompt": "What is AWS Bedrock?"}'`...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1208  100  1174  100    34    379     11  0:00:03  0:00:03 --:--:--   390
{"response": "AWS Bedrock is a new managed foundation service for building and deploying large language models (LLMs) on AWS.\n\nSome key points about AWS Bedrock:\n\n1. It provides a managed infrastructure for training, tuning, and deploying custom LLMs, allowing customers to focus on developing their models rather than managing the underlying infrastructure.\n\n2. It supports a variety of pre-trained model architectures and enables customers to fine-tune these models for their specific use cases.\n\n3. Bedrock integrates with other AWS services like Amazon SageMaker for model training and deployment, and Amazon Lambda for serverless model hosting.\n\n4. It offers features like model versioning, model monitoring, and automatic model scaling to simplify the management of LLMs in production.\n\n5. Bedrock is designed to make it easier for customers to experiment with and deploy custom LLMs for a variety of use cases such as natural language processing, content generation, and decision support.\n\nOverall, AWS Bedrock aims to lower the barriers to entry for building and deploying large language models by providing a fully managed platform on the AWS cloud."}

🟢 Completed in 3.136s

排查部署问题

在尝试使用CDK进行应用部署时,Q CLI代理遇到了一个Docker相关的错误。它立即:

  • 识别出了问题(Docker未运行,导致CDK无法进行打包)
  • 提出了一个解决方案(修改CDK堆栈,采用更简单的部署方式)
  • 进行了必要的代码更改

Amazon Q进行了精准的代码调整:

// Changed from complex Docker bundling approach
const bedrockFunction = new lambda.Function(this, 'BedrockFunction', {
  // ...
  code: lambda.Code.fromAsset(path.join(__dirname, '../lambda'), {
    bundling: {
      image: lambda.Runtime.PYTHON_3_9.bundlingImage,
      command: [
        'bash', '-c', [
          'pip install -r requirements.txt -t /asset-output',
          'cp bedrock_handler.py /asset-output',
        ].join(' && '),
      ],
    },
  }),
  // ...
});

// Changed to simpler approach
const bedrockFunction = new lambda.Function(this, 'BedrockFunction', {
  // ...
  code: lambda.Code.fromAsset(path.join(__dirname, '../lambda')),
  // ...
});

生成文档

当我指示代码助手生成架构图时,Amazon Q不仅生成了架构图,还额外提供了:

  • 详细的draw.io架构图文件
  • ASCII版架构图,以支持文本格式的文档
  • README.md文件中自动插入架构图
  • HTML查看器,用于以JPEG格式导出架构图

我仅仅输入了一个简单的指示,Amazon Q就完成了整个的文档生成工作。

> please generate an architecture diagram for this application that I can load on drawio

架构图

Amazon Q生成的架构图清晰地展示了之前生成的应用的数据流:

> please generate an architecture diagram for this application that I can load on drawio

I'll create an architecture diagram for your serverless application that you can load in draw.io. Let me generate the XML content for a draw.io diagram.

Write to filesystem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Path: /private/tmp/project/QDeveloperBuildOnAWS/bedrock-api/architecture-diagram.drawio

+      1: <mxfile host="app.diagrams.net" modified="2025-03-18T04:50:39.000Z" agent="Mozilla/5.0" etag="diagram_etag" version="21.7.5">
+      2:   <diagram id="serverless-bedrock-app" name="Serverless Bedrock Application">
+      3:     <mxGraphModel dx="1422" dy="762" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1100" pageHeight="850" math="0" shadow="0">
+      4:       <root>
+      5:         <mxCell id="0" />
+      6:         <mxCell id="1" parent="0" />
+      7:         
+      8:         <!-- Title -->
+      9:         <mxCell id="title" value="Serverless Bedrock Application Architecture" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;fontStyle=1" vertex="1" parent="1">
+     10:           <mxGeometry x="250" y="20" width="600" height="40" as="geometry" />
+     11:         </mxCell>
+     12:         
+     13:         <!-- Client -->
+     14:         <mxCell id="client" value="Client Application" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;fontSize=14;" vertex="1" parent="1">
+     15:           <mxGeometry x="120" y="200" width="160" height="80" as="geometry" />
+     16:         </mxCell>
+     17:         
+     18:         <!-- API Gateway -->
+     19:         <mxCell id="apiGateway" value="Amazon API Gateway" style="sketch=0;points=[[0,0,0],[0.25,0,0],[0.5,0,0],[0.75,0,0],[1,0,0],[0,1,0],[0.25,1,0],[0.5,1,0],[0.75,1,0],[1,1,0],[0,0.25,0],[0,0.5,0],[0,0.75,0],[1,0.25,0],[1,0.5,0],[1,0.75,0]];outlineConnect=0;fontColor=#232F3E;gradientColor=#FF4F8B;gradientDirection=north;fillColor=#BC1356;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=14;fontStyle=0;aspect=fixed;shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.api_gateway;" vertex="1" parent="1">
+     20:           <mxGeometry x="400" y="200" width="80" height="80" as="geometry" />
+     21:         </mxCell>
+     22:         
+     23:         <!-- Lambda -->
+     24:         <mxCell id="lambda" value="AWS Lambda&#xa;(Python)" style="sketch=0;points=[[0,0,0],[0.25,0,0],[0.5,0,0],[0.75,0,0],[1,0,0],[0,1,0],[0.25,1,0],[0.5,1,0],[0.75,1,0],[1,1,0],[0,0.25,0],[0,0.5,0],[0,0.75,0],[1,0.25,0],[1,0.5,0],[1,0.75,0]];outlineConnect=0;fontColor=#232F3E;gradientColor=#F78E04;gradientDirection=north;fillColor=#D05C17;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=14;fontStyle=0;aspect=fixed;shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.lambda;" vertex="1" parent="1">
+     25:           <mxGeometry x="600" y="200" width="80" height="80" as="geometry" />
+     26:         </mxCell>
+     27:         
+     28:         <!-- Bedrock -->
+     29:         <mxCell id="bedrock" value="Amazon Bedrock&#xa;(Claude 3 Haiku)" style="sketch=0;points=[[0,0,0],[0.25,0,0],[0.5,0,0],[0.75,0,0],[1,0,0],[0,1,0],[0.25,1,0],[0.5,1,0],[0.75,1,0],[1,1,0],[0,0.25,0],[0,0.5,0],[0,0.75,0],[1,0.25,0],[1,0.5,0],[1,0.75,0]];outlineConnect=0;fontColor=#232F3E;gradientColor=#4AB29A;gradientDirection=north;fillColor=#116D5B;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=14;fontStyle=0;aspect=fixed;shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.sagemaker;" vertex="1" parent="1">
+     30:           <mxGeometry x="800" y="200" width="80" height="80" as="geometry" />
+     31:         </mxCell>
+     32:         
+     33:         <!-- IAM Role -->
+     34:         <mxCell id="iamRole" value="IAM Role" style="sketch=0;outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#BF0816;strokeColor=none;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=14;fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.role;" vertex="1" parent="1">
+     35:           <mxGeometry x="602" y="360" width="78" height="44" as="geometry" />
+     36:         </mxCell>
+     37:         
+     38:         <!-- CDK -->
+     39:         <mxCell id="cdk" value="AWS CDK&#xa;(TypeScript)" style="sketch=0;outlineConnect=0;fontColor=#232F3E;gradientColor=none;fillColor=#232F3D;strokeColor=none;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=center;html=1;fontSize=14;fontStyle=0;aspect=fixed;pointerEvents=1;shape=mxgraph.aws4.template;" vertex="1" parent="1">
+     40:           <mxGeometry x="400" y="360" width="80" height="78" as="geometry" />
+     41:         </mxCell>
+     42:         
+     43:         <!-- Connections -->
+     44:         <mxCell id="clientToApi" value="HTTPS POST" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;fontSize=12;" edge="1" parent="1" source="client" target="apiGateway">
+     45:           <mxGeometry relative="1" as="geometry" />
+     46:         </mxCell>
+     47:         
+     48:         <mxCell id="apiToLambda" value="Invoke" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;fontSize=12;" edge="1" parent="1" source="apiGateway" target="lambda">
+     49:           <mxGeometry relative="1" as="geometry" />
+     50:         </mxCell>
+     51:         
+     52:         <mxCell id="lambdaToBedrock" value="InvokeModel" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;fontSize=12;" edge="1" parent="1" source="lambda" target="bedrock">
+     53:           <mxGeometry relative="1" as="geometry" />
+     54:         </mxCell>
+     55:         
+     56:         <mxCell id="iamToLambda" value="Permissions" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;fontSize=12;" edge="1" parent="1" source="iamRole" target="lambda">
+     57:           <mxGeometry relative="1" as="geometry">
+     58:             <Array as="points">
+     59:               <mxPoint x="640" y="320" />
+     60:               <mxPoint x="640" y="320" />
+     61:             </Array>
+     62:           </mxGeometry>
+     63:         </mxCell>
+     64:         
+     65:         <mxCell id="cdkToResources" value="Deploys" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;fontSize=12;entryX=0.5;entryY=1;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="cdk" target="apiGateway">
+     66:           <mxGeometry relative="1" as="geometry">
+     67:             <Array as="points">
+     68:               <mxPoint x="440" y="320" />
+     69:               <mxPoint x="440" y="320" />
+     70:             </Array>
+     71:           </mxGeometry>
+     72:         </mxCell>
+     73:         
+     74:         <!-- Data Flow -->
+     75:         <mxCell id="dataFlow1" value="1. User sends prompt" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" vertex="1" parent="1">
+     76:           <mxGeometry x="250" y="170" width="120" height="20" as="geometry" />
+     77:         </mxCell>
+     78:         
+     79:         <mxCell id="dataFlow2" value="2. API forwards request" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" vertex="1" parent="1">
+     80:           <mxGeometry x="480" y="170" width="120" height="20" as="geometry" />
+     81:         </mxCell>
+     82:         
+     83:         <mxCell id="dataFlow3" value="3. Lambda calls Bedrock" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" vertex="1" parent="1">
+     84:           <mxGeometry x="680" y="170" width="120" height="20" as="geometry" />
+     85:         </mxCell>
+     86:         
+     87:         <mxCell id="dataFlow4" value="4. Response returned to user" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" vertex="1" parent="1">
+     88:           <mxGeometry x="480" y="300" width="160" height="20" as="geometry" />
+     89:         </mxCell>
+     90:         
+     91:         <!-- Legend -->
+     92:         <mxCell id="legend" value="Legend" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=30;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;whiteSpace=wrap;html=1;fontSize=14;" vertex="1" parent="1">
+     93:           <mxGeometry x="800" y="360" width="200" height="150" as="geometry" />
+     94:         </mxCell>
+     95:         
+     96:         <mxCell id="legendItem1" value="Client: HTML/JS Web Interface" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;fontSize=12;" vertex="1" parent="legend">
+     97:           <mxGeometry y="30" width="200" height="30" as="geometry" />
+     98:         </mxCell>
+     99:         
+    100:         <mxCell id="legendItem2" value="API Gateway: REST API Endpoint" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;fontSize=12;" vertex="1" parent="legend">
+    101:           <mxGeometry y="60" width="200" height="30" as="geometry" />
+    102:         </mxCell>
+    103:         
+    104:         <mxCell id="legendItem3" value="Lambda: Python Function" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;fontSize=12;" vertex="1" parent="legend">
+    105:           <mxGeometry y="90" width="200" height="30" as="geometry" />
+    106:         </mxCell>
+    107:         
+    108:         <mxCell id="legendItem4" value="Bedrock: Claude 3 Haiku Model" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;fontSize=12;" vertex="1" parent="legend">
+    109:           <mxGeometry y="120" width="200" height="30" as="geometry" />
+    110:         </mxCell>
+    111:         
+    112:         <!-- Footer -->
+    113:         <mxCell id="footer" value="Deployed with AWS CDK in TypeScript" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=2" vertex="1" parent="1">
+    114:           <mxGeometry x="350" y="500" width="400" height="30" as="geometry" />
+    115:         </mxCell>
+    116:       </root>
+    117:     </mxGraphModel>
+    118:   </diagram>
+    119: </mxfile>

Enter y to run this tool, otherwise continue chatting.

> y

Writing to filesystem...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Creating: /private/tmp/project/QDeveloperBuildOnAWS/bedrock-api/architecture-diagram.drawio

🟢 Completed in 0.0s

当我在draw.io上导入该架构图时,它完全符合我们的架构要求,如下图所示:

稍后我向Q Developer CLI代理输入“将此架构图转换为ASCII表示”,它会生成以下内容:

> render this diagram as ASCII representation.

For those who prefer text-based diagrams, here's an ASCII representation:

                                SERVERLESS BEDROCK APPLICATION ARCHITECTURE

┌─────────────────┐     HTTP POST    ┌─────────────────┐     Invoke       ┌─────────────────┐    InvokeModel   ┌─────────────────┐
│                 │ ───────────────► │                 │ ───────────────► │                 │ ───────────────► │                 │
│  Client         │                  │  API Gateway    │                  │  Lambda         │                  │  Bedrock        │
│  Application    │ ◄─────────────── │  REST API       │ ◄─────────────── │  Function       │ ◄─────────────── │  Claude 3 Haiku │
│  (HTML/JS)      │     Response     │                 │     Response     │  (Python)       │     Response     │                 │
│                 │                  │                 │                  │                 │                  │                 │
└─────────────────┘                  └─────────────────┘                  └─────────────────┘                  └─────────────────┘
                                            ▲                                     ▲
                                            │                                     │
                                            │                                     │
                                     Deploys│                              Grants │Permissions
                                            │                                     │
                                            │                                     │
                                     ┌─────────────────┐                  ┌─────────────────┐
                                     │                 │                  │                 │
                                     │  AWS CDK        │                  │  IAM Role       │
                                     │  (TypeScript)   │                  │                 │
                                     │                 │                  │                 │
                                     └─────────────────┘                  └─────────────────┘

DATA FLOW:
1. User sends prompt from client application to API Gateway
2. API Gateway forwards the request to Lambda function
3. Lambda function calls Bedrock with the prompt
4. Bedrock generates response and returns to Lambda
5. Lambda formats response and returns via API Gateway
6. Client displays the response to the user

COMPONENTS:
• Client Application: HTML/JS web interface for user interaction
• API Gateway: REST API endpoint (/generate) accepting POST requests
• Lambda Function: Python code that processes requests and calls Bedrock
• Bedrock: Claude 3 Haiku model for generating AI responses
• IAM Role: Provides necessary permissions for Lambda to call Bedrock
• CDK: Infrastructure as code using TypeScript to deploy all resources

最终结果

从一个简单的架构描述出发,我们通过Amazon Q Developer CLI智能代理完成了以下任务:

  • 使用AWS CDK创建了完整的无服务器GenAI应用
  • 实现了与Amazon Bedrock的Claude 3 Haiku模型集成
  • 成功部署了所有资源到亚马逊云科技
  • 测试了应用的功能
  • 生成了全面的架构文档

最终我们通过这个应用的API发送提示词,并接收Claude 3 Haiku生成的AI回复,所有流程都运行在无服务器架构上。

总结 - Amazon Q Developer CLI代理的强大之处

Amazon Q Developer CLI代理在本项目中展现了卓越的能力:

  • 理解需求:能够将架构的总体描述转换为完整的应用架构
  • 全栈开发:生成了基础设施代码和应用代码
  • 问题解决能力:识别并解决了部署中的问题
  • AWS深度集成:执行了正确的AWS命令进行部署和测试
  • 文档生成能力:创建了全面的架构图和文档

整个开发过程——从最初的构思到部署成为可用的应用,我仅仅输入了几个简单的提示词。这标志着开发者与云平台的交互和开发方式的重大改变,使复杂架构可以自动生成和构建,并极大地缩短了从业务想法到实现的时间。Amazon Q Developer CLI智能代理真正改变了开发者的使用体验,它能够在最少的指导下处理复杂任务,让大家专注于创新,而不是技术和云基础设施实现细节。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值