You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Terraform配置S3网站时索引文档路径报错,求解决方案

S3存储桶网站配置索引文档格式错误问题排查

问题描述

使用Terraform配置S3存储桶网站功能时,遇到索引文档格式非法的报错,配置代码如下:

resource "aws_s3_bucket_website_configuration" "travel_agency" {
  bucket = aws_s3_bucket.My_bucket.id

  index_document {
    suffix = "/travel-agency-html-template/index.html"
  }

  error_document {
    key = "/travel-agency-html-template/404.html"
  }
}

执行时报错信息:

Error: error creating S3 bucket (my-test-bucket-for-cloudfront) website configuration: InvalidArgument: The IndexDocument Suffix is not well formed

错误原因与修正方案

  • 索引文档后缀的规则限制:index_document.suffix字段仅允许填写文件名后缀,不能包含前置斜杠或完整路径。S3会根据访问路径自动匹配对应位置的该后缀文件,无需指定完整路径。
  • 错误文档键的规则限制:error_document.key是S3对象的完整键名,但不能以斜杠开头(S3对象键本身不包含前置斜杠)。

修正后的配置代码:

resource "aws_s3_bucket_website_configuration" "travel_agency" {
  bucket = aws_s3_bucket.My_bucket.id

  index_document {
    suffix = "index.html"
  }

  error_document {
    key = "travel-agency-html-template/404.html"
  }
}

补充说明(子目录索引文件的访问处理)

如果静态文件都存放在travel-agency-html-template前缀下,想让存储桶根路径访问该子目录的索引文件,需要添加重定向规则:

resource "aws_s3_bucket_website_configuration" "travel_agency" {
  bucket = aws_s3_bucket.My_bucket.id

  index_document {
    suffix = "index.html"
  }

  error_document {
    key = "travel-agency-html-template/404.html"
  }

  routing_rule {
    condition {
      key_prefix_equals = ""
    }
    redirect {
      replace_key_prefix_with = "travel-agency-html-template/"
    }
  }
}

内容的提问来源于stack exchange,提问作者charles uneze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 09:35:00