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

Terraform创建EC2实例时templatefile路径报错求助

解决Terraform部署AWS EC2实例时的文件不存在报错

报错信息

path参数值无效:cis-userdata.sh文件不存在;该函数仅支持作为配置源码分发的文件,若此文件将由配置中的资源创建,则需从该资源的属性获取结果。

配置文件内容

EC2.tf

# ------------------------------------------------------------------------------------------------------------
# ------------------------------- EC2 Module with Latest Ubuntu AMI ------------------------------------------
# ------------------------------ No Network Interfaces. Imports Only -----------------------------------------
# ------------------------------------------------------------------------------------------------------------

resource "aws_instance" "ec2" {
  ami                     = data.aws_ami.ubuntu.id
  instance_type           = var.instance_type
  iam_instance_profile    = var.iam_instance_profile
  monitoring              = var.monitoring
  disable_api_termination = var.disable_api_termination
  ebs_optimized           = true
  key_name                = var.key_name
  vpc_security_group_ids  = var.security_groups
  subnet_id               = var.subnet_id
  user_data = templatefile(var.template, {
    HOSTNAME      = var.name,
    linuxPlatform = "",
    isRPM         = "",
  })

  metadata_options {
    http_endpoint               = "enabled"
    http_tokens                 = "required"
    http_put_response_hop_limit = 1
  }

  tags = {
    Creator             = var.creator
    "Cost Center"       = var.cost_center
    Stack               = var.stack
    Name                = var.name
    ControlledByAnsible = var.controlled_by_ansible
    ConfigAnsible       = var.configansible
  }

  root_block_device {
    delete_on_termination = true
    encrypted             = true
    kms_key_id            = var.kms_key_arn # Arn instead of id to avoid forced replacement.
    volume_size           = 16
    tags = {
      Creator       = var.creator
      "Cost Center" = var.cost_center
      Stack         = var.stack
      Name          = var.name
    }
  }

  lifecycle {
    ignore_changes = [
      ami,
      user_data,
      root_block_device,
    ]
  }
}

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["xxxx"] # Canonical
}

variables.tf

variable "name" {
  default = "xxx-prod"
}

variable "instance_type" {
  default = "m5.large"
}

variable "public_ip" {
  default = false
}

variable "instance_id" {
  default = ""
}

variable "stateManager" {
  default = ""
}

variable "iam_instance_profile" {
  default = "infra-" # Required for systems manager
}

variable "security_groups" {
  default = ["sg-xxxx"] # 
}

variable "subnet_id" {
  default = "subnet-xxxx" 
}

variable "availability_zone" {
  default = "us-east-1a"
}

variable "disable_api_termination" {
  default = "true"
}

variable "kms_key_arn" {
  default = "arn:aws:kms:us-east-1:xxxxx:key/xxxx"
}

variable "creator" {
  default = "xxx@xxx.com"
}

variable "cost_center" {
  default = "xxx"
}

variable "stack" {
  default = "Production"
}

variable "controlled_by_ansible" {
  default = "False"
}

variable "country" {
  default = ""
}

variable "ec2_number" {
  default = "01"
}

variable "monitoring" {
  default = true
}

variable "device" {
  default = "/dev/xvda"
}

variable "template" {
  default = ("cis-userdata.sh")
}

variable "key_name" {
  default = "xxx"
}

variable "image_id" {
  default = "ami-xxx"
}

variable "volume_size" {
  default = 16
}

问题分析与解决方案

核心问题

templatefile函数无法找到指定路径下的cis-userdata.sh文件,原因可能是文件位置错误、路径配置不正确,或者尝试引用了尚未创建的资源生成的文件。

解决步骤

  • 确认文件位置:确保cis-userdata.sh与Terraform配置文件(EC2.tf、variables.tf)处于同一目录;若文件在子目录中,需在template变量中填写正确的相对路径,例如文件在templates子目录下,应将变量值改为"templates/cis-userdata.sh"
  • 检查资源依赖:如果cis-userdata.sh是通过Terraform资源(如local_file或aws_s3_object)生成的,不能直接用文件名,需引用该资源的属性值。例如使用local_file生成时,应写local_file.userdata.filename
  • 验证文件权限:确保执行Terraform命令的用户对cis-userdata.sh拥有读取权限,避免因权限不足导致文件无法被读取

内容的提问来源于stack exchange,提问作者Jose Roberto Nava Morales

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 06:40:24