将 Astro.js 应用程序部署到 Amplify Hosting - AWS Amplify 托管

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将 Astro.js 应用程序部署到 Amplify Hosting

按照以下说明将 Astro.js 应用程序部署到 Amplify Hosting。您可以使用现有应用程序,也可以使用 Astro 提供的官方示例之一创建入门应用程序。要创建入门应用程序,请参阅 Astro 文档中的使用主题或入门模板

要将 Astro 网站部署SSR到 Amplify Hosting,必须向应用程序添加适配器。我们不维护 Amplify 自有的 Astro 框架适配器。本教程使用由社区成员创建的astro-aws-amplify适配器。此适配器可在网站上的 github.com/alexnguyennz/ astro-aws-amplify 上找到。 GitHub AWS 不维护此适配器。

将 Astro 应用程序部署到 Amplify Hosting
  1. 在您的本地计算机上,导航到要部署的 Astro 应用程序。

  2. 要安装适配器,请打开终端窗口并运行以下命令。此示例使用 github.com/ astro-aws-amplify alexnguyennz/ 上提供的社区适配器。你可以更换 astro-aws-amplify 使用您正在使用的适配器的名称。

    npm install astro-aws-amplify
  3. 在 Astro 应用程序的项目文件夹中,打开该astro.config.mjs文件。更新文件以添加适配器。文件应该呈现以下状态。

    import { defineConfig } from 'astro/config'; import mdx from '@astrojs/mdx'; import awsAmplify from 'astro-aws-amplify'; import sitemap from '@astrojs/sitemap'; // https://astro.build/config export default defineConfig({ site: 'https://example.com', integrations: [mdx(), sitemap()], adapter: awsAmplify(), output: 'server', });
  4. 提交更改并将项目推送到您的 Git 存储库。

    现在你可以将你的 Astro 应用程序部署到 Amplify 了。

  5. 登录 AWS Management Console 然后打开 Amplify 控制台

  6. 所有应用程序页面上,选择创建新应用程序

  7. 在 “开始使用 Amplify 进行构建” 页面上,选择你的 Git 存储库提供商,然后选择 “下一步”。

  8. 添加存储库分支页面上,执行以下操作:

    1. 选择要连接的存储库的名称。

    2. 选择要连接的存储库分支的名称。

    3. 选择下一步

  9. 应用程序设置页面上,找到构建设置部分。对于生成输出目录,请输入.amplify-hosting

  10. 您还必须在编译规范中更新应用程序的前端构建命令。要打开生成规范,请选择编辑YML文件

  11. amplify.yml文件中,找到前端构建命令部分。输入 mv node_modules ./.amplify-hosting/compute/default

    您的编译设置文件应如下所示。

    version: 1 frontend: phases: preBuild: commands: - 'npm ci --cache .npm --prefer-offline' build: commands: - 'npm run build' - 'mv node_modules ./.amplify-hosting/compute/default' artifacts: baseDirectory: .amplify-hosting files: - '**/*' cache: paths: - '.npm/**/*'
  12. 选择保存

  13. 如果您希望 Amplify 能够将应用程序日志传输到 Amazon L CloudWatch ogs,则必须在控制台中明确启用此功能。打开高级设置部分,然后在服务器端渲染 (SSR) 部署部分中选择启用SSR应用程序日志

  14. 选择下一步

  15. 查看页面上,选择保存并部署