将你的 Astro 站点部署到 AWS
AWS 是一个功能齐全的 Web 应用托管平台,可用于部署 Astro 网站。
¥AWS is a full-featured web app hosting platform that can be used to deploy an Astro site.
将项目部署到 AWS 需要使用 AWS 控制台。(大多数这些操作也可以使用 AWS CLI 完成)。本指南将引导你完成使用 AWS 放大、S3 静态网站托管 和 CloudFront 将站点部署到 AWS 的步骤。
¥Deploying your project to AWS requires using the AWS console. (Most of these actions can also be done using the AWS CLI). This guide will walk you through the steps to deploy your site to AWS using AWS Amplify, S3 static website hosting, and CloudFront.
AWS 放大
标题部分 AWS 放大¥AWS Amplify
AWS Amplify 是一组专门构建的工具和功能,可让前端 Web 和移动开发者在 AWS 上快速轻松地构建全栈应用。
¥AWS Amplify is a set of purpose-built tools and features that lets frontend web and mobile developers quickly and easily build full-stack applications on AWS.
-
Create a new Amplify Hosting project.
-
Connect your repository to Amplify.
-
Modify your build settings to match your project’s build process.
当你将提交推送到存储库时,Amplify 将自动部署你的网站并更新它。
¥Amplify will automatically deploy your website and update it when you push a commit to your repository.
S3 静态网站托管
标题部分 S3 静态网站托管¥S3 static website hosting
S3 是任何应用的起点。它是存储项目文件和其他资源的位置。S3 按文件存储和请求数量收费。你可以在 AWS 文档 中找到有关 S3 的更多信息。
¥S3 is the starting point of any application. It is where your project files and other assets are stored. S3 charges for file storage and number of requests. You can find more information about S3 in the AWS documentation.
-
Create an S3 bucket with your project’s name.
-
Disable “Block all public access”. By default, AWS sets all buckets to be private. To make it public, you need to uncheck the “Block public access” checkbox in the bucket’s properties.
-
Upload your built files located in
dist
to S3. You can do this manually in the console or use the AWS CLI. If you use the AWS CLI, use the following command after authenticating with your AWS credentials: -
Update your bucket policy to allow public access. You can find this setting in the bucket’s Permissions > Bucket policy.
-
Enable website hosting for your bucket. You can find this setting in the bucket’s Properties > Static website hosting. Set your index document to
index.html
and your error document to404.html
. Finally, you can find your new website URL in the bucket’s Properties > Static website hosting.
带有 CloudFront 的 S3
标题部分 带有 CloudFront 的 S3¥S3 with CloudFront
CloudFront 是一种提供内容分发网络 (CDN) 功能的 Web 服务。它用于缓存 Web 服务器的内容并将其分发给终端用户。CloudFront 根据传输的数据量收费。将 CloudFront 添加到你的 S3 存储桶更具成本效益,并提供更快的交付速度。
¥CloudFront is a web service that provides content delivery network (CDN) capabilities. It is used to cache content of a web server and distribute it to end users. CloudFront charges for the amount of data transferred. Adding CloudFront to your S3 bucket is more cost-effective and provides a faster delivery.
要将 S3 与 CloudFront 连接起来,请创建一个具有以下值的 CloudFront 分发版:
¥To connect S3 with CloudFront, create a CloudFront distribution with the following values:
-
原域:你的 S3 存储桶静态网站端点。你可以在 S3 存储桶的属性 > 静态网站托管中找到你的端点。或者,你可以选择你的 s3 存储桶并单击标注以将你的存储桶地址替换为你的存储桶静态端点。
-
查看器协议策略:“重定向到 HTTPS”
此配置将使用 CloudFront CDN 网络为你的站点提供服务。你可以在存储桶的分发 > 域名中找到你的 CloudFront 分发 URL。
¥This configuration will serve your site using the CloudFront CDN network. You can find your CloudFront distribution URL in the bucket’s Distributions > Domain name.
使用 GitHub Actions 进行持续部署
标题部分 使用 GitHub Actions 进行持续部署¥Continuous deployment with GitHub Actions
有多种方法可以为 AWS 设置持续部署。托管在 GitHub 上的代码的一种可能性是每次推送提交时都使用 GitHub Actions 来部署你的网站。
¥There are many ways to set up continuous deployment for AWS. One possibility for code hosted on GitHub is to use GitHub Actions to deploy your website every time you push a commit.
-
Create a new policy in your AWS account using IAM with the following permissions. This policy will allow you to upload built files to your S3 bucket and invalidate the CloudFront distribution files when you push a commit.
-
Create a new IAM user and attach the policy to the user. This will provide your
AWS_SECRET_ACCESS_KEY
andAWS_ACCESS_KEY_ID
. -
Add this sample workflow to your repository at
.github/workflows/deploy.yml
and push it to GitHub. You will need to addAWS_ACCESS_KEY_ID
,AWS_SECRET_ACCESS_KEY
,BUCKET_ID
, andDISTRIBUTION_ID
as “secrets” to your repository on GitHub under Settings > Secrets > Actions. Click New repository secret to add each one.
社区资源
标题部分 社区资源¥Community Resources