快速入门指南
本指南将引导您了解安装和使用 @aws/nx-plugin 在 AWS 上快速构建项目的基础知识。
在继续之前,需要以下全局依赖项:
- Git
- Node >= 22(我们建议使用类似 NVM 的工具来管理您的 node 版本)
- 通过运行
node --version来验证
- 通过运行
- UV >= 0.5.29
- 通过运行以下命令安装 Python 3.14:
uv python install 3.14.0 - 使用
uv python list --only-installed验证
- 通过运行以下命令安装 Python 3.14:
- PNPM >= 11(如果你愿意,也可以使用 Yarn >= 4、Bun >= 1 或 NPM >= 10)
- 通过运行
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:初始化一个新的 Nx 工作空间
Section titled “步骤 1:初始化一个新的 Nx 工作空间”运行以下命令,使用您选择的包管理器创建一个 Nx 工作空间:
pnpm create @aws/nx-workspace my-projectyarn create @aws/nx-workspace my-projectnpm create @aws/nx-workspace -- my-projectbun create @aws/nx-workspace my-project完成后,导航到项目目录:
cd my-project步骤 2:使用生成器搭建您的项目
Section titled “步骤 2:使用生成器搭建您的项目”在本快速入门指南中,我们将添加一个 tRPC API、React 网站、Cognito 身份验证以及 CDK 或 Terraform 基础设施。根据您正在构建的项目类型,您可以选择任意生成器组合来快速启动您的项目。查看左侧导航栏中的 生成器 以查看完整的选项列表,或尝试使用 图形构建器 以可视化方式构建您的工作空间。
作为快捷方式,您可以使用下面的按钮复制命令来搭建完整的应用程序。
否则,请按照以下步骤自行运行每个生成器。
添加 tRPC API
Section titled “添加 tRPC API”pnpm nx g @aws/nx-plugin:ts#api --name=demo-api --framework=trpc --auth=iam --no-interactiveyarn nx g @aws/nx-plugin:ts#api --name=demo-api --framework=trpc --auth=iam --no-interactivenpx nx g @aws/nx-plugin:ts#api --name=demo-api --framework=trpc --auth=iam --no-interactivebunx nx g @aws/nx-plugin:ts#api --name=demo-api --framework=trpc --auth=iam --no-interactive您还可以执行试运行以查看哪些文件会被更改
pnpm nx g @aws/nx-plugin:ts#api --name=demo-api --framework=trpc --auth=iam --no-interactive --dry-runyarn nx g @aws/nx-plugin:ts#api --name=demo-api --framework=trpc --auth=iam --no-interactive --dry-runnpx nx g @aws/nx-plugin:ts#api --name=demo-api --framework=trpc --auth=iam --no-interactive --dry-runbunx nx g @aws/nx-plugin:ts#api --name=demo-api --framework=trpc --auth=iam --no-interactive --dry-run- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - ts#api - 填写必需参数
- name: demo-api
- framework: trpc
- auth: iam
- 点击
Generate
这将在 packages/demo-api 文件夹中创建 API。
添加 React 网站
Section titled “添加 React 网站”pnpm nx g @aws/nx-plugin:ts#website --name=demo-website --no-interactiveyarn nx g @aws/nx-plugin:ts#website --name=demo-website --no-interactivenpx nx g @aws/nx-plugin:ts#website --name=demo-website --no-interactivebunx nx g @aws/nx-plugin:ts#website --name=demo-website --no-interactive您还可以执行试运行以查看哪些文件会被更改
pnpm nx g @aws/nx-plugin:ts#website --name=demo-website --no-interactive --dry-runyarn nx g @aws/nx-plugin:ts#website --name=demo-website --no-interactive --dry-runnpx nx g @aws/nx-plugin:ts#website --name=demo-website --no-interactive --dry-runbunx nx g @aws/nx-plugin:ts#website --name=demo-website --no-interactive --dry-run- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - ts#website - 填写必需参数
- name: demo-website
- 点击
Generate
这将在 packages/demo-website 中搭建一个新的 React 网站。
添加 Cognito 身份验证
Section titled “添加 Cognito 身份验证”pnpm nx g @aws/nx-plugin:ts#website#auth --project=@my-project/demo-website --cognitoDomain=my-demo --no-interactiveyarn nx g @aws/nx-plugin:ts#website#auth --project=@my-project/demo-website --cognitoDomain=my-demo --no-interactivenpx nx g @aws/nx-plugin:ts#website#auth --project=@my-project/demo-website --cognitoDomain=my-demo --no-interactivebunx nx g @aws/nx-plugin:ts#website#auth --project=@my-project/demo-website --cognitoDomain=my-demo --no-interactive您还可以执行试运行以查看哪些文件会被更改
pnpm nx g @aws/nx-plugin:ts#website#auth --project=@my-project/demo-website --cognitoDomain=my-demo --no-interactive --dry-runyarn nx g @aws/nx-plugin:ts#website#auth --project=@my-project/demo-website --cognitoDomain=my-demo --no-interactive --dry-runnpx nx g @aws/nx-plugin:ts#website#auth --project=@my-project/demo-website --cognitoDomain=my-demo --no-interactive --dry-runbunx nx g @aws/nx-plugin:ts#website#auth --project=@my-project/demo-website --cognitoDomain=my-demo --no-interactive --dry-run- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - ts#website#auth - 填写必需参数
- project: @my-project/demo-website
- cognitoDomain: my-demo
- 点击
Generate
这将设置必要的基础设施和 React 代码,以便为您的网站添加 Cognito 身份验证。
连接前端到后端
Section titled “连接前端到后端”pnpm nx g @aws/nx-plugin:connection --sourceProject=@my-project/demo-website --targetProject=@my-project/demo-api --no-interactiveyarn nx g @aws/nx-plugin:connection --sourceProject=@my-project/demo-website --targetProject=@my-project/demo-api --no-interactivenpx nx g @aws/nx-plugin:connection --sourceProject=@my-project/demo-website --targetProject=@my-project/demo-api --no-interactivebunx nx g @aws/nx-plugin:connection --sourceProject=@my-project/demo-website --targetProject=@my-project/demo-api --no-interactive您还可以执行试运行以查看哪些文件会被更改
pnpm nx g @aws/nx-plugin:connection --sourceProject=@my-project/demo-website --targetProject=@my-project/demo-api --no-interactive --dry-runyarn nx g @aws/nx-plugin:connection --sourceProject=@my-project/demo-website --targetProject=@my-project/demo-api --no-interactive --dry-runnpx nx g @aws/nx-plugin:connection --sourceProject=@my-project/demo-website --targetProject=@my-project/demo-api --no-interactive --dry-runbunx nx g @aws/nx-plugin:connection --sourceProject=@my-project/demo-website --targetProject=@my-project/demo-api --no-interactive --dry-run- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - connection - 填写必需参数
- sourceProject: @my-project/demo-website
- targetProject: @my-project/demo-api
- 点击
Generate
这将配置必要的提供程序,以确保您的网站可以调用您的 tRPC API。
添加基础设施
Section titled “添加基础设施”根据您选择的 IAC 提供商添加基础设施项目。
pnpm nx g @aws/nx-plugin:ts#infra --name=infra --no-interactiveyarn nx g @aws/nx-plugin:ts#infra --name=infra --no-interactivenpx nx g @aws/nx-plugin:ts#infra --name=infra --no-interactivebunx nx g @aws/nx-plugin:ts#infra --name=infra --no-interactive您还可以执行试运行以查看哪些文件会被更改
pnpm nx g @aws/nx-plugin:ts#infra --name=infra --no-interactive --dry-runyarn nx g @aws/nx-plugin:ts#infra --name=infra --no-interactive --dry-runnpx nx g @aws/nx-plugin:ts#infra --name=infra --no-interactive --dry-runbunx nx g @aws/nx-plugin:ts#infra --name=infra --no-interactive --dry-run- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - ts#infra - 填写必需参数
- name: infra
- 点击
Generate
这将配置一个 CDK 应用程序,您可以使用它在 AWS 上部署基础设施。
pnpm nx g @aws/nx-plugin:terraform#project --name=infra --no-interactiveyarn nx g @aws/nx-plugin:terraform#project --name=infra --no-interactivenpx nx g @aws/nx-plugin:terraform#project --name=infra --no-interactivebunx nx g @aws/nx-plugin:terraform#project --name=infra --no-interactive您还可以执行试运行以查看哪些文件会被更改
pnpm nx g @aws/nx-plugin:terraform#project --name=infra --no-interactive --dry-runyarn nx g @aws/nx-plugin:terraform#project --name=infra --no-interactive --dry-runnpx nx g @aws/nx-plugin:terraform#project --name=infra --no-interactive --dry-runbunx nx g @aws/nx-plugin:terraform#project --name=infra --no-interactive --dry-run- 安装 Nx Console VSCode Plugin 如果您尚未安装
- 在VSCode中打开Nx控制台
- 点击
Generate (UI)在"Common Nx Commands"部分 - 搜索
@aws/nx-plugin - terraform#project - 填写必需参数
- name: infra
- 点击
Generate
这将配置一个 Terraform 项目,您可以使用它在 AWS 上部署基础设施。
步骤 3:在本地运行您的网站和 API
Section titled “步骤 3:在本地运行您的网站和 API”使用以下命令为您的网站及其连接的 API 启动本地开发服务器:
pnpm devyarn devnpm run devbun dev您的网站将在 http://localhost:4200 上可用。
对网站和 API 的更改都将实时反映,因为本地网站和 API 服务器都将热重载。
步骤 4:定义云资源并部署到 AWS
Section titled “步骤 4:定义云资源并部署到 AWS”打开 packages/infra/src/stacks/application-stack.ts 并添加以下代码:
import { Stack, StackProps } from 'aws-cdk-lib';import { DemoApi, DemoWebsite, UserIdentity } from '@my-project/common-constructs';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 并添加以下代码:
# Include metrics tracking for @aws/nx-plugin usagemodule "metrics" { source = "../../common/terraform/src/metrics"}
# 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]}这就是我们部署全栈应用程序所需编写的全部 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 和身份验证部署的资源。
恭喜!🎉 您已成功使用 @aws/nx-plugin 构建并部署了一个全栈应用程序!