在按需付费(非免费套餐)下创建 AWS CloudFront

发布: (2026年2月22日 GMT+8 08:21)
8 分钟阅读
原文: Dev.to

Source: Dev.to

Rost

AWS 免费套餐对我不起作用,并且在 AWS 控制台上,新建 CloudFront 分配时,Pay‑as‑you‑go 被隐藏

当你在 AWS 上托管基于 Hugo 的站点时,通常会把 S3 存储桶放在 CloudFront 之后,并附加一个 Lambda@Edge 函数,使对目录路径的请求(例如 /blog/)被重写为 /blog/index.html,以兼容 S3 网站式源。随后可以使用 hugo deploy通过 AWS CLI 部署来同步已构建的站点。

关键点: 你需要一个使用该 Lambda 的 CloudFront 分配,如果想避免固定费率套餐的承诺,就必须使用 pay‑as‑you‑go(按需付费)定价。控制台已不再允许仅选择免费或专业套餐。

本文展示了如何通过 AWS CLI 复用已有分配的配置,并结合本文 scripts 文件夹中的脚本,创建一个 pay‑as‑you‑go 分配。

当控制台仅提供 免费专业版

Amazon 推出了 CloudFront 固定费率套餐(Free、Pro、Business、Premium),将 CDN 与 WAF、DDoS 防护、Route 53 等服务捆绑在固定的月费中,且没有超额费用。在 CloudFront 控制台 中创建新分配时,现在只会看到 免费专业版(以及更高级别)的套餐选项。专业版 起价为 $15 / 月;此处没有可见的 “按使用量付费” 选项。

通过 CloudFront APIAWS CLIcreate-distribution)创建分配时,按使用量付费 仍是默认方式。因此,如果你已经通过之前的方式(或 CLI)创建了一个分配,并希望再创建一个具有相同行为的分配——例如使用 S3 网站作为源并配合 Lambda@Edge 处理 index.html——可以 导出该分配的配置,进行调整后再创建新分配。新分配将采用按使用量付费,除非之后将其绑定到固定费率套餐。

您需要的内容

  • 一个 现有的 CloudFront 分配,其配置可供复制(例如,用于另一个 Hugo 站点的分配,或在控制台更改之前创建的分配)。
  • 已安装并配置了 AWS CLI,并具备能够运行 cloudfront:GetDistributionConfigcloudfront:CreateDistribution 的凭证。
  • jq(或类似工具)用于编辑 JSON 配置。

以下示例脚本位于本文的 scripts 文件夹中:

  • current-distribution-config.json
  • new-distribution-config.json
  • updated-distribution-config.json
  • final-distribution-config.json

第一步:获取现有分配配置

# Replace ABCDEFG with your existing distribution ID
aws cloudfront get-distribution-config --id ABCDEFG > current-distribution-config.json

get-distribution-config 会返回一个包含 ETagDistributionConfig 的包装对象。对于创建新分配,只需要传入内部的 DistributionConfig,因此我们将其提取出来:

jq '.DistributionConfig' current-distribution-config.json > new-distribution-config.json

此时 new-distribution-config.json 看起来如下(已省略部分内容)。它包括 CallerReferenceAliasesOriginsDefaultRootObject 和带有 Lambda@Edge 关联的 DefaultCacheBehavior

{
  "CallerReference": "8b2b19eb-6d41-4fc0-8d04-a1313e23e2d7",
  "Aliases": {
    "Quantity": 1,
    "Items": ["micro.com"]
  },
  "DefaultRootObject": "index.html",
  "Origins": {
    "Quantity": 1,
    "Items": [
      {
        "Id": "micro.com.s3.us-west-2.amazonaws.com",
        "DomainName": "micro.com.s3-website-us-west-2.amazonaws.com",
        "OriginPath": "",
        "CustomOriginConfig": {
          "HTTPPort": 80,
          "HTTPSPort": 443,
          "OriginProtocolPolicy": "http-only"
        }
      }
    ]
  },
  "DefaultCacheBehavior": {
    "TargetOriginId": "micro.com.s3.us-west-2.amazonaws.com",
    "ViewerProtocolPolicy": "redirect-to-https",
    "LambdaFunctionAssociations": {
      "Quantity": 1,
      "Items": [
        {
          "LambdaFunctionARN": "arn:aws:lambda:us-east-1:...your-lambda-arn",
          "EventType": "origin-request",
          "IncludeBody": false
        }
      ]
    }
  }
}

第2步:设置唯一的 CallerReference

每个分配必须拥有一个 唯一的 CallerReference。如果使用与其他分配相同的值,API 将拒绝请求。请使用新的字符串或时间戳:

NEW_CALLER_REF="new-distr-$(date +%s)"
jq --arg ref "$NEW_CALLER_REF" '.CallerReference = $ref' new-distribution-config.json > updated-distribution-config.json

第3步:调整别名和来源(可选)

如果新站点使用 不同的域名或 S3 存储桶,请在 updated-distribution-config.json 中更新 AliasesOrigins 以匹配(例如新的存储桶网站端点和别名列表)。

如果您希望暂时使用 不带 自定义域名的分配,请清除别名:

jq '.Aliases = {"Quantity": 0, "Items": []}' updated-distribution-config.json > final-distribution-config.json

(继续执行剩余步骤——使用 aws cloudfront create-distribution --distribution-config file://final-distribution-config.json 创建分配——如原文所述。)

更新 Origin(如有需要)

如果需要将分配指向不同的 S3 网站端点,请在同一文件中编辑 Origins 部分(例如,将 IdDomainName 更改为新存储桶的网站主机名,并将 DefaultCacheBehavior.TargetOriginId 设置为相同的 Id)。

第4步:创建新分配

aws cloudfront create-distribution --distribution-config file://final-distribution-config.json

响应中包含新分配的 IdARNDomainNameStatus
新分配默认使用 按需付费 定价。您可以将 DNS(或 Route 53)指向新的 CloudFront 域名;如果您将其用于 Hugo,则像往常一样使用 Hugo deployAWS CLI 进行部署(请参阅下面链接的 Hugo 部署指南)。

快速参考:完整脚本

# 1) Export and extract DistributionConfig
aws cloudfront get-distribution-config --id EABCDEFGZ > current-distribution-config.json
jq '.DistributionConfig' current-distribution-config.json > new-distribution-config.json

# 2) Unique CallerReference
NEW_CALLER_REF="new-distr-$(date +%s)"
jq --arg ref "$NEW_CALLER_REF" '.CallerReference = $ref' new-distribution-config.json > updated-distribution-config.json

# 3) Optional: no custom domain
jq '.Aliases = {"Quantity": 0, "Items": []}' updated-distribution-config.json > final-distribution-config.json

# 4) Create (pay‑as‑you‑go) distribution
aws cloudfront create-distribution --distribution-config file://final-distribution-config.json

按需付费 vs. 固定费率(回顾)

计划定价模型关键点
按需付费按数据传出和请求计费• CloudFront 的永久免费层每月包含 1 TB 数据传输10 M HTTP/HTTPS 请求
• 无月度计划费用。
固定费率计划固定月费(Free、Pro 等)• 文档见此处
• 免费层的额度较低(例如 1 M 请求,100 GB)。
• Pro 起价 $15/月,提供更高容量和捆绑服务(WAF、Route 53 等)。
• 无超额费用。

如果您的流量符合按需付费免费层的范围且不需要捆绑的固定费率功能,通过 CLI 创建分配(如上所示)可保持 按需付费,而控制台 UI 仅显示 FreePro 计划。

有用链接

0 浏览
Back to Blog

相关文章

阅读更多 »