快速入门指南
本指南将引导您了解安装和使用 @aws/nx-plugin 在 AWS 上快速构建项目的基础知识。
在继续之前,需要以下全局依赖项:
- Git
- Node >= 22(我们建议使用类似 NVM 的工具来管理您的 node 版本)
- 通过运行
node --version来验证
- 通过运行
- UV >= 0.5.29
- 通过运行以下命令安装 Python 3.14:
uv python install 3.14 - 使用
uv python list --only-installed验证
- 通过运行以下命令安装 Python 3.14:
- PNPM >= 11(如果你愿意,也可以使用 Yarn >= 4、Bun >= 1 或 NPM >= 11)
- 通过运行
pnpm --version、yarn --version、bun --version或npm --version来验证
- 通过运行
- 需要配置指向目标 AWS 账户的 AWS Credentials 来部署你的应用程序(以及用于某些本地开发工作流)。
- 某些生成器需要 Docker 或 Finch >= 1.6.0。对于 Docker,必须设置多平台构建;Finch 开箱即支持多平台构建。
- 如果你选择使用 Terraform >= 1.12 作为基础设施即代码工具而不是 CDK,则需要安装它
- 通过运行
terraform --version来验证
- 通过运行
- 如果你正在使用 VSCode,我们建议安装 Nx Console VSCode Plugin。
步骤 1:创建工作空间
Section titled “步骤 1:创建工作空间”使用您选择的包管理器创建一个 Nx 工作空间,并打开它创建的目录:
创建工作区@aws/nx-workspace
pnpm create @aws/nx-workspace my-project yarn create @aws/nx-workspace my-project npm create @aws/nx-workspace -- my-project bun create @aws/nx-workspace my-project构建你的命令3
必需
cd my-project系统会提示您选择基础设施即代码提供商,CDK 或 Terraform。
步骤 2:构建您的应用程序
Section titled “步骤 2:构建您的应用程序”我们将构建一个全栈应用程序:一个 tRPC API、一个 React 网站、Cognito 身份验证以及 CDK 或 Terraform 基础设施。您可以让 AI 助手为您搭建脚手架,或者自己运行生成器——两种方式都能达到相同的工作空间。
-
在您的 AI 编码助手中打开工作空间。
新工作空间已经为 Kiro、Claude Code、Cursor、Gemini CLI、GitHub Copilot 和 OpenAI Codex 配置了插件的 MCP server,因此您的助手可以查找插件的生成器并运行它们,无需任何设置。
-
请求应用程序:
Prompt Create a tRPC API called demo-api, and a React website called demo-website. Add Cognito authentication to the website, connect the website to the API, and add an infrastructure project called infra. Then instantiate the generated infrastructure components so the website and API deploy, granting authenticated users access to the API.
您的助手通过 MCP server 发现生成器并按照依赖关系所需的顺序运行它们。查看它所做的更改,然后继续步骤 3。
使用 CLI
Section titled “使用 CLI”运行搭建应用程序的生成器。使用下面的按钮复制所有命令,或者按照图表下方的说明自己运行每个命令。
根据您正在构建的项目类型,您可以选择任意生成器组合来快速启动您的项目。查看左侧导航栏中的 生成器 以查看完整的选项列表,或尝试使用 图形构建器 以可视化方式构建您的工作空间。
添加 tRPC API
Section titled “添加 tRPC API”运行此生成器@aws/nx-plugin:ts#api
pnpm nx g @aws/nx-plugin:ts#api --no-interactive yarn nx g @aws/nx-plugin:ts#api --no-interactive npx nx g @aws/nx-plugin:ts#api --no-interactive bunx nx g @aws/nx-plugin:ts#api --no-interactive- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - ts#api - 填写必需参数
- 点击
Generate
此步骤的选项2
必需
这将在 packages/demo-api 文件夹中创建 API。
添加 React 网站
Section titled “添加 React 网站”运行此生成器@aws/nx-plugin:ts#website
pnpm nx g @aws/nx-plugin:ts#website --no-interactive yarn nx g @aws/nx-plugin:ts#website --no-interactive npx nx g @aws/nx-plugin:ts#website --no-interactive bunx nx g @aws/nx-plugin:ts#website --no-interactive- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - ts#website - 填写必需参数
- 点击
Generate
此步骤的选项1
必需
这将在 packages/demo-website 中搭建一个新的 React 网站。
添加 Cognito 身份验证
Section titled “添加 Cognito 身份验证”运行此生成器@aws/nx-plugin:ts#website#auth
pnpm nx g @aws/nx-plugin:ts#website#auth --no-interactive yarn nx g @aws/nx-plugin:ts#website#auth --no-interactive npx nx g @aws/nx-plugin:ts#website#auth --no-interactive bunx nx g @aws/nx-plugin:ts#website#auth --no-interactive- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - ts#website#auth - 填写必需参数
- 点击
Generate
此步骤的选项1
必需
这将设置必要的基础设施和 React 代码,以便为您的网站添加 Cognito 身份验证。
连接前端到后端
Section titled “连接前端到后端”运行此生成器@aws/nx-plugin:connection
pnpm nx g @aws/nx-plugin:connection --no-interactive yarn nx g @aws/nx-plugin:connection --no-interactive npx nx g @aws/nx-plugin:connection --no-interactive bunx nx g @aws/nx-plugin:connection --no-interactive- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - connection - 填写必需参数
- 点击
Generate
此步骤的选项2
必需
必需
这将配置必要的提供程序,以确保您的网站可以调用您的 tRPC API。
添加基础设施
Section titled “添加基础设施”根据您选择的 IAC 提供商添加基础设施项目。
运行此生成器@aws/nx-plugin:ts#infra
pnpm nx g @aws/nx-plugin:ts#infra --no-interactive yarn nx g @aws/nx-plugin:ts#infra --no-interactive npx nx g @aws/nx-plugin:ts#infra --no-interactive bunx nx g @aws/nx-plugin:ts#infra --no-interactive- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - ts#infra - 填写必需参数
- 点击
Generate
此步骤的选项1
必需
这将配置一个 CDK 应用程序,您可以使用它在 AWS 上部署基础设施。
运行此生成器@aws/nx-plugin:terraform#project
pnpm nx g @aws/nx-plugin:terraform#project --no-interactive yarn nx g @aws/nx-plugin:terraform#project --no-interactive npx nx g @aws/nx-plugin:terraform#project --no-interactive bunx nx g @aws/nx-plugin:terraform#project --no-interactive- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - terraform#project - 填写必需参数
- 点击
Generate
此步骤的选项1
必需
这将配置一个 Terraform 项目,您可以使用它在 AWS 上部署基础设施。
步骤 3:在本地运行您的网站和 API
Section titled “步骤 3:在本地运行您的网站和 API”使用以下命令为您的网站及其连接的 API 启动本地开发服务器:
pnpm devyarn devnpm run devbun dev您的网站将在 http://localhost:4200 上可用。
对网站和 API 的更改都将实时反映,因为本地网站和 API 服务器都将热重载。
从您的网站调用 API
将网站连接到 API 后会添加一个 useDemoApi 钩子,它返回一个 tRPC options proxy:您的 API 上的每个过程都提供 queryOptions 和 mutationOptions 以传递给 TanStack Query。
更新 packages/demo-website/src/routes/index.tsx 以调用 API 的 echo 过程,在请求进行时显示加载动画:
import { useQuery } from '@tanstack/react-query';import { createFileRoute } from '@tanstack/react-router';import { Spinner } from '../components/spinner';import { useDemoApi } from '../hooks/useDemoApi';
export const Route = createFileRoute('/')({ component: RouteComponent,});
function RouteComponent() { const trpc = useDemoApi(); const echo = useQuery( trpc.echo.queryOptions({ message: 'Hello from the API!' }), );
return ( <div className="text-center"> <header> <h1>Welcome</h1> <p>Welcome to your new React website!</p> </header> {echo.isLoading ? ( <Spinner /> ) : echo.error ? ( <p>Error: {echo.error.message}</p> ) : ( <p>{echo.data?.message}</p> )} </div> );}保存文件,页面将重新加载并显示您的 API 回显的消息。该调用是端到端类型安全的——在 packages/demo-api/src/schema/echo.ts 中重命名 message,您的网站将停止编译,直到您完成更改。
请参阅 React 到 tRPC 连接指南了解变更、订阅、错误处理和乐观更新。
步骤 4:定义云资源并部署到 AWS
Section titled “步骤 4:定义云资源并部署到 AWS”打开 packages/infra/src/stacks/application-stack.ts 并添加以下代码:
import { DemoApi, DemoWebsite, UserIdentity,} from '@my-project/common-constructs';import { Stack, StackProps } from 'aws-cdk-lib';import { Construct } from 'constructs';
export class ApplicationStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props);
const identity = new UserIdentity(this, 'identity'); const api = new DemoApi(this, 'api', { integrations: DemoApi.defaultIntegrations(this).build(), }); api.grantInvokeAccess(identity.identityPool.authenticatedRole);
new DemoWebsite(this, 'website'); }}这就是我们部署全栈应用程序所需编写的全部 CDK 代码。
打开 packages/infra/src/main.tf 并添加以下代码:
# Deploy user identitymodule "user_identity" { source = "../../common/terraform/src/core/user-identity"}
# Shared asset bucket — stages Lambda deployment zips for every lambda / API modulemodule "asset_bucket" { source = "../../common/terraform/src/core/asset-bucket"}
# Deploy APImodule "demo_api" { source = "../../common/terraform/src/app/apis/demo-api"
asset_bucket_name = module.asset_bucket.bucket_name}
# Grant authenticated users access to invoke the APIresource "aws_iam_policy" "api_invoke_policy" { name = "DemoApiInvokePolicy" description = "Policy to allow authenticated users to invoke the API"
policy = jsonencode({ Version = "2012-10-17" Statement = [ { Effect = "Allow" Action = "execute-api:Invoke" Resource = "${module.demo_api.api_execution_arn}/*/*" } ] })}
resource "aws_iam_role_policy_attachment" "authenticated_api_access" { role = module.user_identity.authenticated_role_name policy_arn = aws_iam_policy.api_invoke_policy.arn}
# Deploy websiteprovider "aws" { alias = "us_east_1" region = "us-east-1"}
module "demo_website" { source = "../../common/terraform/src/app/static-websites/demo-website"
providers = { aws.us_east_1 = aws.us_east_1 }
depends_on = [module.user_identity, module.demo_api]}
# Print the website URL and user pool id when the deployment finishesoutput "website_url" { value = module.demo_website.website_url}
output "user_pool_id" { value = module.user_identity.user_pool_id}这就是我们部署全栈应用程序所需编写的全部 Terraform 代码。
构建和部署基础设施
Section titled “构建和部署基础设施”接下来,运行以下命令来构建您的项目:
pnpm buildyarn buildnpm run buildbun build引导您的基础设施:
pnpm nx bootstrap infrayarn nx bootstrap infranpx nx bootstrap infrabunx nx bootstrap infrapnpm nx bootstrap infrayarn nx bootstrap infranpx nx bootstrap infrabunx nx bootstrap infra部署您的项目:
pnpm nx deploy-sandbox infrayarn nx deploy-sandbox infranpx nx deploy-sandbox infrabunx nx deploy-sandbox infrapnpm nx apply infrayarn nx apply infranpx nx apply infrabunx nx apply infra步骤 5:使用已部署的云资源测试网站
Section titled “步骤 5:使用已部署的云资源测试网站”-
获取
runtime-config.json文件:Terminal window pnpm nx load-runtime-config demo-websiteTerminal window yarn nx load-runtime-config demo-websiteTerminal window npx nx load-runtime-config demo-websiteTerminal window bunx nx load-runtime-config demo-website -
启动本地网站服务器
Terminal window pnpm nx serve demo-websiteTerminal window yarn nx serve demo-websiteTerminal window npx nx serve demo-websiteTerminal window bunx nx serve demo-website
您的网站将在 http://localhost:4200 上可用,并将指向您为 API 和身份验证部署的资源。
要登录,您需要在 Cognito 用户池中创建用户。
使用 AWS CLI 创建用户
以下命令在用户池中创建用户。您可以在部署输出中找到用户池 ID,或在 Amazon Cognito 控制台中找到。替换用户名、电子邮件、名字和姓氏以及临时密码的值。
aws cognito-idp admin-create-user \ --user-pool-id <user pool id> \ --username <username> \ --user-attributes Name=email,Value=<email> Name=email_verified,Value=true Name=given_name,Value=<first name> Name=family_name,Value=<last name> \ --temporary-password '<temporary password>' \ --message-action SUPPRESS首次以该用户身份登录时,系统会提示您选择新密码,然后设置多因素身份验证,可以使用通过短信发送的代码或来自身份验证器应用程序的基于时间的一次性密码。
您的网站也通过 CloudFront 提供服务。打开其分发域名(在部署后打印到您的终端)以使用完全在 AWS 上运行的版本。
恭喜!🎉 您已成功使用 @aws/nx-plugin 构建并部署了一个全栈应用程序!
步骤 6:清理
Section titled “步骤 6:清理”完成后,删除您部署的资源以避免持续产生费用:
pnpm nx destroy-sandbox infrayarn nx destroy-sandbox infranpx nx destroy-sandbox infrabunx nx destroy-sandbox infrapnpm nx destroy infrayarn nx destroy infranpx nx destroy infrabunx nx destroy infra要删除引导资源,请按照以下步骤操作:
删除您引导的每个区域(包括 us-east-1)中的引导堆栈。这将删除引导角色和容器镜像存储库:
aws cloudformation delete-stack --stack-name CDKToolkit --region <region>pnpm nx bootstrap-destroy infrayarn nx bootstrap-destroy infranpx nx bootstrap-destroy infrabunx nx bootstrap-destroy infra- Dungeon Adventure 教程 — 构建一个全栈 AI 地牢冒险游戏,获得比本指南更深入的演练。
- 工作空间 — 了解工作空间的布局以及生成器共享的配置。
- 添加到现有项目 — 在您现有的代码库中采用该插件。