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

如何通过Terraform排除infra目录,将React应用上传至AWS S3?

解决Terraform上传React应用到S3时排除infra目录的问题

核心思路

通过Terraform的列表推导式配合路径判断,过滤掉infra目录下的所有文件,只上传React应用的业务代码和资源文件。

修改后的Terraform代码

resource "aws_s3_bucket_object" "react_app_files" {
  for_each = toset([
    for file in fileset("../../ReactApp", "**") : file
    if !startswith(file, "infra/")
  ])
  bucket   = aws_s3_bucket.react_app.id
  key      = each.value
  source   = "../../ReactApp/${each.value}"
  
  # 保留你原有的其他配置,比如:
  # acl    = "public-read"
  # content_type = mime_type("../../ReactApp/${each.value}")
}

代码说明

  • fileset("../../ReactApp", "**"):遍历React根目录下所有文件及子目录的路径
  • if !startswith(file, "infra/"):过滤掉所有以infra/开头的路径,彻底排除infra目录下的所有内容
  • toset():把过滤后的路径列表转为集合,满足for_each的使用要求

如果后续需要排除其他目录,直接在if条件里追加判断即可,比如:

if !startswith(file, "infra/") && !startswith(file, "temp/")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 14:52:13