Terraform 和 AWS:未找到配置文件错误

Terraform and AWS: No Configuration Files Found Error

我正在编写一个小脚本,从我的本地计算机获取一个小文件并将其放入 AWS S3 存储桶中。

我的terraform.tf:

provider "aws" {
  region  = "us-east-1"
  version = "~> 1.6"
}
    
terraform {
  backend "s3" {
    bucket     = "${var.bucket_testing}"
    kms_key_id = "arn:aws:kms:us-east-1:12345678900:key/12312313ed-34sd-6sfa-90cvs-1234asdfasd"
    key     = "testexport/exportFile.tfstate"
    region  = "us-east-1"
    encrypt = true
  }
}
    
data "aws_s3_bucket" "pr-ip" {
  bucket = "${var.bucket_testing}"
}
    
resource "aws_s3_bucket_object" "put_file" {
  bucket = "${data.aws_s3_bucket.pr-ip.id}"
  key    = "${var.file_path}/${var.file_name}"
  source = "src/Datafile.txt"
  etag = "${md5(file("src/Datafile.txt"))}"
    
  kms_key_id = "arn:aws:kms:us-east-1:12345678900:key/12312313ed-34sd-6sfa-90cvs-1234asdfasd"
  server_side_encryption = "aws:kms"
}

然而,当我init:

terraform init

#=>

Terraform initialized in an empty directory!
    
The directory has no Terraform configuration files. You may begin working with Terraform immediately by creating Terraform configuration files.

然后尝试 apply:

terraform apply

#=>

Error: No configuration files found!
    
Apply requires configuration to be present. Applying without a configuration would mark everything for destruction, which is normally not what is desired. If you would like to destroy everything, please run 'terraform destroy' instead which does not require any configuration files.

我收到上面的错误。此外,我还设置了我的默认 AWS 访问密钥 ID 和值。

我能做什么?

此错误意味着您 运行 命令的位置有误。您必须位于包含您的配置文件的目录中,因此在 运行 宁 initapply 之前,您必须 cd 到您的 Terraform 项目文件夹。

我遇到了您模拟的相同错误,在我的情况下,这不是 VPN 错误,而是文件不正确 系统命名。我在项目 folder.To 中解决了这个问题,我创建了一个 .tf 文件 使用 vim 编辑器和命令 vi aws.tf,然后用定义的变量填充文件。我的正在工作。

查看我附上的图片

Error: No configuration files found!

当您不在包含您的配置文件的文件夹中时,会出现上述错误。 要纠正这种情况,您可以在您将要工作的项目文件夹中创建一个 .tf注意 - 空 .tf 也将消除错误,但由于它不包含提供商信息,因此用途有限。 请参阅以下示例:-

provider "aws" {
    region = "us-east" #Below value will be asked when the terraform apply command is executed if not provided here
   }
 

因此,为了成功执行 terraform apply 命令,您需要确保以下几点:-

  1. 您需要出现在您的 terraform 项目文件夹中(可以是任何目录)。
  2. 必须包含 .tf 最好包含 terraform 提供者信息。
  3. 执行 terraform init 以初始化后端和提供程序插件。
  4. 你现在可以执行 terraform apply(没有任何配置错误)

我也有同样的问题,记住 terraform 文件名应该以 .tf 作为扩展名结尾

另一个可能的原因是您使用的模块 URL 不正确。

当我有:

source = "git::ssh://git@git.companyname.com/observability.git//modules/ec2?ref=v2.0.0"

而不是:

source = "git::ssh://git@git.companyname.com/observability.git//terraform/modules/ec2?ref=v2.0.0"

我看到了与您相同的错误消息。

今天早上我在部署到生产环境时遇到了这个错误,这个项目已经存在多年而且没有任何改变。我们最终追溯到创建生产部署票证的人使用 Outlook 将此命令粘贴到电子邮件中: terraform init --reconfigure

微软以其无限的智慧将两个连字符合二为一,而一个连字符甚至不是标准的 ASCII 连字符(我认为它被称为“en-dash”): terraform init –reconfigure

这导致 Terraform 0.12.31 给出有用的错误消息:

Terraform initialized in an empty directory!

The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files.

我们花了半个小时,另一双眼睛才注意到连字符不正确,需要 re-typed! (我认为 terraform 认为“重新配置”是我们想要 运行 初始化的目录的名称,这当然不存在。也许可以改进 terraform 来命名它在报告时查找的目录错误?)

感谢 Microsoft 一直提供帮助(不是)!