跳转到内容

快速入门指南

本指南将引导您了解安装和使用 @aws/nx-plugin 在 AWS 上快速构建项目的基础知识。

在继续之前,需要以下全局依赖项:

  • Git
  • Node >= 22(我们建议使用类似 NVM 的工具来管理您的 node 版本)
    • 通过运行 node --version 来验证
  • UV >= 0.5.29
    1. 通过运行以下命令安装 Python 3.14:uv python install 3.14
    2. 使用 uv python list --only-installed 验证

使用您选择的包管理器创建一个 Nx 工作空间,并打开它创建的目录:

创建工作区@aws/nx-workspace

pnpm create @aws/nx-workspace my-project
构建你的命令3

必需

终端窗口
cd my-project

系统会提示您选择基础设施即代码提供商,CDKTerraform

我们将构建一个全栈应用程序:一个 tRPC API、一个 React 网站、Cognito 身份验证以及 CDK 或 Terraform 基础设施。您可以让 AI 助手为您搭建脚手架,或者自己运行生成器——两种方式都能达到相同的工作空间。

  1. 在您的 AI 编码助手中打开工作空间。

    新工作空间已经为 Kiro、Claude Code、Cursor、Gemini CLI、GitHub Copilot 和 OpenAI Codex 配置了插件的 MCP server,因此您的助手可以查找插件的生成器并运行它们,无需任何设置。

  2. 请求应用程序:

    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。

运行搭建应用程序的生成器。使用下面的按钮复制所有命令,或者按照图表下方的说明自己运行每个命令。

Loading the diagram…

根据您正在构建的项目类型,您可以选择任意生成器组合来快速启动您的项目。查看左侧导航栏中的 生成器 以查看完整的选项列表,或尝试使用 图形构建器 以可视化方式构建您的工作空间。

运行此生成器@aws/nx-plugin:ts#api

pnpm nx g @aws/nx-plugin:ts#api --no-interactive
此步骤的选项2

必需

这将在 packages/demo-api 文件夹中创建 API。

运行此生成器@aws/nx-plugin:ts#website

pnpm nx g @aws/nx-plugin:ts#website --no-interactive
此步骤的选项1

必需

这将在 packages/demo-website 中搭建一个新的 React 网站。

运行此生成器@aws/nx-plugin:ts#website#auth

pnpm nx g @aws/nx-plugin:ts#website#auth --no-interactive
此步骤的选项1

必需

这将设置必要的基础设施和 React 代码,以便为您的网站添加 Cognito 身份验证。

运行此生成器@aws/nx-plugin:connection

pnpm nx g @aws/nx-plugin:connection --no-interactive
此步骤的选项2

必需

必需

这将配置必要的提供程序,以确保您的网站可以调用您的 tRPC API。

根据您选择的 IAC 提供商添加基础设施项目。

运行此生成器@aws/nx-plugin:ts#infra

pnpm nx g @aws/nx-plugin:ts#infra --no-interactive
此步骤的选项1

必需

这将配置一个 CDK 应用程序,您可以使用它在 AWS 上部署基础设施。

步骤 3:在本地运行您的网站和 API

Section titled “步骤 3:在本地运行您的网站和 API”

使用以下命令为您的网站及其连接的 API 启动本地开发服务器:

Terminal window
pnpm dev

您的网站将在 http://localhost:4200 上可用。

对网站和 API 的更改都将实时反映,因为本地网站和 API 服务器都将热重载。

试试看:从您的网站调用 API

打开 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 代码。

接下来,运行以下命令来构建您的项目:

Terminal window
pnpm build

引导您的基础设施:

Terminal window
pnpm nx bootstrap infra

部署您的项目:

Terminal window
pnpm nx deploy-sandbox infra

步骤 5:使用已部署的云资源测试网站

Section titled “步骤 5:使用已部署的云资源测试网站”
  1. 获取 runtime-config.json 文件:

    Terminal window
    pnpm nx load-runtime-config demo-website
  2. 启动本地网站服务器

    Terminal window
    pnpm nx serve demo-website

您的网站将在 http://localhost:4200 上可用,并将指向您为 API 和身份验证部署的资源。

要登录,您需要在 Cognito 用户池中创建用户

点击此处查看使用 AWS CLI 的说明

您的网站也通过 CloudFront 提供服务。打开其分发域名(在部署后打印到您的终端)以使用完全在 AWS 上运行的版本。

恭喜!🎉 您已成功使用 @aws/nx-plugin 构建并部署了一个全栈应用程序!

完成后,删除您部署的资源以避免持续产生费用:

Terminal window
pnpm nx destroy-sandbox infra

要删除引导资源,请按照以下步骤操作:

删除您引导的每个区域(包括 us-east-1)中的引导堆栈。这将删除引导角色和容器镜像存储库:

终端窗口
aws cloudformation delete-stack --stack-name CDKToolkit --region <region>